forked from the-raspberry-pi-guy/lcd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_lcd_backlight.py
57 lines (52 loc) · 3.56 KB
/
demo_lcd_backlight.py
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
# Simple string program. Writes and updates strings.
# Demo program for the I2C 16x2 Display from Ryanteck.uk
# Created by Matthew Timmons-Brown for The Raspberry Pi Guy YouTube channel
# Backlight: Enhanced by TOMDENKT - backlight control (on/off)
# Backlight: lcd_backlight(1) = ON, lcd_backlight(0) = OFF
# Backlight: Usage, if lcddriver is set to "display" (like example below)
# Backlight: display.lcd_backlight(0) # Turn backlight off
# Backlight: display.lcd_backlight(1) # Turn backlight on
# If lcddriver.py is NOT in same folder with your scripts,
# uncomment below and set path to lcddriver, e.g. "/home/pi/lcd"
#import sys
#sys.path.append("/home/pi/lcd") # example, path to lcddriver.py
# Import necessary libraries for communication and display use
import lcddriver
import time
# Load the driver and set it to "display"
# If you use something from the driver library use the "display." prefix first
display = lcddriver.lcd()
# Main body of code
try:
print("Press CTRL + C to quit program")
while True:
# Remember that your sentences can only be 16 characters long!
print("Loop: Writing to display and toggle backlight...")
display.lcd_backlight(1) # Make sure backlight is on / turn on
time.sleep(0.5) # Waiting for backlight toggle
display.lcd_backlight(0) # Turn backlight off
time.sleep(0.5) # Waiting for turning backlight on again
display.lcd_backlight(1) # Turn backlight on again
time.sleep(1) # Waiting for text
display.lcd_display_string("Demo Backlight", 1) # Write line of text to first line of display
display.lcd_display_string("Control ON/OFF", 2) # Write line of text to second line of display
time.sleep(2) # Waiting for backlight toggle
display.lcd_backlight(0) # Turn backlight off
time.sleep(0.5) # Waiting for turning backlight on again
display.lcd_backlight(1) # Turn backlight on again
time.sleep(0.5) # Waiting for turning backlight off again
display.lcd_backlight(0) # Turn backlight off
time.sleep(0.5) # Waiting for turning backlight on again
display.lcd_backlight(1) # Turn backlight on again
time.sleep(2) # Give time for the message to be read
display.lcd_display_string("I am a display! ", 1) # Refresh the first line of display with a different message
display.lcd_display_string("Demo Backlight", 2) # Refresh the second line of display with a different message
time.sleep(2) # Give time for the message to be read
display.lcd_clear() # Clear the display of any data
time.sleep(1) # Give time for the message to be read
display.lcd_backlight(0) # Turn backlight off
time.sleep(1.5) # Waiting until restart
except KeyboardInterrupt: # If there is a KeyboardInterrupt (when you press CTRL + C), exit the program and cleanup
print("Exit and cleaning up!")
display.lcd_clear()
display.lcd_backlight(1) # Make sure backlight is on / turn on by leaving