Accessories
Arduino for motors and lights library.
Port.hpp
1 //-------------------------------------------------------------------
2 #ifndef __port_H__
3 #define __port_H__
4 //-------------------------------------------------------------------
5 
6 #define DEFAULTSPEED 255
7 #define DEFAULTDURATION 100
8 
10 enum PIN_TYPE
11 {
12  UNDEFINED = 0,
13  DIGITAL = 1,
14  DIGITAL_INVERTED = 2,
15  ANALOG = 11,
16  ANALOG_INVERTED = 12
17 };
18 
21 enum PORT_STATE
22 {
23  PORT_STOP,
24  PORT_LEFT,
25  PORT_RIGHT
26 };
27 
28 #ifdef ACCESSORIES_DEBUG_MODE
29 #ifdef ARDUINO_ARCH_SAM
30 #define CHECKPIN(val, type, text) Port::CheckPinNb(val, type, text)
31 #else
32 #define CHECKPIN(val, type, text) Port::CheckPinNb(val, type, F(text))
33 #endif
34 #else
35 #define CHECKPIN(val, type, text)
36 #endif
37 
61 class Port
62 {
63  protected:
65  PIN_TYPE pinType;
67  PORT_STATE state;
69  int speed;
70 
79  int MapValue(int inValue, PIN_TYPE inType = UNDEFINED) const;
85  int beginPin(int inPin, PIN_TYPE inType = UNDEFINED) const;
92  void MovePin(int inPin, int inValue, PIN_TYPE inType = UNDEFINED) const;
93 
94  public:
96  Port();
97 
99  inline virtual void begin() {}
105  inline virtual void beginByAccessory(int inStartingPosition) {}
106 
110  inline PORT_STATE GetState() const { return this->state; }
115  inline int GetSpeed() const { return this->speed; }
121  virtual int SetSpeed(int inSpeed);
122 
126  inline bool IsLeftDir() const { return this->state == PORT_LEFT; }
130  inline bool IsRightDir() const { return this->state == PORT_RIGHT; }
134  inline bool IsStopped() const { return this->state == PORT_STOP; }
135 
139  inline virtual void MoveLeftDir(unsigned long inDuration = 0) { this->state = PORT_LEFT; }
143  inline virtual void MoveRightDir(unsigned long inDuration = 0) { this->state = PORT_RIGHT; }
150  void MoveLeftDir(unsigned long inDuration, int inSpeed);
157  void MoveRightDir(unsigned long inDuration, int inSpeed);
161  PORT_STATE MoveToggle(unsigned long inDuration = 0);
163  inline virtual void MoveStop() { this->state = PORT_STOP; }
169  inline virtual void MovePosition(unsigned long inDuration, int inEndPosition) {}
174  inline virtual int GetPosition() { return 0; }
175 
176 #ifdef ACCESSORIES_DEBUG_MODE
177 #ifdef ARDUINO_ARCH_SAM
178  static void CheckPinNb(int inPin, const char *infunc);
179  static void CheckDIOPinNb(GPIO_pin_t inPin, const char *infunc);
180 #else
181  static void CheckPinNb(int inPin, PIN_TYPE inType, const __FlashStringHelper *infunc);
182 #endif
183 #endif
184 #ifdef ACCESSORIES_PRINT_ACCESSORIES
185 
188  virtual void printPort();
192  static void printPortPin(int inPin, PIN_TYPE inType);
193 #endif
194 };
195 
196 
197 //-------------------------------------------------------------------
198 #endif
199 //-------------------------------------------------------------------
PORT_STATE MoveToggle(unsigned long inDuration = 0)
Definition: Port.cpp:115
virtual void MoveRightDir(unsigned long inDuration = 0)
Definition: Port.hpp:143
virtual void MoveLeftDir(unsigned long inDuration = 0)
Definition: Port.hpp:139
virtual void MovePosition(unsigned long inDuration, int inEndPosition)
Definition: Port.hpp:169
bool IsStopped() const
Definition: Port.hpp:134
bool IsRightDir() const
Definition: Port.hpp:130
virtual int GetPosition()
Definition: Port.hpp:174
bool IsLeftDir() const
Definition: Port.hpp:126
virtual int SetSpeed(int inSpeed)
Definition: Port.cpp:108
int GetSpeed() const
Definition: Port.hpp:115
PORT_STATE GetState() const
Definition: Port.hpp:110
virtual void begin()
Definition: Port.hpp:99
Port()
Definition: Port.cpp:49
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
virtual void beginByAccessory(int inStartingPosition)
Definition: Port.hpp:105
int MapValue(int inValue, PIN_TYPE inType = UNDEFINED) const
Definition: Port.cpp:56
PIN_TYPE pinType
Definition: Port.hpp:65
virtual void MoveStop()
Definition: Port.hpp:163
Definition: Port.hpp:61