Arduino-LCD
LCD library for Arduino
MJKDZ.h
Go to the documentation of this file.
1 
19 #ifndef LCD_ADAPTER_MJKDZ_H
20 #define LCD_ADAPTER_MJKDZ_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 MJKDZ : public HD44780::Adapter, private PCF8574 {
49 public:
55  MJKDZ(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;
103  m_port.en = 0;
104  buf[3] = m_port;
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 data:4;
167  uint8_t en:1;
168  uint8_t rw:1;
169  uint8_t rs:1;
170  uint8_t bt:1;
171  };
172  operator uint8_t()
173  {
174  return (as_uint8);
175  }
177  {
178  as_uint8 = 0;
179  }
180  };
182 };
183 };
184 
185 #endif
virtual void write4b(uint8_t data)
Definition: MJKDZ.h:76
Definition: Debug.h:28
virtual void write8b(uint8_t data)
Definition: MJKDZ.h:92
virtual void set_mode(uint8_t flag)
Definition: MJKDZ.h:145
virtual void set_backlight(uint8_t flag)
Definition: MJKDZ.h:155
uint8_t bt
Back-light (P7).
Definition: MJKDZ.h:170
uint8_t data
Data port (P0..P3).
Definition: MJKDZ.h:166
uint8_t en
Enable pulse (P4).
Definition: MJKDZ.h:167
uint8_t rs
Command/Data select (P6).
Definition: MJKDZ.h:169
MJKDZ(TWI &twi, uint8_t subaddr=7)
Definition: MJKDZ.h:55
port_t m_port
Port setting.
Definition: MJKDZ.h:181
virtual void write8n(const void *buf, size_t size)
Definition: MJKDZ.h:114
virtual bool setup()
Definition: MJKDZ.h:65
uint8_t as_uint8
Unsigned byte access.
Definition: MJKDZ.h:164
uint8_t rw
Read/Write (P5).
Definition: MJKDZ.h:168
Software::TWI< BOARD::D1, BOARD::D2 > twi
Definition: Clock.ino:30