forked from DesignSparkRS/RadioGlobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.py
93 lines (79 loc) · 2.74 KB
/
display.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#! /usr/bin/python3
import time
import threading
import liquidcrystal_i2c
DISPLAY_I2C_ADDRESS = 0x27
DISPLAY_I2C_PORT = 1
DISPLAY_COLUMNS = 20
DISPLAY_ROWS = 4
class Display (threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.lcd = liquidcrystal_i2c.LiquidCrystal_I2C(DISPLAY_I2C_ADDRESS, DISPLAY_I2C_PORT, numlines=DISPLAY_ROWS)
self.buffer = ["" for row in range(0, DISPLAY_ROWS)]
self.changed = False
def run(self):
while True:
if self.changed:
for line_num in range(0, DISPLAY_ROWS):
self.lcd.printline(line_num, self.buffer[line_num])
self.changed = False
time.sleep(0.1)
def clear(self):
self.buffer[0] = ""
self.buffer[1] = ""
self.buffer[2] = ""
self.buffer[3] = ""
self.changed = True
def message(self, line_1:str="", line_2:str="", line_3:str="", line_4:str=""):
self.buffer[0] = line_1.center(DISPLAY_COLUMNS)
self.buffer[1] = line_2.center(DISPLAY_COLUMNS)
self.buffer[2] = line_3.center(DISPLAY_COLUMNS)
self.buffer[3] = line_4.center(DISPLAY_COLUMNS)
self.changed = True
def update(self, north:int, east:int, location:str, volume:int, station:str, arrows:bool):
if north >= 0:
self.buffer[0] = ("{:5.2f}N, ").format(north)
else:
self.buffer[0] = ("{:5.2f}S, ").format(abs(north))
if east >= 0:
self.buffer[0] += ("{:6.2f}E").format(east)
else:
self.buffer[0] += ("{:6.2f}W").format(abs(east))
self.buffer[0] = self.buffer[0].center(DISPLAY_COLUMNS)
self.buffer[1] = location.center(DISPLAY_COLUMNS)
# Volume display
self.buffer[2] = ""
bar_length = (volume * DISPLAY_COLUMNS) // 100
for i in range(bar_length):
self.buffer[2] += "-"
for i in range(bar_length, DISPLAY_COLUMNS):
self.buffer[2] += " "
if arrows:
# Trim/pad the station name to fit arrows at each side of the display
station = station[:(DISPLAY_COLUMNS - 4)]
padding = DISPLAY_COLUMNS - 4 - len(station)
start_padding = padding // 2
end_padding = padding - start_padding
while start_padding:
station = " " + station
start_padding -= 1
while end_padding:
station += " "
end_padding -= 1
station = "< " + station + " >"
self.buffer[3] = station.center(DISPLAY_COLUMNS)
self.changed = True
if __name__ == "__main__":
try:
display_thread = Display(1, "Display")
display_thread.start()
display_thread.update(51.45, -2.59, "Bristol, United Kingdom", 45, "BBC Radio Bristol", True)
time.sleep(5)
display_thread.update(0, 0, "Clearing in 2s...", 0, "", False)
time.sleep(2)
display_thread.clear()
except:
exit()