Skip to content

Commit b9b3fdd

Browse files
committed
tweaks & fix examples
1 parent c279c0e commit b9b3fdd

File tree

4 files changed

+61
-41
lines changed

4 files changed

+61
-41
lines changed

examples/nonblocking_serialinput_advanced_class.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,32 @@ def __init__(self):
4040

4141
def userinput_print_help(self):
4242
"""Print Help."""
43-
print(
43+
text = (
4444
"you can change some things:\n"
4545
"- 'tr': toggle print runtime ({runtime_print})\n"
4646
"- 'time set:???': set print runtime intervall ({runtime_print_intervall: > 7.2f}s)\n"
47-
"- 'exit' stop program\n"
47+
"- 'exit' stop program"
4848
"".format(
4949
runtime_print=self.runtime_print,
5050
runtime_print_intervall=self.runtime_print_intervall,
51-
),
52-
end="",
51+
)
5352
)
53+
self.my_input.print(text)
5454

5555
def userinput_event_handling(self, input_string):
5656
"""Handle user input."""
5757
if "tr" in input_string:
5858
self.runtime_print = not self.runtime_print
5959
if "time set" in input_string:
60-
print("time set:")
60+
self.my_input.print("time set:")
6161
value = nb_serialin.parse_value(input_string, "time set")
6262
if nb_serialin.is_number(value):
6363
self.runtime_print_intervall = value
6464
self.runtime_print_next = (
6565
time.monotonic() + self.runtime_print_intervall
6666
)
6767
if "exit" in input_string:
68-
print("Stop Program running.")
68+
self.my_input.print("Stop Program running.")
6969
self.running = False
7070

7171
##########################################
@@ -78,7 +78,7 @@ def runtime_update(self):
7878
self.runtime_print_next = (
7979
time.monotonic() + self.runtime_print_intervall
8080
)
81-
print("{: > 7.2f}s".format(time.monotonic()))
81+
self.my_input.print("{: > 7.2f}s".format(time.monotonic()))
8282
self.led.value = not self.led.value
8383

8484
def update(self):
@@ -93,7 +93,7 @@ def run(self):
9393
try:
9494
self.update()
9595
except KeyboardInterrupt as e:
96-
print("KeyboardInterrupt - Stop Program.", e)
96+
self.my_input.print("KeyboardInterrupt - Stop Program.", e)
9797
self.running = False
9898

9999

@@ -105,7 +105,7 @@ def main():
105105
"""Main."""
106106
# wait some time untill the computer / terminal is ready
107107
# for i in range(10):
108-
# print(".", end="")
108+
# self.my_input.print(".", end="")
109109
# time.sleep(0.5 / 10)
110110
print("")
111111
print(42 * "*")
@@ -115,7 +115,7 @@ def main():
115115
print(42 * "*")
116116

117117
myproject = MyProjectMainClass()
118-
print("run")
118+
myproject.my_input.print("run")
119119
myproject.run()
120120

121121

examples/nonblocking_serialinput_advanced_statusline.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def __init__(self):
2525
self.my_input = nb_serialin.NonBlockingSerialInput(
2626
input_handling_fn=self.userinput_event_handling,
2727
print_help_fn=self.userinput_print_help,
28+
statusline=True,
29+
statusline_intervall=1.2,
2830
)
2931
self.running = False
3032

@@ -33,39 +35,39 @@ def __init__(self):
3335

3436
self.runtime_print = True
3537
self.runtime_print_next = time.monotonic()
36-
self.runtime_print_intervall = 1.0
38+
self.runtime_print_intervall = 3.0
3739

3840
##########################################
3941
# menu
4042

