[Tutor] Pen plotter data format
Dennis Lee Bieber
wlfraed at ix.netcom.com
Sat Apr 1 15:21:19 EDT 2023
On Sat, 01 Apr 2023 02:20:09 -0400, Dennis Lee Bieber
<wlfraed at ix.netcom.com> declaimed the following:
>while True:
> command_string = serial.readline() #block for EOL
> commands = command_string.split(";") #split into each command
> for command in commands: #process each command
> cmd, parameters = command[:2], command[2:]
> #call the handler function matching "cmd"
> #all handlers have the same signature!
> result = command_table[cmd.upper()](parameters)
> if not result:
> serial.write("%s : %s\n" % (command, result))
>
>Adding a command just requires adding a new "def cmd_handler()" and an
>entry in the command_table.
>
I need to state that, the above "parser" will fail if you've added plot
commands for text printing -- as an embedded ";" will cause a split in the
wrong location. For that situation you'll need a stream parsing approach.
Hypothetical command format (Text Output): TO<len>,<some amount of text>
Something similar to:
while True:
command_string = serial.readline()
while command_string:
cmd, command_string = (command_string[:2].upper(),
command_string[2:])
if cmd in [ "TO", <other similar format commands>]:
len, command_string = command_string.split(",", 1)
ilen = int(len)
parameters, command_string = (command_string[:ilen],
command_string[ilen+1:])
#^^^ skips ";" separator
#vvv recreate "signature" for calling handlers
parameters = ",".join([len, parameters])
else:
parameters, command_string = command_string.split(";", 1)
result = command_table[cmd](parameters)
And... before I forget... Adafruit's "blinka" package enables using most of
the CircuitPython interfaces from a Python running on R-Pi (or BeagleBone
Black -- the BB AI is overkill for this). But given the rarity and rising
$$$ for R-Pis I'd recommend using a controller board over a computer board
-- again the Metro Express seems suited if one wants to stay in Python.
More information about the Tutor
mailing list