ME405 Library  Version 0.38
Python library in support of ME405 class
nb_input.NB_Input Class Reference

This class implements a task which reads user input one character at a time and puts characters together into lines of text. More...

Public Member Functions

def __init__ (self, stream serial_device, echo=True)
 Create a non-blocking input object. More...
 
def any (self)
 Check whether there are any lines of input and return the oldest if so. More...
 
def get (self)
 Get one line of characters which have been received through the serial device. More...
 
def check (self)
 This method is run within a task function to watch for characters coming through a serial port. More...
 

Detailed Description

This class implements a task which reads user input one character at a time and puts characters together into lines of text.

The result is code that can be used similarly to the Python input() function but doesn't block other tasks while waiting for the lazy bum user to type something. Lines of text which have been received are put into a list; the lines can then be read from the list as they become available.

Usage Example

The class in this module is designed to be run within a task which is run by the scheduler in cotask.py. See the example file basic_tasks.py and the test code at the bottom of this file for examples of such tasks.

from pyb import USB_VCP
from nb_input import NB_Input
# ...
serial_stream = USB_VCP ()
nb_in = NB_Input (serial_stream, echo=True)
# ...
def blinky_task ():
'''! Task which demonstrates non-blocking input. This task should run
often (say, 50ms) for human input and faster for computer input '''
while True:
if nb_in.any ():
print ("\r\nInput:", nb_in.get ())
yield 0
# ...
# Create a task and run the task scheduler as usual for @c cotask.py

Feature

When running this code with Thonny, one often sees warnings such as WARNING:root:Unexpected echo, indicating that the text which Thonny saw from the microcontroller in response to user input isn't what Thonny expected to see. These warnings may be ignored.

Constructor & Destructor Documentation

◆ __init__()

def nb_input.NB_Input.__init__ (   self,
stream  serial_device,
  echo = True 
)

Create a non-blocking input object.

There should be at most one of these for each serial port.

Parameters
serial_deviceThe UART or similar serial port through which characters will be received
echoIf true, characters will be printed back as they're typed

Member Function Documentation

◆ any()

def nb_input.NB_Input.any (   self)

Check whether there are any lines of input and return the oldest if so.

Returns
True if there are any lines of user input available

◆ check()

def nb_input.NB_Input.check (   self)

This method is run within a task function to watch for characters coming through a serial port.

As characters are received, it assembles them into a line and makes a reference to the line available when the user has pressed Enter.

Returns
A string containing a line of text, or None if no line has been received since the last line was returned

◆ get()

def nb_input.NB_Input.get (   self)

Get one line of characters which have been received through the serial device.

This method pops a line of text from the queue, so each line of text can only be gotten once.

Returns
One line of input, or None if no lines are available

The documentation for this class was generated from the following file: