Accessories
Arduino for motors and lights library.
PortExpander.cpp
1 #include "Accessories.h"
2 
3 #ifndef NO_EXPANDER
4 
5 PortExpander *PortExpander::pFirstExpander = NULL;
6 
8 {
9  this->id = (int) UNDEFINED_ID;
10  this->pNextExpander = NULL;
11 }
12 
13 void PortExpander::Add(PortExpander *inExpander)
14 {
15  if (PortExpander::pFirstExpander == NULL)
16  {
17  PortExpander::pFirstExpander = inExpander;
18  inExpander->SetNextExpander(NULL);
19  }
20 
21  PortExpander *pCurr = PortExpander::pFirstExpander;
22 
23  while (pCurr->GetNextExpander() != NULL)
24  pCurr = pCurr->GetNextExpander();
25 
26  pCurr->SetNextExpander(inExpander);
27  inExpander->SetNextExpander(NULL);
28 }
29 
30 PortExpander *PortExpander::GetById(int inId)
31 {
32  PortExpander *pCurr = PortExpander::pFirstExpander;
33 
34  while (pCurr != NULL)
35  {
36  if (pCurr->id == inId)
37  return pCurr;
38  pCurr = pCurr->GetNextExpander();
39  }
40 
41  return NULL;
42 }
43 
44 #ifdef ACCESSORIES_DEBUG_MODE
45 void PortExpander::CheckAllExpanderPins()
46 {
47  PortExpander* pCurr = PortExpander::pFirstExpander;
48 
49  while (pCurr != NULL)
50  {
51  for (int i = 0; i < pCurr->GetPinsNumber(); i++)
52  if (GPIO_to_Arduino_pin((GPIO_pin_t)EXPANDER_PIN(pCurr->id, i)) != -1)
53  {
54  Serial.print(F("Warning : the pin "));
55  Serial.print(i);
56  Serial.print(F(" of the Expander "));
57  Serial.print(pCurr->id);
58  Serial.println(F(" cannot be used : conflict with internal pin numbering."));
59  }
60 
61  pCurr = pCurr->GetNextExpander();
62  }
63 }
64 
65 void PortExpander::CheckExpanderId(int inId)
66 {
67  PortExpander *pExp = PortExpander::GetById(inId);
68 
69  if ( pExp == NULL)
70  {
71  Serial.print(F("Expander "));
72  Serial.print(inId);
73  Serial.println(F(" unknown !"));
74  }
75 }
76 #endif
77 
78 void PortExpander::beginPin(int inPin, int inExpId, PIN_TYPE inType)
79 {
80  if (GPIO_to_Arduino_pin((GPIO_pin_t)EXPANDER_PIN(inExpId, inPin)) != -1)
81  {
82  Serial.print(F("Warning : the pin "));
83  Serial.print(inPin);
84  Serial.print(F(" of the Expander "));
85  Serial.print(inExpId);
86  Serial.println(F(" cannot be used : conflict with internal pin numbering."));
87  }
88 
89  PortExpander *pCurr = PortExpander::GetById(inExpId);
90  if (pCurr != NULL)
91  pCurr->beginPin(inPin, inType);
92 }
93 
94 void PortExpander::digitalWrite(int inPin, int inExpId, int inValue)
95 {
96  PortExpander *pCcurr = PortExpander::GetById(inExpId);
97  if (pCcurr != NULL)
98  pCcurr->digitalWrite(inPin, inValue);
99 }
100 
101 void PortExpander::analogWrite(int inPin, int inExpId, int inValue)
102 {
103  PortExpander *pCurr = PortExpander::GetById(inExpId);
104  if (pCurr != NULL)
105  pCurr->analogWrite(inPin, inValue);
106 }
107 #endif
virtual void analogWrite(int inPin, int inValue) = 0
virtual void digitalWrite(int inPin, int inValue) = 0
virtual void beginPin(int inPin, PIN_TYPE inType) = 0
virtual byte GetPinsNumber()