Source code for OneInOneOutConsole.OneInOneOutProtocol

"""\
OneInOneOutProtocol.py : application-specific class to manage serial communication with an Arduino OneInOneOutASCII sketch.

Copyright (c) 2015-2017, Garth Zeglin.  All rights reserved. Licensed under the terms
of the BSD 3-clause license.
"""

#================================================================
from __future__ import print_function, absolute_import, unicode_literals

# Import the parent class definition using a relative import.
from .ArduinoProtocol import ArduinoProtocol

#================================================================
[docs]class OneInOneOutProtocol(ArduinoProtocol): """Example class to communicate with the OneInOneOutASCII Arduino sketch. """ def __init__(self, **kwargs ): # initialize the parent class ArduinoProtocol.__init__(self, **kwargs) # define attributes to hold sensor and status values self.arduino_time = 0 #: The most recent Arduino microsecond clock value (integer). self.analog_inputs = dict() #: Dictionary of most recent analog inputs. Elements are channel:value. self.digital_inputs = dict() #: Dictionary of most recent digital inputs. Elements are pin:value. return
[docs] def message_received(self, tokens): """Application-specific message processing to parse lists of tokens received from the Arduino sketch. This implementation just stores values in instance attributes. This method overrides the null implementation in the parent class. :param tokens: list of string tokens """ # detect variable-length token lists if len(tokens) > 0: if tokens[0] == 'dbg': print("Arduino debug message: %s" % " ".join(tokens[1:])) return # process replies of the form 'keyword value' if len(tokens) == 2: if tokens[0] == 'clk': self.arduino_time = int(tokens[1]) if self._debug: print("Received timestamp: %d" % self.arduino_time) # process replies of the form 'keyword value value' elif len(tokens) == 3: if tokens[0] == 'ana': channel = int(tokens[1]) value = int(tokens[2]) self.analog_inputs[channel] = value elif tokens[0] == 'dig': pin = int(tokens[1]) value = int(tokens[2]) self.digital_inputs[pin] = value return