Accessories
Arduino for motors and lights library.
AccessoryLightMulti.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 "AccessoryLightMulti.hpp"
9 
10 #ifndef NO_LIGHT
11 
13 {
14  this->lightsSize = 0;
15  this->pLights = NULL;
16  this->pMovingPositionBlinks = NULL;
17 }
18 
19 #ifdef ACCESSORIES_DEBUG_MODE
20 void AccessoryLightMulti::CheckPort() const
21 {
22  for (uint8_t i = 0; i < this->lightsSize; i++)
23  {
24  if (this->pLights[i].GetPort() == NULL)
25  {
26  Serial.println(F("One light in a multilight accessory have no port !"));
27  }
28  }
29 }
30 #endif
31 
32 void AccessoryLightMulti::begin(unsigned long inId, uint8_t inSize, unsigned long inBlinkDuration)
33 {
34  this->lightsSize = inSize;
35 
36  this->pLights = new AccessoryBaseLight[inSize];
37  this->SetDuration(inBlinkDuration);
38 
39  for (uint8_t i = 0; i < inSize; i++)
40  {
41  this->pLights[i].pOwner = this;
42  // This is the default value for the blinking delay, which can be overwriten by individual SetBlinking()
43  // on a particular light.
44  this->pLights[i].SetBlinking(inBlinkDuration);
45  }
46 
47  this->pMovingPositionBlinks = NULL;
48 }
49 
51 {
52  if (this->pMovingPositionBlinks == NULL || inNewSize > this->GetMovingPositionSize())
53  {
54  int i;
55  int *pNewMovingPositionBlinks = new int[inNewSize];
56  for (i = 0; i < this->GetMovingPositionSize(); i++)
57  pNewMovingPositionBlinks[i] = this->pMovingPositionBlinks[i];
58  for (; i < inNewSize; i++)
59  pNewMovingPositionBlinks[i] = 0;
60  if (this->pMovingPositionBlinks != NULL)
61  delete[] this->pMovingPositionBlinks;
62  this->pMovingPositionBlinks = pNewMovingPositionBlinks;
63  }
64 }
65 
66 unsigned char AccessoryLightMulti::AddMovingPosition(unsigned long inIdMin, int inOnMask, int inBlinkMask)
67 {
69  unsigned char pos = Accessory::AddMovingPosition(inIdMin, inOnMask);
70 
71  this->pMovingPositionBlinks[pos] = inBlinkMask;
72  return pos;
73 }
74 
75 void AccessoryLightMulti::beginLight(uint8_t inIndex, Port *inpPort, int inIntensity)
76 {
77  this->pLights[inIndex].begin(inpPort, inIntensity, this);
78  this->LightOff(inIndex);
79 }
80 
82 {
83  for (uint8_t i = 0; i < this->lightsSize; i++)
84  this->LightOn(i);
85 }
86 
88 {
89  for (uint8_t i = 0; i < this->lightsSize; i++)
90  this->LightOff(i);
91 }
92 
94 {
95  for (uint8_t i = 0; i < this->lightsSize; i++)
96  this->Blink(i);
97 }
98 
99 bool AccessoryLightMulti::ActionEnded()
100 {
101  bool res = false;
102  for (uint8_t i = 0; i < this->lightsSize; i++)
103  res |= this->ActionEnded(i);
104 
105  return res;
106 }
107 
108 ACC_STATE AccessoryLightMulti::Toggle()
109 {
110  ACC_STATE localState = ACC_STATE::STATE_NONE;
111 
112  for (uint8_t i = 0; i < this->lightsSize; i++)
113  localState = this->Toggle(i);
114 
115  return localState;
116 }
117 
118 void AccessoryLightMulti::Move(int inPosition)
119 {
120  if (inPosition != -1)
121  {
122  for (uint8_t i = 0; i < this->lightsSize; i++)
123  if (inPosition & (1 << i))
124  this->SetState(i, LIGHTON);
125  else
126  this->SetState(i, LIGHTOFF);
127  }
128 }
129 
130 void AccessoryLightMulti::MoveBlink(int inOnMask, int inBlinkMask)
131 {
132  if (inOnMask != -1)
133  {
134  for (uint8_t i = 0; i < this->lightsSize; i++)
135  {
136  this->SetBlinking(i, 0);
137  if (inOnMask & (1 << i))
138  {
139  if (inBlinkMask & (1 << i))
140  {
141  this->SetBlinking(i, this->GetDuration());
142  this->SetState(i, LIGHTBLINK);
143  }
144  else
145  this->SetState(i, LIGHTON);
146  }
147  else
148  this->SetState(i, LIGHTOFF);
149  }
150  }
151 }
152 
153 void AccessoryLightMulti::Move(unsigned long inId)
154 {
155  int positionIndex = this->IndexOfMovingPosition(inId);
156 
157  if (positionIndex != -1)
158  {
159  int position = this->GetMovingPosition(inId);
160  if (this->pMovingPositionBlinks != NULL)
161  MoveBlink(position, this->pMovingPositionBlinks[positionIndex]);
162  else
163  Move(position);
164  }
165  else
166  {
167  for (uint8_t i = 0; i < this->lightsSize; i++)
168  this->Toggle(i);
169  }
170 }
171 
172 void AccessoryLightMulti::Event(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent, int inData)
173 {
174  int positionIndex = -1;
175  if (inEvent == ACCESSORIES_EVENT_MOVEPOSITIONID)
176  positionIndex = this->IndexOfMovingPosition(inId);
177  if (inEvent == ACCESSORIES_EVENT_MOVEPOSITIONINDEX)
178  positionIndex = inData;
179 
180  if (positionIndex > -1)
181  {
182  this->SetLastMovingPosition(positionIndex);
183  this->MoveBlink(this->GetMovingPositionByIndex(positionIndex), this->pMovingPositionBlinks[positionIndex]);
184  return;
185  }
186 
187  this->pLights->Event(inEvent, inData);
188 }
189 
190 #ifndef NO_EEPROM
191 int AccessoryLightMulti::EEPROMSave(int inPos, bool inSimulate)
192 {
193  for (uint8_t i = 0; i < this->lightsSize; i++)
194  inPos = this->pLights[i].EEPROMSave(inPos, inSimulate);
195 
196  return inPos;
197 }
198 
199 int AccessoryLightMulti::EEPROMLoad(int inPos)
200 {
201  for (uint8_t i = 0; i < this->lightsSize; i++)
202  inPos = this->pLights[i].EEPROMLoad(inPos);
203 
204  return inPos;
205 }
206 #endif
207 #ifdef ACCESSORIES_PRINT_ACCESSORIES
208 void AccessoryLightMulti::printAccessory()
209 {
210  Serial.println(F(" LightMulti"));
211  for (uint8_t i = 0; i < this->lightsSize; i++)
212  {
213  Serial.print(F(" Light "));
214  Serial.print(i);
215  Serial.print(F(" : "));
216  this->pLights[i].printAccessory();
217  }
218  this->printMovingPositions();
219 }
220 
221 void AccessoryLightMulti::printMovingPositions()
222 {
223  for (int i = 0; i < this->GetMovingPositionSize(); i++)
224  {
225  Serial.print(F(" MovingPosition "));
226  Serial.print(i);
227  Serial.print(F(": id "));
228  Serial.print(this->GetMovingPositionIdByIndex(i));
229  Serial.print(F(" / pos "));
230  Serial.print(this->GetMovingPositionByIndex(i), BIN);
231  Serial.print(F(" / blink "));
232  Serial.println(this->pMovingPositionBlinks[i], BIN);
233  }
234 }
235 #endif
236 
237 #endif
unsigned long GetMovingPositionIdByIndex(int inIndex) const
Definition: Accessory.hpp:313
void Event(ACCESSORIES_EVENT_TYPE inEvent = ACCESSORIES_EVENT_MOVEPOSITIONID, int inData = 0)
void SetLastMovingPosition(uint8_t inLastPositionIndex)
Definition: Accessory.cpp:157
int GetMovingPosition(unsigned long inId) const
Definition: Accessory.cpp:68
uint8_t IndexOfMovingPosition(unsigned long inId) const
Definition: Accessory.cpp:59
void SetBlinking(uint8_t inIndex, unsigned long inBlinkingDelay)
int GetMovingPositionByIndex(int inIndex) const
Definition: Accessory.hpp:307
void Event(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent = ACCESSORIES_EVENT_MOVEPOSITIONID, int inData = 0)
void beginLight(uint8_t inIndex, Port *inpPort, int inIntensity = 255)
void begin(Port *inpPort, int inIntensity = 255, Accessory *inpOwner = 0)
uint8_t AddMovingPosition(unsigned long inId, int inPosition)
Definition: Accessory.cpp:49
unsigned long GetDuration() const
Definition: Accessory.hpp:254
unsigned char AddMovingPosition(unsigned long inId, int inOnMask, int inBlinkMask = 0)
const uint8_t GetMovingPositionSize() const
Definition: Accessory.hpp:323
void AdjustMovingPositionBlinksSize(uint8_t inNewSize)
void SetBlinking(unsigned long inBlinkingDelay)
void SetDuration(unsigned long inDuration)
Definition: Accessory.hpp:344
Definition: Port.hpp:61
void begin(unsigned long inId, uint8_t inSize, unsigned long inBlinkDuration)
Port * GetPort() const
Definition: Accessory.hpp:185