Skip to content

Commit cd4b9de

Browse files
committed
partly working.
1 parent 32097cd commit cd4b9de

5 files changed

+241
-30
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
# SPDX-FileCopyrightText: Copyright (c) 2021 Stefan Krüger for s-light
5+
#
6+
# SPDX-License-Identifier: Unlicense
7+
8+
"""Simple Minimal example of CircuitPython_nonblocking_serialinput library usage."""
9+
10+
import time
11+
import sys
12+
import board
13+
import digitalio
14+
import nonblocking_serialinput as nb_serialin
15+
16+
##########################################
17+
# globals
18+
19+
20+
class MyProjectMainClass:
21+
"""This is just the Container Class for my Project."""
22+
23+
def __init__(self):
24+
super()
25+
self.my_input = nb_serialin.NonBlockingSerialInput(
26+
input_handling_fn=self.userinput_event_handling,
27+
print_help_fn=self.userinput_print_help,
28+
)
29+
self.running = False
30+
31+
self.led = digitalio.DigitalInOut(board.LED)
32+
self.led.direction = digitalio.Direction.OUTPUT
33+
34+
self.runtime_print = True
35+
self.runtime_print_next = time.monotonic()
36+
self.runtime_print_intervall = 1.0
37+
38+
##########################################
39+
# menu
40+
41+
def userinput_print_help(self):
42+
"""Print Help."""
43+
print(
44+
"you can change some things:\n"
45+
"- 'tr': toggle print runtime ({runtime_print})\n"
46+
"- 'time set:???': set print runtime intervall ({runtime_print_intervall: > 7.2f}s)\n"
47+
"- 'exit' stop program\n"
48+
"".format(
49+
runtime_print=self.runtime_print,
50+
runtime_print_intervall=self.runtime_print_intervall,
51+
),
52+
end="",
53+
)
54+
55+
def userinput_event_handling(self, input_string):
56+
"""Handle user input."""
57+
if "tr" in input_string:
58+
self.runtime_print = not self.runtime_print
59+
if "time set" in input_string:
60+
print("time set:")
61+
value = nb_serialin.parse_value(input_string, "time set")
62+
if nb_serialin.is_number(value):
63+
self.runtime_print_intervall = value
64+
self.runtime_print_next = (
65+
time.monotonic() + self.runtime_print_intervall
66+
)
67+
if "exit" in input_string:
68+
print("Stop Program running.")
69+
self.running = False
70+
71+
##########################################
72+
# main things
73+
74+
def runtime_update(self):
75+
"""If enabled: print runtime & toggle LED."""
76+
if self.runtime_print:
77+
if self.runtime_print_next < time.monotonic():
78+
self.runtime_print_next = (
79+
time.monotonic() + self.runtime_print_intervall
80+
)
81+
print("{: > 7.2f}s".format(time.monotonic()))
82+
self.led.value = not self.led.value
83+
84+
def update(self):
85+
"""Update."""
86+
self.my_input.update()
87+
self.runtime_update()
88+
89+
def run(self):
90+
"""Run."""
91+
self.running = True
92+
while self.running:
93+
try:
94+
self.update()
95+
except KeyboardInterrupt as e:
96+
print("KeyboardInterrupt - Stop Program.", e)
97+
self.running = False
98+
99+
100+
##########################################
101+
# main
102+
103+
104+
def main():
105+
"""Main."""
106+
# wait some time untill the computer / terminal is ready
107+
# for i in range(10):
108+
# print(".", end="")
109+
# time.sleep(0.5 / 10)
110+
print("")
111+
print(42 * "*")
112+
print("nonblocking_serialinput_advanced_class.py")
113+
print("Python Version: " + sys.version)
114+
print("board: " + board.board_id)
115+
print(42 * "*")
116+
117+
myproject = MyProjectMainClass()
118+
print("run")
119+
myproject.run()
120+
121+
122+
##########################################
123+
if __name__ == "__main__":
124+
main()
125+
126+
##########################################

examples/nonblocking_serialinput_simpletest.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,33 @@ def main():
3131
"""Main."""
3232
# wait for serial terminal to get ready..
3333
time.sleep(1)
34-
print("")
35-
print("nonblocking_serialinput_simpletest.py")
36-
print(42 * "*")
34+
# we have to use the *drop-in* my_input.print() function.
35+
# otherwise the rmote echo handling does not work.
36+
my_input.print("")
37+
my_input.print("nonblocking_serialinput_simpletest.py")
38+
my_input.print(42 * "*")
3739

3840
runtime_print_next = time.monotonic()
39-
runtime_print_intervall = 1.0
41+
runtime_print_intervall = 5.0
4042
running = True
4143
while running:
4244
# input handling
4345
my_input.update()
4446
input_string = my_input.input()
4547
if input_string is not None:
46-
# print("input_string: {}".format(repr(input_string)))
48+
# my_input.print("input_string: {}".format(repr(input_string)))
4749
# we have at least a empty string.
4850
if "exit" in input_string:
49-
print("Stop Program running.")
51+
my_input.print("Stop Program running.")
5052
running = False
5153
elif "hello" in input_string:
52-
print("World :-)")
54+
my_input.print("World :-)")
5355
else:
54-
print("type 'exit' to stop the program.")
56+
my_input.print("type 'exit' to stop the program.")
5557
# live sign
5658
if runtime_print_next < time.monotonic():
5759
runtime_print_next = time.monotonic() + runtime_print_intervall
58-
print("{: > 7.2f}s".format(time.monotonic()))
60+
my_input.print("{: > 7.2f}s".format(time.monotonic()))
5961
led.value = not led.value
6062

