Rpi-hw  0.7.2
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
hd44780.hpp
1 /*
2  Title --- display/hd44780.hpp
3 
4  Copyright (C) 2013 Giacomo Trudu - wicker25[at]gmail[dot]com
5 
6  This file is part of Rpi-hw.
7 
8  Rpi-hw is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation version 3 of the License.
11 
12  Rpi-hw is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with Rpi-hw. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef _RPI_HW_DISPLAY_HD44780_HPP_
22 #define _RPI_HW_DISPLAY_HD44780_HPP_
23 
24 #include <cctype>
25 #include <vector>
26 
27 #include <rpi-hw/types.hpp>
28 #include <rpi-hw/exception.hpp>
29 #include <rpi-hw/math.hpp>
30 #include <rpi-hw/utils.hpp>
31 #include <rpi-hw/time.hpp>
32 
33 #include <rpi-hw/preprocessor/enumerate.hpp>
34 
35 #include <rpi-hw/iface/base.hpp>
36 #include <rpi-hw/iface/output.hpp>
37 #include <rpi-hw/iface/input.hpp>
38 
39 namespace rpihw { // Begin main namespace
40 
41 namespace display { // Begin displays namespace
42 
43 /*!
44  @class hd44780
45  @brief Hitachi HD44780 LCD controller.
46 
47  @example display/lcd16x2.cpp
48  @example display/lcd16x2_unicode.cpp
49  @example display/lcd20x4demo.cpp
50 */
51 class hd44780 {
52 
53 public:
54 
55  //! The ROM codes.
56  enum RomCodes {
57 
58  ROM_A00 = 0, //!< Japanese version.
59  ROM_A02 = 1 //!< European version.
60  };
61 
62  //! The controller command set.
63  enum Commands {
64 
65  CLEAR = 0x01, //!< Clear display.
66  HOME = 0x02, //!< Cursor home.
67  ENTRY = 0x04, //!< Entry mode set.
68  POWER = 0x08, //!< Display on/off control.
69  SHIFT = 0x10, //!< Cursor/display shift.
70  FUNC = 0x20, //!< Function set.
71  CGRAM = 0x40, //!< Set CGRAM address.
72  DDRAM = 0x80 //!< Set DDRAM address.
73  };
74 
75  //! The controller command set (bit flags).
76  enum Settings {
77 
78  ENTRY_S = 0x01, //!< Shifts display when byte written to display.
79  ENTRY_ID = 0x02, //!< Increments the cursor after each byte written to display.
80 
81  POWER_B = 0x01, //!< Sets a blinking cursor (else sets a solid cursor).
82  POWER_C = 0x02, //!< Turns on the cursor (else turn off).
83  POWER_D = 0x04, //!< Turns on the screen (else turn off).
84 
85  SHIFT_RL = 0x04, //!< Sets cursor shift direction to right (else to left).
86  SHIFT_SC = 0x08, //!< Sets display-shift (else sets cursor-move).
87 
88  FUNC_F = 0x04, //!< Sets the 5x10 character font (else 5x8).
89  FUNC_N = 0x08, //!< Sets number of display lines to 2/4 (else 1).
90  FUNC_DL = 0x10 //!< Sets the 8 bit mode (else sets the 4 bit mode).
91  };
92 
93  //! The custom characters.
95 
96  CCHAR0 = 0, //!< Custom character #0.
97  CCHAR1 = 1, //!< Custom character #1.
98  CCHAR2 = 2, //!< Custom character #2.
99  CCHAR3 = 3, //!< Custom character #3.
100  CCHAR4 = 4, //!< Custom character #4.
101  CCHAR5 = 5, //!< Custom character #5.
102  CCHAR6 = 6, //!< Custom character #6.
103  CCHAR7 = 7 //!< Custom character #7.
104  };
105 
106  //! The cursor modes (bitwise flags).
107  enum CursorMode {
108 
109  NO_CURSOR = 0x00, //!< No cursor.
110  CURSOR_SOLID = 0x01, //!< Solid cursor.
111  CURSOR_BLINKING = 0x02 //!< Blinking the cursor.
112  };
113 
114  //! The autoscroll modes (bitwise flags).
116 
117  NO_SCROLL = 0x00, //!< No scroll.
118  VSCROLL = 0x01, //!< Vertical scroll.
119  HSCROLL = 0x02, //!< Horizontal scroll.
120  HSCROLL_LINE = 0x04 //!< Horizontal scroll line.
121  };
122 
123  /*!
124  @brief Constructor method (4-bit mode).
125  @param[in] rs The GPIO pin connected to the rs pin.
126  @param[in] e The GPIO pin connected to the e pin.
127  @param[in] d4 The GPIO pin connected to the d4 pin.
128  @param[in] d5 The GPIO pin connected to the d5 pin.
129  @param[in] d6 The GPIO pin connected to the d6 pin.
130  @param[in] d7 The GPIO pin connected to the d7 pin.
131  */
132  hd44780( uint8_t rs, uint8_t e, __ENUM_PARAMS( uint8_t, d, 4, 7 ) );
133 
134  /*!
135  @brief Constructor method (8-bit mode).
136  @param[in] rs The GPIO pin connected to the rs pin.
137  @param[in] e The GPIO pin connected to the e pin.
138  @param[in] d0 The GPIO pin connected to the d0 pin.
139  @param[in] d1 The GPIO pin connected to the d1 pin.
140  @param[in] d2 The GPIO pin connected to the d2 pin.
141  @param[in] d3 The GPIO pin connected to the d3 pin.
142  @param[in] d4 The GPIO pin connected to the d4 pin.
143  @param[in] d5 The GPIO pin connected to the d5 pin.
144  @param[in] d6 The GPIO pin connected to the d6 pin.
145  @param[in] d7 The GPIO pin connected to the d7 pin.
146  */
147  hd44780( uint8_t rs, uint8_t e, __ENUM_PARAMS( uint8_t, d, 0, 7 ) );
148 
149  //! Destructor method.
150  virtual ~hd44780();
151 
152  /*!
153  @brief Initializes the display.
154  @param[in] cols Number of the display columns.
155  @param[in] rows Number of the display rows.
156  @param[in] rom_code The ROM code (\c ROM_A00 or \c ROM_A02).
157  @param[in] font If \c true uses 5x10 dots font, else uses 5x8 dots font.
158  */
159  void init( uint8_t cols, uint8_t rows, RomCodes rom_code = ROM_A00, bool font = 0 );
160 
161  /*!
162  @brief Sends a command to the display.
163  @param[in] data The command.
164  */
165  void cmd( uint8_t data );
166 
167  //! Homes the cursor.
168  void home();
169 
170  /*!
171  @brief Sets the position of the cursor on the display.
172  @param[in] x The new horizontal position of the cursor.
173  @param[in] y The new vertical position of the cursor.
174  */
175  void move( uint8_t x, uint8_t y );
176 
177  /*!
178  @brief Returns the current x-position of the cursor.
179  @return The cursor x-position.
180  */
181  uint8_t getCursorX() const;
182 
183  /*!
184  @brief Returns the current y-position of the cursor.
185  @return The cursor y-position.
186  */
187  uint8_t getCursorY() const;
188 
189  /*!
190  @brief Reads a character from the buffer at specified position.
191  @param[in] x The horizontal position of the character.
192  @param[in] y The vertical position of the character.
193  @return The 8-bit character at position.
194  */
195  uint8_t getChar( uint8_t x, uint8_t y ) const;
196 
197  //! Moves the cursor to the new line.
198  void newLine();
199 
200  /*!
201  @brief Writes a character on the display.
202  @param[in] c The 8-bit character.
203  */
204  void write( uint8_t c );
205 
206  /*!
207  @brief Moves the cursor position and writes a character on the display.
208  @param[in] x The new horizontal position of the cursor.
209  @param[in] y The new vertical position of the cursor.
210  @param[in] c The 8-bit character.
211  */
212  void write( uint8_t x, uint8_t y, uint8_t c );
213 
214  /*!
215  @brief Writes a string on the display.
216  @param[in] text The string to be written.
217  */
218  void write( const std::string &text );
219 
220  /*!
221  @brief Writes a string on the display.
222  @param[in] text The string to be written.
223  @param[in] flags The parameters of the text.
224  */
225  void write( const std::string &text, uint8_t flags );
226 
227  /*!
228  @brief Moves the cursor position and writes a string on the display.
229  @param[in] x The new horizontal position of the cursor.
230  @param[in] y The new vertical position of the cursor.
231  @param[in] text The string to be written.
232  */
233  void write( uint8_t x, uint8_t y, const std::string &text );
234 
235  /*!
236  @brief Moves the cursor position and writes a string on the display.
237  @param[in] x The new horizontal position of the cursor.
238  @param[in] y The new vertical position of the cursor.
239  @param[in] text The string to be written.
240  @param[in] flags The parameters of the text.
241  */
242  void write( uint8_t x, uint8_t y, const std::string &text, uint8_t flags );
243 
244  /*!
245  @brief Writes a unicode string on the display.
246  @param[in] text The unicode string to be written.
247  */
248  void write( const std::u32string &text );
249 
250  /*!
251  @brief Writes a unicode string on the display.
252  @param[in] text The string to be written.
253  @param[in] flags The parameters of the text.
254  */
255  void write( const std::u32string &text, uint8_t flags );
256 
257  /*!
258  @brief Moves the cursor position and writes a unicode string on the display.
259  @param[in] x The new horizontal position of the cursor.
260  @param[in] y The new vertical position of the cursor.
261  @param[in] text The unicode string to be written.
262  */
263  void write( uint8_t x, uint8_t y, const std::u32string &text );
264 
265  /*!
266  @brief Moves the cursor position and writes a unicode string on the display.
267  @param[in] x The new horizontal position of the cursor.
268  @param[in] y The new vertical position of the cursor.
269  @param[in] text The unicode string to be written.
270  @param[in] flags The parameters of the text.
271  */
272  void write( uint8_t x, uint8_t y, const std::u32string &text, uint8_t flags );
273 
274  /*!
275  @brief Writes a unicode string on the display.
276  @param[in] text The unicode string to be written.
277  */
278  void write( const std::wstring &text );
279 
280  /*!
281  @brief Writes a unicode string on the display.
282  @param[in] text The string to be written.
283  @param[in] flags The parameters of the text.
284  */
285  void write( const std::wstring &text, uint8_t flags );
286 
287  /*!
288  @brief Moves the cursor position and writes a unicode string on the display.
289  @param[in] x The new horizontal position of the cursor.
290  @param[in] y The new vertical position of the cursor.
291  @param[in] text The unicode string to be written.
292  */
293  void write( uint8_t x, uint8_t y, const std::wstring &text );
294 
295  /*!
296  @brief Moves the cursor position and writes a unicode string on the display.
297  @param[in] x The new horizontal position of the cursor.
298  @param[in] y The new vertical position of the cursor.
299  @param[in] text The unicode string to be written.
300  @param[in] flags The parameters of the text.
301  */
302  void write( uint8_t x, uint8_t y, const std::wstring &text, uint8_t flags );
303 
304  /*!
305  @brief Scrolls the contents of the display to the left.
306  @param[in] cursor If \c true, will also moves the cursor.
307  */
308  void scrollLeft( bool cursor = 1 );
309 
310  /*!
311  @brief Scrolls the contents of the display to the right.
312  @param[in] cursor If \c true, will also moves the cursor.
313  */
314  void scrollRight( bool cursor = 1 );
315 
316  /*!
317  @brief Scrolls the contents of the display to the up.
318  @param[in] cursor If \c true, will also moves the cursor.
319  */
320  void scrollUp( bool cursor = 1 );
321 
322  /*!
323  @brief Scrolls the contents of the display to the down.
324  @param[in] cursor If \c true, will also moves the cursor.
325  */
326  void scrollDown( bool cursor = 1 );
327 
328  /*!
329  @brief Scrolls a line to the left.
330  @param[in] line The number of the line to be moved.
331  @param[in] cursor If \c true, will also moves the cursor.
332  */
333  void scrollLeftLine( uint8_t line, bool cursor = 1 );
334 
335  /*!
336  @brief Scrolls a line to the right.
337  @param[in] line The number of the line to be moved.
338  @param[in] cursor If \c true, will also moves the cursor.
339  */
340  void scrollRightLine( uint8_t line, bool cursor = 1 );
341 
342  /*!
343  @brief Defines a custom character.
344  @param[in] index The index position of the custom character.
345  @param[in] data The array containing the character's pixel data (an element per row).
346  */
347  void defChar( uint8_t index, const uint8_t *data );
348 
349  /*!
350  @brief Sets the cursor mode.
351  @param[in] mode The bitwise flags of the cursor mode. You can use \c NO_CURSOR, \c CURSOR_SOLID and \c CURSOR_BLINKING.
352  */
353  void setCursor( uint8_t mode );
354 
355  /*!
356  @brief Sets the typing delay between individual characters (milliseconds).
357  @param[in] delay The typing delay.
358  */
359  void setTypingDelay( size_t delay );
360 
361  /*!
362  @brief Returns the typing delay between individual characters (milliseconds).
363  @return The typing delay.
364  */
365  size_t getTypingDelay() const;
366 
367  /*!
368  @brief Sets the autoscroll mode.
369  @param[in] mode The bitwise flags of the autoscroll mode. You can use \c NO_SCROLL, \c VSCROLL, \c HSCROLL and \c HSCROLL_LINE.
370  */
371  void setAutoscroll( uint8_t mode );
372 
373  //! Clears the display.
374  void clear();
375 
376 protected:
377 
378  //! Output interface to the control pins.
380 
381  //! Output interface to the data pins.
383 
384  //! DDRAM line addresses
385  uint8_t m_lines[4];
386 
387  //@{
388  //! Size of the display.
389  uint8_t m_width, m_height;
390  //@}
391 
392  //! ROM code.
394 
395  //! Font height.
396  uint8_t m_font_height;
397 
398  //! Text buffer.
399  std::vector< uint8_t > m_buffer;
400 
401  //@{
402  //! Cursor position.
403  uint8_t m_pos_x, m_pos_y;
404  //@}
405 
406  //! Typing delay (milliseconds).
407  size_t m_typing_delay = 0;
408 
409  //! Autoscroll mode.
411 
412  //! Toggles the enable pin.
413  void strobe();
414 
415  /*!
416  @brief Sends data to the display.
417  @param[in] data The 4-bit data to be sended.
418  @param[in] delay The delay time after the sending of the data.
419  */
420  void sendData( uint8_t data, size_t delay = 0 );
421 
422  /*!
423  @brief Sends a serial data to the display (a nibble at a time).
424  @param[in] data The 8-bit data to be sended.
425  @param[in] delay The delay time after the sending of the data.
426  */
427  void sendSerial( uint8_t data, size_t delay = 0 );
428 
429  /*!
430  @brief Puts a character on the display (low level).
431  @param[in] c The 8-bit character.
432  */
433  void putChar( uint8_t c );
434 
435  /*!
436  @brief Maps a unicode character to the corresponding code.
437  @param[in] code The unicode character.
438  @return The character code.
439  */
440  uint8_t encodeChar( char32_t code );
441 
442  /*!
443  @brief Maps a unicode character to the corresponding code (ROM A00, Japanese version).
444  @param[in] code The unicode character.
445  @return The character code.
446  */
447  uint8_t encodeCharA00( char32_t code );
448 
449  /*!
450  @brief Maps a unicode character to the corresponding code (ROM A02, European version).
451  @param[in] code The unicode character.
452  @return The character code.
453  */
454  uint8_t encodeCharA02( char32_t code );
455 };
456 
457 } // End of displays namespace
458 
459 } // End of main namespace
460 
461 
462 // Include inline methods
463 #include <rpi-hw/display/hd44780-inl.hpp>
464 
465 #endif /* _RPI_HW_DISPLAY_HD44780_HPP_ */
Sets cursor shift direction to right (else to left).
Definition: hd44780.hpp:85
Output interface.
Definition: output.hpp:43
uint8_t getCursorX() const
Returns the current x-position of the cursor.
Definition: hd44780-inl.hpp:40
Sets the 8 bit mode (else sets the 4 bit mode).
Definition: hd44780.hpp:90
CustomCharacters
The custom characters.
Definition: hd44780.hpp:94
std::vector< uint8_t > m_buffer
Text buffer.
Definition: hd44780.hpp:399
void scrollLeft(bool cursor=1)
Scrolls the contents of the display to the left.
Custom character #0.
Definition: hd44780.hpp:96
uint8_t m_pos_x
Cursor position.
Definition: hd44780.hpp:403
uint8_t getChar(uint8_t x, uint8_t y) const
Reads a character from the buffer at specified position.
Definition: hd44780-inl.hpp:54
uint8_t m_width
Size of the display.
Definition: hd44780.hpp:389
void sendSerial(uint8_t data, size_t delay=0)
Sends a serial data to the display (a nibble at a time).
Sets a blinking cursor (else sets a solid cursor).
Definition: hd44780.hpp:81
Custom character #6.
Definition: hd44780.hpp:102
void write(uint8_t c)
Writes a character on the display.
Increments the cursor after each byte written to display.
Definition: hd44780.hpp:79
void clear()
Clears the display.
RomCodes
The ROM codes.
Definition: hd44780.hpp:56
void move(uint8_t x, uint8_t y)
Sets the position of the cursor on the display.
Horizontal scroll line.
Definition: hd44780.hpp:120
Custom character #3.
Definition: hd44780.hpp:99
Custom character #5.
Definition: hd44780.hpp:101
Commands
The controller command set.
Definition: hd44780.hpp:63
uint8_t m_autoscroll
Autoscroll mode.
Definition: hd44780.hpp:410
void init(uint8_t cols, uint8_t rows, RomCodes rom_code=ROM_A00, bool font=0)
Initializes the display.
Settings
The controller command set (bit flags).
Definition: hd44780.hpp:76
uint8_t m_lines[4]
DDRAM line addresses.
Definition: hd44780.hpp:385
iface::output * m_data
Output interface to the data pins.
Definition: hd44780.hpp:382
void newLine()
Moves the cursor to the new line.
Custom character #4.
Definition: hd44780.hpp:100
void home()
Homes the cursor.
Custom character #2.
Definition: hd44780.hpp:98
void scrollDown(bool cursor=1)
Scrolls the contents of the display to the down.
void setCursor(uint8_t mode)
Sets the cursor mode.
void scrollLeftLine(uint8_t line, bool cursor=1)
Scrolls a line to the left.
void scrollRight(bool cursor=1)
Scrolls the contents of the display to the right.
uint8_t encodeCharA00(char32_t code)
Maps a unicode character to the corresponding code (ROM A00, Japanese version).
void defChar(uint8_t index, const uint8_t *data)
Defines a custom character.
Sets number of display lines to 2/4 (else 1).
Definition: hd44780.hpp:89
uint8_t m_font_height
Font height.
Definition: hd44780.hpp:396
Set DDRAM address.
Definition: hd44780.hpp:72
Sets display-shift (else sets cursor-move).
Definition: hd44780.hpp:86
AutoscrollMode
The autoscroll modes (bitwise flags).
Definition: hd44780.hpp:115
CursorMode
The cursor modes (bitwise flags).
Definition: hd44780.hpp:107
void scrollRightLine(uint8_t line, bool cursor=1)
Scrolls a line to the right.
Sets the 5x10 character font (else 5x8).
Definition: hd44780.hpp:88
Shifts display when byte written to display.
Definition: hd44780.hpp:78
iface::output * m_mode
Output interface to the control pins.
Definition: hd44780.hpp:379
void scrollUp(bool cursor=1)
Scrolls the contents of the display to the up.
Cursor/display shift.
Definition: hd44780.hpp:69
Turns on the screen (else turn off).
Definition: hd44780.hpp:83
void setTypingDelay(size_t delay)
Sets the typing delay between individual characters (milliseconds).
virtual ~hd44780()
Destructor method.
void strobe()
Toggles the enable pin.
Hitachi HD44780 LCD controller.
Definition: hd44780.hpp:51
RomCodes m_rom_code
ROM code.
Definition: hd44780.hpp:393
Turns on the cursor (else turn off).
Definition: hd44780.hpp:82
Display on/off control.
Definition: hd44780.hpp:68
void putChar(uint8_t c)
Puts a character on the display (low level).
uint8_t getCursorY() const
Returns the current y-position of the cursor.
Definition: hd44780-inl.hpp:47
void sendData(uint8_t data, size_t delay=0)
Sends data to the display.
uint8_t encodeChar(char32_t code)
Maps a unicode character to the corresponding code.
size_t getTypingDelay() const
Returns the typing delay between individual characters (milliseconds).
Custom character #7.
Definition: hd44780.hpp:103
size_t m_typing_delay
Typing delay (milliseconds).
Definition: hd44780.hpp:407
hd44780(uint8_t rs, uint8_t e, uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
Constructor method (4-bit mode).
Custom character #1.
Definition: hd44780.hpp:97
void setAutoscroll(uint8_t mode)
Sets the autoscroll mode.
void cmd(uint8_t data)
Sends a command to the display.
Definition: hd44780-inl.hpp:30
Set CGRAM address.
Definition: hd44780.hpp:71
uint8_t encodeCharA02(char32_t code)
Maps a unicode character to the corresponding code (ROM A02, European version).