Skip to content

Commit

Permalink
added functionalities: extended strings, custom characters, some demos
Browse files Browse the repository at this point in the history
  • Loading branch information
juvus committed Jul 14, 2021
1 parent c452199 commit 90a305a
Show file tree
Hide file tree
Showing 12 changed files with 441 additions and 3 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,35 @@ You can buy one of these great little I2C LCD on eBay or somewhere like [the Pi
```

# Demos
## Simple strings
This is a demo developed by user @Tomtom0201, showing how simple text stings could be displayed on LCD:

<p align="center">
<img src="imgs/demo_simple_strings.jpg" width="640" height="480">
</p>


## Extended strings
This is a demo debeloped by @juvus, showing how extended strings could be used. Extended strings can contain special placeholders `{0xFF}` - hex code of the symbol wrapped with curly bracers. Hex code of the symbol can be found in characters table. For example code of the symbol `ö` is 0xEF, and so this symbol could be printed by using `{0xEF}` placeholder in string. Table can be found in `/imgs/characters_table.png`.

<p align="center">
<img src="imgs/demo_extended_strings.jpg" width="640" height="480">
</p>

## Custom characters
It is possible to define in CG RAM memory 8 absolutelly custom characters. This characters can be pronted on LCD the same way as any characters from the character table. Codes of the custom characters are unique and the following: 1st - {0x00}, 2nd - {0x01}, 3rd - {0x02}, 4th - {0x03}, 5th - {0x04}, 6th - {0x05}, 7th - {0x06} and 8th - {0x07}. Please, see the `demo_lcd_custom_characters.py` file for more details, how it is possible to define you own characters. This demo was debeloped by @juvus.

<p align="center">
<img src="imgs/demo_custom_characters.jpg" width="640" height="480">
</p>

## Progress bar
This is a demo of graphical progress bar created with custom characters. This bar could be used, for example, for showing the current level of battery charge. This demo was debeloped by @juvus.

<p align="center">
<img src="imgs/demo_progress_bar.jpg" width="640" height="480">
</p>

## Backlight Control
This is a demo developed by user and watcher @Tomtom0201, showcasing backlight control of the LCD (available on some hardware):

Expand Down
121 changes: 121 additions & 0 deletions demo_lcd_custom_characters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#! /usr/bin/env python

# Custom characters string program. Writes strings with custom characters, that can't
# be found in the characters table. It is possible to use a custom characters by definig
# it's code on paseholder whyle useng and extended string.
# Appearance of the custom characters can be defined by redifenition of the fields of
# CustomCaracters class instance.
# Codes of the custom characters, that can be used in placeholders are the following:
# 1st - {0x00}, 2nd - {0x01}, 3rd - {0x02}, 4th - {0x03}, 5th - {0x04},
# 6th - {0x05}, 7th - {0x06} and 8th - {0x07}
# Remember to use method load_custom_characters_data() to load the custom characters
# data into the CG RAM.
# Demo program for the I2C 16x2 Display from Ryanteck.uk
# Created by Dmitry Safonov

# Import necessary libraries for communication and display use
import drivers
from time import sleep

# Load the driver and set it to "display"
# If you use something from the driver library use the "display." prefix first
display = drivers.Lcd()

# Create object with custom characters data
cc = drivers.CustomCharacters(display)

# Redefine the default characters:
# Custom caracter #1. Code {0x00}.
cc.char_1_data = ["01110",
"01010",
"01110",
"00100",
"11111",
"11111",
"11111",
"11111"]

# Custom caracter #2. Code {0x01}.
cc.char_2_data = ["11111",
"10101",
"10101",
"11111",
"11111",
"10101",
"10101",
"11111"]

# Custom caracter #3. Code {0x02}.
cc.char_3_data = ["10001",
"10001",
"10001",
"11111",
"11111",
"11111",
"11111",
"11111"]

# Custom caracter #4. Code {0x03}.
cc.char_4_data = ["11111",
"11011",
"10001",
"10001",
"10001",
"10001",
"11011",
"11111"]

# Custom caracter #5. Code {0x04}.
cc.char_5_data = ["00000",
"00000",
"11011",
"11011",
"00000",
"10001",
"01110",
"00000"]

# Custom caracter #6. Code {0x05}.
cc.char_6_data = ["01010",
"11111",
"11111",
"01110",
"00100",
"00000",
"00000",
"00000"]

# Custom caracter #7. Code {0x06}.
cc.char_7_data = ["11111",
"11011",
"10001",
"10101",
"10101",
"10101",
"11011",
"11111"]

# Custom caracter #8. Code {0x07}.
cc.char_8_data = ["11111",
"10001",
"11111",
"00000",
"00000",
"11111",
"10001",
"11111"]
# Load custom characters data to CG RAM:
cc.load_custom_characters_data()

# Main body of code
try:
while True:
# Remember that your sentences can only be 16 characters long!
print("Printing custom characters:")
display.lcd_display_string("Custom caracters:", 1) # Write line of text to first line of display
display.lcd_display_extended_string("{0x00}{0x01}{0x02}{0x03}{0x04}{0x05}{0x06}{0x07}", 2) # Write line of text to second line of display
sleep(2) # Give time for the message to be read
except KeyboardInterrupt:
# If there is a KeyboardInterrupt (when you press ctrl+c), exit the program and cleanup
print("Cleaning up!")
display.lcd_clear()
28 changes: 28 additions & 0 deletions demo_lcd_extended_strings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#! /usr/bin/env python

# Extended strings program. Writes and updates special (extended) strings
# with placeholders "{}" so, that it is possible to draw any character from the
# characters table using a caharcter code.
# Demo program for the I2C 16x2 Display from Ryanteck.uk
# Created by Matthew Timmons-Brown for The Raspberry Pi Guy YouTube channel

# Import necessary libraries for communication and display use
import drivers
from time import sleep

# Load the driver and set it to "display"
# If you use something from the driver library use the "display." prefix first
display = drivers.Lcd()

# Main body of code
try:
while True:
# Remember that your sentences can only be 16 characters long!
print("Writing simple string")
display.lcd_display_string("Simple string", 1) # Write line of text to first line of display
display.lcd_display_extended_string("Ext. str:{0xEF}{0xF6}{0xA5}{0xDF}{0xA3}", 2) # Write line of text to second line of display
sleep(2) # Give time for the message to be read
except KeyboardInterrupt:
# If there is a KeyboardInterrupt (when you press ctrl+c), exit the program and cleanup
print("Cleaning up!")
display.lcd_clear()
143 changes: 143 additions & 0 deletions demo_lcd_process_bar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#! /usr/bin/env python

# Programm showing anumated progress bar using custom characters.
# Demo program for the I2C 16x2 Display
# Created by Dmitry Safonov

# Import necessary libraries for communication and display use
import drivers
from time import sleep

# Load the driver and set it to "display"
# If you use something from the driver library use the "display." prefix first
display = drivers.Lcd()

# Create object with custom characters data
cc = drivers.CustomCharacters(display)

# Redefine the default characters that will be used to create process bar:
# Left full character. Code {0x00}.
cc.char_1_data = ["01111",
"11000",
"10011",
"10111",
"10111",
"10011",
"11000",
"01111"]

# Left empty character. Code {0x01}.
cc.char_2_data = ["01111",
"11000",
"10000",
"10000",
"10000",
"10000",
"11000",
"01111"]

# Central full character. Code {0x02}.
cc.char_3_data = ["11111",
"00000",
"11011",
"11011",
"11011",
"11011",
"00000",
"11111"]

# Central empty character. Code {0x03}.
cc.char_4_data = ["11111",
"00000",
"00000",
"00000",
"00000",
"00000",
"00000",
"11111"]

# Right full character. Code {0x04}.
cc.char_5_data = ["11110",
"00011",
"11001",
"11101",
"11101",
"11001",
"00011",
"11110"]

# Right empty character. Code {0x05}.
cc.char_6_data = ["11110",
"00011",
"00001",
"00001",
"00001",
"00001",
"00011",
"11110"]

# Load custom characters data to CG RAM:
cc.load_custom_characters_data()

MIN_CHARGE = 0
MAX_CHARGE = 100

# Main body of code
charge = 0
charge_delta = 5
bar_repr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

try:
while True:
# Remember that your sentences can only be 16 characters long!
display.lcd_display_string("Battery charge:", 1)

# Render charge bar:
bar_string = ""
for i in range(10):
if i == 0:
# Left character
if bar_repr[i] == 0:
# Left empty character
bar_string = bar_string + "{0x01}"
else:
# Left full character
bar_string = bar_string + "{0x00}"
elif i == 9:
# Right character
if bar_repr[i] == 0:
# Right empty character
bar_string = bar_string + "{0x05}"
else:
# Right full character
bar_string = bar_string + "{0x04}"
else:
# Central character
if bar_repr[i] == 0:
# Central empty character
bar_string = bar_string + "{0x03}"
else:
# Central full character
bar_string = bar_string + "{0x02}"

# Print the string to display:
display.lcd_display_extended_string(bar_string + " {0}% ".format(charge), 2)

# Update the charge and recalculate bar_repr
charge += charge_delta
if (charge >= MAX_CHARGE) or (charge <= MIN_CHARGE):
charge_delta = -1 * charge_delta

for i in range(10):
if charge >= ((i + 1) * 10):
bar_repr[i] = 1
else:
bar_repr[i] = 0

# Wait for some time
sleep(1)

except KeyboardInterrupt:
# If there is a KeyboardInterrupt (when you press ctrl+c), exit the program and cleanup
print("Cleaning up!")
display.lcd_clear()
File renamed without changes.
2 changes: 1 addition & 1 deletion drivers/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .i2c_dev import Lcd
from .i2c_dev import Lcd, CustomCharacters
Loading

0 comments on commit 90a305a

Please sign in to comment.