Skip to content

Commit 170030c

Browse files
committed
tweaks, fixes and simplify example
1 parent 6b94e37 commit 170030c

File tree

4 files changed

+223
-148
lines changed

4 files changed

+223
-148
lines changed

examples/nonblocking_serialinput_advanced.py

Lines changed: 46 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -12,103 +12,78 @@
1212
import sys
1313
import board
1414
import nonblocking_serialinput as nb_serialin
15+
import digitalio
1516

1617
##########################################
1718
# globals
18-
my_input = nb_serialin.NonBlockingSerialInput()
19+
led = digitalio.DigitalInOut(board.LED)
20+
led.direction = digitalio.Direction.OUTPUT
1921

22+
running = True
2023

21-
class MyProjectMainClass(object):
22-
"""This is just the Container Class for my Project."""
23-
24-
def __init__(self, arg):
25-
super(MyProjectMainClass, self).__init__()
26-
self.arg = arg
27-
24+
runtime_print = True
25+
runtime_print_next = time.monotonic()
26+
runtime_print_intervall = 1.0
2827

2928
##########################################
3029
# menu
3130

3231

33-
def print_help(self):
32+
def userinput_print_help():
3433
"""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)
34+
global runtime_print
35+
global runtime_print_intervall
4436
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"
37+
"you can change some things:\n"
38+
"- 'tr': toggle print runtime ({runtime_print})\n"
39+
"- 'time set:???': set print runtime intervall ({runtime_print_intervall: > 7.2f}s)\n"
40+
"- 'exit' stop program\n"
5341
"".format(
54-
profile_list=profile_list,
55-
heater_target=self.reflowcontroller.heater_target,
42+
runtime_print=runtime_print,
43+
runtime_print_intervall=runtime_print_intervall,
5644
),
5745
end="",
5846
)
59-
self.print_temperature()
6047

6148

62-
def check_input(self):
49+
def userinput_handling(input_string):
6350
"""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")
51+
global running
52+
global runtime_print
53+
global runtime_print_intervall
54+
55+
if "tr" in input_string:
56+
runtime_print = not runtime_print
57+
if "time set" in input_string:
58+
print("time set:")
59+
value = nb_serialin.parse_value(input_string, "time set")
7460
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
61+
runtime_print_intervall = value
62+
if "exit" in input_string:
63+
print("Stop Program running.")
64+
running = False
10365

10466

67+
my_input = nb_serialin.NonBlockingSerialInput(
68+
parse_input_fn=userinput_handling,
69+
print_help_fn=userinput_print_help,
70+
)
71+
10572
##########################################
10673
# functions
10774

10875

109-
def main_update(self):
76+
def main_update():
11077
"""Do all the things your main code want's to do...."""
111-
pass
78+
global runtime_print
79+
global runtime_print_next
80+
global runtime_print_intervall
81+
82+
if runtime_print:
83+
if runtime_print_next < time.monotonic():
84+
runtime_print_next = time.monotonic() + runtime_print_intervall
85+
print("{: > 7.2f}s".format(time.monotonic()))
86+
led.value = not led.value
11287

11388

11489
##########################################
@@ -128,6 +103,7 @@ def main():
128103
print(42 * "*")
129104
print("run")
130105

106+
global running
131107
running = True
132108
while running:
133109
try:
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
5+
# SPDX-FileCopyrightText: Copyright (c) 2021 Stefan Krüger for s-light
6+
#
7+
# SPDX-License-Identifier: Unlicense
8+
9+
"""Simple Minimal example of CircuitPython_nonblocking_serialinput library usage."""
10+
11+
import time
12+
import sys
13+
import board
14+
import nonblocking_serialinput as nb_serialin
15+
16+
##########################################
17+
# globals
18+
my_input = nb_serialin.NonBlockingSerialInput()
19+
20+
21+
class MyProjectMainClass(object):
22+
"""This is just the Container Class for my Project."""
23+
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
112+
113+
114+
##########################################
115+
# main
116+
117+
118+
def main():
119+
"""Main."""
120+
# wait some time untill the computer / terminal is ready
121+
for index in range(10):
122+
print(".", end="")
123+
time.sleep(0.5 / 10)
124+
print("")
125+
print(42 * "*")
126+
print("Python Version: " + sys.version)
127+
print("board: " + board.board_id)
128+
print(42 * "*")
129+
print("run")
130+
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()
140+
141+
142+
##########################################
143+
if __name__ == "__main__":
144+
main()
145+
146+
##########################################

0 commit comments

Comments
 (0)