|
1 | 1 | # Examples |
2 | 2 |
|
3 | | -Usage examples of this `micropython-package-template` library |
| 3 | +Usage examples of this `micropython-i2c-lcd` library |
4 | 4 |
|
5 | 5 | --------------- |
6 | 6 |
|
7 | | -## TBD |
| 7 | +## General |
8 | 8 |
|
9 | | -Some text with *italic*, **bold** and `hightlighted` words. |
| 9 | +An example of all implemented functionalities can be found at the |
| 10 | +[MicroPython I2C LCD examples folder][ref-micropython-i2c-lcd-examples] |
10 | 11 |
|
11 | | -```{note} |
12 | | -A reStructuredText highlighted note |
| 12 | +## Setup Display |
| 13 | + |
| 14 | +```python |
| 15 | +from lcd_i2c import LCD |
| 16 | +from machine import I2C, Pin |
| 17 | + |
| 18 | +I2C_ADDR = 0x27 |
| 19 | +NUM_ROWS = 2 |
| 20 | +NUM_COLS = 16 |
| 21 | + |
| 22 | +# define custom I2C interface, default is 'I2C(0)' |
| 23 | +# check the docs of your device for further details and pin infos |
| 24 | +i2c = I2C(1, scl=Pin(3), sda=Pin(2), freq=800_000) |
| 25 | +lcd = LCD(addr=I2C_ADDR, cols=NUM_COLS, rows=NUM_ROWS, i2c=i2c) |
| 26 | + |
| 27 | +# get LCD infos/properties |
| 28 | +print("LCD is on I2C address {}".format(lcd.addr)) |
| 29 | +print("LCD has {} columns and {} rows".format(lcd.cols, lcd.rows)) |
| 30 | +print("LCD is used with a charsize of {}".format(lcd.charsize)) |
| 31 | +print("Cursor position is {}".format(lcd.cursor_position)) |
| 32 | + |
| 33 | +# start LCD, not automatically called during init to be Arduino compatible |
| 34 | +lcd.begin() |
| 35 | +``` |
| 36 | + |
| 37 | +## Text |
| 38 | + |
| 39 | +### Show Text |
| 40 | + |
| 41 | +```python |
| 42 | +# LCD has already been setup, see section "Setup Display" |
| 43 | + |
| 44 | +lcd.print("Hello World") |
| 45 | +``` |
| 46 | + |
| 47 | +### Clear Text |
| 48 | + |
| 49 | +This command clears the text on the screen and sets the cursor position back |
| 50 | +to its home position at `(0, 0)` |
| 51 | + |
| 52 | +```python |
| 53 | +# LCD has already been setup, see section "Setup Display" |
| 54 | + |
| 55 | +lcd.clear() |
| 56 | +``` |
| 57 | + |
| 58 | +### Scroll Text |
| 59 | + |
| 60 | +```python |
| 61 | +# LCD has already been setup, see section "Setup Display" |
| 62 | +from time import sleep |
| 63 | + |
| 64 | +text = "Hello World" |
| 65 | + |
| 66 | +# show text on LCD |
| 67 | +lcd.print(text) |
| 68 | + |
| 69 | +# scroll text to the left |
| 70 | +for _ in text: |
| 71 | + lcd.scroll_display_left() |
| 72 | + sleep(0.5) |
| 73 | + |
| 74 | +# scroll text to the right |
| 75 | +for _ in text: |
| 76 | + lcd.scroll_display_right() |
| 77 | + sleep(0.5) |
| 78 | +``` |
| 79 | + |
| 80 | +### Text Flow |
| 81 | + |
| 82 | +```python |
| 83 | +# LCD has already been setup, see section "Setup Display" |
| 84 | + |
| 85 | +# set text flow right to left |
| 86 | +lcd.right_to_left() |
| 87 | +lcd.print("Right to left") |
| 88 | + |
| 89 | +# set text flow left to right |
| 90 | +lcd.left_to_right() |
| 91 | +lcd.print("Left to right") |
| 92 | +``` |
| 93 | + |
| 94 | +### Autoscroll |
| 95 | + |
| 96 | +```python |
| 97 | +# LCD has already been setup, see section "Setup Display" |
| 98 | + |
| 99 | +# activate autoscroll |
| 100 | +lcd.autoscroll() |
| 101 | + |
| 102 | +# disable autoscroll |
| 103 | +lcd.no_autoscroll() |
13 | 104 | ``` |
14 | 105 |
|
15 | | -```{eval-rst} |
16 | | -.. warning:: |
17 | | - Some eval warning |
| 106 | +### Custom Characters |
| 107 | + |
| 108 | +Custom characters can be defined for 8 CGRAM locations. The character has to |
| 109 | +be defined as binary of HEX list. In case you can't see the matrix, simply use |
| 110 | +the [LCD Character Creator page of Max Promer](https://maxpromer.github.io/LCD-Character-Creator/) |
| 111 | + |
| 112 | +The following example defines a upright happy smiley `:-)` at the first (0) |
| 113 | +location in the displays CGRAM using 5x10 pixels. Maybe you can see it ... |
| 114 | + |
| 115 | +``` |
| 116 | +00000 |
| 117 | +00000 |
| 118 | +10001 |
| 119 | +00100 |
| 120 | +00100 |
| 121 | +10001 |
| 122 | +01110 |
| 123 | +00000 |
| 124 | +``` |
| 125 | + |
| 126 | +```python |
| 127 | +# LCD has already been setup, see section "Setup Display" |
| 128 | + |
| 129 | +# custom char can be set for location 0 ... 7 |
| 130 | +lcd.create_char( |
| 131 | + location=0, |
| 132 | + charmap=[0x00, 0x00, 0x11, 0x04, 0x04, 0x11, 0x0E, 0x00] |
| 133 | +) |
| 134 | + |
| 135 | +# show custom char stored at location 0 |
| 136 | +lcd.print(chr(0)) |
| 137 | +``` |
| 138 | + |
| 139 | +## Backlight |
| 140 | + |
| 141 | +The following functions can be used to control the LCD backlight |
| 142 | + |
| 143 | +```python |
| 144 | +# LCD has already been setup, see section "Setup Display" |
| 145 | + |
| 146 | +# turn LCD off |
| 147 | +lcd.no_backlight() |
| 148 | + |
| 149 | +# turn LCD on |
| 150 | +lcd.backlight() |
| 151 | + |
| 152 | +# turn LCD off |
| 153 | +lcd.set_backlight(False) |
| 154 | + |
| 155 | +# turn LCD on |
| 156 | +lcd.set_backlight(True) |
| 157 | + |
| 158 | +# get current backlight value |
| 159 | +print("Backlight value: {}".format(lcd.get_backlight())) |
| 160 | + |
| 161 | +# get current backlight value via property |
| 162 | +print("Backlight value: {}".format(lcd.backlightval)) |
18 | 163 | ``` |
19 | 164 |
|
| 165 | +## Cursor |
| 166 | + |
| 167 | +The following functions can be used to control the cursor |
| 168 | + |
20 | 169 | ```python |
21 | | -def hello_world(): |
22 | | - print('Hello') |
| 170 | +# LCD has already been setup, see section "Setup Display" |
| 171 | + |
| 172 | +# turn cursor on (show) |
| 173 | +lcd.cursor() |
| 174 | + |
| 175 | +# turn cursor off (hide) |
| 176 | +lcd.no_cursor() |
| 177 | + |
| 178 | +# turn cursor on (show) |
| 179 | +lcd.cursor_on() |
| 180 | + |
| 181 | +# turn cursor off (hide) |
| 182 | +lcd.cursor_off() |
| 183 | + |
| 184 | +# blink cursor |
| 185 | +lcd.blink() |
| 186 | + |
| 187 | +# stop blinking cursor |
| 188 | +lcd.no_blink() |
| 189 | + |
| 190 | +# set cursor to home position (0, 0) |
| 191 | +lcd.home() |
| 192 | + |
| 193 | +# set cursor position to first line, third column |
| 194 | +lcd.set_cursor(col=3, row=0) |
| 195 | + |
| 196 | +# set cursor position to second line, seventh column |
| 197 | +lcd.cursor_position = (7, 1) |
| 198 | + |
| 199 | +# get current cursor position via property |
| 200 | +print("Cursor position: {}".format(lcd.cursor_position)) |
| 201 | +``` |
| 202 | + |
| 203 | +## Display |
| 204 | + |
| 205 | +```python |
| 206 | +# LCD has already been setup, see section "Setup Display" |
| 207 | + |
| 208 | +# turn display off |
| 209 | +lcd.no_display() |
| 210 | + |
| 211 | +# turn display on |
| 212 | +lcd.display() |
23 | 213 | ``` |
24 | 214 |
|
25 | | -| Type | Individual | Prefered food | |
26 | | -| ----- | ---------- | ------------- | |
27 | | -| Car | E-Power | Electricity | |
28 | | -| Animal | Cat | Mice | |
| 215 | +<!-- Links --> |
| 216 | +[ref-micropython-i2c-lcd-examples]: https://github.com/brainelectronics/micropython-i2c-lcd/tree/develop/examples |
0 commit comments