serialOM.py : easy ObjectModel access for Python and microPython
-
Hi all; this is something I have been working on to power my PrintEYE replacement but it took on a life of it's own and I'd like to get it out of my system.. so here goes.
serialOM.py
is a self-contained python class that connects to the OM on a RRF device via USB or UART, and provides a local copy of the OM that can be periodically updated.https://github.com/easytarget/serialOM
The
readme
there does a proper TL;DR but the highlights are:- Python3/microPython cross-compatible with both PySerial and the mpy UART support
- Keys to be fetched can be specified on a per-mode basis and the 'seqs' key is used to only do verbose fetches when needed.
- Controller reboots and mode changes are handled appropriately. soft failures (timeouts) are tolerated and detectable. Hard (comms port) errors trigger a custom exception.
- more.. see readme.
Basic use can be as simple as:
from serialOM import serialOM from serial import Serial rrf = Serial('/dev/ttyACM0',57600) OM = serialOM(rrf, {'FFF':[],'CNC':[],'Laser':[]}, quiet=True) print('state: ' + OM.model['state']['status'] + ', up: ' + str(OM.model['state']['upTime']) + 's')
There is an associated printPy program that implements a robust logging / console info loop. It reconnects as USB ports change etc. and logs human-readable print/CNC/Laser status, value and progress data to the console and an optional file.
I consider the CPython side of this 'done' (unless bugs.. quite likely because I'm no Pythonista), it runs well from a Pi3 with a USB connection to my Duet2 WiFi.
There is a work in progress microPython version of this loop;
printMPy
which can do the same console output, but also has RGB 'mood/activity' light control and button/status controls being implemented.Both of the print*Py loops use a separate output class. currently the same 'TXT' demo works on both. This is a template for my in-progress I2C output class can be run in a thread to allow screen animations independent of the serial comms.
-
@EasyTarget thanks for posting this here and sharing the code with the community!
-
@EasyTarget This is really useful tool! Thank you!
-
@berniej Thankyou! I'm glad if this is useful for others.
Please note that I have just done some tweaks to the response reading and sequence processing that address a couple of concerns I had, it should be less liable to briefly de-syncing when console (or json) status messages arrive.
There is a new release,
v1.1.0
in the repo, the changes are all internal, usage has not changed.Finally; I got to do some
gc.mem_free()
data gathering at various points in the loop under micropython; I max out at about 24K total additional RAM consumed by the serialOM loop; even when I have cycled the controller through three modes and run a simulation in each to fully populate the local OM with typical data.
I'm pretty happy with this, better than I expected. -
-
A quick note that I just published a small update as release:
v1.2.0
This removes the need to have a the separate compatLib.py library present; all the cross-python handling is now done in serialOM.py itself. Nothing else is changed.