COSA
An Object-Oriented Platform for Arduino Programming
SRPI.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_SOFT_SRPI_HH
22 #define COSA_SOFT_SRPI_HH
23 
24 #include "Cosa/OutputPin.hh"
25 #include "Cosa/InputPin.hh"
26 
27 namespace Soft {
28 
68 template<uint8_t N>
69 class SRPI {
70 public:
72  static const uint8_t PINS = N * CHARBITS;
73 
83  m_sda(sda),
84  m_scl(scl),
85  m_pld(pld, 1)
86  {
87  update();
88  }
89 
96  bool is_set(uint8_t pin)
97  __attribute__((always_inline))
98  {
99  uint8_t ix = (pin >> 3);
100  return ((m_port[ix] & _BV(pin & 0x7)) != 0);
101  }
102 
109  void is_clear(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 
120  void update()
121  {
122  m_pld.toggle();
123  m_pld.toggle();
124  for (uint8_t ix = 0; ix < N; ix++)
125  m_port[ix] = m_sda.read(m_scl);
126  }
127 
131  class InputPin {
132  public:
133  InputPin(SRPI<N>* srpi, uint8_t pin) :
134  m_srpi(srpi),
135  m_pin(pin)
136  {
137  }
138 
144  bool is_set()
145  __attribute__((always_inline))
146  {
147  return (m_srpi->is_set(m_pin));
148  }
149 
155  void is_clear(uint8_t pin)
156  __attribute__((always_inline))
157  {
158  return (m_srpi->is_clear(m_pin));
159  }
160 
161  protected:
163  const uint8_t m_pin;
164  };
165 
166 protected:
168  uint8_t m_port[N];
169 
172 
175 
178 };
179 };
180 #endif
void update()
Definition: SRPI.hh:120
void is_clear(uint8_t pin)
Definition: SRPI.hh:155
SRPI(Board::DigitalPin sda=Board::D3, Board::DigitalPin scl=Board::D4, Board::DigitalPin pld=Board::D5)
Definition: SRPI.hh:80
const uint8_t m_pin
Definition: SRPI.hh:163
Definition: SPI.hh:32
InputPin(SRPI< N > *srpi, uint8_t pin)
Definition: SRPI.hh:133
OutputPin m_scl
Definition: SRPI.hh:174
uint8_t m_port[N]
Definition: SRPI.hh:168
OutputPin m_pld
Definition: SRPI.hh:177
::InputPin m_sda
Definition: SRPI.hh:171
#define CHARBITS
Definition: Types.h:57
SRPI< N > * m_srpi
Definition: SRPI.hh:162
void is_clear(uint8_t pin)
Definition: SRPI.hh:109
void toggle() const
Definition: OutputPin.hh:163
bool is_set(uint8_t pin)
Definition: SRPI.hh:96
static const uint8_t PINS
Definition: SRPI.hh:72