forked from the-raspberry-pi-guy/lcd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_scrolling_text.py
49 lines (40 loc) · 1.49 KB
/
demo_scrolling_text.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
# -*- coding: utf-8 -*-
# Example: Scrolling text on display if the string length is major than columns in display.
# Created by Dídac García.
# 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 for stop this script!")
def long_string(display, text = '', num_line = 1, num_cols = 20):
"""
Parameters: (driver, string to print, number of line to print, number of columns of your display)
Return: This function send to display your scrolling string.
"""
if(len(text) > num_cols):
display.lcd_display_string(text[:num_cols],num_line)
time.sleep(1)
for i in range(len(text) - num_cols + 1):
text_to_print = text[i:i+num_cols]
display.lcd_display_string(text_to_print,num_line)
time.sleep(0.2)
time.sleep(1)
else:
display.lcd_display_string(text,num_line)
# Example of short string
long_string(display, "Hello World!", 1)
time.sleep(1)
# Example of long string
long_string(display, "Hello again. This is a long text.", 2)
display.lcd_clear()
time.sleep(1)
while True:
# An example of infinite scrolling text
long_string(display, "Hello friend! This is a long text!", 2)
except KeyboardInterrupt: # If there is a KeyboardInterrupt (when you press ctrl+c), exit the program and cleanup
print("Cleaning up!")
display.lcd_clear()