4143
def userinput_print_help(self):
4244
"""Print Help."""
43-
print(
45+
text = (
4446
"you can change some things:\n"
4547
"- 'tr': toggle print runtime ({runtime_print})\n"
4648
"- 'time set:???': set print runtime intervall ({runtime_print_intervall: > 7.2f}s)\n"
47-
"- 'exit' stop program\n"
49+
"- 'exit' stop program"
4850
"".format(
4951
runtime_print=self.runtime_print,
5052
runtime_print_intervall=self.runtime_print_intervall,
51-
),
52-
end="",
53+
)
5354
)
55+
self.my_input.print(text)
5456

5557
def userinput_event_handling(self, input_string):
5658
"""Handle user input."""
5759
if "tr" in input_string:
5860
self.runtime_print = not self.runtime_print
5961
if "time set" in input_string:
60-
print("time set:")
62+
self.my_input.print("time set:")
6163
value = nb_serialin.parse_value(input_string, "time set")
6264
if nb_serialin.is_number(value):
6365
self.runtime_print_intervall = value
6466
self.runtime_print_next = (
6567
time.monotonic() + self.runtime_print_intervall
6668
)
6769
if "exit" in input_string:
68-
print("Stop Program running.")
70+
self.my_input.print("Stop Program running.")
6971
self.running = False
7072

7173
##########################################
@@ -78,7 +80,8 @@ def runtime_update(self):
7880
self.runtime_print_next = (
7981
time.monotonic() + self.runtime_print_intervall
8082
)
81-
print("{: > 7.2f}s".format(time.monotonic()))
83+
self.my_input.print(":-)")
84+
self.my_input.print("{: > 7.2f}s".format(time.monotonic()))
8285
self.led.value = not self.led.value
8386

8487
def update(self):
@@ -93,7 +96,7 @@ def run(self):
9396
try:
9497
self.update()
9598
except KeyboardInterrupt as e:
96-
print("KeyboardInterrupt - Stop Program.", e)
99+
self.my_input.print("KeyboardInterrupt - Stop Program.", e)
97100
self.running = False
98101

99102

@@ -105,7 +108,7 @@ def main():
105108
"""Main."""
106109
# wait some time untill the computer / terminal is ready
107110
# for i in range(10):
108-
# print(".", end="")
111+
# self.my_input.print(".", end="")
109112
# time.sleep(0.5 / 10)
110113
print("")
111114
print(42 * "*")
@@ -115,7 +118,7 @@ def main():
115118
print(42 * "*")
116119

117120
myproject = MyProjectMainClass()
118-
print("run")
121+
myproject.my_input.print("run")
119122
myproject.run()
120123

121124

examples/nonblocking_serialinput_simpletest_statusline.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
##########################################
2222
# menu
2323

24-
my_input = nb_serialin.NonBlockingSerialInput(
25-
statusline=True, echo=False, statusline_intervall=0.5
26-
)
24+
my_input = nb_serialin.NonBlockingSerialInput(statusline=True, statusline_intervall=0.2)
2725

2826
##########################################
2927
# main
@@ -38,7 +36,7 @@ def main():
3836
my_input.print(42 * "*")
3937

4038
runtime_print_next = time.monotonic()
41-
runtime_print_intervall = 1.7
39+
runtime_print_intervall = 1
4240
running = True
4341
while running:
4442
# input handling
@@ -57,7 +55,7 @@ def main():
5755
# live sign
5856
if runtime_print_next < time.monotonic():
5957
runtime_print_next = time.monotonic() + runtime_print_intervall
60-
my_input.print("ping")
58+
my_input.print(":-)")
6159
my_input.print("{: > 7.2f}s".format(time.monotonic()))
6260
led.value = not led.value
6361

nonblocking_serialinput.py

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import usb_cdc
3333
import ansi_escape_code as terminal
3434

35-
__version__ = "0.0.0-auto.0"
35+
__version__ = "1.0.0-auto.0"
3636
__repo__ = "https://github.com/s-light/CircuitPython_nonblocking_serialinput.git"
3737

3838
# pylint: disable=too-many-instance-attributes
@@ -142,7 +142,7 @@ def _statusline_fn_default():
142142

