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;
23 this->debounceDelay = 50;
27 this->startingMillis = 0;
33 #ifdef ACCESSORIES_DEBUG_MODE 34 if (this->pMovingPositions_P != NULL)
36 Serial.println(F(
"Warning : a PROGMEM Moving position list is already defined !"));
40 if (inNewSize <= this->movingPositionsSize)
45 for (; i < this->movingPositionsSize; i++)
46 pNewList[i] = this->pMovingPositions[i];
48 for (; i < inNewSize; i++)
49 pNewList[i].Id = UNDEFINED_ID;
51 this->movingPositionsSize = inNewSize;
52 if (this->pMovingPositions != NULL)
53 delete[] this->pMovingPositions;
54 this->pMovingPositions = pNewList;
62 this->pMovingPositions[this->movingPositionsAddCounter].
Id = inId;
63 this->pMovingPositions[this->movingPositionsAddCounter++].
Position = inPosition;
65 return this->movingPositionsAddCounter - 1;
71 #ifdef ACCESSORIES_DEBUG_MODE 72 if (this->pMovingPositions != NULL)
74 Serial.println(F(
"Warning : a Moving position list is already defined !"));
78 this->pMovingPositions_P = inMovingPositions_P;
85 if (GetMovingPositionByIndex(i, &local)->Id == -1)
89 this->movingPositionsSize = i;
90 this->movingPositionsAddCounter = -1;
92 return this->movingPositionsSize;
98 for (
int i = 0; i < this->movingPositionsSize; i++)
100 this->GetMovingPositionByIndex(i, &local);
101 if (local.
Id == inId)
110 if (this->pMovingPositions != NULL)
112 apPosition->
Id = this->pMovingPositions[inIndex].
Id;
116 if (this->pMovingPositions_P != NULL)
118 memcpy_P(apPosition, this->pMovingPositions_P + inIndex,
sizeof(
MovingPosition));
126 for (
int i = 0; i < this->movingPositionsSize; i++)
128 this->GetMovingPositionByIndex(i, apPosition);
129 if (apPosition->
Id == inId)
138 if (this->duration > 0)
139 this->startingMillis = millis();
141 #ifdef ACCESSORIES_DEBUG_MODE 142 #ifdef ACCESSORIES_DEBUG_VERBOSE_MODE 143 Serial.print(F(
"Accessory start action "));
144 Serial.println(this->startingMillis);
151 if (this->duration > 0)
152 this->startingMillis = millis();
155 #ifdef ACCESSORIES_DEBUG_MODE 156 #ifdef ACCESSORIES_DEBUG_VERBOSE_MODE 157 Serial.print(F(
"Accessory start action at "));
158 Serial.print(this->startingMillis);
159 Serial.print(F(
"ms for state "));
160 Serial.print(inState);
162 if (this->startingMillis == 0)
163 Serial.println(
" ended");
170 #ifdef ACCESSORIES_DEBUG_MODE 171 void Accessory::CheckPort()
const 175 Serial.println(F(
"One accessory have no port !"));
180 #ifdef ACCESSORIES_DEBUG_VERBOSE_MODE 183 Serial.print(F(
"End (reset) action at "));
184 Serial.print(millis() - this->startingMillis);
185 Serial.print(F(
"ms for "));
186 Serial.print(this->duration);
187 Serial.println(F(
"ms"));
189 this->startingMillis = 0;
195 if (this->startingMillis <= 0)
198 if ((
unsigned long)(millis() - this->startingMillis) > this->duration)
200 #ifdef ACCESSORIES_DEBUG_MODE 201 #ifdef ACCESSORIES_DEBUG_VERBOSE_MODE 202 Serial.print(F(
"End action at "));
203 Serial.print(millis() - this->startingMillis);
204 Serial.print(F(
"ms for "));
205 Serial.print(this->duration);
206 Serial.println(F(
"ms"));
209 this->startingMillis = 0;
218 this->lastMovingPosition = inLastPositionIndex;
220 Accessories::EEPROMSave();
226 if (this->state != inNewState)
228 this->state = inNewState;
230 Accessories::EEPROMSave();
240 EEPROM.write(inPos, this->state);
250 this->state = (ACC_STATE)EEPROM.read(inPos++);
258 #ifdef ACCESSORIES_PRINT_ACCESSORIES 259 void Accessory::printMovingPositions()
262 for (
int i = 0; i < this->movingPositionsSize; i++)
264 this->GetMovingPositionByIndex(i, &local);
266 Serial.print(F(
": id "));
267 Serial.print(local.
Id);
268 Serial.print(F(
" / pos "));
276 void Accessory::Add(
Accessory *inpAccessory)
278 if (Accessory::pFirstAccessory == NULL)
280 Accessory::pFirstAccessory = inpAccessory;
285 Accessory *pCurr = Accessory::pFirstAccessory;
296 uint8_t accCount = 0;
297 Accessory *pCurr = Accessory::pFirstAccessory;
298 while (pCurr != NULL)
307 Accessory *Accessory::GetById(
unsigned long inId)
309 Accessory *pCurr = Accessory::pFirstAccessory;
311 while (pCurr != NULL)
323 Accessory *pCurr = Accessory::pFirstAccessory;
325 while (pCurr != NULL)
335 bool Accessory::CanMove(
unsigned long inId)
347 #ifdef ACCESSORIES_DEBUG_MODE 349 Serial.println(F(
"Same position : Cant move !"));
357 bool Accessory::IsMovementPending()
361 #ifdef ACCESSORIES_DEBUG_MODE 362 Serial.println(F(
"ActionPending : Cant move !"));
369 #ifdef ACCESSORIES_DEBUG_MODE 370 Serial.println(F(
"Debounce : Cant move !"));
383 #ifdef ACCESSORIES_DEBUG_MODE 384 Serial.print(F(
" ---- Stack id "));
385 Serial.println(inId);
412 #ifdef ACCESSORIES_DEBUG_MODE 413 Serial.print(F(
"Toggle : Accessory id "));
414 Serial.println(inId);
422 bool Accessory::MovePosition(
unsigned long inId)
440 #ifdef ACCESSORIES_DEBUG_MODE 441 Serial.print(F(
" ---- Stack id "));
442 Serial.println(inId);
450 #ifdef ACCESSORIES_DEBUG_MODE 451 Serial.print(F(
"MovePosition : Accessory id "));
453 Serial.print(F(
" to position "));
474 if (inEvent == ACCESSORIES_EVENT_MOVEPOSITIONINDEX && (inData < 0 || inData >= acc->
GetMovingPositionSize()))
476 #ifdef ACCESSORIES_DEBUG_MODE 477 Serial.print(F(
"Accessory id "));
479 Serial.print(F(
" bad MovePositionIndex event "));
480 Serial.println(inData);
485 acc->
Event(inId, inEvent, inData);
static ActionsStack Actions
virtual ACC_STATE Toggle() = 0
static uint8_t GetCount()
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)
const uint8_t GetLastMovingPosition() const
void SetStateRaw(ACC_STATE inNewState)
void SetLastMovingPosition(uint8_t inLastPositionIndex)
virtual bool ActionEnded()
int GetMovingPositionValueById(unsigned long inId) const
virtual int EEPROMSave(int inPos, bool inSimulate = false)
virtual void ResetAction()
const uint8_t GetMovingPositionSize() const
virtual void SetState(ACC_STATE inNewState)
virtual void Event(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent = ACCESSORIES_EVENT_MOVEPOSITIONID, int inData = 0)
uint8_t IndexOfMovingPositionById(unsigned long inId) const
bool IsActionDelayPending() const
uint8_t AddMovingPositions(const MovingPosition *inMovingPositions_P)
virtual void Move(unsigned long inMovingPositionId) = 0
static bool IsActionPending()
void AdjustMovingPositionsSize(uint8_t inNewSize)
static void ExecuteEvent(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent = ACCESSORIES_EVENT_MOVEPOSITIONID, int inData = 0)
unsigned char Add(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent, int inData = 0)
uint8_t AddMovingPosition(unsigned long inId, int inPosition)
virtual void StartAction()