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 || this->IsBlinking())
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  else if (!this->IsBlinking())
239  {
240  this->currentState = LIGHT_OFF;
241  this->pPort->MoveStop();
242  }
243  }
244  break;
245  case LIGHT_OFF:
246  if (this->state == LIGHTON || this->IsBlinking())
247  {
248  if (this->IsFading() || (this->IsBlinking() && millis() - this->startingMillis > this->blinkingDelay - FADING_FULL_DELAY))
249  {
250  this->currentState = LIGHT_ASCENDING;
251  this->startingMillis = millis();
252  this->LightFadingRaw(0);
253  return false;
254  }
255  else if (!this->IsBlinking())
256  {
257  this->currentState = LIGHT_ON;
258  this->pPort->MoveLeftDir();
259  }
260  }
261  break;
262  case LIGHT_ASCENDING:
263  {
264  bool endStateAsc = false;
265  if (this->fadingStep == 0)
266  endStateAsc = true;
267  else
268  if (millis() - this->startingMillis > this->fadingDelay)
269  {
270  int curr = (int) this->fadingCurrentValue + this->fadingStep;
271  if (curr > this->pPort->GetSpeed() || curr < 0)
272  endStateAsc = true;
273  else
274  {
275  this->LightFadingRaw((unsigned char)curr);
276  this->startingMillis = millis();
277  }
278  }
279  //else
280  //Serial.println("Fading delay not finished");
281  if (endStateAsc)
282  {
283  // End of fading
284  this->currentState = LIGHT_ON;
285  if (this->IsBlinking())
286  this->startingMillis = millis();
287  else
288  this->startingMillis = 0;
289  this->LightOnRaw();
290  return true;
291  }
292  }
293  break;
294 
295  case LIGHT_DESCENDING:
296  {
297  bool endStateDesc = false;
298  if (this->fadingStep == 0)
299  endStateDesc = true;
300  else
301  if (millis() - this->startingMillis > this->fadingDelay)
302  {
303  int curr = (int) this->fadingCurrentValue - this->fadingStep;
304  if (curr <= 0)
305  endStateDesc = true;
306  else
307  {
308  this->LightFadingRaw((unsigned char)curr);
309  this->startingMillis = millis();
310  }
311  }
312  //else
313  //Serial.println("Fading delay not finished");
314  if (endStateDesc)
315  {
316  // End of fading
317  this->currentState = LIGHT_OFF;
318  if (this->IsBlinking())
319  this->startingMillis = millis();
320  else
321  this->startingMillis = 0;
322  this->LightOffRaw();
323  return true;
324  }
325  }
326  break;
327 
328  }
329  return false;
330 }
331 
332 #ifndef NO_EEPROM
333 int AccessoryBaseLight::EEPROMSave(int inPos, bool inSimulate)
334 {
335  if (!inSimulate)
336  EEPROM.write(inPos, this->state);
337 
338  return inPos + 1;
339 }
340 
341 int AccessoryBaseLight::EEPROMLoad(int inPos)
342 {
343  this->state = (ACC_STATE)EEPROM.read(inPos++);
344 
345  return inPos;
346 }
347 #endif
348 #ifdef ACCESSORIES_PRINT_ACCESSORIES
349 void AccessoryBaseLight::printAccessory()
350 {
351  Serial.print(F("Fading Step: "));
352  Serial.print(this->fadingStep);
353  Serial.print(F(" / Fading Delay: "));
354  Serial.print(this->fadingDelay);
355  Serial.print(F(" / Blinking Delay: "));
356  Serial.print(this->blinkingDelay);
357  Serial.print(F(" / "));
358  if (this->GetPort() != NULL)
359  this->GetPort()->printPort();
360  Serial.println(F(" "));
361 }
362 #endif
363 
364 #endif
Port * GetPort() const
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 void MoveLeftDir(unsigned long inDuration = 0)
Definition: Port.hpp:143
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
AccessoryBaseLight(Accessory *inpOwner = 0)
virtual void ResetAction()
Definition: Accessory.hpp:385