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_VERBOSE_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 this->SetState(LIGHTON);
119 void AccessoryBaseLight::LightOff()
121 this->SetState(LIGHTOFF);
124 ACC_STATE AccessoryBaseLight::Toggle()
126 if (this->state == LIGHTON || this->state == LIGHTBLINK)
127 this->SetState(LIGHTOFF);
129 this->SetState(LIGHTBLINK);
138 case ACCESSORIES_EVENT_MOVEPOSITIONID:
139 case ACCESSORIES_EVENT_TOGGLE:
143 case ACCESSORIES_EVENT_MOVE:
146 case ACCESSORIES_MOVE_STRAIGHT:
147 case ACCESSORIES_MOVE_TOP:
148 case ACCESSORIES_MOVE_LEFT:
149 case ACCESSORIES_MOVE_DIVERGE:
150 case ACCESSORIES_MOVE_BOTTOM:
151 case ACCESSORIES_MOVE_RIGHT:
152 case ACCESSORIES_MOVE_ON:
155 case ACCESSORIES_MOVE_OFF:
156 case ACCESSORIES_MOVE_STOP:
159 case ACCESSORIES_MOVE_MORE:
160 case ACCESSORIES_MOVE_LESS:
162 int oldValue = this->pPort->
GetSpeed();
163 this->pPort->
SetSpeed(oldValue + inData);
171 case ACCESSORIES_EVENT_MOVEPOSITION:
173 int oldValue = this->pPort->
GetSpeed();
180 case ACCESSORIES_EVENT_SETSPEED:
189 void AccessoryBaseLight::StartAction()
191 if (this->blinkingDelay > 0 || this->
IsFading())
193 this->startingMillis = millis();
196 #ifdef ACCESSORIES_DEBUG_VERBOSE_MODE 197 Serial.print(F(
"AccessoryBaseLight start action "));
198 Serial.println(this->startingMillis);
202 bool AccessoryBaseLight::ActionEnded()
204 #ifdef ACCESSORIES_DEBUG_VERBOSE_MODE 206 Serial.println(F(
"End action of light."));
210 if (this->state == LIGHTON && this->currentState == LIGHT_ON)
213 if (this->state == LIGHTOFF && this->currentState == LIGHT_OFF)
216 #ifdef ACCESSORIES_DEBUG_VERBOSE_MODE 217 Serial.print(F(
"Light current state : "));
218 switch (this->currentState)
221 Serial.println(F(
"On"));
224 Serial.println(F(
"Off"));
226 case LIGHT_ASCENDING:
227 Serial.println(F(
"Ascending"));
229 case LIGHT_DESCENDING:
230 Serial.println(F(
"Descending"));
234 switch (this->currentState)
238 if (this->state == LIGHTOFF || (this->
IsBlinking() && millis() - this->startingMillis > this->blinkingDelay - FADING_FULL_DELAY))
244 this->currentState = LIGHT_DESCENDING;
245 this->startingMillis = millis();
246 this->LightFadingRaw(this->pPort->
GetSpeed());
253 this->startingMillis = millis();
254 this->currentState = LIGHT_OFF;
262 if (this->state == LIGHTON || (this->
IsBlinking() && millis() - this->startingMillis > this->blinkingDelay - FADING_FULL_DELAY))
268 this->currentState = LIGHT_ASCENDING;
269 this->startingMillis = millis();
270 this->LightFadingRaw(0);
277 this->startingMillis = millis();
278 this->currentState = LIGHT_ON;
284 case LIGHT_ASCENDING:
286 bool endStateAsc =
false;
287 if (this->fadingStep == 0)
290 if (millis() - this->startingMillis > this->fadingDelay)
292 int curr = (int) this->fadingCurrentValue + this->fadingStep;
293 if (curr > this->pPort->
GetSpeed() || curr <= 0)
297 this->LightFadingRaw((
unsigned char)curr);
298 this->startingMillis = millis();
306 this->currentState = LIGHT_ON;
308 this->startingMillis = millis();
310 this->startingMillis = 0;
317 case LIGHT_DESCENDING:
319 bool endStateDesc =
false;
320 if (this->fadingStep == 0)
323 if (millis() - this->startingMillis > this->fadingDelay)
325 int curr = (int) this->fadingCurrentValue - this->fadingStep;
330 this->LightFadingRaw((
unsigned char)curr);
331 this->startingMillis = millis();
339 this->currentState = LIGHT_OFF;
341 this->startingMillis = millis();
343 this->startingMillis = 0;
355 int AccessoryBaseLight::EEPROMSave(
int inPos,
bool inSimulate)
358 EEPROM.write(inPos, this->state);
363 int AccessoryBaseLight::EEPROMLoad(
int inPos)
365 this->state = (ACC_STATE)EEPROM.read(inPos++);
370 #ifdef ACCESSORIES_PRINT_ACCESSORIES 371 void AccessoryBaseLight::printAccessory()
373 Serial.print(F(
"Fading Step: "));
374 Serial.print(this->fadingStep);
375 Serial.print(F(
" / Fading Delay: "));
376 Serial.print(this->fadingDelay);
377 Serial.print(F(
" / Blinking Delay: "));
378 Serial.print(this->blinkingDelay);
379 Serial.print(F(
" / "));
382 Serial.println(F(
" "));
virtual void ResetAction()
bool IsActionDelayPending() const
void Event(ACCESSORIES_EVENT_TYPE inEvent = ACCESSORIES_EVENT_MOVEPOSITIONID, int inData = 0)
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)