-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlcdui.py
More file actions
138 lines (114 loc) · 4.09 KB
/
lcdui.py
File metadata and controls
138 lines (114 loc) · 4.09 KB
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
#!/usr/bin/env python2
############3##################################################################
"""
Sharks LCD UI controller. Python2
"""
__author__='Donour Sizemore'
import time, datetime
import os,sys
import adafruit.Adafruit_CharLCDPlate as af
import client
VERBOSITY = 0
class Display:
def __init__(self, header = "MWRSCALE"):
self.header = header
self.lcd = af.Adafruit_CharLCDPlate()
self.refresh()
self.last_button_press = None
def set_startup(self):
self.lcd.clear()
self.lcd.backlight(self.lcd.RED)
def disable(self):
self.lcd.clear()
self.lcd.backlight(self.lcd.OFF)
def refresh(self, line2="---", status = ""):
self.lcd.home()
self.lcd.backlight(self.lcd.GREEN)
line1 = self.header + status
line1 += ' '*16
line2 += ' '*16
self.lcd.message(line1 + "\n" + line2)
def buttons(self):
"""
Poll all hardware buttons and return list of active buttons
"""
result = []
if self.last_button_press is not None:
if time.time() - self.last_button_press < 0.25:
return result
btns = [self.lcd.SELECT, self.lcd.LEFT, self.lcd.UP, self.lcd.DOWN, self.lcd.RIGHT]
for b in btns:
r = self.lcd.buttonPressed(b)
if r != 0:
result.append(b)
self.last_button_press = time.time()
return result
def settings_menu(self, options = ['option1', 'option2', 'option3']):
done = False
selected_option = 0
while done == False and len(options) > 0:
self.lcd.backlight(self.lcd.BLUE)
self.lcd.home()
line1 = "SelectConfig +-"
line2 = options[selected_option % len(options)] + ' '*16
self.lcd.message(line1 + "\n" + line2)
buttons = self.buttons()
if self.lcd.SELECT in buttons:
done = True
elif self.lcd.LEFT in buttons:
return None
elif self.lcd.UP in buttons:
selected_option += 1
elif self.lcd.DOWN in buttons:
selected_option -= 1
time.sleep(0.1)
return options[selected_option % len(options)]
if __name__ == "__main__":
config_dir = '/sharks.configs'
freq = 8 # display update frequency
if len(sys.argv) < 2:
print("usage: %s <ip address>" % sys.argv[0])
sys.exit(-1)
else:
addr = sys.argv[1]
fd = file("/etc/sharks.header", "r");
header = fd.read().strip()
config_options = os.listdir(config_dir)
d = Display(header)
d.set_startup()
host = addr
cli = client.recv_server(host)
status_animation = []
status_animation.append(" ^-------")
status_animation.append(" -^------")
status_animation.append(" --^-----")
status_animation.append(" ---^----")
status_animation.append(" ----^---")
status_animation.append(" -----^--")
status_animation.append(" ------^-")
status_animation.append(" -------^")
status_animation.append(" ------^-")
status_animation.append(" -----^--")
status_animation.append(" ----^---")
status_animation.append(" ---^----")
status_animation.append(" --^-----")
status_animation.append(" -^------")
animation_count = 0.0
if VERBOSITY > 0:
print "LCD UI Starting:", header, config_options, host
while True:
cli.register(host)
for i in range(0,freq):
sample = cli.get_sample()
if(sample != None):
if VERBOSITY > 1:
print sample
d.refresh(str(sample), status_animation[int(animation_count)%len(status_animation)])
buttons = d.buttons()
if len(buttons) > 0:
if d.lcd.RIGHT in buttons:
config = d.settings_menu(config_options)
if config is not None:
os.system(config_dir + "/"+config)
time.sleep(1.0/freq)
animation_count = (animation_count + 1)