COSA
An Object-Oriented Platform for Arduino Programming
MCP23008.cpp
Go to the documentation of this file.
1 
21 #include "MCP23008.hh"
22 
23 bool
25 {
26  uint8_t reg[REG_MAX];
27  uint8_t config;
28  int res;
29 
30  memset(reg, 0, sizeof(reg));
31  reg[IODIR] = 0xff;
32  twi.acquire(this);
33  res = twi.write((uint8_t) IODIR, reg, sizeof(reg));
34  twi.release();
35  if (res != sizeof(reg) + 1) return (false);
36 
37  config = _BV(SEQOP);
38  twi.acquire(this);
39  res = twi.write((uint8_t) IOCON, &config, sizeof(config));
40  twi.release();
41  m_reg = IOCON;
42 
43  return (res == sizeof(config) + 1);
44 }
45 
46 bool
48 {
49  m_iodir = iodir;
50  twi.acquire(this);
51  int res = twi.write((uint8_t) IODIR, &m_iodir, sizeof(m_iodir));
52  twi.release();
53  m_reg = IODIR;
54  return (res == sizeof(m_iodir) + 1);
55 }
56 
57 bool
58 MCP23008::pullup(uint8_t gppu)
59 {
60  m_gppu = gppu;
61  twi.acquire(this);
62  int res = twi.write((uint8_t) GPPU, &m_gppu, sizeof(m_gppu));
63  twi.release();
64  m_reg = GPPU;
65  return (res == sizeof(m_gppu) + 1);
66 }
67 
68 bool
70 {
71  uint8_t mask = _BV(pin & PIN_MASK);
72  int res;
73  switch (mode) {
74  case DISABLE:
75  m_gpinten &= ~mask;
76  m_defval &= ~mask;
77  m_intcon &= ~mask;
78  break;
79  case ON_CHANGE:
80  m_gpinten |= mask;
81  m_defval &= ~mask;
82  m_intcon &= ~mask;
83  break;
84  case ON_FALLING:
85  m_gpinten |= mask;
86  m_defval |= mask;
87  m_intcon |= mask;
88  break;
89  case ON_RISING:
90  m_gpinten |= mask;
91  m_defval &= ~mask;
92  m_intcon |= mask;
93  break;
94  }
95  twi.acquire(this);
96  res = twi.write((uint8_t) GPINTEN, &m_gpinten, sizeof(m_gpinten));
97  if (res != sizeof(m_gpinten) + 1) goto error;
98  res = twi.write((uint8_t) DEFVAL, &m_defval, sizeof(m_defval));
99  if (res != sizeof(m_defval) + 1) goto error;
100  res = twi.write((uint8_t) INTCON, &m_intcon, sizeof(m_intcon));
101  if (res != sizeof(m_intcon) + 1) goto error;
102  twi.release();
103  return (true);
104  error:
105  twi.release();
106  return (false);
107 }
108 
109 uint8_t
111 {
112  uint8_t res;
113  twi.acquire(this);
114  if (m_reg != GPIO) {
115  twi.write((uint8_t) GPIO);
116  m_reg = GPIO;
117  }
118  twi.read(&res, sizeof(res));
119  twi.release();
120  return (res);
121 }
122 
123 bool
124 MCP23008::read(void* buf, size_t size)
125 {
126  if (UNLIKELY(size == 0)) return (true);
127  int res = 0;
128  twi.acquire(this);
129  if (m_reg != GPIO) {
130  twi.write((uint8_t) GPIO);
131  m_reg = GPIO;
132  }
133  res = twi.read(buf, size);
134  twi.release();
135  return (res == (int) size);
136 }
137 
138 bool
139 MCP23008::write(uint8_t value)
140 {
141  m_olat = value;
142  twi.acquire(this);
143  int res = twi.write((uint8_t) OLAT, &m_olat, sizeof(m_olat));
144  twi.release();
145  m_reg = OLAT;
146  return (res == sizeof(m_olat) + 1);
147 }
148 
149 bool
150 MCP23008::write(void* buf, size_t size)
151 {
152  if (UNLIKELY(size == 0)) return (true);
153  twi.acquire(this);
154  int res = twi.write((uint8_t) OLAT, buf, size);
155  twi.release();
156  uint8_t* bp = (uint8_t*) buf;
157  size_t ix = size - 1;
158  m_olat = bp[ix];
159  m_reg = OLAT;
160  return (res == (int) size + 1);
161 }
TWI twi
Definition: TWI.cpp:27
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
Interrupt disabled.
Definition: MCP23008.hh:54
uint8_t m_gpinten
Definition: MCP23008.hh:237
Configuration Register.
Definition: MCP23008.hh:215
Interrupt on change (toggle).
Definition: MCP23008.hh:55
Output Latch Register.
Definition: MCP23008.hh:220
InterruptMode
Definition: MCP23008.hh:53
bool write(uint8_t value)
Definition: MCP23008.cpp:139
I/O Direction Register.
Definition: MCP23008.hh:210
Interrupt Control Register.
Definition: MCP23008.hh:214
Pull-up Resistor Configuration.
Definition: MCP23008.hh:216
void release()
Definition: TWI.cpp:58
Port Register.
Definition: MCP23008.hh:219
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
int read(void *buf, size_t size)
Definition: TWI.hh:326
uint8_t m_reg
Definition: MCP23008.hh:252
uint8_t m_olat
Definition: MCP23008.hh:249
int write(void *buf, size_t size)
Definition: TWI.hh:282
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
void acquire(TWI::Driver *dev)
Definition: TWI.cpp:36
Definition: GPIO.hh:30
uint8_t m_gppu
Definition: MCP23008.hh:246
static const uint8_t PIN_MASK
Definition: MCP23008.hh:206
Sequence Operation mode disable.
Definition: MCP23008.hh:230
#define UNLIKELY(x)
Definition: Types.h:153
Default Compare Register.
Definition: MCP23008.hh:213
Interrupt on rising (low to high).
Definition: MCP23008.hh:57