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 
20 enum PORT_STATE
21 {
22  PORT_STOP,
23  PORT_LEFT,
24  PORT_RIGHT
25 };
26 
27 #ifdef ACCESSORIES_DEBUG_MODE
28 #ifdef ARDUINO_ARCH_SAM
29 #define CHECKPIN(val, type, text) Port::CheckPinNb(val, type, text)
30 #else
31 #define CHECKPIN(val, type, text) Port::CheckPinNb(val, type, F(text))
32 #endif
33 #else
34 #define CHECKPIN(val, type, text)
35 #endif
36 
60 class Port
61 {
62  protected:
64  int speed;
65 
71  byte type_state;
72 
73 
74 public:
77  PIN_TYPE GetPinType() const { return (PIN_TYPE)(type_state & B00001111); }
80  PORT_STATE GetPortState() const { return (PORT_STATE)((type_state >> 4) & B00000011); }
81 
84  void SetPinType(PIN_TYPE inType) { type_state = (int)inType | this->GetPortState() << 4; }
87  void SetPortState(PORT_STATE inState) { type_state = this->GetPinType() | (int)inState << 4; }
88 
97  int MapValue(int inValue, PIN_TYPE inType = UNDEFINED) const;
103  int beginPin(int inPin, PIN_TYPE inType = UNDEFINED) const;
110  void MovePin(int inPin, int inValue, PIN_TYPE inType = UNDEFINED) const;
111 
112  public:
114  Port();
115 
117  inline virtual void begin() {}
123  inline virtual void beginByAccessory(int inStartingPosition) {}
124 
129  inline int GetSpeed() const { return this->speed; }
135  virtual int SetSpeed(int inSpeed);
136 
140  inline bool IsLeftDir() const { return this->GetPortState() == PORT_LEFT; }
144  inline bool IsRightDir() const { return this->GetPortState() == PORT_RIGHT; }
148  inline bool IsStopped() const { return this->GetPortState() == PORT_STOP; }
149 
153  inline virtual void MoveLeftDir(unsigned long inDuration = 0) { this->SetPortState(PORT_LEFT); }
157  inline virtual void MoveRightDir(unsigned long inDuration = 0) { this->SetPortState(PORT_RIGHT); }
164  void MoveLeftDir(unsigned long inDuration, int inSpeed);
171  void MoveRightDir(unsigned long inDuration, int inSpeed);
175  PORT_STATE MoveToggle(unsigned long inDuration = 0);
177  inline virtual void MoveStop() { this->SetPortState(PORT_STOP); }
183  inline virtual void MovePosition(unsigned long inDuration, int inEndPosition) {}
188  inline virtual int GetPosition() { return 0; }
189 
190 #ifdef ACCESSORIES_DEBUG_MODE
191 #ifdef ARDUINO_ARCH_SAM
192  static void CheckPinNb(int inPin, const char *infunc);
193  static void CheckDIOPinNb(GPIO_pin_t inPin, const char *infunc);
194 #else
195  static void CheckPinNb(int inPin, PIN_TYPE inType, const __FlashStringHelper *infunc);
196 #endif
197 #endif
198 #ifdef ACCESSORIES_PRINT_ACCESSORIES
199 
202  virtual void printPort();
206  static void printPortPin(int inPin, PIN_TYPE inType);
207 #endif
208 };
209 
210 
211 //-------------------------------------------------------------------
212 #endif
213 //-------------------------------------------------------------------
virtual int GetPosition()
Definition: Port.hpp:188
virtual void MoveStop()
Definition: Port.hpp:177
PORT_STATE MoveToggle(unsigned long inDuration = 0)
Definition: Port.cpp:185
virtual void MoveRightDir(unsigned long inDuration = 0)
Definition: Port.hpp:157
virtual void MoveLeftDir(unsigned long inDuration = 0)
Definition: Port.hpp:153
virtual void MovePosition(unsigned long inDuration, int inEndPosition)
Definition: Port.hpp:183
bool IsStopped() const
Definition: Port.hpp:148
bool IsRightDir() const
Definition: Port.hpp:144
bool IsLeftDir() const
Definition: Port.hpp:140
virtual int SetSpeed(int inSpeed)
Definition: Port.cpp:178
int GetSpeed() const
Definition: Port.hpp:129
Port()
Definition: Port.cpp:78
void MovePin(int inPin, int inValue, PIN_TYPE inType = UNDEFINED) const
Definition: Port.cpp:138
int beginPin(int inPin, PIN_TYPE inType = UNDEFINED) const
Definition: Port.cpp:103
virtual void beginByAccessory(int inStartingPosition)
Definition: Port.hpp:123
int MapValue(int inValue, PIN_TYPE inType = UNDEFINED) const
Definition: Port.cpp:85
virtual void begin()
Definition: Port.hpp:117
void SetPortState(PORT_STATE inState)
Definition: Port.hpp:87
void SetPinType(PIN_TYPE inType)
Definition: Port.hpp:84
PORT_STATE GetPortState() const
Definition: Port.hpp:80
PIN_TYPE GetPinType() const
Definition: Port.hpp:77
byte type_state
Definition: Port.hpp:71
int speed
Definition: Port.hpp:64
Definition: Port.hpp:60