COSA
An Object-Oriented Platform for Arduino Programming
PCF8591.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_PCF8591_HH
22 #define COSA_PCF8591_HH
23 
24 #include "Cosa/TWI.hh"
25 
48 class PCF8591 : private TWI::Driver {
49 public:
54  enum {
55  AIN0 = 0x00,
56  AIN1 = 0x01,
57  AIN2 = 0x02,
58  AIN3 = 0x03,
59  CHANNEL_MASK = 0x03,
61  FOUR_INPUTS = 0x00,
66  } __attribute__((packed));
67 
73  PCF8591(uint8_t addr = 0) :
74  TWI::Driver(0x48 | (addr & 0x7)),
75  m_cntl(0)
76  {}
77 
84  bool begin(uint8_t cntl);
85 
89  void end()
90  {
91  twi.release();
92  }
93 
99  uint8_t sample()
100  __attribute__((always_inline))
101  {
102  uint8_t res;
103  twi.read(&res, 1);
104  return (res);
105  }
106 
112  uint8_t sample(uint8_t cntl)
113  __attribute__((always_inline))
114  {
115  begin(cntl);
116  uint8_t res = sample();
117  end();
118  return (res);
119  }
120 
128  int sample(uint8_t* buf, uint8_t size)
129  __attribute__((always_inline))
130  {
131  return (twi.read(buf, size));
132  }
133 
140  bool convert(uint8_t value);
141 
142 private:
144  uint8_t m_cntl;
145 };
146 
147 #endif
TWI twi
Definition: TWI.cpp:27
Definition: TWI.hh:51
uint8_t sample(uint8_t cntl)
Definition: PCF8591.hh:112
PCF8591(uint8_t addr=0)
Definition: PCF8591.hh:73
bool begin(uint8_t cntl)
Definition: PCF8591.cpp:24
bool convert(uint8_t value)
Definition: PCF8591.cpp:34
void end()
Definition: PCF8591.hh:89
void release()
Definition: TWI.cpp:58
int read(void *buf, size_t size)
Definition: TWI.hh:326
Driver(uint8_t addr)
Definition: TWI.hh:70
int sample(uint8_t *buf, uint8_t size)
Definition: PCF8591.hh:128
uint8_t sample()
Definition: PCF8591.hh:99