8 #include "AccessoryBaseLight.hpp" 15 const uint8_t PROGMEM gamma8[] = {
16 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
18 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
19 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
20 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
21 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
22 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
23 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
24 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
25 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
26 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
27 90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
28 115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
29 144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
30 177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
31 215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 };
36 this->state = LIGHTOFF;
37 this->currentState = LIGHT_OFF;
38 this->startingMillis = 0;
39 this->fadingStep = this->fadingDelay = 0;
40 this->blinkingDelay = 0;
41 this->pOwner = inpOwner;
44 void AccessoryBaseLight::SetState(ACC_STATE inState)
46 if (inState == LIGHTBLINK && this->blinkingDelay == 0)
49 #ifdef ACCESSORIES_DEBUG_MODE 50 Serial.print(F(
"AccessoryBaseLight SetState "));
51 Serial.println(inState == LIGHTON ?
"ON" : inState == LIGHTOFF ?
"OFF" :
"BLINK");
54 this->SetStateRaw(inState);
57 void AccessoryBaseLight::SetStateRaw(ACC_STATE inNewState)
59 if (this->state != inNewState)
61 if (inNewState == LIGHTBLINK && this->startingMillis == 0)
63 this->state = inNewState;
70 this->fadingStep = inStep;
71 this->fadingDelay = inDelay;
72 this->fadingCurrentValue = 0;
74 #ifdef ACCESSORIES_DEBUG_MODE 76 Serial.println(F(
"Fading on a light is not allowed on a DIGITAL pin !"));
77 if (this->blinkingDelay > 0 && FADING_FULL_DELAY > this->blinkingDelay)
78 Serial.println(F(
"Light fading duration greater than blinking duration !"));
85 this->pOwner = inpOwner;
86 this->pPort = inpPort;
91 void AccessoryBaseLight::LightFadingRaw(uint8_t inValue)
93 #ifdef ACCESSORIES_DEBUG_MODE 94 #ifdef ACCESSORIES_DEBUG_VERBOSE_MODE 95 Serial.print(F(
"AccessoryBaseLight Fading at "));
96 Serial.println(inValue);
99 this->fadingCurrentValue = inValue;
100 this->pPort->
MoveLeftDir(0, pgm_read_byte(&gamma8[inValue]));
103 void AccessoryBaseLight::LightOnRaw()
105 this->LightFadingRaw(this->pPort->
GetSpeed());
108 void AccessoryBaseLight::LightOffRaw()
110 this->LightFadingRaw(0);
114 void AccessoryBaseLight::LightOn()
116 #ifdef ACCESSORIES_DEBUG_MODE 117 Serial.println(F(
"AccessoryBaseLight ON"));
119 this->SetState(LIGHTON);
122 void AccessoryBaseLight::LightOff()
124 #ifdef ACCESSORIES_DEBUG_MODE 125 Serial.println(F(
"AccessoryBaseLight OFF"));
127 this->SetState(LIGHTOFF);
130 ACC_STATE AccessoryBaseLight::Toggle()
132 if (this->state == LIGHTON || this->state == LIGHTBLINK)
133 this->SetState(LIGHTOFF);
135 this->SetState(LIGHTBLINK);
144 case ACCESSORIES_EVENT_MOVEPOSITIONID:
145 case ACCESSORIES_EVENT_TOGGLE:
149 case ACCESSORIES_EVENT_MOVE:
152 case ACCESSORIES_MOVE_STRAIGHT:
153 case ACCESSORIES_MOVE_TOP:
154 case ACCESSORIES_MOVE_LEFT:
155 case ACCESSORIES_MOVE_DIVERGE:
156 case ACCESSORIES_MOVE_BOTTOM:
157 case ACCESSORIES_MOVE_RIGHT:
158 case ACCESSORIES_MOVE_ON:
161 case ACCESSORIES_MOVE_OFF:
162 case ACCESSORIES_MOVE_STOP:
165 case ACCESSORIES_MOVE_MORE:
166 case ACCESSORIES_MOVE_LESS:
168 int oldValue = this->pPort->
GetSpeed();
169 this->pPort->
SetSpeed(oldValue + inData);
177 case ACCESSORIES_EVENT_MOVEPOSITION:
179 int oldValue = this->pPort->
GetSpeed();
186 case ACCESSORIES_EVENT_SETSPEED:
195 void AccessoryBaseLight::StartAction()
197 if (this->blinkingDelay > 0 || this->
IsFading())
199 this->startingMillis = millis();
202 #ifdef ACCESSORIES_DEBUG_MODE 203 #ifdef ACCESSORIES_DEBUG_VERBOSE_MODE 204 Serial.print(F(
"AccessoryBaseLight start action "));
205 Serial.println(this->startingMillis);
210 bool AccessoryBaseLight::ActionEnded()
212 #ifdef ACCESSORIES_DEBUG_MODE 213 #ifdef ACCESSORIES_DEBUG_VERBOSE_MODE 215 Serial.println(F(
"End action of light."));
220 if (this->state == LIGHTON && this->currentState == LIGHT_ON)
223 if (this->state == LIGHTOFF && this->currentState == LIGHT_OFF)
226 #ifdef ACCESSORIES_DEBUG_MODE 227 #ifdef ACCESSORIES_DEBUG_VERBOSE_MODE 228 Serial.print(F(
"Light current state : "));
229 switch (this->currentState)
232 Serial.println(F(
"On"));
235 Serial.println(F(
"Off"));
237 case LIGHT_ASCENDING:
238 Serial.println(F(
"Ascending"));
240 case LIGHT_DESCENDING:
241 Serial.println(F(
"Descending"));
246 switch (this->currentState)
250 if (this->state == LIGHTOFF || (this->
IsBlinking() && millis() - this->startingMillis > this->blinkingDelay - FADING_FULL_DELAY))
256 this->currentState = LIGHT_DESCENDING;
257 this->startingMillis = millis();
258 this->LightFadingRaw(this->pPort->
GetSpeed());
265 this->startingMillis = millis();
266 this->currentState = LIGHT_OFF;
274 if (this->state == LIGHTON || (this->
IsBlinking() && millis() - this->startingMillis > this->blinkingDelay - FADING_FULL_DELAY))
280 this->currentState = LIGHT_ASCENDING;
281 this->startingMillis = millis();
282 this->LightFadingRaw(0);
289 this->startingMillis = millis();
290 this->currentState = LIGHT_ON;
296 case LIGHT_ASCENDING:
298 bool endStateAsc =
false;
299 if (this->fadingStep == 0)
302 if (millis() - this->startingMillis > this->fadingDelay)
304 int curr = (int) this->fadingCurrentValue + this->fadingStep;
305 if (curr > this->pPort->
GetSpeed() || curr <= 0)
309 this->LightFadingRaw((
unsigned char)curr);
310 this->startingMillis = millis();
318 this->currentState = LIGHT_ON;
320 this->startingMillis = millis();
322 this->startingMillis = 0;
329 case LIGHT_DESCENDING:
331 bool endStateDesc =
false;
332 if (this->fadingStep == 0)
335 if (millis() - this->startingMillis > this->fadingDelay)
337 int curr = (int) this->fadingCurrentValue - this->fadingStep;
342 this->LightFadingRaw((
unsigned char)curr);
343 this->startingMillis = millis();
351 this->currentState = LIGHT_OFF;
353 this->startingMillis = millis();
355 this->startingMillis = 0;
367 int AccessoryBaseLight::EEPROMSave(
int inPos,
bool inSimulate)
370 EEPROM.write(inPos, this->state);
375 int AccessoryBaseLight::EEPROMLoad(
int inPos)
377 this->state = (ACC_STATE)EEPROM.read(inPos++);
382 #ifdef ACCESSORIES_PRINT_ACCESSORIES 383 void AccessoryBaseLight::printAccessory()
385 Serial.print(F(
"Fading Step: "));
386 Serial.print(this->fadingStep);
387 Serial.print(F(
" / Fading Delay: "));
388 Serial.print(this->fadingDelay);
389 Serial.print(F(
" / Blinking Delay: "));
390 Serial.print(this->blinkingDelay);
391 Serial.print(F(
" / "));
394 Serial.println(F(
" "));
bool IsActionDelayPending() const
void Event(ACCESSORIES_EVENT_TYPE inEvent = ACCESSORIES_EVENT_MOVEPOSITIONID, int inData = 0)
virtual void ResetAction()
virtual void MoveLeftDir(unsigned long inDuration = 0)
virtual int SetSpeed(int inSpeed)
void begin(Port *inpPort, int inIntensity = 255, Accessory *inpOwner = 0)
PIN_TYPE GetPinType() const
void SetFading(uint8_t inStep, uint8_t inDelay)
void SetStateRaw(ACC_STATE inNewState)
AccessoryBaseLight(Accessory *inpOwner = 0)