COSA
An Object-Oriented Platform for Arduino Programming
PCF8574.cpp
Go to the documentation of this file.
1 
21 #include "PCF8574.hh"
22 
23 bool
25 {
26  m_ddr = ddr;
27  m_port |= m_ddr;
28  twi.acquire(this);
29  int res = twi.write(&m_port, sizeof(m_port));
30  twi.release();
31  return (res == sizeof(m_port));
32 }
33 
34 uint8_t
36 {
37  uint8_t res;
38  twi.acquire(this);
39  twi.read(&res, sizeof(res));
40  twi.release();
41  return (res & m_ddr);
42 }
43 
44 bool
45 PCF8574::write(uint8_t pin, uint8_t value)
46 {
47  uint8_t mask = _BV(pin & PIN_MASK);
48  if (value)
49  m_port |= mask;
50  else
51  m_port &= ~mask;
52  twi.acquire(this);
53  int res = twi.write(&m_port, sizeof(m_port));
54  twi.release();
55  return (res == sizeof(m_port));
56 }
57 
58 bool
59 PCF8574::write(uint8_t value)
60 {
61  twi.acquire(this);
62  m_port = value | m_ddr;
63  int res = twi.write(&m_port, sizeof(m_port));
64  twi.release();
65  return (res == sizeof(m_port));
66 }
67 
68 bool
69 PCF8574::write(void* buf, size_t size)
70 {
71  twi.acquire(this);
72  uint8_t* bp = (uint8_t*) buf;
73  size_t n = size;
74  while (n--) *bp++ |= m_ddr;
75  int res = twi.write(buf, size);
76  twi.release();
77  return (res == (int) size);
78 }
TWI twi
Definition: TWI.cpp:27
uint8_t m_ddr
Definition: PCF8574.hh:140
bool write(uint8_t pin, uint8_t value)
Definition: PCF8574.cpp:45
uint8_t m_port
Definition: PCF8574.hh:143
void release()
Definition: TWI.cpp:58
int read(void *buf, size_t size)
Definition: TWI.hh:326
bool data_direction(uint8_t ddr)
Definition: PCF8574.cpp:24
int write(void *buf, size_t size)
Definition: TWI.hh:282
void acquire(TWI::Driver *dev)
Definition: TWI.cpp:36
static const uint8_t PIN_MASK
Definition: PCF8574.hh:137
uint8_t read()
Definition: PCF8574.cpp:35