-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_app.py
162 lines (141 loc) · 6.26 KB
/
config_app.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import curses
import string
import time
import config_parser as config
class Config():
def __init__(self):
self.config_list = config.read_config()
self.quit = False
def main(self, stdscr):
while self.quit == False:
stdscr.clear()
stdscr.timeout(2000)
stdscr.addstr(0, 0, "CONFIGURATION SETTINGS", curses.A_BOLD)
stdscr.addstr(2, 0, "1. Units: ")
if self.config_list['units'] == 'F':
units = "Fahrenheit"
else:
units = "Celsius"
stdscr.addstr(units)
stdscr.addstr(3, 0, "2. Threshold Amplifier Shutdown Temperature: ")
stdscr.addstr(str(self.config_list['max_limit']) + '°'+self.config_list['units'])
stdscr.addstr(4, 0, "3. Sensor Display Names: ")
stdscr.addstr(str([self.config_list['sensor_1_name'],
self.config_list['sensor_2_name'],
self.config_list['sensor_3_name'],
self.config_list['sensor_4_name'],
self.config_list['sensor_5_name']]))
while self.quit == False:
choice = self.curses_input(stdscr, 8,0,"Select Option 1 - 3 using a USB keyboard:", True)
if choice == '1':
units = 'A'
while self.quit == False and units.upper() != 'C' and units.upper() != 'F' and units.upper() != 'Q':
units = self.curses_input(stdscr, 8,0,"For Fahrenheit type 'F', for Celsius type 'C', or to go back type 'Q':", True)
if units.upper() != self.config_list['units']:
if units.upper() == 'Q':
break
else:
self.config_list['units'] = units.upper()
old_max_limit = self.config_list['max_limit']
# 'F' to 'C'
if self.config_list['units'] == 'C':
self.config_list['max_limit'] = round((old_max_limit - 32) * 5.0/9.0, 1)
# 'C' to 'F'
else:
self.config_list['max_limit'] = round(9.0/5.0 * old_max_limit + 32, 1)
self.save_config(stdscr)
break
elif choice == '2':
max_limit = 'bad' # not a number
while max_limit.isdigit() == False and max_limit.upper() != 'Q':
if max_limit.isdigit() == False:
stdscr.clrtoeol()
max_limit = self.curses_input(stdscr, 8,0,"Type the new max limit temperature in °"+self.config_list['units']+", or type 'Q' + Enter to go back:")
if max_limit.upper() == 'Q':
break
else:
self.config_list['max_limit'] = float(max_limit)
self.save_config(stdscr)
break
elif choice == '3':
names = ['','','','','']
for n in range(1,6):
stdscr.clrtoeol()
name = self.config_list['sensor_'+str(n)+'_name']
name = self.curses_input(stdscr, 8,0,"Type in new name to replace \"" + name + "\", or type 'Q' + Enter to go back:")
if name.upper() == 'Q' or self.quit:
break
names[n-1] = name
if self.quit == False and name.upper() != 'Q':
for n in range(1,6):
self.config_list['sensor_'+str(n)+'_name'] = names[n-1]
self.save_config(stdscr)
break
curses.endwin
def save_config(self,stdscr):
config.write_config(self.config_list)
curses.noecho()
stdscr.erase()
try:
stdscr.addstr(1, 0, "SETTINGS SAVED! PULL CONFIG JUMPER TO GO BACK TO RUN MODE")
except:
pass
stdscr.refresh()
curses.napms(5000)
def curses_input(self, stdscr, row, col, prompt_string, ascii_mode=False):
"""
Get an input string with curses.
Row and col are the start position ot the prompt_string.
"""
stdscr.addstr(row, col, str(prompt_string), curses.A_REVERSE)
stdscr.addstr(" ")
#stdscr.addstr(row + 1, col, " " * (curses.COLS - 1))
stdscr.refresh()
input_val = ""
while self.quit == False:
if ascii_mode:
curses.noecho()
input_val = stdscr.getch()
if input_val != -1:
input_val = chr(input_val)
break
else:
#curses.cbreak()
curses.noecho()
#stdscr.keypad(1)
input_val = self.get_str(stdscr, 20)
#curses.nocbreak()
#stdscr.keypad(1)
#curses.endwin()
break
return input_val
def get_str(self, stdscr, return_len):
word = ''
while self.quit == False and len(word) <= return_len:
ch = stdscr.getch()
if ch != -1:
if ch == 8 or ch == 127 or ch == curses.KEY_BACKSPACE:
if len(word) > 0:
word = word[:-1]
stdscr.addstr("\b \b")
elif ch >= 258 and ch <= 261 or ch == 330:
pass
elif ch == 10:
y,_ = stdscr.getyx()
stdscr.move(y, 0)
stdscr.clrtoeol()
stdscr.refresh()
break
else:
##print(ch)
stdscr.addch(ch)
word += chr(ch)
return word
def run(self):
self.quit = False
curses.wrapper(self.main)
def stop(self):
self.quit = True
if __name__ == "__main__":
c = Config()
c.run()