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  int speed;
66 
68  byte type_state;
69 
70 
71 public:
74  PIN_TYPE GetPinType() const { return (PIN_TYPE)(type_state & B00001111); }
77  PORT_STATE GetPortState() const { return (PORT_STATE)((type_state >> 4) & B00000011); }
78 
81  void SetPinType(PIN_TYPE inType) { type_state = (int)inType | this->GetPortState() << 4; }
84  void SetPortState(PORT_STATE inState) { type_state = this->GetPinType() | (int)inState << 4; }
85 
94  int MapValue(int inValue, PIN_TYPE inType = UNDEFINED) const;
100  int beginPin(int inPin, PIN_TYPE inType = UNDEFINED) const;
107  void MovePin(int inPin, int inValue, PIN_TYPE inType = UNDEFINED) const;
108 
109  public:
111  Port();
112 
114  inline virtual void begin() {}
120  inline virtual void beginByAccessory(int inStartingPosition) {}
121 
126  inline int GetSpeed() const { return this->speed; }
132  virtual int SetSpeed(int inSpeed);
133 
137  inline bool IsLeftDir() const { return this->GetPortState() == PORT_LEFT; }
141  inline bool IsRightDir() const { return this->GetPortState() == PORT_RIGHT; }
145  inline bool IsStopped() const { return this->GetPortState() == PORT_STOP; }
146 
150  inline virtual void MoveLeftDir(unsigned long inDuration = 0) { this->SetPortState(PORT_LEFT); }
154  inline virtual void MoveRightDir(unsigned long inDuration = 0) { this->SetPortState(PORT_RIGHT); }
161  void MoveLeftDir(unsigned long inDuration, int inSpeed);
168  void MoveRightDir(unsigned long inDuration, int inSpeed);
172  PORT_STATE MoveToggle(unsigned long inDuration = 0);
174  inline virtual void MoveStop() { this->SetPortState(PORT_STOP); }
180  inline virtual void MovePosition(unsigned long inDuration, int inEndPosition) {}
185  inline virtual int GetPosition() { return 0; }
186 
187 #ifdef ACCESSORIES_DEBUG_MODE
188 #ifdef ARDUINO_ARCH_SAM
189  static void CheckPinNb(int inPin, const char *infunc);
190  static void CheckDIOPinNb(GPIO_pin_t inPin, const char *infunc);
191 #else
192  static void CheckPinNb(int inPin, PIN_TYPE inType, const __FlashStringHelper *infunc);
193 #endif
194 #endif
195 #ifdef ACCESSORIES_PRINT_ACCESSORIES
196 
199  virtual void printPort();
203  static void printPortPin(int inPin, PIN_TYPE inType);
204 #endif
205 };
206 
207 
208 //-------------------------------------------------------------------
209 #endif
210 //-------------------------------------------------------------------
virtual int GetPosition()
Definition: Port.hpp:185
virtual void MoveStop()
Definition: Port.hpp:174
PORT_STATE MoveToggle(unsigned long inDuration = 0)
Definition: Port.cpp:115
virtual void MoveRightDir(unsigned long inDuration = 0)
Definition: Port.hpp:154
virtual void MoveLeftDir(unsigned long inDuration = 0)
Definition: Port.hpp:150
virtual void MovePosition(unsigned long inDuration, int inEndPosition)
Definition: Port.hpp:180
bool IsStopped() const
Definition: Port.hpp:145
bool IsRightDir() const
Definition: Port.hpp:141
bool IsLeftDir() const
Definition: Port.hpp:137
virtual int SetSpeed(int inSpeed)
Definition: Port.cpp:108
int GetSpeed() const
Definition: Port.hpp:126
virtual void beginByAccessory(int inStartingPosition)
Definition: Port.hpp:120
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 MapValue(int inValue, PIN_TYPE inType = UNDEFINED) const
Definition: Port.cpp:56
virtual void begin()
Definition: Port.hpp:114
void SetPortState(PORT_STATE inState)
Definition: Port.hpp:84
void SetPinType(PIN_TYPE inType)
Definition: Port.hpp:81
PORT_STATE GetPortState() const
Definition: Port.hpp:77
PIN_TYPE GetPinType() const
Definition: Port.hpp:74
byte type_state
Definition: Port.hpp:68
int speed
Definition: Port.hpp:65
Definition: Port.hpp:61