Accessories
Arduino for motors and lights library.
AccessoryBaseLight.cpp
1 /**********************************************************************
2 project: <Accessories>
3 author: <Thierry PARIS>
4 description: <Class for a light, flashing or not, with optional fading>
5 ***********************************************************************/
6 
7 #include "Accessories.h"
8 #include "AccessoryBaseLight.hpp"
9 #ifndef NO_EEPROM
10 #include "EEPROM.h"
11 #endif
12 
13 #ifndef NO_LIGHT
14 
16 {
17  this->pPort = NULL;
18  this->state = LIGHTOFF;
19  this->currentState = LIGHT_OFF;
20  this->startingMillis = 0;
21  this->fadingStep = this->fadingDelay = 0;
22  this->blinkingDelay = 0;
23  this->pOwner = inpOwner;
24 }
25 
26 void AccessoryBaseLight::SetState(ACC_STATE inState)
27 {
28  if (inState == LIGHTBLINK && this->blinkingDelay == 0)
29  inState = LIGHTON;
30 
31 #ifdef ACCESSORIES_DEBUG_MODE
32  Serial.print(F("AccessoryBaseLight SetState "));
33  Serial.println(inState == LIGHTON ? "ON" : inState == LIGHTOFF ? "OFF" : "BLINK");
34 #endif
35 
36  this->SetStateRaw(inState);
37 }
38 
39 void AccessoryBaseLight::SetStateRaw(ACC_STATE inNewState)
40 {
41  if (this->state != inNewState)
42  {
43  this->state = inNewState;
44  this->pOwner->SetStateRaw(inNewState);
45  }
46 }
47 
48 void AccessoryBaseLight::SetFading(uint8_t inStep, uint8_t inDelay)
49 {
50  this->fadingStep = inStep;
51  this->fadingDelay = inDelay;
52  this->fadingCurrentValue = 0;
53 
54 #ifdef ACCESSORIES_DEBUG_MODE
55  if (this->pPort->GetPinType() == DIGITAL || this->pPort->GetPinType() == DIGITAL_INVERTED)
56  Serial.println(F("Fading on a light is not allowed on a DIGITAL pin !"));
57  if (this->blinkingDelay > 0 && FADING_FULL_DELAY > this->blinkingDelay)
58  Serial.println(F("Light fading duration greater than blinking duration !"));
59 #endif
60 }
61 
62 void AccessoryBaseLight::begin(Port *inpPort, int inIntensity, Accessory *inpOwner)
63 {
64  if (inpOwner != NULL)
65  this->pOwner = inpOwner;
66  this->pPort = inpPort;
67  this->pPort->SetSpeed(inIntensity);
68  this->LightOff();
69 }
70 
71 void AccessoryBaseLight::LightFadingRaw(uint8_t inValue)
72 {
73 #ifdef ACCESSORIES_DEBUG_MODE
74 #ifdef ACCESSORIES_DEBUG_VERBOSE_MODE
75  Serial.print(F("AccessoryBaseLight Fading at "));
76  Serial.println(inValue);
77 #endif
78 #endif
79  this->fadingCurrentValue = inValue;
80  this->pPort->MoveLeftDir(-1, inValue);
81 }
82 
83 void AccessoryBaseLight::LightOnRaw()
84 {
85  this->LightFadingRaw(this->pPort->GetSpeed());
86 }
87 
88 void AccessoryBaseLight::LightOffRaw()
89 {
90  this->LightFadingRaw(0);
91  this->pPort->MoveStop();
92 }
93 
94 void AccessoryBaseLight::LightOn()
95 {
96 #ifdef ACCESSORIES_DEBUG_MODE
97  Serial.println(F("AccessoryBaseLight ON"));
98 #endif
99  this->SetState(LIGHTON);
100 }
101 
102 void AccessoryBaseLight::LightOff()
103 {
104 #ifdef ACCESSORIES_DEBUG_MODE
105  Serial.println(F("AccessoryBaseLight OFF"));
106 #endif
107  this->SetState(LIGHTOFF);
108 }
109 
110 ACC_STATE AccessoryBaseLight::Toggle()
111 {
112  if (this->state == LIGHTON || this->state == LIGHTBLINK)
113  this->SetState(LIGHTOFF);
114  else
115  this->SetState(LIGHTBLINK); // will be converted in LIGHTON if Duration is 0 !
116 
117  return this->state;
118 }
119 
120 void AccessoryBaseLight::Event(ACCESSORIES_EVENT_TYPE inEvent, int inData)
121 {
122  switch (inEvent)
123  {
124  case ACCESSORIES_EVENT_MOVEPOSITIONID:
125  case ACCESSORIES_EVENT_TOGGLE:
126  this->Toggle();
127  break;
128 
129  case ACCESSORIES_EVENT_MOVE:
130  switch (inData)
131  {
132  case ACCESSORIES_MOVE_STRAIGHT:
133  case ACCESSORIES_MOVE_TOP:
134  case ACCESSORIES_MOVE_LEFT:
135  case ACCESSORIES_MOVE_DIVERGE:
136  case ACCESSORIES_MOVE_BOTTOM:
137  case ACCESSORIES_MOVE_RIGHT:
138  case ACCESSORIES_MOVE_ON:
139  this->LightOn();
140  break;
141  case ACCESSORIES_MOVE_OFF:
142  case ACCESSORIES_MOVE_STOP:
143  this->LightOff();
144  break;
145  case ACCESSORIES_MOVE_MORE:
146  case ACCESSORIES_MOVE_LESS:
147  {
148  int oldValue = this->pPort->GetSpeed();
149  this->pPort->SetSpeed(oldValue + inData);
150  this->LightOn();
151  this->pPort->SetSpeed(oldValue);
152  }
153  break;
154  }
155  break;
156 
157  case ACCESSORIES_EVENT_MOVEPOSITION:
158  {
159  int oldValue = this->pPort->GetSpeed();
160  this->pPort->SetSpeed(inData);
161  this->LightOn();
162  this->pPort->SetSpeed(oldValue);
163  }
164  break;
165 
166  case ACCESSORIES_EVENT_SETSPEED:
167  this->pPort->SetSpeed(inData);
168  break;
169 
170  default:
171  break;
172  }
173 }
174 
175 void AccessoryBaseLight::StartAction()
176 {
177  if (this->blinkingDelay > 0 || this->IsFading())
178  {
179  this->startingMillis = millis();
180  }
181 
182 #ifdef ACCESSORIES_DEBUG_MODE
183 #ifdef ACCESSORIES_DEBUG_VERBOSE_MODE
184  Serial.print(F("AccessoryBaseLight start action "));
185  Serial.println(this->startingMillis);
186 #endif
187 #endif
188 }
189 
190 bool AccessoryBaseLight::ActionEnded()
191 {
192 #ifdef ACCESSORIES_DEBUG_MODE
193 #ifdef ACCESSORIES_DEBUG_VERBOSE_MODE
194  if (this->pOwner->IsActionDelayPending())
195  Serial.println(F("End action of light."));
196 #endif
197 #endif
198  this->pOwner->ResetAction();
199 
200  if (this->state == LIGHTON && this->currentState == LIGHT_ON)
201  return true;
202 
203  if (this->state == LIGHTOFF && this->currentState == LIGHT_OFF)
204  return true;
205 
206 #ifdef ACCESSORIES_DEBUG_MODE
207 #ifdef ACCESSORIES_DEBUG_VERBOSE_MODE
208  Serial.print(F("Light current state : "));
209  switch (this->currentState)
210  {
211  case LIGHT_ON:
212  Serial.println(F("On"));
213  break;
214  case LIGHT_OFF:
215  Serial.println(F("Off"));
216  break;
217  case LIGHT_ASCENDING:
218  Serial.println(F("Ascending"));
219  break;
220  case LIGHT_DESCENDING:
221  Serial.println(F("Descending"));
222  break;
223  }
224 #endif
225 #endif
226  switch (this->currentState)
227  {
228  case LIGHT_ON:
229  if (this->state == LIGHTOFF)
230  {
231  if (this->IsFading() || (this->IsBlinking() && millis() - this->startingMillis > this->blinkingDelay - FADING_FULL_DELAY))
232  {
233  this->currentState = LIGHT_DESCENDING;
234  this->startingMillis = millis();
235  this->LightFadingRaw(this->pPort->GetSpeed());
236  return false;
237  }
238  this->currentState = LIGHT_OFF;
239  this->pPort->MoveStop();
240  }
241  break;
242  case LIGHT_OFF:
243  if (this->state == LIGHTON)
244  {
245  if (this->IsFading() || (this->IsBlinking() && millis() - this->startingMillis > this->blinkingDelay - FADING_FULL_DELAY))
246  {
247  this->currentState = LIGHT_ASCENDING;
248  this->startingMillis = millis();
249  this->LightFadingRaw(0);
250  return false;
251  }
252  this->currentState = LIGHT_ON;
253  this->pPort->MoveLeftDir();
254  }
255  break;
256  case LIGHT_ASCENDING:
257  {
258  bool endStateAsc = false;
259  if (this->fadingStep == 0)
260  endStateAsc = true;
261  else
262  if (millis() - this->startingMillis > this->fadingDelay)
263  {
264  int curr = (int) this->fadingCurrentValue + this->fadingStep;
265  if (curr > this->pPort->GetSpeed() || curr < 0)
266  endStateAsc = true;
267  else
268  {
269  this->LightFadingRaw((unsigned char)curr);
270  this->startingMillis = millis();
271  }
272  }
273  //else
274  //Serial.println("Fading delay not finished");
275  if (endStateAsc)
276  {
277  // End of fading
278  this->currentState = LIGHT_ON;
279  if (this->IsBlinking())
280  this->startingMillis = millis();
281  else
282  this->startingMillis = 0;
283  this->LightOnRaw();
284  return true;
285  }
286  }
287  break;
288 
289  case LIGHT_DESCENDING:
290  {
291  bool endStateDesc = false;
292  if (this->fadingStep == 0)
293  endStateDesc = true;
294  else
295  if (millis() - this->startingMillis > this->fadingDelay)
296  {
297  int curr = (int) this->fadingCurrentValue - this->fadingStep;
298  if (curr <= 0)
299  endStateDesc = true;
300  else
301  {
302  this->LightFadingRaw((unsigned char)curr);
303  this->startingMillis = millis();
304  }
305  }
306  //else
307  //Serial.println("Fading delay not finished");
308  if (endStateDesc)
309  {
310  // End of fading
311  this->currentState = LIGHT_OFF;
312  if (this->IsBlinking())
313  this->startingMillis = millis();
314  else
315  this->startingMillis = 0;
316  this->LightOffRaw();
317  return true;
318  }
319  }
320  break;
321 
322  }
323  return false;
324 }
325 
326 #ifndef NO_EEPROM
327 int AccessoryBaseLight::EEPROMSave(int inPos, bool inSimulate)
328 {
329  if (!inSimulate)
330  EEPROM.write(inPos, this->state);
331 
332  return inPos + 1;
333 }
334 
335 int AccessoryBaseLight::EEPROMLoad(int inPos)
336 {
337  this->state = (ACC_STATE)EEPROM.read(inPos++);
338 
339  return inPos;
340 }
341 #endif
342 #ifdef ACCESSORIES_PRINT_ACCESSORIES
343 void AccessoryBaseLight::printAccessory()
344 {
345  Serial.print(F("Fading Step: "));
346  Serial.print(this->fadingStep);
347  Serial.print(F(" / Fading Delay: "));
348  Serial.print(this->fadingDelay);
349  Serial.print(F(" / Blinking Delay: "));
350  Serial.print(this->blinkingDelay);
351  Serial.print(F(" / "));
352  if (this->GetPort() != NULL)
353  this->GetPort()->printPort();
354  Serial.println(F(" "));
355 }
356 #endif
357 
358 #endif
Port * GetPort() const
virtual void ResetAction()
Definition: Accessory.hpp:385
bool IsActionDelayPending() const
Definition: Accessory.hpp:398
void Event(ACCESSORIES_EVENT_TYPE inEvent = ACCESSORIES_EVENT_MOVEPOSITIONID, int inData = 0)
virtual void MoveStop()
Definition: Port.hpp:167
int GetSpeed() const
Definition: Port.hpp:119
virtual int SetSpeed(int inSpeed)
Definition: Port.cpp:108
Definition: Port.hpp:61
void begin(Port *inpPort, int inIntensity = 255, Accessory *inpOwner = 0)
PIN_TYPE GetPinType() const
Definition: Port.hpp:110
void SetFading(uint8_t inStep, uint8_t inDelay)
void SetStateRaw(ACC_STATE inNewState)
Definition: Accessory.cpp:165
virtual void MoveLeftDir(unsigned long inDuration = 0)
Definition: Port.hpp:143
AccessoryBaseLight(Accessory *inpOwner = 0)