This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatcher.py
148 lines (122 loc) · 3.27 KB
/
dispatcher.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import RPIO
from RPLCD import CharLCD
import socketserver
import sys
import json
from threading import Thread, Lock
from time import sleep
HOST = ''
if len(sys.argv) > 1:
PORT = int(sys.argv[1])
else:
PORT = 4000
refresh_rate = 0.05
NB_COLUMNS = 8
screensMutex = Lock()
screens = [{
'text': ''
}, {
'text': ''
}, {
'text': ''
}, {
'text': ''
}]
current_texts = ['', '', '', '']
offset_texts = [0, 0, 0, 0]
pins_e = [19, 5, 7, 8]
pins_out = [11, 13, 15, 16]
pin_rs = 3
pin_rw = 10
# pins_backlight = [22, 24, 21, 23]
nb_columns = 8
nb_rows = 2
lcds = []
for i in range(0, 4):
lcds.append(CharLCD(
pin_rs = pin_rs,
pin_e = pins_e[i],
pin_rw = pin_rw,
pins_data = pins_out,
numbering_mode = RPIO.BOARD,
cols = nb_columns,
rows = nb_rows,
dotsize = 8
))
# Set screen data using a mutex
def setScreen(id, s):
global screens
screensMutex.acquire()
try:
screens[id] = s
finally:
screensMutex.release()
# Get screens data using a mutex
def getScreens():
global screens
screensMutex.acquire()
try:
return screens
finally:
screensMutex.release()
# Handle recieved data
class TCPHandler(socketserver.BaseRequestHandler):
def handle(self):
self.data = self.request.recv(1024).strip()
data = json.loads(self.data.decode('utf-8'))
for screenId in data['screens']:
setScreen(screenId, data)
print("Update!")
updateRefreshRate()
self.request.sendall('goodby\n'.encode('utf-8'))
# Launch TCP server
def runServer():
if __name__ == "__main__":
server = socketserver.TCPServer((HOST, PORT), TCPHandler)
server.serve_forever()
# Write text on LCD screens
def write():
def printLongText(str, i):
separator = " | "
longStr = str + separator + str
off = offset_texts[i]
printOnLCD(longStr[off:off + NB_COLUMNS * 2], i)
offset_texts[i] += 1
if (offset_texts[i] > len(str) + len(separator) - 1):
offset_texts[i] = 0
while 1:
sleep(refresh_rate)
screens = getScreens()
for i, screen in enumerate(screens):
# On new text
newText = screen['text']
if (current_texts[i] != newText):
current_texts[i] = newText
# If the new text fit into the screen
if (len(newText) <= NB_COLUMNS * 2):
printOnLCD(newText, i)
else:
offset_texts[i] = 0
printLongText(newText, i)
else:
if (len(newText) > NB_COLUMNS * 2):
printLongText(newText, i)
def printOnLCD(str, i):
lcd = lcds[i]
lcd.clear()
lcd.home()
lcd.write_string(str[:NB_COLUMNS])
lcd.cursor_pos = (1, 0)
lcd.write_string(str[NB_COLUMNS:])
def updateRefreshRate():
global refresh_rate
nb_long_text = 0
screens = getScreens()
for screen in screens:
if (len(screen['text']) > NB_COLUMNS * 2):
nb_long_text += 1
refresh_rate = (4 - nb_long_text) * 0.05 # 0.15 seconds for 1 screen
serverThread = Thread(target=runServer)
serverThread.start()
writerThread = Thread(target=write)
writerThread.start()