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  if (inType < ANALOG)
15  pin = GPIO_to_Arduino_pin((GPIO_pin_t)inPin);
16 
17  if (pin <= 0 || pin >= NUM_DIGITAL_PINS)
18  {
19  if (pin < A0 || pin > A7)
20  {
21  Serial.print(F("Pin "));
22  Serial.print(inPin);
23  Serial.print(F(" error in "));
24  Serial.println(inFunc);
25  }
26  }
27 }
28 #else
29 void Port::CheckPinNb(int inPin, PIN_TYPE inType, const __FlashStringHelper *inFunc)
30 {
31  int pin = inPin;
32  if (inType < ANALOG)
33  pin = GPIO_to_Arduino_pin((GPIO_pin_t)inPin);
34 
35  if (pin <= 0 || pin >= NUM_DIGITAL_PINS)
36  {
37  if (pin < A0 || pin > A7)
38  {
39  Serial.print(F("Pin "));
40  Serial.print(inPin);
41  Serial.print(F(" error in "));
42  Serial.println(inFunc);
43  }
44  }
45 }
46 #endif
47 #endif
48 
50 {
51  this->pinType = DIGITAL;
52  this->state = PORT_STOP;
53  this->speed = DEFAULTSPEED;
54 }
55 
56 int Port::MapValue(int inValue, PIN_TYPE inType) const
57 {
58  if (inType == UNDEFINED)
59  inType = this->pinType;
60 
61  if (inType == ANALOG_INVERTED)
62  return 255 - inValue;
63 
64  if (inType == DIGITAL_INVERTED)
65  {
66  if (inValue == LOW)
67  return HIGH;
68  return LOW;
69  }
70 
71  return inValue;
72 }
73 
74 int Port::beginPin(int inPin, PIN_TYPE inType) const
75 {
76  int pin = -1;
77  if (inType == UNDEFINED)
78  inType = this->pinType;
79  if (inType < ANALOG)
80  {
81  pin = Arduino_to_GPIO_pin(inPin);
82  pinMode2f((GPIO_pin_t)pin, OUTPUT);
83  }
84  else
85  pin = inPin;
86 
87  MovePin(pin, LOW, inType);
88 
89  return pin;
90 }
91 
92 void Port::MovePin(int inPin, int inValue, PIN_TYPE inType) const
93 {
94  if (inType == UNDEFINED)
95  inType = this->pinType;
96 
97  CHECKPIN(inPin, inType, "Incorrect pin number in MovePin");
98 
99  if (inType < ANALOG)
100  digitalWrite2f((GPIO_pin_t)inPin, this->MapValue(inValue, inType));
101  else
102  if (inValue == HIGH)
103  analogWrite(inPin, this->MapValue(this->GetSpeed(), inType));
104  else
105  analogWrite(inPin, this->MapValue(0, inType));
106 }
107 
108 int Port::SetSpeed(int inSpeed)
109 {
110  uint8_t oldSpeed = this->speed;
111  this->speed = inSpeed;
112  return oldSpeed;
113 }
114 
115 PORT_STATE Port::MoveToggle(unsigned long inDuration)
116 {
117  if (this->IsLeftDir())
118  {
119  MoveRightDir(inDuration);
120  }
121  else
122  {
123  MoveLeftDir(inDuration);
124  }
125 
126  return this->GetState();
127 }
128 
129 void Port::MoveLeftDir(unsigned long inDuration, int inSpeed)
130 {
131  int oldSpeed = -1;
132  if (inSpeed >= 0)
133  oldSpeed = this->SetSpeed(inSpeed);
134  this->MoveLeftDir(inDuration);
135  if (oldSpeed != -1)
136  this->SetSpeed(oldSpeed);
137 }
138 
139 void Port::MoveRightDir(unsigned long inDuration, int inSpeed)
140 {
141  int oldSpeed = -1;
142  if (inSpeed >= 0)
143  oldSpeed = this->SetSpeed(inSpeed);
144  this->MoveRightDir(inDuration);
145  if (oldSpeed != -1)
146  this->SetSpeed(oldSpeed);
147 }
148 
149 #ifdef ACCESSORIES_PRINT_ACCESSORIES
150 void Port::printPort()
151 {
152 }
153 
154 void Port::printPortPin(int inPin, PIN_TYPE inType)
155 {
156  Serial.print(" ");
157  if (inType < ANALOG)
158  Serial.print(GPIO_to_Arduino_pin((GPIO_pin_t)inPin));
159  else
160  Serial.print(inPin);
161  Serial.print(" (");
162  switch (inType)
163  {
164  case UNDEFINED: Serial.print("UNDEFINED)"); break;
165  case DIGITAL: Serial.print("DIGITAL)"); break;
166  case DIGITAL_INVERTED: Serial.print("DIGITAL_INVERTED)"); break;
167  case ANALOG: Serial.print("ANALOG)"); break;
168  case ANALOG_INVERTED: Serial.print("ANALOG_INVERTED)"); break;
169  }
170 }
171 #endif
PORT_STATE GetState() const
Definition: Port.hpp:110
virtual void MoveLeftDir(unsigned long inDuration = 0)
Definition: Port.hpp:139
virtual void MoveRightDir(unsigned long inDuration = 0)
Definition: Port.hpp:143
bool IsLeftDir() const
Definition: Port.hpp:126
PORT_STATE MoveToggle(unsigned long inDuration = 0)
Definition: Port.cpp:115
virtual int SetSpeed(int inSpeed)
Definition: Port.cpp:108
int GetSpeed() const
Definition: Port.hpp:115
void MovePin(int inPin, int inValue, PIN_TYPE inType = UNDEFINED) const
Definition: Port.cpp:92
int beginPin(int inPin, PIN_TYPE inType = UNDEFINED) const
Definition: Port.cpp:74
int speed
Definition: Port.hpp:69
PORT_STATE state
Definition: Port.hpp:67
int MapValue(int inValue, PIN_TYPE inType = UNDEFINED) const
Definition: Port.cpp:56
PIN_TYPE pinType
Definition: Port.hpp:65
Port()
Definition: Port.cpp:49