Skip to content

Commit a059063

Browse files
committed
Adding curses.wrapper()
It resets the terminal to a good state if the program has an unexpected exit.
1 parent 0d2e940 commit a059063

File tree

1 file changed

+54
-82
lines changed

1 file changed

+54
-82
lines changed

console.py

+54-82
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def get_pressure_data():
3333
return_data += "Pressure:\t9.00E2\n"
3434
return return_data
3535

36-
def loop():
36+
def loop(data_window, data_text_window, data_logging_window, stdscr):
3737

3838
continuing = 1
3939

@@ -71,111 +71,83 @@ def loop():
7171
return continuing
7272

7373

74+
def main(stdscr):
75+
curses.curs_set(0)
7476

7577

76-
send = ""
77-
out = ""
78-
79-
80-
preamble = "@253" #attention + default address, needed even with RS232
81-
command = ""
82-
terminator = ";FF"
83-
84-
85-
#Serial port communication
86-
# port = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=3.0)
87-
# assemble = preamble + command + terminator
88-
# port.write(assemble)
89-
# time.sleep(1)
90-
# while port.inWaiting() > 0:
91-
# out += port.read(1)
92-
# port.close()
93-
94-
95-
stdscr = curses.initscr()
96-
97-
#initialize screen
98-
curses.noecho()
99-
curses.cbreak()
100-
curses.curs_set(0)
101-
102-
if curses.has_colors():
103-
curses.start_color()
104-
105-
#some color choices
106-
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
107-
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
108-
curses.init_pair(3, curses.COLOR_BLUE, curses.COLOR_BLACK)
109-
110-
111-
#header
112-
stdscr.addstr("MKS INTERFACE", curses.A_REVERSE)
113-
stdscr.chgat(-1, curses.A_REVERSE)
78+
#some color choices
79+
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
80+
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
81+
curses.init_pair(3, curses.COLOR_BLUE, curses.COLOR_BLACK)
11482

83+
#header
84+
stdscr.addstr("MKS INTERFACE", curses.A_REVERSE)
85+
stdscr.chgat(-1, curses.A_REVERSE)
11586

116-
stdscr.addstr(curses.LINES-1, 0, "'r' to refresh, 'p' to poll, 'q' to quit")
87+
stdscr.addstr(curses.LINES-1, 0, "'r' to refresh, 'p' to poll, 'q' to quit")
11788

11889

119-
# green r, blue p, red q
120-
stdscr.chgat(curses.LINES-1, 1, 1, curses.A_BOLD | curses.color_pair(2))
121-
stdscr.chgat(curses.LINES-1, 17, 1, curses.A_BOLD | curses.color_pair(3))
122-
stdscr.chgat(curses.LINES-1, 30, 1, curses.A_BOLD | curses.color_pair(1))
123-
124-
#window to display gauge setup
125-
data_window = curses.newwin(curses.LINES-2, curses.COLS, 1, 0)
126-
127-
#border windows
128-
data_text_border_window = data_window.subwin(curses.LINES-6, (curses.COLS-4)/2, 3, 2)
129-
data_logging_border_window = data_window.subwin(curses.LINES-6, (curses.COLS-4)/2, 3, (curses.COLS-4)/2 + 2)
130-
131-
132-
133-
#creating subwindow to cleanly display text without touching window's borders
134-
data_text_window = data_text_border_window.subwin(curses.LINES-8, (curses.COLS-6)/2-1, 4, 3)
135-
90+
# green r, blue p, red q
91+
stdscr.chgat(curses.LINES-1, 1, 1, curses.A_BOLD | curses.color_pair(2))
92+
stdscr.chgat(curses.LINES-1, 17, 1, curses.A_BOLD | curses.color_pair(3))
93+
stdscr.chgat(curses.LINES-1, 30, 1, curses.A_BOLD | curses.color_pair(1))
94+
13695

137-
#subwindow for showing pressure log
138-
data_logging_window = data_logging_border_window.subwin(curses.LINES-8, (curses.COLS-6)/2-1, 4, (curses.COLS-4)/2 + 3)
96+
#window to display gauge setup
97+
data_window = curses.newwin(curses.LINES-2, curses.COLS, 1, 0)
13998

99+
#border windows
100+
data_text_border_window = data_window.subwin(curses.LINES-6, (curses.COLS-4)/2, 3, 2)
101+
data_logging_border_window = data_window.subwin(curses.LINES-6, (curses.COLS-4)/2, 3, (curses.COLS-4)/2 + 2)
140102

103+
104+
#creating subwindow to cleanly display text without touching window's borders
105+
data_text_window = data_text_border_window.subwin(curses.LINES-8, (curses.COLS-6)/2-1, 4, 3)
141106

142107

143-
#data_text_window.addstr("Press 'R' to load data")
144-
#data_logging_window.addstr("Press 'P' to log here")
108+
#subwindow for showing pressure log
109+
data_logging_window = data_logging_border_window.subwin(curses.LINES-8, (curses.COLS-6)/2-1, 4, (curses.COLS-4)/2 + 3)
145110

111+
#draw a box around main window
112+
data_window.box()
146113

147-
#draw a box around main window
148-
data_window.box()
114+
data_text_border_window.box()
115+
data_logging_border_window.box()
149116

150-
data_text_border_window.box()
151-
data_logging_border_window.box()
117+
data_text_window.addstr("Press 'r' to load data")
118+
data_logging_window.addstr("Press 'p' to log pressure data here")
152119

153-
#data_logging_window.box()
154-
#data_text_window.box()
120+
#update internal curses structures
121+
stdscr.noutrefresh()
122+
data_window.noutrefresh()
155123

124+
#redraw screen
125+
curses.doupdate()
156126

157-
data_text_window.addstr("Press 'r' to load data")
158-
data_logging_window.addstr("Press 'p' to log pressure data here")
127+
while True:
128+
if not loop(data_window, data_text_window, data_logging_window, stdscr):
129+
break
159130

160131

161-
#update internal curses structures
162-
stdscr.noutrefresh()
163-
data_window.noutrefresh()
164132

165-
#redraw screen
166-
curses.doupdate()
133+
send = ""
134+
out = ""
167135

168-
while True:
169-
if not loop():
170-
break
171136

137+
preamble = "@253" #attention + default address, needed even with RS232
138+
command = ""
139+
terminator = ";FF"
172140

141+
curses.wrapper(main)
173142

174143

144+
#Serial port communication
145+
# port = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=3.0)
146+
# assemble = preamble + command + terminator
147+
# port.write(assemble)
148+
# time.sleep(1)
149+
# while port.inWaiting() > 0:
150+
# out += port.read(1)
151+
# port.close()
175152

176-
#restore terminal settings
177-
curses.nocbreak()
178-
curses.echo()
179-
curses.curs_set(1)
180153

181-
curses.endwin()

0 commit comments

Comments
 (0)