COSA
An Object-Oriented Platform for Arduino Programming
PCF8574.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_PCF8574_HH
22 #define COSA_PCF8574_HH
23 
24 #include "Cosa/TWI.hh"
25 
49 class PCF8574 : private TWI::Driver {
50 public:
56  PCF8574(uint8_t subaddr = 7) :
57  TWI::Driver(0x20 | (subaddr & 0x7)),
58  m_ddr(0xff),
59  m_port(0)
60  {}
61 
68  bool data_direction(uint8_t ddr);
69 
75  bool input_pin(uint8_t pin)
76  __attribute__((always_inline))
77  {
78  return (data_direction(m_ddr | _BV(pin & PIN_MASK)));
79  }
80 
86  bool output_pin(uint8_t pin)
87  __attribute__((always_inline))
88  {
89  return (data_direction(m_ddr & ~_BV(pin & PIN_MASK)));
90  }
91 
97  bool read(uint8_t pin)
98  __attribute__((always_inline))
99  {
100  return ((read() & _BV(pin & PIN_MASK)) != 0);
101  }
102 
107  uint8_t read();
108 
116  bool write(uint8_t pin, uint8_t value);
117 
124  bool write(uint8_t value);
125 
133  bool write(void* buf, size_t size);
134 
135 protected:
137  static const uint8_t PIN_MASK = 0x07;
138 
140  uint8_t m_ddr;
141 
143  uint8_t m_port;
144 
151  PCF8574(uint8_t addr, uint8_t subaddr) :
152  TWI::Driver(addr | (subaddr & 0x7)),
153  m_ddr(0xff),
154  m_port(0)
155  {}
156 };
157 
158 class PCF8574A : protected PCF8574 {
159 public:
165  PCF8574A(uint8_t subaddr = 7) : PCF8574(0x38, subaddr) {}
166 };
167 #endif
Definition: TWI.hh:51
bool read(uint8_t pin)
Definition: PCF8574.hh:97
uint8_t m_ddr
Definition: PCF8574.hh:140
bool write(uint8_t pin, uint8_t value)
Definition: PCF8574.cpp:45
PCF8574A(uint8_t subaddr=7)
Definition: PCF8574.hh:165
bool output_pin(uint8_t pin)
Definition: PCF8574.hh:86
uint8_t m_port
Definition: PCF8574.hh:143
PCF8574(uint8_t subaddr=7)
Definition: PCF8574.hh:56
bool data_direction(uint8_t ddr)
Definition: PCF8574.cpp:24
PCF8574(uint8_t addr, uint8_t subaddr)
Definition: PCF8574.hh:151
Driver(uint8_t addr)
Definition: TWI.hh:70
static const uint8_t PIN_MASK
Definition: PCF8574.hh:137
bool input_pin(uint8_t pin)
Definition: PCF8574.hh:75
uint8_t read()
Definition: PCF8574.cpp:35