Arduino-LCD
LCD library for Arduino
DFRobot_IIC.h
Go to the documentation of this file.
1 
19 #ifndef DFROBOT_IIC_H
20 #define DFROBOT_IIC_H
21 
22 #include "LCD.h"
23 #include "Driver/HD44780.h"
24 #include "TWI.h"
25 #include "Driver/PCF8574.h"
26 
47 namespace LCD {
48 class DFRobot_IIC : public HD44780::Adapter, private PCF8574 {
49 public:
55  DFRobot_IIC(TWI& twi, uint8_t subaddr = 7) :
56  PCF8574(twi, subaddr),
57  m_port()
58  {}
59 
65  virtual bool setup()
66  {
67  ddr(0);
68  return (false);
69  }
70 
76  virtual void write4b(uint8_t data)
77  {
78  uint8_t buf[2];
79  m_port.data = data;
80  m_port.en = 1;
81  buf[0] = m_port;
82  m_port.en = 0;
83  buf[1] = m_port;
84  write(buf, sizeof(buf));
85  }
86 
92  virtual void write8b(uint8_t data)
93  {
94  uint8_t buf[4];
95  m_port.data = (data >> 4);
96  m_port.en = 1;
97  buf[0] = m_port;
98  m_port.en = 0;
99  buf[1] = m_port;
100  m_port.data = data;
101  m_port.en = 1;
102  buf[2] = m_port.as_uint8;
103  m_port.en = 0;
104  buf[3] = m_port.as_uint8;
105  write(buf, sizeof(buf));
106  }
107 
114  virtual void write8n(const void* buf, size_t size)
115  {
116  const uint8_t* bp = (const uint8_t*) buf;
117  while (size != 0) {
118  uint8_t tmp[32];
119  uint8_t n = (size > sizeof(tmp) / 4 ? sizeof(tmp) / 4 : size);
120  size -= n;
121  uint8_t m = n * 4;
122  for (uint8_t i = 0; i < m;) {
123  uint8_t data = *bp++;
124  m_port.data = (data >> 4);
125  m_port.en = 1;
126  tmp[i++] = m_port;
127  m_port.en = 0;
128  tmp[i++] = m_port;
129  m_port.data = data;
130  m_port.en = 1;
131  tmp[i++] = m_port;
132  m_port.en = 0;
133  tmp[i++] = m_port;
134  }
135  write(tmp, m);
136  }
137  }
138 
145  virtual void set_mode(uint8_t flag)
146  {
147  m_port.rs = flag;
148  }
149 
155  virtual void set_backlight(uint8_t flag)
156  {
157  m_port.bt = flag;
158  write(m_port.as_uint8);
159  }
160 
161 protected:
163  union port_t {
164  uint8_t as_uint8;
165  struct {
166  uint8_t rs:1;
167  uint8_t rw:1;
168  uint8_t en:1;
169  uint8_t bt:1;
170  uint8_t data:4;
171  };
172  operator uint8_t() { return (as_uint8); }
173  port_t() { as_uint8 = 0; }
174  };
176 };
177 };
178 #endif
uint8_t rw
Read/Write (P1).
Definition: DFRobot_IIC.h:167
Definition: Debug.h:28
uint8_t rs
Command/Data select (P0).
Definition: DFRobot_IIC.h:166
uint8_t as_uint8
Unsigned byte access.
Definition: DFRobot_IIC.h:164
virtual void write4b(uint8_t data)
Definition: DFRobot_IIC.h:76
uint8_t en
Enable pulse (P2).
Definition: DFRobot_IIC.h:168
port_t m_port
Port setting.
Definition: DFRobot_IIC.h:175
virtual void set_backlight(uint8_t flag)
Definition: DFRobot_IIC.h:155
DFRobot_IIC(TWI &twi, uint8_t subaddr=7)
Definition: DFRobot_IIC.h:55
uint8_t bt
Back-light (P3).
Definition: DFRobot_IIC.h:169
virtual void write8b(uint8_t data)
Definition: DFRobot_IIC.h:92
virtual void set_mode(uint8_t flag)
Definition: DFRobot_IIC.h:145
uint8_t data
Data port (P4..P7).
Definition: DFRobot_IIC.h:170
Software::TWI< BOARD::D1, BOARD::D2 > twi
Definition: Clock.ino:30
virtual void write8n(const void *buf, size_t size)
Definition: DFRobot_IIC.h:114
virtual bool setup()
Definition: DFRobot_IIC.h:65