88from font import vga1_8x16 as fontsmall
99
1010
11+ """
1112
13+ MicroHydra settings!
14+
15+ Updated for version 0.6:
16+ Distinguished Confirm button from other menu options, added scroll bar, added wrap-around for settings scrolling.
17+ Fixed issue with double-input when returning from menu option.
18+ Added ESC to exit without saving for settings menus
19+ Improved RGB hinting for color submenus
20+
21+ """
1222
1323
1424
@@ -65,7 +75,12 @@ def get_color(tft, font, kb, beep, setting_name, previous_color, ui_color, bg_co
6575 tft .vline (233 ,12 ,117 ,black )
6676 tft .vline (234 ,13 ,117 ,black )
6777
68- tft .text (font , setting_name , 120 - ((len (setting_name )* 16 ) // 2 ), 20 , ui_color , bg_color )
78+ tft .text (fontsmall , setting_name , 120 - ((len (setting_name )* 8 ) // 2 ), 20 , ui_color , bg_color )
79+ tft .text (fontsmall , "R/31" , 62 , 40 , 63488 , bg_color )
80+ tft .text (fontsmall , "G/63" , 106 , 40 , 2016 , bg_color )
81+ tft .text (fontsmall , "B/31" , 150 , 40 , 31 , bg_color )
82+
83+
6984
7085 rgb_select_index = 0
7186
@@ -168,7 +183,10 @@ def get_color(tft, font, kb, beep, setting_name, previous_color, ui_color, bg_co
168183 if ui_sound :
169184 beep .play (("C4" ,"D4" ,"E4" ), 50 , volume )
170185 return mh .combine_color565 (r ,g ,b )
171-
186+ elif "`" in pressed_keys and "`" not in prev_pressed_keys :
187+ if ui_sound :
188+ beep .play (("E4" ,"D4" ,"C4" ), 50 , volume )
189+ return previous_color
172190
173191
174192
@@ -186,14 +204,14 @@ def get_color(tft, font, kb, beep, setting_name, previous_color, ui_color, bg_co
186204 tft .text (font , str (clr ), 62 + (44 * idx ), 60 , ui_color , bg_color )
187205
188206 # pointer!
189- tft .fill_rect (62 , 94 , 120 , 24 , bg_color )
207+ tft .fill_rect (62 , 94 , 134 , 24 , bg_color )
190208 for i in range (0 ,16 ):
191209 tft .hline (
192- x = (78 - i ) + (42 * rgb_select_index ),
210+ x = (78 - i ) + (44 * rgb_select_index ),
193211 y = 94 + i ,
194212 length = 2 + (i * 2 ),
195213 color = mh .combine_color565 (r ,g ,b ))
196- tft .fill_rect (62 + (42 * rgb_select_index ), 110 , 34 , 8 , mh .combine_color565 (r ,g ,b ))
214+ tft .fill_rect (62 + (44 * rgb_select_index ), 110 , 34 , 8 , mh .combine_color565 (r ,g ,b ))
197215
198216
199217
@@ -260,7 +278,11 @@ def get_volume(tft, font, kb, beep, setting_name, previous_vol, ui_color, bg_col
260278 if ui_sound :
261279 beep .play (("C4" ,"D4" ,"E4" ), 50 , current_value )
262280 return current_value
263-
281+ elif "`" in pressed_keys and "`" not in prev_pressed_keys :
282+ if ui_sound :
283+ beep .play (("E4" ,"D4" ,"C4" ), 50 , previous_vol )
284+ return previous_vol
285+
264286 # graphics!
265287
266288 if redraw :
@@ -315,6 +337,10 @@ def get_text(tft, font, kb, beep, setting_name, previous_value, ui_color, bg_col
315337 elif 'SPC' in pressed_keys and 'SPC' not in prev_pressed_keys :
316338 current_value = current_value + ' '
317339 redraw = True
340+ elif "ESC" in pressed_keys and "ESC" not in prev_pressed_keys :
341+ if ui_sound :
342+ beep .play (("E4" ,"D4" ,"C4" ), 50 , volume )
343+ return previous_value
318344 else :
319345 for key in pressed_keys :
320346 if len (key ) == 1 and key not in prev_pressed_keys :
@@ -394,7 +420,11 @@ def get_bool(tft, font, kb, beep, setting_name, previous_val, ui_color, bg_color
394420 if ui_sound :
395421 beep .play (("C4" ,"D4" ,"E4" ), 50 , volume )
396422 return current_value
397-
423+ elif "`" in pressed_keys and "`" not in prev_pressed_keys :
424+ if ui_sound :
425+ beep .play (("E4" ,"D4" ,"C4" ), 50 , volume )
426+ return previous_val
427+
398428 # graphics!
399429 if redraw :
400430 tft .fill_rect (62 , 75 , 128 , 32 , bg_color )
@@ -465,7 +495,11 @@ def get_int(tft, font, kb, beep, setting_name, previous_val, minimum, maximum, u
465495 if ui_sound :
466496 beep .play (("C4" ,"D4" ,"E4" ), 50 , volume )
467497 return current_value
468-
498+ elif "`" in pressed_keys and "`" not in prev_pressed_keys :
499+ if ui_sound :
500+ beep .play (("E4" ,"D4" ,"C4" ), 50 , volume )
501+ return previous_val
502+
469503 # graphics!
470504 if redraw :
471505 tft .fill_rect (62 , 75 , 128 , 32 , bg_color )
@@ -476,6 +510,14 @@ def get_int(tft, font, kb, beep, setting_name, previous_val, minimum, maximum, u
476510 prev_pressed_keys = pressed_keys
477511
478512
513+
514+
515+
516+
517+
518+
519+
520+
479521#--------------------------------------------------------------------------------------------------
480522#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
481523#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Main Loop: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -487,6 +529,9 @@ def get_int(tft, font, kb, beep, setting_name, previous_val, minimum, maximum, u
487529
488530def main_loop ():
489531
532+ #bump up our clock speed so the UI feels smoother (240mhz is the max officially supported, but the default is 160mhz)
533+ machine .freq (240_000_000 )
534+
490535 #init the keyboard
491536 kb = keyboard .KeyBoard ()
492537 pressed_keys = []
@@ -572,14 +617,14 @@ def main_loop():
572617 if ui_sound :
573618 beep .play (("E3" ,"C3" ), 100 , volume )
574619 if cursor_index < 0 :
575- cursor_index = 0
620+ cursor_index = len ( setting_names ) - 1
576621 refresh_display = True
577622 elif "." in pressed_keys and "." not in prev_pressed_keys : # down arrow
578623 cursor_index += 1
579624 if ui_sound :
580625 beep .play (("D3" ,"C3" ), 100 , volume )
581626 if cursor_index >= len (setting_names ):
582- cursor_index = len ( setting_names ) - 1
627+ cursor_index = 0
583628 refresh_display = True
584629
585630 if "GO" in pressed_keys or "ENT" in pressed_keys :
@@ -590,34 +635,44 @@ def main_loop():
590635 config ["ui_color" ] = ui_color
591636 mid_color = mh .mix_color565 (ui_color , bg_color )
592637 force_redraw_display = True
638+ pressed_keys = kb .get_pressed_keys ()
593639
594640 elif setting_names [cursor_index ] == 'bg_color' :
595641 bg_color = get_color (tft , font , kb , beep , 'bg_color:' , bg_color , ui_color , bg_color , ui_sound , volume )
596642 config ["bg_color" ] = bg_color
597643 mid_color = mh .mix_color565 (ui_color , bg_color )
598644 force_redraw_display = True
599-
645+ pressed_keys = kb .get_pressed_keys ()
646+
600647 elif setting_names [cursor_index ] == 'volume' :
601648 volume = get_volume (tft , font , kb , beep , 'volume:' , volume , ui_color , bg_color , ui_sound )
602649 config ["volume" ] = volume
603650 force_redraw_display = True
651+ pressed_keys = kb .get_pressed_keys ()
604652
605653 elif setting_names [cursor_index ] == 'wifi_ssid' :
606654 wifi_ssid = get_text (tft , font , kb , beep , 'wifi_ssid:' , wifi_ssid , ui_color , bg_color , ui_sound , volume )
607655 config ["wifi_ssid" ] = wifi_ssid
608656 force_redraw_display = True
657+ pressed_keys = kb .get_pressed_keys ()
658+
609659 elif setting_names [cursor_index ] == 'wifi_pass' :
610660 wifi_pass = get_text (tft , font , kb , beep , 'wifi_pass:' , wifi_pass , ui_color , bg_color , ui_sound , volume )
611661 config ["wifi_pass" ] = wifi_pass
612662 force_redraw_display = True
663+ pressed_keys = kb .get_pressed_keys ()
664+
613665 elif setting_names [cursor_index ] == 'sync_clock' :
614666 sync_clock = get_bool (tft , font , kb , beep , 'sync_clock:' , sync_clock , ui_color , bg_color , ui_sound , volume )
615667 config ["sync_clock" ] = sync_clock
616668 force_redraw_display = True
669+ pressed_keys = kb .get_pressed_keys ()
670+
617671 elif setting_names [cursor_index ] == 'timezone' :
618672 timezone = get_int (tft , font , kb , beep , 'timezone:' , timezone , - 13 ,13 , ui_color , bg_color , ui_sound , volume )
619673 config ["timezone" ] = timezone
620674 force_redraw_display = True
675+ pressed_keys = kb .get_pressed_keys ()
621676
622677 elif setting_names [cursor_index ] == 'confirm' :
623678 with open ("config.json" , "w" ) as conf : #save changes
@@ -631,6 +686,20 @@ def main_loop():
631686 Pin (38 , Pin .OUT ).value (0 ) #backlight off
632687 spi .deinit ()
633688 # return home
689+ machine .freq (160_000_000 )
690+ time .sleep_ms (10 )
691+ machine .reset ()
692+ elif "`" in pressed_keys and "`" not in prev_pressed_keys :
693+ if ui_sound :
694+ beep .play ((("C3" ,"E3" ,"D3" ),"D4" ,"C4" ), 100 , volume )
695+ del beep
696+ # shut off the display
697+ tft .fill (black )
698+ tft .sleep_mode (True )
699+ Pin (38 , Pin .OUT ).value (0 ) #backlight off
700+ spi .deinit ()
701+ # return home
702+ machine .freq (160_000_000 )
634703 time .sleep_ms (10 )
635704 machine .reset ()
636705
@@ -640,10 +709,11 @@ def main_loop():
640709
641710 #scroll up and down logic
642711 if cursor_index >= setting_screen_index + 4 :
643- setting_screen_index += 1
712+ setting_screen_index += cursor_index - ( setting_screen_index + 3 )
644713 force_redraw_display = True
645714 elif cursor_index < setting_screen_index :
646- setting_screen_index -= 1
715+ #setting_screen_index -= 1
716+ setting_screen_index -= setting_screen_index - cursor_index
647717 force_redraw_display = True
648718
649719 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -656,14 +726,23 @@ def main_loop():
656726
657727 #blackout previous text
658728 if not force_redraw_display :
659- tft .fill_rect (x = 0 , y = (32 * (prev_cursor_index - setting_screen_index )) + 4 , width = display_width , height = 32 , color = bg_color )
729+ tft .fill_rect (x = 0 , y = (32 * (prev_cursor_index - setting_screen_index )) + 4 , width = 238 , height = 32 , color = bg_color )
660730
661731 # draw text
662732 for i in range (setting_screen_index , setting_screen_index + 4 ):
663733
664734 #blackout previous text
665735 if force_redraw_display :
666- tft .fill_rect (0 ,4 + ((i - setting_screen_index ) * 32 ),240 ,32 ,bg_color )
736+ tft .fill_rect (0 ,4 + ((i - setting_screen_index ) * 32 ),238 ,32 ,bg_color )
737+
738+ #scroll bar
739+ max_screen_index = len (setting_names ) - 4
740+ scrollbar_height = 135 // max_screen_index
741+ scrollbar_position = math .floor ((135 - scrollbar_height ) * (setting_screen_index / max_screen_index ))
742+
743+ tft .fill_rect (238 , 0 , 2 , 135 , bg_color )
744+ tft .fill_rect (238 , scrollbar_position , 2 , scrollbar_height , mid_color )
745+
667746
668747 if setting_names [i ] != 'confirm' and setting_names [i ] != 'wifi_pass' :
669748 # display value:
@@ -672,20 +751,33 @@ def main_loop():
672751 ((240 - (8 * len ( str (config [setting_names [i ]]) ))) + (16 * len (setting_names [i ]) ) ) // 2 , # centered in the empty space
673752 (32 * (i - setting_screen_index )) + 18 ,
674753 mid_color ,bg_color )
675-
676- if cursor_index == i : # the currently selected text
677- tft .text (font ,'>' + setting_names [i ] + '' ,- 2 , (32 * (i - setting_screen_index )) + 4 ,white ,mid_color )
678-
679- elif prev_cursor_index == i or force_redraw_display : # unselected text
680- tft .text (font ,setting_names [i ],6 , (32 * (i - setting_screen_index )) + 4 ,ui_color ,bg_color )
681-
754+
755+ #custom style for the confirm button
756+ if setting_names [i ] == "confirm" :
757+ if cursor_index == i : # the currently selected text
758+ tft .text (font ,"< Confirm >" ,32 , (32 * (i - setting_screen_index )) + 4 ,white ,mid_color )
759+
760+ elif prev_cursor_index == i or force_redraw_display : # unselected text
761+ tft .text (font ,"Confirm" ,64 , (32 * (i - setting_screen_index )) + 4 ,ui_color ,bg_color )
762+
763+ else :
764+ if cursor_index == i : # the currently selected text
765+ tft .text (font ,'>' + setting_names [i ] + '' ,- 2 , (32 * (i - setting_screen_index )) + 4 ,white ,mid_color )
766+
767+ elif prev_cursor_index == i or force_redraw_display : # unselected text
768+ tft .text (font ,setting_names [i ],6 , (32 * (i - setting_screen_index )) + 4 ,ui_color ,bg_color )
769+
682770 #dividing lines
683- tft .hline (0 ,36 ,display_width ,mid_color )
684- tft .hline (0 ,68 ,display_width ,mid_color )
685- tft .hline (0 ,100 ,display_width ,mid_color )
771+ tft .hline (0 ,36 ,234 ,mid_color )
772+ tft .hline (0 ,68 ,234 ,mid_color )
773+ tft .hline (0 ,100 ,234 ,mid_color )
774+
775+
686776
687777 refresh_display = False
688-
778+
779+
780+
689781 #update prev app selector index to current one for next cycle
690782 prev_cursor_index = cursor_index
691783 force_redraw_display = False
0 commit comments