Commit b1ea2f3e authored by Fjen Undso's avatar Fjen Undso
Browse files

support resizing. cleanup

parent 904672c6
......@@ -11,136 +11,175 @@ from gui.container.focusswitcher import FocusSwitcher
from gui.container.numberbar import NumberBarContainer
class NCHL:
fs_main = None
continous = None
numbers = None
checkboxes_top = None
checkboxes_bottom = None
def __init__(self, screen):
self.screen = screen
self.logger = logging.getLogger(__name__)
self.strips = [LEDStrip(host_id=i) for i in range(1, 9)]
self.strips_all = LEDStrip()
# hide cursor
curses.curs_set(0)
curses.mousemask(1)
# init colors
curses.start_color()
curses.use_default_colors()
curses.init_pair(curses.COLOR_RED, 0, curses.COLOR_RED)
curses.init_pair(curses.COLOR_GREEN, 0, curses.COLOR_GREEN)
curses.init_pair(curses.COLOR_BLUE, 0, curses.COLOR_BLUE)
curses.init_pair(curses.COLOR_WHITE, 0, curses.COLOR_WHITE)
curses.init_pair(curses.COLOR_YELLOW, curses.COLOR_YELLOW, -1)
self.setup_screen()
def setup_screen(self):
self.screen.clear()
max_y, max_x = self.screen.getmaxyx()
scr_center_x = max_x // 2
# logging
for h in self.logger.handlers:
self.logger.removeHandler(h)
self.logger.addHandler(CursesHandler(self.screen))
self.logger.setLevel(logging.INFO)
# draw title
title = "nchl - ncurses HoneyLight control"
self.screen.addstr(0, scr_center_x - len(title) // 2, title, curses.A_BOLD)
# draw colors
numbers_text = [
("R", curses.COLOR_RED),
("G", curses.COLOR_GREEN),
("B", curses.COLOR_BLUE),
("W", curses.COLOR_WHITE),
]
progress_bars = [
ProgressBar(
window=self.screen,
row=1,
col=scr_center_x - 14 + i * 8,
width=5,
height=12,
value=255,
max_value=255,
attr=curses.color_pair(numbers_text[i][1]),
text=numbers_text[i][0],
)
for i in range(len(numbers_text))
]
self.numbers = [
NumberRange(
window=self.screen,
col=scr_center_x - 14 + i * 8,
row=13,
value=255,
prev_key="KEY_DOWN",
next_key="KEY_UP",
)
for i in range(len(numbers_text))
]
fs_pbn = [
NumberBarContainer(elements=i) for i in zip(self.numbers, progress_bars)
]
fs_n = FocusSwitcher(elements=fs_pbn, prev_key="KEY_LEFT", next_key="KEY_RIGHT")
# draw hosts
self.checkboxes_top = [
CheckBox(
window=self.screen,
col=scr_center_x - 21 + i // 2 * 11,
row=15,
toggle_key=" ",
toggle_key_unfocused=str(i),
text="ESP({})".format(i),
name=i,
)
for i in [1, 3, 5, 7]
]
self.checkboxes_bottom = [
CheckBox(
window=self.screen,
col=scr_center_x - 21 + (i - 1) // 2 * 11,
row=16,
toggle_key=" ",
toggle_key_unfocused=str(i),
text="ESP({})".format(i),
name=i,
)
for i in [2, 4, 6, 8]
]
fs_cb = FocusSwitcher(
elements=self.checkboxes_top + self.checkboxes_bottom,
prev_key="KEY_LEFT",
next_key="KEY_RIGHT",
)
# draw settings
self.continous = CheckBox(
window=self.screen,
col=scr_center_x - 12,
row=18,
toggle_key_unfocused="c",
text="(c)ontinuous updating",
)
self.screen.addstr(
19,
scr_center_x - 20,
"(s)end | all (o)ff | all (w)hite | (q)uit",
curses.color_pair(curses.COLOR_YELLOW),
)
# focusswitcher
fs_n_cb = FocusSwitcher(elements=[fs_n, fs_cb], prev_key="\t")
self.fs_main = FocusSwitcher(elements=[fs_n_cb, self.continous])
self.fs_main.toggle_focus()
self.screen.refresh()
def loop(self):
while True:
k = self.screen.getkey()
self.logger.info(k)
# send key to main window
self.fs_main.send_key(k)
# quit
if k == "q":
break
# all off:
if k == "o":
self.strips_all.set_all([0] * 4)
self.strips_all.send()
continue
# all white
if k == "w":
self.strips_all.set_all([0, 255, 100, 255])
self.strips_all.send()
continue
# send values to ESPs
if k == "s" or self.continous.active:
color = [self.numbers[i].value for i in range(1, 4)]
for e in self.checkboxes_top + self.checkboxes_bottom:
if e.active:
self.strips[e.name].set_all(color)
self.strips[e.name].send()
if k == "KEY_RESIZE":
self.setup_screen()
def main(stdscr):
stdscr.clear()
max_y, max_x = stdscr.getmaxyx()
scr_center_x = max_x // 2
# hide cursor
curses.curs_set(0)
curses.mousemask(1)
# init colors
curses.start_color()
curses.use_default_colors()
curses.init_pair(curses.COLOR_RED, 0, curses.COLOR_RED)
curses.init_pair(curses.COLOR_GREEN, 0, curses.COLOR_GREEN)
curses.init_pair(curses.COLOR_BLUE, 0, curses.COLOR_BLUE)
curses.init_pair(curses.COLOR_WHITE, 0, curses.COLOR_WHITE)
curses.init_pair(curses.COLOR_YELLOW, curses.COLOR_YELLOW, -1)
# logging
logger = logging.getLogger(__name__)
logger.addHandler(CursesHandler(stdscr))
logger.setLevel(logging.INFO)
# draw title
title = "nchl - ncurses HoneyLight control"
stdscr.addstr(0, scr_center_x - len(title) // 2, title, curses.A_BOLD)
# draw colors
numbers_text = [
('R', curses.COLOR_RED),
('G', curses.COLOR_GREEN),
('B', curses.COLOR_BLUE),
('W', curses.COLOR_WHITE)
]
progress_bars = [
ProgressBar(
window=stdscr,
row=1,
col=scr_center_x - 14 + i * 8,
width=5,
height=12,
value=255,
max_value=255,
attr=curses.color_pair(numbers_text[i][1]),
text=numbers_text[i][0]) for i in range(len(numbers_text))
]
numbers = [
NumberRange(
window=stdscr,
col=scr_center_x - 14 + i * 8,
row=13,
value=255,
prev_key='KEY_DOWN',
next_key='KEY_UP') for i in range(len(numbers_text))
]
fs_pbn = [
NumberBarContainer(elements=i) for i in zip(numbers, progress_bars)
]
fs_n = FocusSwitcher(elements=fs_pbn, prev_key='KEY_LEFT',
next_key='KEY_RIGHT')
# draw hosts
checkboxes_top = [
CheckBox(
window=stdscr,
col=scr_center_x - 21 + i // 2 * 11,
row=15,
toggle_key=' ',
toggle_key_unfocused=str(i),
text='ESP({})'.format(i),
name=i) for i in [1, 3, 5, 7]
]
checkboxes_bottom = [
CheckBox(
window=stdscr,
col=scr_center_x - 21 + (i - 1) // 2 * 11,
row=16,
toggle_key=' ',
toggle_key_unfocused=str(i),
text='ESP({})'.format(i),
name=i) for i in [2, 4, 6, 8]
]
fs_cb = FocusSwitcher(elements=checkboxes_top + checkboxes_bottom,
prev_key='KEY_LEFT', next_key='KEY_RIGHT')
# draw settings
continous = CheckBox(
window=stdscr,
col=scr_center_x - 12,
row=18,
toggle_key_unfocused='c',
text='(c)ontinuous updating'
)
stdscr.addstr(19, scr_center_x - 20,
'(s)end | all (o)ff | all (w)hite | (q)uit',
curses.color_pair(curses.COLOR_YELLOW))
# focusswitcher
fs_n_cb = FocusSwitcher(elements=[fs_n, fs_cb], prev_key='\t')
fs_main = FocusSwitcher(elements=[fs_n_cb, continous])
fs_main.toggle_focus()
stdscr.refresh()
strips = [LEDStrip(host_id=i) for i in range(1, 9)]
strips_all = LEDStrip()
while True:
k = stdscr.getkey()
logger.info(k)
fs_main.send_key(k)
# quit
if k == 'q':
break
# all off:
if k == 'o':
strips_all.set_all([0] * 4)
strips_all.send()
continue
# all white
if k == 'w':
strips_all.set_all([0, 255, 100, 255])
strips_all.send()
continue
# send values to ESPs
if k == 's' or continous.active:
color = [numbers[i].value for i in range(1, 4)]
for e in checkboxes_top + checkboxes_bottom:
if e.active:
strips[e.name].set_all(color)
strips[e.name].send()
if __name__ == '__main__':
nchl = NCHL(stdscr)
nchl.loop()
if __name__ == "__main__":
wrapper(main)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment