Arduino-GPIO
General Purpose Input/Output (GPIO) library for Arduino
SRPIO.h
Go to the documentation of this file.
1 
19 #ifndef SRPIO_H
20 #define SRPIO_H
21 
22 #include "GPIO.h"
23 
30 template<uint8_t BITORDER, BOARD::pin_t DATA_PIN, BOARD::pin_t CLOCK_PIN>
31 class SRPIO {
32 public:
40  {
42  m_clock.output();
43  }
44 
50  void write(uint8_t value)
51  {
52  if (BITORDER == LSBFIRST) {
53  uint8_t mask = 1;
54  do {
55  if (value & mask) m_data.input(); else m_data.output();
56  m_clock.toggle();
57  mask <<= 1;
58  m_clock.toggle();
59  } while (mask);
60  }
61  else {
62  uint8_t mask = 0x80;
63  do {
64  if (value & mask) m_data.input(); else m_data.output();
65  m_clock.toggle();
66  mask >>= 1;
67  m_clock.toggle();
68  } while (mask);
69  }
70  m_data.input();
71  }
72 
77  void operator<<(uint8_t value)
78  {
79  write(value);
80  }
81 
88  uint8_t read()
89  {
90  uint8_t res = 0;
91  if (BITORDER == LSBFIRST) {
92  uint8_t mask = 1;
93  do {
94  m_clock.toggle();
95  res |= (m_data ? mask : 0);
96  m_clock.toggle();
97  mask <<= 1;
98  } while (mask);
99  }
100  else {
101  uint8_t mask = 0x80;
102  do {
103  m_clock.toggle();
104  res |= (m_data ? mask : 0);
105  m_clock.toggle();
106  mask >>= 1;
107  } while (mask);
108  }
109  return (res);
110  }
111 
116  void operator>>(uint8_t& value)
117  {
118  value = read();
119  }
120 
121 protected:
124 };
125 
126 #endif
void operator>>(uint8_t &value)
Definition: SRPIO.h:116
GPIO< PIN > & input()
Definition: GPIO.h:36
uint8_t read()
Definition: SRPIO.h:88
void toggle()
Definition: GPIO.h:113
void open_drain()
Definition: GPIO.h:65
void write(uint8_t value)
Definition: SRPIO.h:50
void operator<<(uint8_t value)
Definition: SRPIO.h:77
void output()
Definition: GPIO.h:56
Definition: SRPIO.h:31
SRPIO()
Definition: SRPIO.h:39
GPIO< CLOCK_PIN > m_clock
Definition: SRPIO.h:123
GPIO< DATA_PIN > m_data
Definition: SRPIO.h:122