Skip to content

Commit a7fee08

Browse files
committed
prepare example
1 parent 0df736a commit a7fee08

File tree

3 files changed

+314
-6
lines changed

3 files changed

+314
-6
lines changed
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+
##########################################

examples/nonblocking_serialinput_simpletest.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,100 @@
77
# SPDX-License-Identifier: Unlicense
88

99
"""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+
runtime_print = True
21+
runtime_print_next = time.monotonic()
22+
runtime_print_intervall = 1.0
23+
24+
##########################################
25+
# menu
26+
27+
28+
def userinput_print_help(self):
29+
"""Print Help."""
30+
global runtime_print
31+
global runtime_print_intervall
32+
print(
33+
"you do some things:\n"
34+
"- 't': toggle print runtime ({runtime_print})\n"
35+
"- 'time set:???': set print runtime intervall ({runtime_print_intervall: > 7.2f}s)\n"
36+
"- 'exit' stop program\n"
37+
"".format(
38+
runtime_print=runtime_print,
39+
runtime_print_intervall=runtime_print_intervall,
40+
),
41+
end="",
42+
)
43+
44+
45+
def userinput_handling(input_string):
46+
"""Check Input."""
47+
global runtime_print
48+
global runtime_print_intervall
49+
50+
if "t" in input_string:
51+
runtime_print = not runtime_print
52+
if "time set" in input_string:
53+
value = nb_serialin.parse_value(input_string, "time set")
54+
if nb_serialin.is_number(value):
55+
runtime_print_intervall = value
56+
57+
58+
##########################################
59+
# functions
60+
61+
62+
def main_update(self):
63+
"""Do all the things your main code want's to do...."""
64+
global runtime_print
65+
global runtime_print_next
66+
global runtime_print_intervall
67+
68+
if runtime_print:
69+
if runtime_print_next < time.monotonic():
70+
runtime_print_next = time.monotonic() + runtime_print_intervall
71+
print("{: > 7.2f}s)".format(time.monotonic()))
72+
73+
74+
##########################################
75+
# main
76+
77+
78+
def main():
79+
"""Main."""
80+
# wait some time untill the computer / terminal is ready
81+
for index in range(10):
82+
print(".", end="")
83+
time.sleep(0.5 / 10)
84+
print("")
85+
print(42 * "*")
86+
print("Python Version: " + sys.version)
87+
print("board: " + board.board_id)
88+
print(42 * "*")
89+
print("run")
90+
91+
running = True
92+
while running:
93+
try:
94+
my_input.update()
95+
except KeyboardInterrupt as e:
96+
print("KeyboardInterrupt - Stop Program.", e)
97+
running = False
98+
else:
99+
main_update()
100+
101+
102+
##########################################
103+
if __name__ == "__main__":
104+
main()
105+
106+
##########################################

nonblocking_serialinput.py

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,79 @@
2121
2222
**Software and Dependencies:**
2323
24-
* Adafruit CircuitPython firmware for the supported boards:
25-
https://github.com/adafruit/circuitpython/releases
26-
27-
* Core Module usb_cdc:
28-
https://circuitpython.readthedocs.io/en/latest/shared-bindings/usb_cdc/index.html
24+
* Adafruit CircuitPython firmware `>= 7.0.0 for the supported boards.
25+
<https://github.com/adafruit/circuitpython/releases>`_
26+
* Core Module `usb_cdc:
27+
<https://circuitpython.readthedocs.io/en/latest/shared-bindings/usb_cdc/index.html>`_
2928
"""
3029

31-
# imports
30+
import time
31+
import board
32+
import supervisor
3233

3334
__version__ = "0.0.0-auto.0"
3435
__repo__ = "https://github.com/s-light/CircuitPython_nonblocking_serialinput.git"
36+
37+
##########################################
38+
# NonBlockingSerialInput Class
39+
40+
41+
class NonBlockingSerialInput(object):
42+
"""docstring for NonBlockingSerialInput."""
43+
44+
def __init__(self, arg):
45+
super(NonBlockingSerialInput, self).__init__()
46+
self.arg = arg
47+
48+
49+
def memo(self):
50+
if supervisor.runtime.serial_connected:
51+
self.print_help()
52+
# prepare new input
53+
self.print_help()
54+
print(">> ", end="")
55+
56+
57+
##########################################
58+
# helper
59+
60+
61+
def parse_value(self, input_string, pre_text):
62+
value = None
63+
# strip pre_text
64+
# ignore error 'whitespace before :'
65+
# pylama:ignore=E203
66+
input_string = input_string[len(pre_text) + 1 :]
67+
if "None" in input_string:
68+
value = None
69+
elif "False" in input_string:
70+
value = False
71+
elif "True" in input_string:
72+
value = True
73+
else:
74+
try:
75+
value = float(input_string)
76+
except ValueError as e:
77+
print(
78+
"Exception parsing '{pre_text}': {error}".format(
79+
pre_text=pre_text,
80+
error=e,
81+
)
82+
)
83+
return value
84+
85+
86+
def is_number(s):
87+
"""
88+
Return true if string is a number.
89+
90+
based on
91+
https://stackoverflow.com/questions/354038/how-do-i-check-if-a-string-is-a-number-float
92+
"""
93+
try:
94+
float(s)
95+
except TypeError:
96+
# return NaN
97+
return False
98+
else:
99+
return True

0 commit comments

Comments
 (0)