143143
def _statusline_update_check_intervall(self):
144144
"""Update the Statusline if intervall is over."""
145-
if self.statusline_next_update <= time.monotonic():
145+
if self.statusline and self.statusline_next_update <= time.monotonic():
146146
self.statusline_next_update = time.monotonic() + self.statusline_intervall
147147
self.print(content=None)
148148

@@ -190,8 +190,8 @@ def print(self, *args, content=True):
190190
move += terminal.ANSIControl.cursor.previous_line(1)
191191
# earease statusline
192192
move += terminal.ANSIControl.erase_line(2)
193-
if self.echo:
194-
move += terminal.ANSIControl.cursor.next_line(1)
193+
# if self.echo:
194+
# move += terminal.ANSIControl.cursor.next_line(1)
195195
# move += terminal.ANSIControl.cursor.position("1, 1")
196196
move += terminal.ANSIControl.cursor.horizontal_absolute(1)
197197
# print("\n\n\n{}\n\n\n".format(repr(move)))
@@ -205,9 +205,13 @@ def print(self, *args, content=True):
205205
# print statement is finished.
206206
# now we have to reprint echo & statusline
207207
if self.statusline:
208-
print(self._get_statusline(), end="")
209-
# print(terminal.ANSIControl.cursor.next_line(1), end="")
208+
if self.echo:
209+
print(self._get_statusline())
210+
else:
211+
print(self._get_statusline(), end="")
212+
# print(terminal.ANSIControl.cursor.next_line(1), end="")
210213
if self.echo:
214+
print(terminal.ANSIControl.cursor.horizontal_absolute(1), end="")
211215
print(self._get_echo_line(), end="")
212216
# print(self._get_echo_line())
213217
# if not self.echo and not self.statusline:
@@ -284,7 +288,10 @@ def input(self):
284288
"""
285289
try:
286290
result = self.input_list.pop(0)
287-
self.print(result)
291+
if self.echo:
292+
self.print(self.echo_pre_text, result)
293+
else:
294+
self.print(result)
288295
if self.verbose:
289296
self.print("result: {}".format(repr(result)))
290297
except IndexError:
@@ -294,8 +301,7 @@ def input(self):
294301
##########################################
295302
# main handling
296303

297-
def update(self):
298-
"""Main update funciton. please call as often as possible."""
304+
def _handle_input(self):
299305
if self.serial.connected:
300306
available = self.serial.in_waiting
301307
while available:
@@ -311,21 +317,34 @@ def update(self):
311317
# errors="strict",
312318
self._buffer_check_and_handle_line_ends()
313319
available = self.serial.in_waiting
320+
321+
def _handle_input_handling_fn(self):
314322
parsed_input = False
315323
if self.input_handling_fn:
316324
while self.input_list:
317325
# first in first out
318326
oldest_input = self.input_list.pop(0)
319-
self.print(oldest_input)
327+
text = oldest_input
328+
# isprintable is not implemented in CP
329+
# if not text.isprintable():
330+
# text = repr(text)
331+
if len(text) == 0:
332+
text = repr(text)
333+
if self.echo:
334+
text = self.echo_pre_text + text
335+
self.print(text)
320336
self.input_handling_fn(oldest_input)
321337
parsed_input = True
322338
if parsed_input and self.print_help_fn:
323339
self.print_help_fn()
324-
if self.echo:
325-
# self.echo_print()
326-
self.print()
327-
if self.statusline:
328-
self._statusline_update_check_intervall()
340+
if self.echo or self.statusline:
341+
self.print(content=None)
342+
343+
def update(self):
344+
"""Main update funciton. please call as often as possible."""
345+
self._handle_input()
346+
self._handle_input_handling_fn()
347+
self._statusline_update_check_intervall()
329348

330349

331350
##########################################

0 commit comments

Comments
 (0)