nonblocking_serialinput

CircuitPython helper library to handle serial user input in an nonblocking way.

  • Author(s): Stefan Krüger

Implementation Notes

Hardware:

Software and Dependencies:

class nonblocking_serialinput.NonBlockingSerialInput(*, input_handling_fn=None, print_help_fn=None, serial=usb_cdc.console, echo=True, echo_pre_text='>> ', statusline=False, statusline_fn=None, statusline_intervall=1, encoding='utf-8', line_end_custom=None, use_universal_line_end_basic=True, use_universal_line_end_advanced=False, verbose=False)

Non Blocking Serial Input Class.

This CircuitPython helper class can be used as non-blocking drop-in for the build in input() method. And also as event / callback based handling. It implements the full input buffer handling and line-end parsing.

all parameters are keyword parameters.

Parameters
  • input_handling_fn (function) – function to call if there is one ore more fully received new lines. input_handling(input_string: string) Default: None

  • print_help_fn (function) – function to call when a help text should be printed fully received new lines. print_help() Default: None

  • serial (Serial) – serial connection object to use Default: usb_cdc.console

  • echo (bool) – enable/disable remote echo Default: True

  • echo_pre_text (string) – Text to put on line start if echo is active Default: “>> “

  • statusline (string) – enable/disable status line handling - Not implemented yet - issue #1: Default: None

  • statusline_fn (function) – callback function for statusline output. must return the string to use as statusline. def statusline_fn() string: Default: “uptime:{runtime}”

  • statusline_intervall (string) – time intervall in seconds to update the statusline Default: 1s

  • encoding (string) – input string encoding Default: “utf-8”

  • line_end_custom (string, list) – set custom line ends Default: None

  • use_universal_line_end_basic (bool) – use a basic default set of line_ends ['\n', '\r', '\r\n']] Default: True

  • use_universal_line_end_advanced (bool) – use a advanced default set of line_ends ['\v', '\f', '\x1c',...] Default: False

  • verbose (bool) – print debugging information in some internal functions. Default to False

echo_print()

Update the echho line.

input()

Get oldest input if available.

test Otherwise an emtpy string.

Return string

if available oldest input_line. otherwise ""

print(*args, content=True)

Print information & variables to the connected serial.

This is a drop in replacement for the global print function. it is needed for the statusline handling to work. (we need to move the cursor…)

currently it is not supported to print without newline at end.

Parameters
  • *args (object) – things to print

  • content (bool) – if false just update statusline & echo (default: True).

statusline_print()

Update the Statusline.

update()

Main update funciton. please call as often as possible.

nonblocking_serialinput.find_first_line_end(input_string, line_end_list=None, start=0)

Find first line_end from line_end_list in input_string.

Parameters
  • input_string (string) – input search

  • line_end_list (list) – list with strings to search for.

  • start (int) – start position for search. (default = 0)

Return int

index of first found line_end; -1 if nothing is found.

nonblocking_serialinput.is_number(value)

Return true if string is a number.

based on https://stackoverflow.com/questions/354038/how-do-i-check-if-a-string-is-a-number-float

Parameters

value (string) – input to check

Return bool

True if value is a number, otherwise False.

nonblocking_serialinput.parse_value(input_string, pre_text='')

Parse Value from input_string.

known values are numbers (float() is used), None, True, False

Parameters
  • input_string (string) – input to parse

  • pre_text (string) – text at start of input_string to ignore. defaults to ""

Returns

parsed value

Return type

float, None, bool

nonblocking_serialinput.splitlines_advanced(input_string, line_end_list=None)

Split lines in input_string at all line_ends in line_end_list.

This function searches for the all occurenc of all of strings in line_end_list. then splits at these points. the resulting list is returned. this also returns empty string segments. the search happens in the order of line_end_list. if the string does not end with a line_end symbol this last part will be returned in rest

Parameters
  • input_string (string) – input to split

  • line_end_list (list) – list with strings where the splitting should happen.

Return tuple

Tuple (result_list, rest);