Romi32U4 library
PololuHD44780.h
Go to the documentation of this file.
1 // Copyright Pololu Corporation. For more information, see http://www.pololu.com/
2 
12 #pragma once
13 #include <Arduino.h>
14 #include <util/delay.h>
15 
56 class PololuHD44780Base : public Print
57 {
58 public:
59 
61 
64  virtual void initPins() = 0;
65 
67  void init()
68  {
69  if (!initialized)
70  {
71  initialized = true;
72  init2();
73  }
74  }
75 
80  void reinitialize()
81  {
82  initialized = true;
83  init2();
84  }
85 
102  virtual void send(uint8_t data, bool rsValue, bool only4bits) = 0;
103 
104 private:
105 
106  void sendAndDelay(uint8_t data, bool rsValue, bool only4bit);
107 
109  void sendCommand(uint8_t cmd)
110  {
111  sendAndDelay(cmd, false, false);
112  }
113 
115  void sendCommand4Bit(uint8_t cmd)
116  {
117  sendAndDelay(cmd, false, true);
118  }
119 
121  void sendData(uint8_t data)
122  {
123  sendAndDelay(data, true, false);
124  }
125 
126 public:
127 
130  void clear();
131 
135  void loadCustomCharacter(const uint8_t * picture, uint8_t number);
136 
140  void loadCustomCharacterFromRam(const uint8_t * picture, uint8_t number);
141 
145  void loadCustomCharacter(const char * picture, uint8_t number)
146  {
147  loadCustomCharacter((const uint8_t *)picture, number);
148  }
149 
152  void createChar(uint8_t number, uint8_t picture[])
153  {
154  loadCustomCharacterFromRam(picture, number);
155  }
156 
166  void gotoXY(uint8_t x, uint8_t y);
167 
170  void setCursor(uint8_t col, uint8_t row)
171  {
172  gotoXY(col, row);
173  }
174 
178  void noDisplay();
179 
182  void display();
183 
193  void noCursor();
194 
205  void cursor();
206 
216  void noBlink();
217 
228  void blink();
229 
237  void cursorSolid();
238 
246  void cursorBlinking();
247 
251  void hideCursor();
252 
256  void scrollDisplayLeft();
257 
261  void scrollDisplayRight();
262 
269  void home();
270 
273  void leftToRight();
274 
277  void rightToLeft();
278 
284  void autoscroll();
285 
287  void noAutoscroll();
288 
289  //void initPrintf();
290  //void initPrintf(uint8_t lcdWidth, uint8_t lcdHeight);
291 
294  void command(uint8_t cmd)
295  {
296  sendCommand(cmd);
297  }
298 
300  virtual size_t write(uint8_t c);
301 
308  virtual size_t write(const uint8_t * buffer, size_t size);
309 
310  // This allows us to easily call overrides of write that are
311  // defined in Print.
312  using Print::write;
313 
314 private:
315  bool initialized;
316 
317  /* The lower three bits of this store the arguments to the
318  * last "Display on/off control" HD44780 command that we sent.
319  * bit 2: D: Whether the display is on.
320  * bit 1: C: Whether the cursor is shown.
321  * bit 0: B: Whether the cursor is blinking. */
322  uint8_t displayControl;
323 
324  /* The lower two bits of this variable store the arguments to the
325  * last "Entry mode set" HD44780 command that we sent.
326  * bit 1: I/D: 0 for moving the cursor to the left after data is written,
327  * 1 for moving the cursor to the right.
328  * bit 0: 1 for autoscrolling. */
329  uint8_t entryMode;
330 
331  void setEntryMode(uint8_t entryMode);
332  void setDisplayControl(uint8_t displayControl);
333 
334  void init2();
335 };
336 
361 {
362 public:
378  PololuHD44780(uint8_t rs, uint8_t e, uint8_t db4, uint8_t db5,
379  uint8_t db6, uint8_t db7)
380  {
381  this->rs = rs;
382  this->e = e;
383  this->db4 = db4;
384  this->db5 = db5;
385  this->db6 = db6;
386  this->db7 = db7;
387  }
388 
389  virtual void initPins()
390  {
391  digitalWrite(e, LOW);
392  pinMode(e, OUTPUT);
393  }
394 
395  virtual void send(uint8_t data, bool rsValue, bool only4bits)
396  {
397  digitalWrite(rs, rsValue);
398 
399  pinMode(rs, OUTPUT);
400  pinMode(db4, OUTPUT);
401  pinMode(db5, OUTPUT);
402  pinMode(db6, OUTPUT);
403  pinMode(db7, OUTPUT);
404 
405  if (!only4bits) { sendNibble(data >> 4); }
406  sendNibble(data & 0x0F);
407  }
408 
409 private:
410 
411  void sendNibble(uint8_t data)
412  {
413  digitalWrite(db4, data >> 0 & 1);
414  digitalWrite(db5, data >> 1 & 1);
415  digitalWrite(db6, data >> 2 & 1);
416  digitalWrite(db7, data >> 3 & 1);
417 
418  digitalWrite(e, HIGH);
419  _delay_us(1); // Must be at least 450 ns.
420  digitalWrite(e, LOW);
421  _delay_us(1); // Must be at least 550 ns.
422  }
423 
424  uint8_t rs, e, db4, db5, db6, db7;
425 };
virtual size_t write(uint8_t c)
void gotoXY(uint8_t x, uint8_t y)
virtual void initPins()=0
PololuHD44780(uint8_t rs, uint8_t e, uint8_t db4, uint8_t db5, uint8_t db6, uint8_t db7)
virtual void send(uint8_t data, bool rsValue, bool only4bits)
void command(uint8_t cmd)
void loadCustomCharacterFromRam(const uint8_t *picture, uint8_t number)
void createChar(uint8_t number, uint8_t picture[])
void setCursor(uint8_t col, uint8_t row)
Main class for interfacing with the HD44780 LCDs.
General class for handling the HD44780 protocol.
Definition: PololuHD44780.h:56
virtual void send(uint8_t data, bool rsValue, bool only4bits)=0
virtual void initPins()
void loadCustomCharacter(const uint8_t *picture, uint8_t number)
void loadCustomCharacter(const char *picture, uint8_t number)