|
| 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 | +########################################## |
0 commit comments