COSA
An Object-Oriented Platform for Arduino Programming
Pin.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_PIN_HH
22 #define COSA_PIN_HH
23 
24 #include "Cosa/Types.h"
25 #include "Cosa/Bits.h"
26 #include "Cosa/IOStream.hh"
27 
28 class OutputPin;
29 
35 class Pin {
36 public:
42  static uint8_t MASK(uint8_t pin)
43  __attribute__((always_inline))
44  {
45  return (_BV(Board::BIT(pin)));
46  }
47 
53  static volatile uint8_t* PIN(uint8_t pin)
54  __attribute__((always_inline))
55  {
56  return (Board::SFR(pin));
57  }
58 
64  static volatile uint8_t* DDR(uint8_t pin)
65  __attribute__((always_inline))
66  {
67  return (Board::SFR(pin) + 1);
68  }
69 
75  static volatile uint8_t* PORT(uint8_t pin)
76  __attribute__((always_inline))
77  {
78  return (Board::SFR(pin) + 2);
79  }
80 
84  enum Direction {
85  MSB_FIRST = 0,
87  } __attribute__((packed));
88 
93  Pin(uint8_t pin) :
94  m_sfr(Board::SFR(pin)),
95  m_mask(MASK(pin)),
96  m_pin(pin)
97  {}
98 
103  uint8_t pin() const
104  {
105  return (m_pin);
106  }
107 
112  bool is_set() const
113  __attribute__((always_inline))
114  {
115  return ((*PIN() & m_mask) != 0);
116  }
117 
122  bool is_high() const
123  __attribute__((always_inline))
124  {
125  return ((*PIN() & m_mask) != 0);
126  }
127 
132  bool is_on() const
133  __attribute__((always_inline))
134  {
135  return ((*PIN() & m_mask) != 0);
136  }
137 
142  bool is_clear() const
143  __attribute__((always_inline))
144  {
145  return ((*PIN() & m_mask) == 0);
146  }
147 
152  bool is_low() const
153  __attribute__((always_inline))
154  {
155  return ((*PIN() & m_mask) == 0);
156  }
157 
162  bool is_off() const
163  __attribute__((always_inline))
164  {
165  return ((*PIN() & m_mask) == 0);
166  }
167 
172  bool read() const
173  __attribute__((always_inline))
174  {
175  return ((*PIN() & m_mask) != 0);
176  }
177 
185  uint8_t read(OutputPin& clk, Direction order = MSB_FIRST) const;
186 
194  __attribute__((always_inline))
195  {
196  return ((*Board::SFR(pin) & MASK(pin)) != 0);
197  }
198 
204  operator bool() const
205  __attribute__((always_inline))
206  {
207  return ((*PIN() & m_mask) != 0);
208  }
209 
215  Pin& operator>>(uint8_t& var)
216  __attribute__((always_inline))
217  {
218  var = ((*PIN() & m_mask) != 0);
219  return (*this);
220  }
221 
222 protected:
224  volatile uint8_t* const m_sfr;
225 
227  const uint8_t m_mask;
228 
230  const uint8_t m_pin;
231 
236  volatile uint8_t* PIN() const
237  {
238  return (m_sfr);
239  }
240 
245  volatile uint8_t* DDR() const
246  {
247  return (m_sfr + 1);
248  }
249 
254  volatile uint8_t* PORT() const
255  {
256  return (m_sfr + 2);
257  }
258 
263  volatile uint8_t* PCIMR() const
264  __attribute__((always_inline))
265  {
266  return (Board::PCIMR(m_pin));
267  }
268 };
269 
275 #define asserted(pin) \
276  for (uint8_t __i = (pin.toggle(), 1); __i != 0; __i--, pin.toggle())
277 
278 #endif
static bool read(Board::DigitalPin pin)
Definition: Pin.hh:193
uint8_t pin() const
Definition: Pin.hh:103
Definition: Pin.hh:35
static uint8_t MASK(uint8_t pin)
Definition: Pin.hh:42
volatile uint8_t *const m_sfr
Definition: Pin.hh:224
Pin(uint8_t pin)
Definition: Pin.hh:93
static volatile uint8_t * PIN(uint8_t pin)
Definition: Pin.hh:53
bool is_high() const
Definition: Pin.hh:122
Direction
Definition: Pin.hh:84
bool is_set() const
Definition: Pin.hh:112
bool is_off() const
Definition: Pin.hh:162
Pin & operator>>(uint8_t &var)
Definition: Pin.hh:215
bool is_low() const
Definition: Pin.hh:152
bool read() const
Definition: Pin.hh:172
bool is_on() const
Definition: Pin.hh:132
bool is_clear() const
Definition: Pin.hh:142
static volatile uint8_t * PORT(uint8_t pin)
Definition: Pin.hh:75
const uint8_t m_pin
Definition: Pin.hh:230
const uint8_t m_mask
Definition: Pin.hh:227
volatile uint8_t * PORT() const
Definition: Pin.hh:254
volatile uint8_t * PIN() const
Definition: Pin.hh:236
volatile uint8_t * PCIMR() const
Definition: Pin.hh:263
static volatile uint8_t * DDR(uint8_t pin)
Definition: Pin.hh:64
volatile uint8_t * DDR() const
Definition: Pin.hh:245