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