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 PIN_TYPE GetPinType() const { return this->pinType; }
114  inline PORT_STATE GetState() const { return this->state; }
119  inline int GetSpeed() const { return this->speed; }
125  virtual int SetSpeed(int inSpeed);
126 
130  inline bool IsLeftDir() const { return this->state == PORT_LEFT; }
134  inline bool IsRightDir() const { return this->state == PORT_RIGHT; }
138  inline bool IsStopped() const { return this->state == PORT_STOP; }
139 
143  inline virtual void MoveLeftDir(unsigned long inDuration = 0) { this->state = PORT_LEFT; }
147  inline virtual void MoveRightDir(unsigned long inDuration = 0) { this->state = PORT_RIGHT; }
154  void MoveLeftDir(unsigned long inDuration, int inSpeed);
161  void MoveRightDir(unsigned long inDuration, int inSpeed);
165  PORT_STATE MoveToggle(unsigned long inDuration = 0);
167  inline virtual void MoveStop() { this->state = PORT_STOP; }
173  inline virtual void MovePosition(unsigned long inDuration, int inEndPosition) {}
178  inline virtual int GetPosition() { return 0; }
179 
180 #ifdef ACCESSORIES_DEBUG_MODE
181 #ifdef ARDUINO_ARCH_SAM
182  static void CheckPinNb(int inPin, const char *infunc);
183  static void CheckDIOPinNb(GPIO_pin_t inPin, const char *infunc);
184 #else
185  static void CheckPinNb(int inPin, PIN_TYPE inType, const __FlashStringHelper *infunc);
186 #endif
187 #endif
188 #ifdef ACCESSORIES_PRINT_ACCESSORIES
189 
192  virtual void printPort();
196  static void printPortPin(int inPin, PIN_TYPE inType);
197 #endif
198 };
199 
200 
201 //-------------------------------------------------------------------
202 #endif
203 //-------------------------------------------------------------------
virtual void MovePosition(unsigned long inDuration, int inEndPosition)
Definition: Port.hpp:173
virtual void MoveStop()
Definition: Port.hpp:167
PORT_STATE MoveToggle(unsigned long inDuration = 0)
Definition: Port.cpp:115
virtual void MoveRightDir(unsigned long inDuration = 0)
Definition: Port.hpp:147
virtual void MoveLeftDir(unsigned long inDuration = 0)
Definition: Port.hpp:143
bool IsStopped() const
Definition: Port.hpp:138
bool IsRightDir() const
Definition: Port.hpp:134
virtual int GetPosition()
Definition: Port.hpp:178
bool IsLeftDir() const
Definition: Port.hpp:130
virtual int SetSpeed(int inSpeed)
Definition: Port.cpp:108
int GetSpeed() const
Definition: Port.hpp:119
PORT_STATE GetState() const
Definition: Port.hpp:114
PIN_TYPE GetPinType() 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
virtual void beginByAccessory(int inStartingPosition)
Definition: Port.hpp:105
int MapValue(int inValue, PIN_TYPE inType = UNDEFINED) const
Definition: Port.cpp:56
int speed
Definition: Port.hpp:69
PORT_STATE state
Definition: Port.hpp:67
PIN_TYPE pinType
Definition: Port.hpp:65
Definition: Port.hpp:61