|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: UTF-8 -*- |
| 3 | + |
| 4 | +"""I2C LCD showcase""" |
| 5 | + |
| 6 | +from lcd_i2c import LCD |
| 7 | +from machine import I2C, Pin |
| 8 | +from time import sleep |
| 9 | + |
| 10 | +I2C_ADDR = 0x27 |
| 11 | +I2C_NUM_ROWS = 2 |
| 12 | +I2C_NUM_COLS = 16 |
| 13 | +FREQ = 800000 # Try lowering this value in case of "Errno 5" |
| 14 | + |
| 15 | + |
| 16 | +def print_and_wait(text: str, sleep_time: int = 2) -> None: |
| 17 | + """ |
| 18 | + Print to console and wait some time. |
| 19 | +
|
| 20 | + :param text: The text to print to console |
| 21 | + :type text: str |
| 22 | + :param sleep_time: The sleep time in seconds |
| 23 | + :type sleep_time: int |
| 24 | + """ |
| 25 | + print(text) |
| 26 | + sleep(sleep_time) |
| 27 | + |
| 28 | + |
| 29 | +# define custom I2C interface, default is 'I2C(0)' |
| 30 | +# check the docs of your device for further details and pin infos |
| 31 | +i2c = I2C(1, scl=Pin(3), sda=Pin(2), freq=FREQ) |
| 32 | +lcd = LCD(addr=I2C_ADDR, cols=I2C_NUM_COLS, rows=I2C_NUM_ROWS, i2c=i2c) |
| 33 | + |
| 34 | +# get LCD infos/properties |
| 35 | +print("LCD is on I2C address {}".format(lcd.addr)) |
| 36 | +print("LCD has {} columns and {} rows".format(lcd.cols, lcd.rows)) |
| 37 | +print("LCD is used with a charsize of {}".format(lcd.charsize)) |
| 38 | +print("Cursor position is {}".format(lcd.cursor_position)) |
| 39 | + |
| 40 | +# start LCD, not automatically called during init to be Arduino compatible |
| 41 | +lcd.begin() |
| 42 | + |
| 43 | +# print text on sceen at first row, starting on first column |
| 44 | +lcd.print("Hello World") |
| 45 | +print_and_wait("Show 'Hello World' on LCD") |
| 46 | + |
| 47 | +# turn LCD off |
| 48 | +lcd.no_backlight() |
| 49 | +print_and_wait("Turn LCD backlight off") |
| 50 | + |
| 51 | +# get current backlight value |
| 52 | +print_and_wait("Backlight value: {}".format(lcd.get_backlight())) |
| 53 | + |
| 54 | +# turn LCD on |
| 55 | +lcd.backlight() |
| 56 | +print_and_wait("Turn LCD backlight on") |
| 57 | + |
| 58 | +# get current backlight value |
| 59 | +print_and_wait("Backlight value: {}".format(lcd.get_backlight())) |
| 60 | + |
| 61 | +# clear LCD display content |
| 62 | +lcd.clear() |
| 63 | +print_and_wait("Clear display content") |
| 64 | + |
| 65 | +# turn cursor on (show) |
| 66 | +lcd.cursor() |
| 67 | +print_and_wait("Turn cursor on (show)") |
| 68 | + |
| 69 | +# blink cursor |
| 70 | +lcd.blink() |
| 71 | +print_and_wait("Blink cursor") |
| 72 | + |
| 73 | +# return cursor to home position |
| 74 | +lcd.home() |
| 75 | +print_and_wait("Return cursor to home position") |
| 76 | + |
| 77 | +# stop blinking cursor |
| 78 | +lcd.no_blink() |
| 79 | +print_and_wait("Stop blinking cursor") |
| 80 | + |
| 81 | +# turn cursor off (hide) |
| 82 | +lcd.no_cursor() |
| 83 | +print_and_wait("Turn cursor off (hide)") |
| 84 | + |
| 85 | +# print_and_wait text on sceen |
| 86 | +lcd.print("Hello again") |
| 87 | +print_and_wait("Show 'Hello again' on LCD") |
| 88 | + |
| 89 | +# turn display off |
| 90 | +lcd.no_display() |
| 91 | +print_and_wait("Turn LCD off") |
| 92 | + |
| 93 | +# turn display on |
| 94 | +lcd.display() |
| 95 | +print_and_wait("Turn LCD on") |
| 96 | + |
| 97 | +# scroll display to the left |
| 98 | +for _ in "Hello again": |
| 99 | + lcd.scroll_display_left() |
| 100 | + sleep(0.5) |
| 101 | +print_and_wait("Scroll display to the left") |
| 102 | + |
| 103 | +# scroll display to the right |
| 104 | +for _ in "Hello again": |
| 105 | + lcd.scroll_display_right() |
| 106 | + sleep(0.5) |
| 107 | +print_and_wait("Scroll display to the right") |
| 108 | + |
| 109 | +# set text flow right to left |
| 110 | +lcd.clear() |
| 111 | +lcd.right_to_left() |
| 112 | +lcd.print("Right to left") |
| 113 | +print_and_wait("Set text flow right to left") |
| 114 | + |
| 115 | +# set text flow left to right |
| 116 | +lcd.left_to_right() |
| 117 | +lcd.clear() |
| 118 | +lcd.print("Left to right") |
| 119 | +print_and_wait("Set text flow left to right") |
| 120 | + |
| 121 | +# activate autoscroll |
| 122 | +lcd.autoscroll() |
| 123 | +print_and_wait("Activate autoscroll") |
| 124 | + |
| 125 | +# disable autoscroll |
| 126 | +lcd.no_autoscroll() |
| 127 | +print_and_wait("Disable autoscroll") |
| 128 | + |
| 129 | +# set cursor to second line, seventh column |
| 130 | +lcd.clear() |
| 131 | +lcd.cursor() |
| 132 | +# lcd.cursor_position = (7, 1) |
| 133 | +lcd.set_cursor(col=7, row=1) |
| 134 | +print_and_wait("Set cursor to row 1, column 7") |
| 135 | +lcd.no_cursor() |
| 136 | + |
| 137 | +# set custom char number 0 as :-) |
| 138 | +# custom char can be set for location 0 ... 7 |
| 139 | +lcd.create_char( |
| 140 | + location=0, |
| 141 | + charmap=[0x00, 0x00, 0x11, 0x04, 0x04, 0x11, 0x0E, 0x00] |
| 142 | + # this is the binary matrix, feel it, see it |
| 143 | + # 00000 |
| 144 | + # 00000 |
| 145 | + # 10001 |
| 146 | + # 00100 |
| 147 | + # 00100 |
| 148 | + # 10001 |
| 149 | + # 01110 |
| 150 | + # 00000 |
| 151 | +) |
| 152 | +print_and_wait("Create custom char ':-)'") |
| 153 | + |
| 154 | +# show custom char stored at location 0 |
| 155 | +lcd.print(chr(0)) |
| 156 | +print_and_wait("Show custom char") |
0 commit comments