6163

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 board
13+
import digitalio
14+
import nonblocking_serialinput as nb_serialin
15+
16+
##########################################
17+
# globals
18+
led = digitalio.DigitalInOut(board.LED)
19+
led.direction = digitalio.Direction.OUTPUT
20+
21+
##########################################
22+
# menu
23+
24+
my_input = nb_serialin.NonBlockingSerialInput(statusline=True)
25+
26+
##########################################
27+
# main
28+
29+
30+
def main():
31+
"""Main."""
32+
# wait for serial terminal to get ready..
33+
time.sleep(1)
34+
my_input.print("")
35+
my_input.print("nonblocking_serialinput_simpletest.py")
36+
my_input.print(42 * "*")
37+
38+
runtime_print_next = time.monotonic()
39+
runtime_print_intervall = 1.0
40+
running = True
41+
while running:
42+
# input handling
43+
my_input.update()
44+
input_string = my_input.input()
45+
if input_string is not None:
46+
# print("input_string: {}".format(repr(input_string)))
47+
# we have at least a empty string.
48+
if "exit" in input_string:
49+
my_input.print("Stop Program running.")
50+
running = False
51+
elif "hello" in input_string:
52+
my_input.print("World :-)")
53+
else:
54+
my_input.print("type 'exit' to stop the program.")
55+
# live sign
56+
if runtime_print_next < time.monotonic():
57+
runtime_print_next = time.monotonic() + runtime_print_intervall
58+
my_input.print("{: > 7.2f}s".format(time.monotonic()))
59+
led.value = not led.value
60+
61+
62+
##########################################
63+
if __name__ == "__main__":
64+
main()
65+
66+
##########################################

examples/nonblocking_serialinput_statusline_dev.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,8 @@
2727
# main
2828

2929

30-
def main():
31-
"""Main."""
32-
# wait for serial terminal to get ready..
33-
time.sleep(1)
34-
print("")
35-
print("nonblocking_serialinput_statusline_dev.py")
36-
print("*" * 42)
30+
def test_move():
31+
"""Test Moving around..."""
3732
test_string_colors = (
3833
terminal.ANSIColors.fg.lightblue
3934
+ "Hello "
@@ -85,10 +80,24 @@ def main():
8580
# now we have to reprint the echo & statusline.
8681
print(">> ")
8782
print("this is a status line - it should stay as last line.")
83+
time.sleep(2)
8884

89-
# print("oh... the program just did a print statement... and with this another one..")
9085

91-
time.sleep(10)
86+
def main():
87+
"""Main."""
88+
# wait for serial terminal to get ready..
89+
time.sleep(1)
90+
print("")
91+
print("nonblocking_serialinput_statusline_dev.py")
92+
print("*" * 42)
93+
# test_move()
94+
print(">> ", end="")
95+
time.sleep(2)
96+
move = ""
97+
# move += terminal.ANSIControl.cursor.previous_line(0)
98+
move += terminal.ANSIControl.erase_line(0)
99+
print(move, end="")
100+
time.sleep(5)
92101

93102

94103
##########################################

nonblocking_serialinput.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,14 @@ def __init__(
132132

133133
##########################################
134134
# output handling
135+
136+
# statusline
137+
# echo
138+
135139
@staticmethod
136140
def _statusline_fn_default():
137141
"""Default statusline"""
138-
return "uptime:{uptime: >8.2f}".format(uptime=time.monotonic)
142+
return "uptime:{uptime: >8.2f}".format(uptime=time.monotonic())
139143

140144
def _statusline_update_check_intervall(self):
141145
"""Update the Statusline if intervall is over."""
@@ -185,23 +189,25 @@ def echo_print(self):
185189
if self.echo:
186190

187191
move = ""
188-
line_count = 1
189-
if self.statusline:
190-
# jump over statusline
191-
line_count += 1
192-
# eareas
193-
move += terminal.ANSIControl.cursor.previous_line(line_count)
192+
# line_count = 1
193+
# if self.statusline:
194+
# # jump over statusline
195+
# line_count += 1
196+
# # eareas
197+
# move += terminal.ANSIControl.cursor.previous_line(line_count)
198+
# move += terminal.ANSIControl.cursor.previous_line(0)
194199
move += terminal.ANSIControl.erase_line(0)
195200

196201
# reprint line
197202
line = self._get_echo_line()
198203

199204
# move back to bottom of screen
200-
line_count = 1
201-
if self.statusline:
202-
# jump over statusline
203-
line_count += 1
204-
moveback = terminal.ANSIControl.cursor.next_line(line_count)
205+
moveback = ""
206+
# line_count = 1
207+
# if self.statusline:
208+
# # jump over statusline
209+
# line_count += 1
210+
# moveback = terminal.ANSIControl.cursor.next_line(line_count)
205211

206212
# execute all the things ;-)
207213
print(
@@ -303,6 +309,7 @@ def input(self):
303309
"""
304310
try:
305311
result = self.input_list.pop(0)
312+
self.print(result)
306313
if self.verbose:
307314
self.print("result: {}".format(repr(result)))
308315
except IndexError:
@@ -332,6 +339,7 @@ def update(self):
332339
while self.input_list:
333340
# first in first out
334341
oldest_input = self.input_list.pop(0)
342+
self.print(oldest_input)
335343
self.input_handling_fn(oldest_input)
336344
parsed_input = True
337345
if parsed_input and self.print_help_fn:

0 commit comments

Comments
 (0)