1111import time
1212import sys
1313import board
14+ import digitalio
1415import nonblocking_serialinput as nb_serialin
1516
1617##########################################
1718# globals
18- my_input = nb_serialin .NonBlockingSerialInput ()
1919
2020
21- class MyProjectMainClass ( object ) :
21+ class MyProjectMainClass :
2222 """This is just the Container Class for my Project."""
2323
24- def __init__ (self , arg ):
25- super (MyProjectMainClass , self ).__init__ ()
26- self .arg = arg
27-
28-
29- ##########################################
30- # menu
31-
32-
33- def print_help (self ):
34- """Print Help."""
35- profile_list = ""
36- # for name, profile in self.profiles.items():
37- # profile_list += " {}\n".format(profile.title)
38- # ^--> random order..
39- for name in self .profiles_names :
40- current = ""
41- if self .profiles [name ] is self .profile_selected :
42- current = "*"
43- profile_list += " {: 1}{}\n " .format (current , self .profiles [name ].title_short )
44- print (
45- "you do some things:\n "
46- "- 't': toggle print runtime ({print_runtime})\n "
47- "- 's': set print runtime intervall ({intervall: > 7.2f})\n "
48- "- 'pn' select next profil\n "
49- "{profile_list}"
50- "- 'calibrate'\n "
51- "- 'start' reflow cycle\n "
52- "- 'stop' reflow cycle\n "
53- "" .format (
54- profile_list = profile_list ,
55- heater_target = self .reflowcontroller .heater_target ,
56- ),
57- end = "" ,
58- )
59- self .print_temperature ()
60-
61-
62- def check_input (self ):
63- """Check Input."""
64- input_string = input ()
65- # sys.stdin.read(1)
66- if "pn" in input_string :
67- self .reflowcontroller .profile_select_next ()
68- if "pid p" in input_string :
69- value = nb_serialin .parse_value (input_string , "pid p" )
70- if value :
71- self .reflowcontroller .pid .P_gain = value
72- if "h" in input_string :
73- value = nb_serialin .parse_value (input_string , "h" )
74- if nb_serialin .is_number (value ):
75- self .reflowcontroller .heater_target = value
76- # prepare new input
77- self .print_help ()
78- print (">> " , end = "" )
79-
80-
81- @staticmethod
82- def input_parse_pixel_set (input_string ):
83- """parse pixel_set."""
84- # row = 0
85- # col = 0
86- # value = 0
87- # sep_pos = input_string.find(",")
88- # sep_value = input_string.find(":")
89- # try:
90- # col = int(input_string[1:sep_pos])
91- # except ValueError as e:
92- # print("Exception parsing 'col': ", e)
93- # try:
94- # row = int(input_string[sep_pos + 1 : sep_value])
95- # except ValueError as e:
96- # print("Exception parsing 'row': ", e)
97- # try:
98- # value = int(input_string[sep_value + 1 :])
99- # except ValueError as e:
100- # print("Exception parsing 'value': ", e)
101- # pixel_index = 0
102- pass
103-
104-
105- ##########################################
106- # functions
107-
108-
109- def main_update (self ):
110- """Do all the things your main code want's to do...."""
111- pass
24+ def __init__ (self ):
25+ super ()
26+ self .my_input = nb_serialin .NonBlockingSerialInput (
27+ input_handling_fn = self .userinput_event_handling ,
28+ print_help_fn = self .userinput_print_help ,
29+ )
30+ self .running = False
31+
32+ self .led = digitalio .DigitalInOut (board .LED )
33+ self .led .direction = digitalio .Direction .OUTPUT
34+
35+ self .runtime_print = True
36+ self .runtime_print_next = time .monotonic ()
37+ self .runtime_print_intervall = 1.0
38+
39+ ##########################################
40+ # menu
41+
42+ def userinput_print_help (self ):
43+ """Print Help."""
44+ print (
45+ "you can change some things:\n "
46+ "- 'tr': toggle print runtime ({runtime_print})\n "
47+ "- 'time set:???': set print runtime intervall ({runtime_print_intervall: > 7.2f}s)\n "
48+ "- 'exit' stop program\n "
49+ "" .format (
50+ runtime_print = self .runtime_print ,
51+ runtime_print_intervall = self .runtime_print_intervall ,
52+ ),
53+ end = "" ,
54+ )
55+
56+ def userinput_event_handling (self , input_string ):
57+ """Handle user input."""
58+ if "tr" in input_string :
59+ self .runtime_print = not self .runtime_print
60+ if "time set" in input_string :
61+ print ("time set:" )
62+ value = nb_serialin .parse_value (input_string , "time set" )
63+ if nb_serialin .is_number (value ):
64+ self .runtime_print_intervall = value
65+ self .runtime_print_next = (
66+ time .monotonic () + self .runtime_print_intervall
67+ )
68+ if "exit" in input_string :
69+ print ("Stop Program running." )
70+ self .running = False
71+
72+ ##########################################
73+ # main things
74+
75+ def runtime_update (self ):
76+ """If enabled: print runtime & toggle LED."""
77+ if self .runtime_print :
78+ if self .runtime_print_next < time .monotonic ():
79+ self .runtime_print_next = (
80+ time .monotonic () + self .runtime_print_intervall
81+ )
82+ print ("{: > 7.2f}s" .format (time .monotonic ()))
83+ self .led .value = not self .led .value
84+
85+ def update (self ):
86+ """Update."""
87+ self .my_input .update ()
88+ self .runtime_update ()
89+
90+ def run (self ):
91+ """Run."""
92+ self .running = True
93+ while self .running :
94+ try :
95+ self .update ()
96+ except KeyboardInterrupt as e :
97+ print ("KeyboardInterrupt - Stop Program." , e )
98+ self .running = False
11299
113100
114101##########################################
@@ -118,25 +105,19 @@ def main_update(self):
118105def main ():
119106 """Main."""
120107 # wait some time untill the computer / terminal is ready
121- for index in range (10 ):
122- print ("." , end = "" )
123- time .sleep (0.5 / 10 )
108+ # for i in range(10):
109+ # print(".", end="")
110+ # time.sleep(0.5 / 10)
124111 print ("" )
125112 print (42 * "*" )
113+ print ("nonblocking_serialinput_advanced_class.py" )
126114 print ("Python Version: " + sys .version )
127115 print ("board: " + board .board_id )
128116 print (42 * "*" )
129- print ("run" )
130117
131- running = True
132- while running :
133- try :
134- my_input .update ()
135- except KeyboardInterrupt as e :
136- print ("KeyboardInterrupt - Stop Program." , e )
137- running = False
138- else :
139- main_update ()
118+ myproject = MyProjectMainClass ()
119+ print ("run" )
120+ myproject .run ()
140121
141122
142123##########################################
0 commit comments