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