Accessories
Arduino for motors and lights library.
AccessoryLight.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 "AccessoryLight.hpp"
9 
10 #ifndef NO_LIGHT
11 
13 {
14 }
15 
16 void AccessoryLight::begin(Port *inpPort, unsigned long inId, unsigned long inBlinkDuration, int inIntensity)
17 {
18  this->pPort = inpPort;
19  this->pLight = new AccessoryBaseLight(this);
20 
21  this->pLight->SetBlinking(inBlinkDuration);
22 
23  this->pLight->begin(inpPort, inIntensity);
24  this->AddMovingPosition(inId, LIGHTON);
25 }
26 
27 void AccessoryLight::Move(unsigned long inId)
28 {
29  if (this->GetMovingPositionSize() == 1)
30  {
31  this->Toggle();
32  return;
33  }
34 
35  int position = this->IndexOfMovingPositionById(inId);
36 
37  if (position != -1)
38  this->SetState((ACC_STATE) position);
39 }
40 
41 void AccessoryLight::Event(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent, int inData)
42 {
43  if (inEvent == ACCESSORIES_EVENT_MOVEPOSITIONINDEX)
44  {
45  this->SetLastMovingPosition(inData);
46  int oldValue = this->pPort->GetSpeed();
47  this->pPort->SetSpeed(this->GetMovingPositionValueByIndex(inData));
48  this->LightOn();
49  this->pPort->SetSpeed(oldValue);
50  return;
51  }
52 
53  this->pLight->Event(inEvent, inData);
54 }
55 
56 #ifndef NO_EEPROM
57 int AccessoryLight::EEPROMLoad(int inPos)
58 {
59  inPos = this->Accessory::EEPROMLoad(inPos);
60 
61  switch (this->GetState())
62  {
63  case LIGHTON: this->pPort->MoveLeftDir(); break;
64  case LIGHTOFF: this->pPort->MoveStop(); break;
65  case LIGHTBLINK: break;
66  }
67 
68  return inPos;
69 }
70 #endif
71 
72 #ifdef ACCESSORIES_PRINT_ACCESSORIES
73 void AccessoryLight::printAccessory()
74 {
75  Serial.print(F(" Light : ID "));
76  Serial.print(this->GetMovingPositionIdByIndex(0));
77  Serial.print(F(" / "));
78  this->pLight->printAccessory();
79 }
80 #endif
81 
82 #endif
unsigned long GetMovingPositionIdByIndex(uint8_t inIndex) const
Definition: Accessory.hpp:339
virtual void MoveStop()
Definition: Port.hpp:177
virtual void MoveLeftDir(unsigned long inDuration = 0)
Definition: Port.hpp:153
ACC_STATE GetState() const
Definition: Accessory.hpp:229
virtual int EEPROMLoad(int inPos)
int EEPROMLoad(int inPos)
void Event(ACCESSORIES_EVENT_TYPE inEvent = ACCESSORIES_EVENT_MOVEPOSITIONID, int inData = 0)
int GetMovingPositionValueByIndex(uint8_t inIndex) const
Definition: Accessory.hpp:333
virtual int SetSpeed(int inSpeed)
Definition: Port.cpp:190
void Event(unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent = ACCESSORIES_EVENT_MOVEPOSITIONID, int inData = 0)
void SetState(ACC_STATE inState)
ACC_STATE Toggle()
const uint8_t GetMovingPositionSize() const
Definition: Accessory.hpp:349
uint8_t AddMovingPosition(unsigned long inId, int inPosition)
Definition: Accessory.cpp:58
void begin(Port *inpPort, int inIntensity = 255, Accessory *inpOwner = 0)
void SetLastMovingPosition(uint8_t inLastPositionIndex)
Definition: Accessory.cpp:216
void SetBlinking(unsigned long inBlinkingDelay)
uint8_t IndexOfMovingPositionById(unsigned long inId) const
Definition: Accessory.cpp:95
int GetSpeed() const
Definition: Port.hpp:129
Port * pPort
Definition: Accessory.hpp:187
Definition: Port.hpp:60
void begin(Port *inpPort, unsigned long inId, unsigned long inBlinkDuration = 0, int inIntensity = 255)