COSA
An Object-Oriented Platform for Arduino Programming
SRPO.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_SRPO_HH
22 #define COSA_SRPO_HH
23 
24 #include "Cosa/SPI.hh"
25 
73 template<uint8_t N>
74 class SRPO : public SPI::Driver {
75 public:
77  static const uint8_t PINS = N * CHARBITS;
78 
85 #if !defined(BOARD_ATTINY)
88  SPI::Driver(cs, SPI::PULSE_HIGH, rate)
89  {
90  clear();
91  update();
92  }
93 #else
96  SPI::Driver(cs, SPI::PULSE_HIGH, rate)
97  {
98  clear();
99  update();
100  }
101 #endif
102 
109  bool is_set(uint8_t pin)
110  __attribute__((always_inline))
111  {
112  uint8_t ix = (pin >> 3);
113  return ((m_port[ix] & _BV(pin & 0x7)) != 0);
114  }
115 
122  void is_clear(uint8_t pin)
123  __attribute__((always_inline))
124  {
125  uint8_t ix = (pin >> 3);
126  return ((m_port[ix] & _BV(pin & 0x7)) == 0);
127  }
128 
134  void set(uint8_t pin)
135  __attribute__((always_inline))
136  {
137  uint8_t ix = (pin >> 3);
138  m_port[ix] |= _BV(pin & 0x7);
139  }
140 
146  void clear(uint8_t pin)
147  __attribute__((always_inline))
148  {
149  uint8_t ix = (pin >> 3);
150  m_port[ix] &= ~_BV(pin & 0x7);
151  }
152 
158  void toggle(uint8_t pin)
159  __attribute__((always_inline))
160  {
161  if (is_set(pin))
162  clear(pin);
163  else
164  set(pin);
165  }
166 
171  void set()
172  __attribute__((always_inline))
173  {
174  memset(m_port, 0xff, N);
175  }
176 
181  void clear()
182  __attribute__((always_inline))
183  {
184  memset(m_port, 0, N);
185  }
186 
190  void update()
191  {
192  spi.acquire(this);
193  spi.begin();
194  uint8_t ix = N - 1;
196  while (ix--) {
197  uint8_t data = m_port[ix];
199  spi.transfer_start(data);
200  }
202  spi.end();
203  spi.release();
204  }
205 
209  class OutputPin {
210  public:
211  OutputPin(SRPO<N>* srpo, uint8_t pin) :
212  m_srpo(srpo),
213  m_pin(pin)
214  {
215  }
216 
221  void set()
222  __attribute__((always_inline))
223  {
224  m_srpo->set(m_pin);
225  }
226 
231  void clear()
232  __attribute__((always_inline))
233  {
234  m_srpo->clear(m_pin);
235  }
236 
241  void toggle()
242  __attribute__((always_inline))
243  {
244  m_srpo->toggle(m_pin);
245  }
246 
247  protected:
249  const uint8_t m_pin;
250  };
251 
252 protected:
254  uint8_t m_port[N];
255 };
256 #endif
void clear()
Definition: SRPO.hh:231
Pulse high on end of transaction.
Definition: SPI.hh:83
Clock
Definition: SPI.hh:60
Definition: SPI.hh:57
void acquire(Driver *dev)
Definition: SOFT_SPI.cpp:43
void toggle()
Definition: SRPO.hh:241
SRPO< N > * m_srpo
Definition: SRPO.hh:248
uint8_t m_port[N]
Definition: SRPO.hh:254
Definition: SRPO.hh:74
void is_clear(uint8_t pin)
Definition: SRPO.hh:122
void update()
Definition: SRPO.hh:190
bool is_set(uint8_t pin)
Definition: SRPO.hh:109
#define CHARBITS
Definition: Types.h:57
void begin()
Definition: SPI.hh:216
void end()
Definition: SPI.hh:226
void clear()
Definition: SRPO.hh:181
uint8_t transfer_await()
Definition: SPI.hh:249
void toggle(uint8_t pin)
Definition: SRPO.hh:158
void transfer_start(uint8_t data)
Definition: SPI.hh:238
const uint8_t m_pin
Definition: SRPO.hh:249
SPI spi
Definition: SPI.cpp:29
SRPO(Board::DigitalPin cs=Board::D10, SPI::Clock rate=SPI::DEFAULT_CLOCK)
Definition: SRPO.hh:86
void release()
Definition: SOFT_SPI.cpp:64
void clear(uint8_t pin)
Definition: SRPO.hh:146
Default clock rate.
Definition: SPI.hh:68
OutputPin(SRPO< N > *srpo, uint8_t pin)
Definition: SRPO.hh:211
static const uint8_t PINS
Definition: SRPO.hh:77
Driver(Board::DigitalPin cs, Pulse pulse=DEFAULT_PULSE, Clock rate=DEFAULT_CLOCK, uint8_t mode=0, Order order=MSB_ORDER, Interrupt::Handler *irq=NULL)
Definition: SOFT_SPI.cpp:25