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 ARDUINO_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 = EXPANDER_PORT_EXPID(inPin);
147  if (expanderId > 0)
148  {
149 #ifdef ARDUINO_DEBUG_MODE
150  PortExpander::CheckExpanderId(expanderId);
151 #endif
152  int pinExp = EXPANDER_PORT_PIN(inPin);
153  if (inType < ANALOG)
154  PortExpander::digitalWrite(pinExp, expanderId, this->MapValue(inValue, inType));
155  else
156  {
157  if (inValue == HIGH)
158  PortExpander::analogWrite(pinExp, expanderId, this->MapValue(this->GetSpeed(), inType));
159  else
160  PortExpander::analogWrite(pinExp, expanderId, this->MapValue(0, inType));
161  }
162  }
163  else
164  {
165 #endif
166  if (inType < ANALOG)
167  digitalWrite2f((GPIO_pin_t)inPin, this->MapValue(inValue, inType));
168  else
169  if (inValue == HIGH)
170  analogWrite(inPin, this->MapValue(this->GetSpeed(), inType));
171  else
172  analogWrite(inPin, this->MapValue(0, inType));
173 #ifndef NO_EXPANDER
174 }
175 #endif
176 }
177 
178 int Port::SetSpeed(int inSpeed)
179 {
180  uint8_t oldSpeed = this->speed;
181  this->speed = inSpeed;
182  return oldSpeed;
183 }
184 
185 PORT_STATE Port::MoveToggle(unsigned long inDuration)
186 {
187  if (this->IsLeftDir())
188  {
189  MoveRightDir(inDuration);
190  }
191  else
192  {
193  MoveLeftDir(inDuration);
194  }
195 
196  return this->GetPortState();
197 }
198 
199 void Port::MoveLeftDir(unsigned long inDuration, int inSpeed)
200 {
201  int oldSpeed = -1;
202  if (inSpeed >= 0)
203  oldSpeed = this->SetSpeed(inSpeed);
204  this->MoveLeftDir(inDuration);
205  if (oldSpeed != -1)
206  this->SetSpeed(oldSpeed);
207 }
208 
209 void Port::MoveRightDir(unsigned long inDuration, int inSpeed)
210 {
211  int oldSpeed = -1;
212  if (inSpeed >= 0)
213  oldSpeed = this->SetSpeed(inSpeed);
214  this->MoveRightDir(inDuration);
215  if (oldSpeed != -1)
216  this->SetSpeed(oldSpeed);
217 }
218 
219 #ifdef ACCESSORIES_PRINT_ACCESSORIES
220 void Port::printPort()
221 {
222 }
223 
224 void Port::printPortPin(int inPin, PIN_TYPE inType)
225 {
226  Serial.print(" ");
227 #ifndef NO_EXPANDER
228  unsigned long expanderId = EXPANDER_PORT_EXPID(inPin);
229  if (expanderId > 0)
230  {
231  Serial.print(expanderId);
232  Serial.print("/");
233  Serial.print(EXPANDER_PORT_PIN(inPin));
234  }
235  else
236 #endif
237 
238  {
239  if (inType < ANALOG)
240  Serial.print(GPIO_to_Arduino_pin((GPIO_pin_t)inPin));
241  else
242  Serial.print(inPin);
243  }
244 
245  Serial.print(" (");
246 
247  switch (inType)
248  {
249  case UNDEFINED: Serial.print("UNDEFINED)"); break;
250  case DIGITAL: Serial.print("DIGITAL)"); break;
251  case DIGITAL_INVERTED: Serial.print("DIGITAL_INVERTED)"); break;
252  case ANALOG: Serial.print("ANALOG)"); break;
253  case ANALOG_INVERTED: Serial.print("ANALOG_INVERTED)"); break;
254  }
255 }
256 #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:185
virtual int SetSpeed(int inSpeed)
Definition: Port.cpp:178
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: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
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