8 #include "ActionsStack.hpp" 13 Accessory *Accessory::pFirstAccessory = NULL;
18 this->movingPositionsSize = 0;
19 this->movingPositionsAddCounter = 0;
20 this->pMovingPositions = NULL;
21 this->pMovingPositions_P = NULL;
22 this->lastMovingPosition = 255;
26 this->startingMillis = 0;
32 #ifdef ACCESSORIES_DEBUG_MODE 33 if (this->pMovingPositions_P != NULL)
35 Serial.println(F(
"Warning : a PROGMEM Moving position list is already defined !"));
39 if (inNewSize <= this->movingPositionsSize)
44 for (; i < this->movingPositionsSize; i++)
45 pNewList[i] = this->pMovingPositions[i];
47 for (; i < inNewSize; i++)
48 pNewList[i].Id = UNDEFINED_ID;
50 this->movingPositionsSize = inNewSize;
51 if (this->pMovingPositions != NULL)
52 delete[] this->pMovingPositions;
53 this->pMovingPositions = pNewList;
61 this->pMovingPositions[this->movingPositionsAddCounter].
Id = inId;
62 this->pMovingPositions[this->movingPositionsAddCounter++].
Position = inPosition;
64 return this->movingPositionsAddCounter - 1;
70 #ifdef ACCESSORIES_DEBUG_MODE 71 if (this->pMovingPositions != NULL)
73 Serial.println(F(
"Warning : a Moving position list is already defined !"));
77 this->pMovingPositions_P = inMovingPositions_P;
84 if (GetMovingPositionByIndex(i, &local)->Id == -1)
88 this->movingPositionsSize = i;
89 this->movingPositionsAddCounter = -1;
91 return this->movingPositionsSize;
97 for (
int i = 0; i < this->movingPositionsSize; i++)
99 this->GetMovingPositionByIndex(i, &local);
100 if (local.
Id == inId)
109 if (this->pMovingPositions != NULL)
111 apPosition->
Id = this->pMovingPositions[inIndex].
Id;
115 if (this->pMovingPositions_P != NULL)
117 memcpy_P(apPosition, this->pMovingPositions_P + inIndex,
sizeof(
MovingPosition));
125 for (
int i = 0; i < this->movingPositionsSize; i++)
127 this->GetMovingPositionByIndex(i, apPosition);
128 if (apPosition->
Id == inId)
137 if (this->duration > 0)
138 this->startingMillis = millis();
140 #ifdef ACCESSORIES_DEBUG_MODE 141 #ifdef ACCESSORIES_DEBUG_VERBOSE_MODE 142 Serial.print(F(
"Accessory start action "));
143 Serial.println(this->startingMillis);
150 if (this->duration > 0)
151 this->startingMillis = millis();
154 #ifdef ACCESSORIES_DEBUG_MODE 155 #ifdef ACCESSORIES_DEBUG_VERBOSE_MODE 156 Serial.print(F(
"Accessory start action at "));
157 Serial.print(this->startingMillis);
158 Serial.print(F(
"ms for state "));
159 Serial.print(inState);
161 if (this->startingMillis == 0)
162 Serial.println(
" ended");
169 #ifdef ACCESSORIES_DEBUG_MODE 170 void Accessory::CheckPort()
const 174 Serial.println(F(
"One accessory have no port !"));
179 #ifdef ACCESSORIES_DEBUG_VERBOSE_MODE 182 Serial.print(F(
"End (reset) action at "));
183 Serial.print(millis() - this->startingMillis);
184 Serial.print(F(
"ms for "));
185 Serial.print(this->duration);
186 Serial.println(F(
"ms"));
188 this->startingMillis = 0;
194 if (this->startingMillis <= 0)
197 if ((
unsigned long)(millis() - this->startingMillis) > this->duration)
199 #ifdef ACCESSORIES_DEBUG_MODE 200 #ifdef ACCESSORIES_DEBUG_VERBOSE_MODE 201 Serial.print(F(
"End action at "));
202 Serial.print(millis() - this->startingMillis);
203 Serial.print(F(
"ms for "));
204 Serial.print(this->duration);
205 Serial.println(F(
"ms"));
208 this->startingMillis = 0;
217 this->lastMovingPosition = inLastPositionIndex;
219 Accessories::EEPROMSave();
225 if (this->state != inNewState)
227 this->state = inNewState;
229 Accessories::EEPROMSave();
239 EEPROM.write(inPos, this->state);
249 this->state = (ACC_STATE)EEPROM.read(inPos++);
257 #ifdef ACCESSORIES_PRINT_ACCESSORIES 258 void Accessory::printMovingPositions()
261 for (
int i = 0; i < this->movingPositionsSize; i++)
263 this->GetMovingPositionByIndex(i, &local);
265 Serial.print(F(
": id "));
266 Serial.print(local.
Id);
267 Serial.print(F(
" / pos "));
275 void Accessory::Add(
Accessory *inpAccessory)
277 if (Accessory::pFirstAccessory == NULL)
279 Accessory::pFirstAccessory = inpAccessory;
284 Accessory *pCurr = Accessory::pFirstAccessory;
295 uint8_t accCount = 0;
296 Accessory *pCurr = Accessory::pFirstAccessory;
297 while (pCurr != NULL)
306 Accessory *Accessory::GetById(
unsigned long inId)
308 Accessory *pCurr = Accessory::pFirstAccessory;
310 while (pCurr != NULL)
322 Accessory *pCurr = Accessory::pFirstAccessory;
324 while (pCurr != NULL)
334 bool Accessory::CanMove(
unsigned long inId)
346 #ifdef ACCESSORIES_DEBUG_MODE 348 Serial.println(F(
"Same position : Cant move !"));
356 bool Accessory::IsMovementPending()
360 #ifdef ACCESSORIES_DEBUG_MODE 361 Serial.println(F(
"ActionPending : Cant move !"));
368 #ifdef ACCESSORIES_DEBUG_MODE 369 Serial.println(F(
"Debounce : Cant move !"));
382 #ifdef ACCESSORIES_DEBUG_MODE 383 Serial.print(F(
" ---- Stack id "));
384 Serial.println(inId);
411 #ifdef ACCESSORIES_DEBUG_MODE 412 Serial.print(F(
"Toggle : Accessory id "));
413 Serial.println(inId);
421 bool Accessory::MovePosition(
unsigned long inId)
439 #ifdef ACCESSORIES_DEBUG_MODE 440 Serial.print(F(
" ---- Stack id "));
441 Serial.println(inId);
449 #ifdef ACCESSORIES_DEBUG_MODE 450 Serial.print(F(
"MovePosition : Accessory id "));
452 Serial.print(F(
" to position "));
473 if (inEvent == ACCESSORIES_EVENT_MOVEPOSITIONINDEX && (inData < 0 || inData >= acc->
GetMovingPositionSize()))
475 #ifdef ACCESSORIES_DEBUG_MODE 476 Serial.print(F(
"Accessory id "));
478 Serial.print(F(
" bad MovePositionIndex event "));
479 Serial.println(inData);
484 acc->
Event(inId, inEvent, inData);
static ActionsStack Actions
virtual ACC_STATE Toggle() = 0
const uint8_t GetMovingPositionSize() const
static bool IsActionPending()
Accessory * GetNextAccessory() const
void SetNextAccessory(Accessory *inAccessory)
unsigned int GetDebounceDelay() const
virtual int SetSpeed(int inSpeed)
unsigned long GetLastMoveTime() const
virtual int EEPROMLoad(int inPos)
int GetMovingPositionValueById(unsigned long inId) const
virtual int EEPROMSave(int inPos, bool inSimulate = false)
virtual void Move(unsigned long inMovingPositionId) = 0
void SetStateRaw(ACC_STATE inNewState)
unsigned char Add(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent, int inData = 0)
virtual bool ActionEnded()
virtual void ResetAction()
static uint8_t GetCount()
virtual void SetState(ACC_STATE inNewState)
void SetLastMovingPosition(uint8_t inLastPositionIndex)
virtual void StartAction()
uint8_t AddMovingPositions(const MovingPosition *inMovingPositions_P)
const uint8_t GetLastMovingPosition() const
bool IsActionDelayPending() const
void AdjustMovingPositionsSize(uint8_t inNewSize)
static void ExecuteEvent(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent = ACCESSORIES_EVENT_MOVEPOSITIONID, int inData = 0)
uint8_t AddMovingPosition(unsigned long inId, int inPosition)
virtual void Event(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent = ACCESSORIES_EVENT_MOVEPOSITIONID, int inData = 0)
uint8_t IndexOfMovingPositionById(unsigned long inId) const