7 #include "Accessories.hpp" 8 #include "ActionsStack.hpp" 11 #include "AccessoriesCircularBuffer.hpp" 14 bool Accessories::SerialStarted =
false;
15 unsigned long Accessories::WaitEndDate = 0;
17 int Accessories::EEPROMStart = -1;
18 int Accessories::EEPROMSize = -1;
19 int Accessories::EEPROMRecordSize = 0;
20 unsigned long Accessories::EEPROMStartingDelay = 0;
29 Accessories::EEPROMStart = inEEPROMStart;
30 Accessories::EEPROMSize = inEEPROMSize;
31 Accessories::EEPROMStartingDelay = 0;
34 #ifdef ACCESSORIES_DEBUG_MODE 38 Serial.println(F(
""));
39 Serial.println(F(
"Accessories V1.1.1"));
40 Serial.println(F(
"Developed by Thierry Paris."));
41 Serial.println(F(
"(c) Locoduino 2016-2018"));
42 Serial.println(F(
""));
44 Serial.println(F(
"*** Setup Accessories started."));
47 if (EEPROMStart + EEPROMSize != -2 && (EEPROMSize == -1 || EEPROMStart == -1))
49 Serial.print(F(
" Error : EEPROM will not be used : "));
50 Serial.println(F(
" EEPROMSize or EEPROMStart is not defined by begin."));
56 #ifdef ACCESSORIES_DEBUG_MODE 59 Serial.print(F(
"Commander event : Address : "));
60 Serial.print(inId, DEC);
61 Serial.print(F(
" / "));
64 case ACCESSORIES_EVENT_NONE: Serial.println(F(
"NONE"));
break;
65 case ACCESSORIES_EVENT_TOGGLE: Serial.println(F(
"TOGGLE"));
break;
66 case ACCESSORIES_EVENT_MOVE:
67 Serial.print(F(
"MOVE "));
68 switch ((ACCESSORIES_MOVE_TYPE)inEventData)
70 case ACCESSORIES_MOVE_MORE: Serial.println(F(
"MORE"));
break;
71 case ACCESSORIES_MOVE_LESS: Serial.println(F(
"LESS"));
break;
72 case ACCESSORIES_MOVE_STOP: Serial.println(F(
"STOP"));
break;
73 case ACCESSORIES_MOVE_LEFT: Serial.println(F(
"LEFT"));
break;
74 case ACCESSORIES_MOVE_RIGHT: Serial.println(F(
"RIGHT"));
break;
75 case ACCESSORIES_MOVE_CENTER: Serial.println(F(
"CENTER"));
break;
76 case ACCESSORIES_MOVE_TOP: Serial.println(F(
"TOP"));
break;
77 case ACCESSORIES_MOVE_BOTTOM: Serial.println(F(
"BOTTOM"));
break;
78 case ACCESSORIES_MOVE_STRAIGHT: Serial.println(F(
"STRAIGHT"));
break;
79 case ACCESSORIES_MOVE_DIVERGE: Serial.println(F(
"DIVERGE"));
break;
80 case ACCESSORIES_MOVE_ON: Serial.println(F(
"ON"));
break;
81 case ACCESSORIES_MOVE_OFF: Serial.println(F(
"OFF"));
break;
84 case ACCESSORIES_EVENT_MOVEPOSITION:
85 Serial.print(F(
"MOVEPOSITION : "));
86 Serial.println(inEventData, DEC);
88 case ACCESSORIES_EVENT_MOVEPOSITIONID:
89 Serial.println(F(
"MOVEPOSITIONID "));
91 case ACCESSORIES_EVENT_MOVEPOSITIONINDEX:
92 Serial.print(F(
"MOVEPOSITIONINDEX : "));
93 Serial.println(inEventData, DEC);
95 case ACCESSORIES_EVENT_CONFIG:
96 Serial.print(F(
"CONFIG : "));
97 Serial.print(ACCESSORIESCONFIGADDRESS(inEventData), DEC);
98 Serial.print(F(
" / "));
99 Serial.println(ACCESSORIESCONFIGVALUE(inEventData), DEC);
101 case ACCESSORIES_EVENT_SETSPEED:
102 Serial.print(F(
"SET SPEED : "));
103 Serial.println(inEventData);
105 case ACCESSORIES_EVENT_SETDURATION:
106 Serial.print(F(
"SET DURATION : "));
107 Serial.println(inEventData);
124 ArduiEmulator::ArduinoForm::_eventLog(
"Received", inId, inEventType, inEventData);
133 #ifdef ACCESSORIES_PRINT_ACCESSORIES 136 Serial.println(F(
"********* Accessories List"));
139 while (pCurr != NULL)
141 pCurr->printAccessory();
144 Serial.println(F(
"********* End of Accessories"));
147 Serial.println(F(
"********* Groups"));
148 AccessoryGroup::printGroups();
149 Serial.println(F(
"********* End of groups"));
162 if (pLoopAccessory == NULL)
164 #ifdef ACCESSORIES_DEBUG_MODE 165 Serial.println(F(
"*** Setup Accessories Finished."));
169 while (pCurr != NULL)
180 EEPROMStartingDelay = 0;
182 #ifdef ACCESSORIES_DEBUG_MODE 183 bool good = Accessories::EEPROMLoad();
185 Serial.println(F(
"EEPROM loading aborted !"));
187 Accessories::EEPROMLoad();
199 if (pLoopAccessory == NULL)
202 pLoopAccessory->
loop();
215 if (EEPROMStartingDelay > 0)
224 unsigned long start = millis();
225 while (millis() < start + inWaitDelay)
279 #define EEPROM_VERSION 0 281 void Accessories::EEPROMSave()
283 if (EEPROMStart == -1 || EEPROMSize == -1)
286 if (EEPROMStartingDelay == 0)
287 EEPROMStartingDelay = millis();
290 void Accessories::EEPROMSaveRaw()
292 unsigned long mill = millis();
293 if (mill - EEPROMStartingDelay < EEPROM_SAVE_DELAY)
296 int pos = EEPROMStart;
303 EEPROM.update(pos++, EEPROM_VERSION);
304 EEPROM.update(pos++, accCount);
306 EEPROM.update(pos++, grpCount);
309 EEPROM.update(pos++, EEPROMSize / 256);
310 EEPROM.update(pos++, EEPROMSize % 256);
312 if (EEPROMRecordSize == 0)
317 while (pCurr != NULL)
319 EEPROMRecordSize = pCurr->
EEPROMSave(EEPROMRecordSize,
true);
327 circularBuffer.begin(pos+3, EEPROMRecordSize, (EEPROMSize - 10) / EEPROMRecordSize);
328 circularBuffer.clear();
331 EEPROM.update(pos++, EEPROMRecordSize / 256);
332 EEPROM.update(pos++, EEPROMRecordSize % 256);
334 EEPROM.update(pos++, (uint8_t) (EEPROM_VERSION + accCount +
338 EEPROMSize + EEPROMRecordSize));
340 pos = circularBuffer.startWrite();
343 while (pCurr != NULL)
352 EEPROMStartingDelay = 0;
355 bool Accessories::EEPROMLoad()
357 if (EEPROMStart == -1 || EEPROMSize == -1)
360 int pos = EEPROMStart;
366 if (EEPROM.read(pos++) != EEPROM_VERSION)
369 if (EEPROM.read(pos++) != accCount)
373 if (EEPROM.read(pos++) != grpCount)
375 if (EEPROM.read(pos++) != accCount)
379 b1 = EEPROM.read(pos++);
380 b2 = EEPROM.read(pos++);
381 if (EEPROMSize != b1 * 256 + b2)
384 b1 = EEPROM.read(pos++);
385 b2 = EEPROM.read(pos++);
386 EEPROMRecordSize = b1 * 256 + b2;
388 if (EEPROM.read(pos++) != (uint8_t)(EEPROM_VERSION + accCount +
392 EEPROMSize + EEPROMRecordSize))
394 EEPROMRecordSize = 0;
398 circularBuffer.begin(pos, EEPROMRecordSize, (EEPROMSize - 10) / EEPROMRecordSize);
401 pos = circularBuffer.getStartRead();
404 while (pCurr != NULL)
413 EEPROMStartingDelay = 0;
virtual int EEPROMLoad(int inPos)
static int EEPROMSaveAll(int inPos, bool inSimulate = false)
-
static uint8_t GetCount()
-
static uint8_t GetCount()
-
virtual int EEPROMSave(int inPos, bool inSimulate = false)
-
static void ExecuteEvent(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent = ACCESSORIES_EVENT_MOVEPOSITIONID, int inData = 0)
+
static uint8_t GetCount()
static void printAccessories()
static void EventAll(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent = ACCESSORIES_EVENT_MOVEPOSITIONID, int inData = 0)
-
static void RaiseEvent(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent = ACCESSORIES_EVENT_MOVEPOSITIONID, int inData = 0)
+
static void ReceiveEvent(unsigned long inID, ACCESSORIES_EVENT_TYPE lastEventType, int inData)
-
static void begin(int inEEPROMStart = -1, int inEEPROMSize = -1)
+
static void RaiseEvent(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent = ACCESSORIES_EVENT_MOVEPOSITIONID, int inData = 0)
+
static void printEvent(unsigned long inId, ACCESSORIES_EVENT_TYPE inEventType, int inEventData)
+
static Accessory * GetFirstAccessory()
+
static void begin(int inEEPROMStart = -1, int inEEPROMSize = -1)
+
static void ExecuteEvent(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent = ACCESSORIES_EVENT_MOVEPOSITIONID, int inData = 0)
+
static ActionsStack Actions
Accessory * GetNextAccessory() const
-
+
Action * GetActionToExecute()
+
static bool IsActionPending()
-
-
static ActionsStack Actions
-
Action * GetActionToExecute()
-
static Accessory * GetFirstAccessory()
-
static void wait(unsigned long inDelay)
-
+
static int EEPROMLoadAll(int inPos)
+
ACCESSORIES_EVENT_TYPE Event
+
static uint8_t GetCount()
+
static void wait(unsigned long inDelay)