COSA
An Object-Oriented Platform for Arduino Programming
MCP23008.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_MCP23008_HH
22 #define COSA_MCP23008_HH
23 
24 #include "Cosa/TWI.hh"
25 
50 class MCP23008 : private TWI::Driver {
51 public:
54  DISABLE = 0,
55  ON_CHANGE = 1,
56  ON_FALLING = 2,
57  ON_RISING = 3
58  } __attribute__((packed));
59 
65  MCP23008(uint8_t subaddr = 0) :
66  TWI::Driver(0x20 | (subaddr & SUBADDR_MASK)),
67  m_iodir(0xff),
68  m_gpinten(0),
69  m_defval(0),
70  m_intcon(0),
71  m_gppu(0),
72  m_olat(0)
73  {}
74 
79  bool begin();
80 
87  bool data_direction(uint8_t iodir);
88 
95  bool pullup(uint8_t gppu);
96 
102  bool input_pin(uint8_t pin)
103  __attribute__((always_inline))
104  {
105  return (data_direction(m_iodir | _BV(pin & PIN_MASK)));
106  }
107 
113  bool pullup_pin(uint8_t pin)
114  __attribute__((always_inline))
115  {
116  return (pullup(m_gppu | _BV(pin & PIN_MASK)));
117  }
118 
126  bool interrupt_pin(uint8_t pin, InterruptMode mode);
127 
133  bool output_pin(uint8_t pin)
134  __attribute__((always_inline))
135  {
136  return (data_direction(m_iodir & ~_BV(pin & PIN_MASK)));
137  }
138 
144  bool read_pin(uint8_t pin)
145  __attribute__((always_inline))
146  {
147  return ((read() & _BV(pin & PIN_MASK)) != 0);
148  }
149 
157  bool write_pin(uint8_t pin, uint8_t value)
158  __attribute__((always_inline))
159  {
160  uint8_t mask = _BV(pin & PIN_MASK);
161  if (value)
162  m_olat |= mask;
163  else
164  m_olat &= ~mask;
165  return (write(m_olat));
166  }
167 
172  uint8_t read();
173 
181  bool read(void* buf, size_t size);
182 
189  bool write(uint8_t value);
190 
199  bool write(void* buf, size_t size);
200 
201 protected:
203  static const uint8_t SUBADDR_MASK = 0x07;
204 
206  static const uint8_t PIN_MASK = 0x07;
207 
209  enum {
210  IODIR = 0x00,
211  IPOL = 0x01,
212  GPINTEN = 0x02,
213  DEFVAL = 0x03,
214  INTCON = 0x04,
215  IOCON = 0x05,
216  GPPU = 0x06,
217  INTF = 0x07,
218  INTCAP = 0x08,
219  GPIO = 0x09,
220  OLAT = 0x0a,
221  REG_MAX = 0x0b
222  } __attribute__((packed));
223 
225  enum {
226  INTPOL = 1,
227  ODR = 2,
228  HAEN = 3,
229  DISSLW = 4,
230  SEQOP = 5
231  } __attribute__((packed));
232 
234  uint8_t m_iodir;
235 
237  uint8_t m_gpinten;
238 
240  uint8_t m_defval;
241 
243  uint8_t m_intcon;
244 
246  uint8_t m_gppu;
247 
249  uint8_t m_olat;
250 
252  uint8_t m_reg;
253 };
254 #endif
Definition: TWI.hh:51
Number of Registers.
Definition: MCP23008.hh:221
bool begin()
Definition: MCP23008.cpp:24
uint8_t read()
Definition: MCP23008.cpp:110
bool pullup(uint8_t gppu)
Definition: MCP23008.cpp:58
Slew Rate disable.
Definition: MCP23008.hh:229
Interrupt disabled.
Definition: MCP23008.hh:54
bool write_pin(uint8_t pin, uint8_t value)
Definition: MCP23008.hh:157
bool input_pin(uint8_t pin)
Definition: MCP23008.hh:102
uint8_t m_gpinten
Definition: MCP23008.hh:237
Configuration Register.
Definition: MCP23008.hh:215
static const uint8_t SUBADDR_MASK
Definition: MCP23008.hh:203
Interrupt on change (toggle).
Definition: MCP23008.hh:55
Output Latch Register.
Definition: MCP23008.hh:220
Interrupt Flag Register.
Definition: MCP23008.hh:217
InterruptMode
Definition: MCP23008.hh:53
bool write(uint8_t value)
Definition: MCP23008.cpp:139
Hardware Address Enable (SPI).
Definition: MCP23008.hh:228
I/O Direction Register.
Definition: MCP23008.hh:210
Interrupt Control Register.
Definition: MCP23008.hh:214
MCP23008(uint8_t subaddr=0)
Definition: MCP23008.hh:65
Pull-up Resistor Configuration.
Definition: MCP23008.hh:216
Input Polarity Register.
Definition: MCP23008.hh:211
bool read_pin(uint8_t pin)
Definition: MCP23008.hh:144
uint8_t m_intcon
Definition: MCP23008.hh:243
bool data_direction(uint8_t iodir)
Definition: MCP23008.cpp:47
uint8_t m_iodir
Definition: MCP23008.hh:234
bool interrupt_pin(uint8_t pin, InterruptMode mode)
Definition: MCP23008.cpp:69
uint8_t m_reg
Definition: MCP23008.hh:252
uint8_t m_olat
Definition: MCP23008.hh:249
Interrupt on falling (high to low).
Definition: MCP23008.hh:56
Interrupt on Change Control Register.
Definition: MCP23008.hh:212
uint8_t m_defval
Definition: MCP23008.hh:240
Driver(uint8_t addr)
Definition: TWI.hh:70
Interrupt Capture Register.
Definition: MCP23008.hh:218
Polarity of Interrupt Pin.
Definition: MCP23008.hh:226
Definition: GPIO.hh:30
uint8_t m_gppu
Definition: MCP23008.hh:246
static const uint8_t PIN_MASK
Definition: MCP23008.hh:206
Open-drain Output enable.
Definition: MCP23008.hh:227
Sequence Operation mode disable.
Definition: MCP23008.hh:230
Default Compare Register.
Definition: MCP23008.hh:213
bool pullup_pin(uint8_t pin)
Definition: MCP23008.hh:113
bool output_pin(uint8_t pin)
Definition: MCP23008.hh:133
Interrupt on rising (low to high).
Definition: MCP23008.hh:57