Accessories
Arduino for motors and lights library.
Port.cpp
1 /*************************************************************
2 project: <Accessories>
3 author: <Thierry PARIS>
4 description: <Port>
5 *************************************************************/
6 
7 #include "Accessories.h"
8 
9 #ifdef ACCESSORIES_DEBUG_MODE
10 #ifdef ARDUINO_ARCH_SAM
11 void Port::CheckPinNb(int inPin, PIN_TYPE inType, const char *inFunc)
12 {
13  int pin = inPin;
14 
15 #ifndef NO_EXPANDER
16  // Check for expander id validity and pin number
17  unsigned long expanderId = 0;
18  if (GPIO_to_Arduino_pin((GPIO_pin_t)inPin) == -1)
19  expanderId = EXPANDER_PORT_EXPID(inPin);
20 
21  if (expanderId > 0)
22  {
23  PortExpander::CheckExpanderId(expanderId);
24  }
25  else
26  {
27 #endif
28  if (inType < ANALOG)
29  pin = GPIO_to_Arduino_pin((GPIO_pin_t)inPin);
30 
31  if (pin <= 0 || pin >= NUM_DIGITAL_PINS)
32  {
33  if (pin < A0 || pin > A7)
34  {
35  Serial.print(F("Pin "));
36  Serial.print(inPin);
37  Serial.print(F(" error in "));
38  Serial.println(inFunc);
39  }
40  }
41 #ifndef NO_EXPANDER
42  }
43 #endif
44 }
45 
46 #else
47 void Port::CheckPinNb(int inPin, PIN_TYPE inType, const __FlashStringHelper* inFunc)
48 {
49  int pin = inPin;
50 
51 #ifndef NO_EXPANDER
52  // Check for expander id validity and pin number
53  unsigned long expanderId = 0;
54  if (GPIO_to_Arduino_pin((GPIO_pin_t)inPin) == -1)
55  expanderId = EXPANDER_PORT_EXPID(inPin);
56 
57  if (expanderId > 0)
58  {
59  PortExpander::CheckExpanderId(expanderId);
60  }
61  else
62  {
63 #endif
64  if (inType < ANALOG)
65  pin = GPIO_to_Arduino_pin((GPIO_pin_t)inPin);
66 
67  if (pin <= 0 || pin >= NUM_DIGITAL_PINS)
68  {
69  if (pin < A0 || pin > A7)
70  {
71  Serial.print(F("Pin "));
72  Serial.print(inPin);
73  Serial.print(F(" error in "));
74  Serial.println(inFunc);
75  }
76  }
77 #ifndef NO_EXPANDER
78  }
79 #endif
80 }
81 #endif
82 #endif
83 
85 {
86  this->SetPinType(DIGITAL);
87  this->SetPortState(PORT_STOP);
88  this->speed = DEFAULTSPEED;
89 }
90 
91 int Port::MapValue(int inValue, PIN_TYPE inType) const
92 {
93  if (inType == UNDEFINED)
94  inType = this->GetPinType();
95 
96  if (inType == ANALOG_INVERTED)
97  return 255 - inValue;
98 
99  if (inType == DIGITAL_INVERTED)
100  {
101  if (inValue == LOW)
102  return HIGH;
103  return LOW;
104  }
105 
106  return inValue;
107 }
108 
109 int Port::beginPin(int inPin, PIN_TYPE inType) const
110 {
111  int pin = inPin;
112  if (inType == UNDEFINED)
113  inType = this->GetPinType();
114 
115 #ifndef NO_EXPANDER
116  // Check for expander id validity and pin number
117  unsigned long expanderId = 0;
118  if (GPIO_to_Arduino_pin((GPIO_pin_t)inPin) == -1)
119  expanderId = EXPANDER_PORT_EXPID(inPin);
120 
121  if (expanderId > 0)
122  {
123 #ifdef ACCESSORIES_DEBUG_MODE
124  PortExpander::CheckExpanderId(expanderId);
125 #endif
126  int pinExp = EXPANDER_PORT_PIN(inPin);
127  PortExpander::beginPin(pinExp, expanderId, inType);
128  }
129  else
130  {
131 #endif
132 
133  if (inType < ANALOG)
134  {
135  pin = Arduino_to_GPIO_pin(inPin);
136  pinMode2f((GPIO_pin_t) pin, OUTPUT);
137  }
138 #ifndef NO_EXPANDER
139  }
140 #endif
141 
142  MovePin(pin, LOW, inType);
143 
144  return pin;
145 }
146 
147 void Port::MovePin(int inPin, int inValue, PIN_TYPE inType) const
148 {
149  if (inType == UNDEFINED)
150  inType = this->GetPinType();
151 
152  CHECKPIN(inPin, inType, "Incorrect pin number in MovePin");
153 
154 #ifndef NO_EXPANDER
155  unsigned long expanderId = 0;
156  if (GPIO_to_Arduino_pin((GPIO_pin_t)inPin) == -1)
157  expanderId = EXPANDER_PORT_EXPID(inPin);
158 
159  if (expanderId > 0)
160  {
161 #ifdef ACCESSORIES_DEBUG_MODE
162  PortExpander::CheckExpanderId(expanderId);
163 #endif
164  int pinExp = EXPANDER_PORT_PIN(inPin);
165  if (inType < ANALOG)
166  PortExpander::digitalWrite(pinExp, expanderId, this->MapValue(inValue, inType));
167  else
168  {
169  if (inValue == HIGH)
170  PortExpander::analogWrite(pinExp, expanderId, this->MapValue(this->GetSpeed(), inType));
171  else
172  PortExpander::analogWrite(pinExp, expanderId, this->MapValue(0, inType));
173  }
174  }
175  else
176  {
177 #endif
178  if (inType < ANALOG)
179  digitalWrite2f((GPIO_pin_t)inPin, this->MapValue(inValue, inType));
180  else
181  if (inValue == HIGH)
182  analogWrite(inPin, this->MapValue(this->GetSpeed(), inType));
183  else
184  analogWrite(inPin, this->MapValue(0, inType));
185 #ifndef NO_EXPANDER
186 }
187 #endif
188 }
189 
190 int Port::SetSpeed(int inSpeed)
191 {
192  uint8_t oldSpeed = this->speed;
193  this->speed = inSpeed;
194  return oldSpeed;
195 }
196 
197 PORT_STATE Port::MoveToggle(unsigned long inDuration)
198 {
199  if (this->IsLeftDir())
200  {
201  MoveRightDir(inDuration);
202  }
203  else
204  {
205  MoveLeftDir(inDuration);
206  }
207 
208  return this->GetPortState();
209 }
210 
211 void Port::MoveLeftDir(unsigned long inDuration, int inSpeed)
212 {
213  int oldSpeed = -1;
214  if (inSpeed >= 0)
215  oldSpeed = this->SetSpeed(inSpeed);
216  this->MoveLeftDir(inDuration);
217  if (oldSpeed != -1)
218  this->SetSpeed(oldSpeed);
219 }
220 
221 void Port::MoveRightDir(unsigned long inDuration, int inSpeed)
222 {
223  int oldSpeed = -1;
224  if (inSpeed >= 0)
225  oldSpeed = this->SetSpeed(inSpeed);
226  this->MoveRightDir(inDuration);
227  if (oldSpeed != -1)
228  this->SetSpeed(oldSpeed);
229 }
230 
231 #ifdef ACCESSORIES_PRINT_ACCESSORIES
232 void Port::printPort()
233 {
234 }
235 
236 void Port::printPortPin(int inPin, PIN_TYPE inType)
237 {
238  Serial.print(" ");
239 #ifndef NO_EXPANDER
240  unsigned long expanderId = 0;
241  if (GPIO_to_Arduino_pin((GPIO_pin_t)inPin) == -1)
242  expanderId = EXPANDER_PORT_EXPID(inPin);
243 
244  if (expanderId > 0)
245  {
246  Serial.print(expanderId);
247  Serial.print("/");
248  Serial.print(EXPANDER_PORT_PIN(inPin));
249  }
250  else
251 #endif
252 
253  {
254  if (inType < ANALOG)
255  Serial.print(GPIO_to_Arduino_pin((GPIO_pin_t)inPin));
256  else
257  Serial.print(inPin);
258  }
259 
260  Serial.print(" (");
261 
262  switch (inType)
263  {
264  case UNDEFINED: Serial.print("UNDEFINED)"); break;
265  case DIGITAL: Serial.print("DIGITAL)"); break;
266  case DIGITAL_INVERTED: Serial.print("DIGITAL_INVERTED)"); break;
267  case ANALOG: Serial.print("ANALOG)"); break;
268  case ANALOG_INVERTED: Serial.print("ANALOG_INVERTED)"); break;
269  }
270 }
271 #endif
PORT_STATE GetPortState() const
Definition: Port.hpp:80
virtual void MoveLeftDir(unsigned long inDuration = 0)
Definition: Port.hpp:153
virtual void MoveRightDir(unsigned long inDuration = 0)
Definition: Port.hpp:157
bool IsLeftDir() const
Definition: Port.hpp:140
PORT_STATE MoveToggle(unsigned long inDuration = 0)
Definition: Port.cpp:197
virtual int SetSpeed(int inSpeed)
Definition: Port.cpp:190
int GetSpeed() const
Definition: Port.hpp:129
virtual void analogWrite(int inPin, int inValue) = 0
virtual void digitalWrite(int inPin, int inValue) = 0
void MovePin(int inPin, int inValue, PIN_TYPE inType = UNDEFINED) const
Definition: Port.cpp:147
virtual void beginPin(int inPin, PIN_TYPE inType) = 0
int beginPin(int inPin, PIN_TYPE inType = UNDEFINED) const
Definition: Port.cpp:109
PIN_TYPE GetPinType() const
Definition: Port.hpp:77
int MapValue(int inValue, PIN_TYPE inType = UNDEFINED) const
Definition: Port.cpp:91
int speed
Definition: Port.hpp:64
void SetPortState(PORT_STATE inState)
Definition: Port.hpp:87
void SetPinType(PIN_TYPE inType)
Definition: Port.hpp:84
Port()
Definition: Port.cpp:84