-
Notifications
You must be signed in to change notification settings - Fork 0
/
HD44780LCD.h
636 lines (559 loc) · 22.2 KB
/
HD44780LCD.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/**
* @file HD44780LCD.h
* @author Aditya Agarwal (aditya.agarwal@dumblebots.com)
*
* @brief Simple Library to use a 16x2 LCD display driven by the HD44780 driver with MBed OS
*
* @copyright Copyright (c) 2023
*
*/
#ifndef __HD44780LCD_H__
#define __HD44780LCD_H__
#include "mbed.h"
/**
* @brief Class that provides a simple interface to use a 16x2 LCD driven with the HD44780 driver
*
*/
class HD44780LCD
: public Stream
{
/** the default address of the I2C Peripheral that controls the LCD */
static constexpr uint8_t DEFAULT_I2C_ADDR = (0x27<<1);
/**
* @brief Class that provides an interface to use the display via the PC8574 I2C-driven chip
*
*/
class I2CInterface {
/** I2C bus being used to connect to the PC8574 chip */
I2C con;
/** Address of the PC8574 chip on the I2C Bus */
uint8_t addr;
/** Bitmask holding the status of the backlight */
uint8_t backlightMask;
public:
/**
* @brief Construct a new I2CInterface object
*
* @param I2cSda Microcontroller Pin to which the SDA (Serial Data) Pin of the LCD is connected (must be an SDA Pin)
* @param I2cScl Microcontroller Pin to which the SCL (Serial Clock) Pin of the LCD is connected (must be an SCL Pin)
* @param addr Address of the PC8574 chip on the I2C Bus
*/
I2CInterface(PinName I2cSda, PinName I2cScl, uint8_t addr = DEFAULT_I2C_ADDR);
/**
* @brief Send a byte (data or instruction) to the LCD (two nibbles in half-bus mode)
*
* @param byte Byte to send (data or instruction indicated by ```isData```)
* @param isData Whether the byte was data or an instruction
*/
void send_byte(uint8_t byte, uint8_t isData);
/**
* @brief Send a nibble (data or instruction) to the LCD
*
* @param nibble Nibble to send (data or instruction indicated by ```isData```)
* @param isData Whether the byte was data or an instruction
*/
void send_nibble(uint8_t nibble, uint8_t isData);
/**
* @brief Enable the LCD's backlight
*
*/
void enable_backlight();
/**
* @brief Disable the LCD's backlight
*
*/
void disable_backlight();
/**
* @brief Toggle the LCD's backlight
*
*/
void toggle_backlight();
/**
* @brief Check the state of the LCD's backlight
*
* @return true if the backlight is switched on, false otherwise
*/
bool is_backlight_on() const;
};
/** Interface to communicate with the LCD */
I2CInterface con;
/** Location of the cursor in the DDRAM of the LCD */
uint32_t cursorLoc;
/** Bitmask containing the enabled/disabled states of the display, underline cursor and blinking cursor */
uint16_t displayState {0};
/** Bitmask containing the entry mode of the LCD */
uint16_t cursorMovement {0};
public:
/**
* @brief Different types of movements the cursor/display can perform after printing a character
*
*/
enum EntryMode {
UNINITIALIZED,
CURSOR_INC,
CURSOR_DEC,
DISPLAY_DEC,
DISPLAY_INC
};
/**
* @brief Different types of cursors that can be displayed
*
*/
enum CursorMode {
// the mode is a composition of the two values
BLINKING_CURSOR = 0x01,
UNDERLINE_CURSOR = 0x02,
};
HD44780LCD() = delete;
HD44780LCD(const HD44780LCD &) = delete;
/**
* @brief Construct a new HD44780LCD object
*
* @param i2c_sda Microcontroller Pin to which the SDA (Serial Data) Pin of the LCD is connected (must be an SDA Pin)
* @param i2c_scl Microcontroller Pin to which the SCL (Serial Clock) Pin of the LCD is connected (must be an SCL Pin)
*
*/
HD44780LCD(PinName i2c_sda, PinName i2c_scl);
/**
* @brief Initializes the LCD by running its initialization sequence
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
*/
void initialize();
// methods to display characters on the LCD
/**
* @brief Send a single byte of data (character) to the LCD to display
*
* @remark This method alters the cursor position
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
* @param byte Character to send to be displayed
*
*/
void send_data(const uint8_t byte);
/**
* @brief Send an array of bytes (characters) to the LCD to display
*
* @remark This method alters the cursor position
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
* @param buf Pointer to array of characters
* @param len Number of characters to pick from the buffer
*/
void send_buffer(const uint8_t *buf, const uint32_t len);
/**
* @brief Create a glyph for a custom character in the LCD's memory
* See Example section for usage
*
* @remark This method does not alter the cursor position
*
* @remark The location of the character must be between 0 and 8 (exclusive), values outside
* this range will cause undefined behaviour
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
* @example To create a smiley face and display it
* @code
* uint8_t smiley[8] = {
* 0b00000,
* 0b01010,
* 0b01010,
* 0b01010,
* 0b00000,
* 0b10001,
* 0b01110,
* 0b00000
* };
*
* lcd.create_custom_char(7, smiley);
* lcd.send_data(7);
* lcd.send_buffer("\x07\x07\x07", 3); // prints 3 more smiley faces
* @endcode
*
* @param loc Location (between 0 and 8 exclusive) in CGRAM to store glyph
* @param glyph Pattern of glyph
*
*/
void create_custom_char(const uint32_t loc, const uint8_t glyph[8]);
// method to manage backlight
/**
* @brief Enable the LCD's backlight
*
* @remark This method does not alter the cursor position
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
*/
void enable_backlight();
/**
* @brief Disable the LCD's backlight
*
* @remark This method does not alter the cursor position
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
*/
void disable_backlight();
/**
* @brief Toggle the LCD's backlight
*
* @remark This method does not alter the cursor position
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
*/
void toggle_backlight();
/**
* @brief Check the state of the LCD's backlight
*
* @remark This method does not alter the cursor position
*
* @attention This method can be called from ISR context
*
* @return true if the backlight is switched on, false otherwise
*/
bool is_backlight_on() const;
// methods to manage entry mode
/**
* @brief Set the cursor to automatically decrement after printing a character
*
* @remark This method does not alter the cursor position
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
*/
void set_cursor_auto_dec();
/**
* @brief Set the cursor to automatically increment after printing a character
*
* @remark This method does not alter the cursor position
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
*/
void set_cursor_auto_inc();
/**
* @brief Set the display to automatically scroll left after printing a character
*
* @remark This method does not alter the cursor position
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
* @warning Setters/Getters for cursor position do not work in a well-defined way after calling this method
*
*/
void set_display_auto_dec();
/**
* @brief Set the display to automatically scroll right after printing a character
*
* @remark This method does not alter the cursor position
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
* @warning Setters/Getters for cursor position do not work in a well-defined way after calling this method
*
*/
void set_display_auto_inc();
/**
* @brief Get the entry mode of the LCD
*
* @remark This method does not alter the cursor position
*
* @attention This method can be called from ISR context
*
* @return EntryMode The direction the cursor moves/display scrolls in after a character is printed
* as set using either of the following methods
* ```HD44780LCD::set_cursor_auto_dec()```
* ```HD44780LCD::set_cursor_auto_inc()```
* ```HD44780LCD::set_display_auto_dec()```
* ```HD44780LCD::set_display_auto_inc()```
*/
EntryMode get_entry_mode() const;
// methods to manage display
/**
* @brief Clear the display
*
* @remark This method does not alter the cursor position
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
*/
void clear_display();
/**
* @brief Enable the display (starts displaying characters without mutating memory)
*
* @remark This method does not alter the cursor position
*
* @remark See also ```HD44780LCD::disable_display()```
* @remark See also ```HD44780LCD::toggle_display()```
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
*/
void enable_display();
/**
* @brief Disable the display (hides characters without mutating memory)
*
* @remark This method does not alter the cursor position
*
* @remark See also ```HD44780LCD::enable_display()```
* @remark See also ```HD44780LCD::toggle_display()```
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
*/
void disable_display();
/**
* @brief Toggle the display (show or hide characters being shown without mutating memory)
*
* @remark This method does not alter the cursor position
*
* @remark See also ```HD44780LCD::enable_display()```
* @remark See also ```HD44780LCD::disable_display()```
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
*/
void toggle_display();
/**
* @brief Check whether the display is enabled or not
*
* @remark This method does not alter the cursor position
*
* @attention This method can be called from ISR context
*
* @return true if the display is enabled, false otherwise
*/
bool is_display_enabled() const;
// methods to manage cursor position
/**
* @brief Set the cursor to the home position
*
* @remark This method alters the cursor position
*/
void set_cursor_home();
/**
* @brief Move the cursor one position to the left (wrap-around to previous line if reached start-of-line)
*
* @remark This method alters the cursor position
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
*/
void move_cursor_left();
/**
* @brief Move the cursor one position to the right (wrap-around to next line if reached end-of-line)
*
* @remark This method alters the cursor position
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
*/
void move_cursor_right();
/**
* @brief Set the position of the cursor on the display
*
* @remark This method alters the cursor position
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
* @param r Row to which the cursor should be moved
* @param c Column to which the cursor should be moved
*/
void set_cursor_pos(const uint32_t r, const uint32_t c);
/**
* @brief Get the row in which the cursor is situated
*
* @remark This method does not alter the cursor position
*
* @attention This method can be called from ISR context
*
* @return uint32_t Row in which the cursor is situated
*/
uint32_t get_cursor_row() const;
/**
* @brief Get the column in which the cursor is situated
*
* @remark This method does not alter the cursor position
*
* @attention This method can be called from ISR context
*
* @return uint32_t Column in which the cursor is situated
*/
uint32_t get_cursor_col() const;
// methods to manage cursor aesthetic
/**
* @brief Enable displaying the underline cursor
*
* @remark This method does not alter the cursor position
*
* @remark See also ```HD44780LCD::disable_cursor_display()```
* @remark See also ```HD44780LCD::toggle_cursor_display()```
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
*/
void enable_cursor_display();
/**
* @brief Disable displaying the underline cursor
*
* @remark This method does not alter the cursor position
*
* @remark See also ```HD44780LCD::enable_cursor_display()```
* @remark See also ```HD44780LCD::toggle_cursor_display()```
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
*/
void disable_cursor_display();
/**
* @brief Toggle displaying the underline cursor
*
* @remark This method does not alter the cursor position
*
* @remark See also ```HD44780LCD::enable_cursor_display()```
* @remark See also ```HD44780LCD::disable_cursor_display()```
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
*/
void toggle_cursor_display();
/**
* @brief Check if displaying the underline cursor is enabled
*
* @remark This method does not alter the cursor position
*
* @attention This method can be called from ISR context
*
* @return true if displaying the underline cursor is enabled, false otherwise
*/
bool is_cursor_displayed() const;
/**
* @brief Enable displaying the blinking cursor
*
* @remark This method does not alter the cursor position
*
* @remark See also ```HD44780LCD::disable_blinking_display()```
* @remark See also ```HD44780LCD::toggle_blinking_display()```
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
*/
void enable_blinking_cursor();
/**
* @brief Disable displaying the blinking cursor
*
* @remark This method does not alter the cursor position
*
* @remark See also ```HD44780LCD::enable_blinking_display()```
* @remark See also ```HD44780LCD::toggle_blinking_display()```
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
*/
void disable_blinking_cursor();
/**
* @brief Toggle blinking the blinking cursor
*
* @remark This method does not alter the cursor position
*
* @remark See also ```HD44780LCD::enable_blinking_display()```
* @remark See also ```HD44780LCD::disable_blinking_display()```
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
*/
void toggle_blinking_cursor();
/**
* @brief Check if displaying the blinking cursor is enabled
*
* @remark This method does not alter the cursor position
*
* @attention This method can be called from ISR context
*
* @return true if displaying the blinking cursor is enabled, false otherwise
*/
bool is_blinking_cursor_displayed() const;
// methods to move display
/**
* @brief Scroll the contents of display one position to the left
*
* @warning This method scrolls the display contents to the left without moving the cursor's location,
* but the position changes as the cursor is also moved to the left
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
*/
void scroll_display_left();
/**
* @brief Scroll the contents of the display one position to the right
*
* @warning This method scrolls the display contents to the right without moving the cursor's location,
* but the position changes as the cursor is also moved to the right
*
* @attention Can not call this method from ISR context
* @attention It is unsafe to call this method from multiple threads concurently
*
*/
void scroll_display_right();
// methods for getting stream
/**
* @brief Get a pointer to the underlying stream for using with file stream APIs
*
* @remark This method does not alter the cursor position
*
* @attention It is safe to call this method from ISR context
*
* @return HD44780LCD* Pointer to underlying stream
*/
HD44780LCD* get_stream();
// methods inherited from Stream
int _putc(int c) override;
int _getc() override;
private:
/**
* @brief Update the position of the cursor in the display to match the stored value
*
* @attention Can not call this method from ISR context
*/
void update_display_cursor_pos();
/**
* @brief Increment the position of the cursor while handling wrap-around
*
*/
void inc_cursor_loc();
/**
* @brief Decrement the position of the cursor while handling wrap-around
*
*/
void dec_cursor_loc();
};
#endif //__HD44780LCD_H__