I'd like to develop a generic strategy for specialising Firmata sketches for bespoke circuits. The aim is to preserve all the functionality of StandardFirmata whilst also permitting build-specific messages to be sent. I have a strategy in mind, building on the Base85 C code distributed as part of the code-versioning tool 'git' which I think should permit the easy encoding of arbitrary multi-byte data into 7-bit data space of Firmata Sysex messages. I'd like to get feedback on the feasibility and possible beartraps of the proposed approach, as well as preferred alternatives.
An example which requires this approach is a two-wheeled robot platform. The proposed Arduino sketch code should support the sending of messages to control the two stepper motors. It should also permit the requesting of measured distances from an ultrasonic sensor. However, students experimenting with the robot may also wish to attach arbitrary other analog sensors or digital components to the robot, which they should be able to query over bluetooth serial as part of control logic in python through pyFirmata. Building on StandardFirmata, changing the Serial data rate (to be consistent with Arduino code upload) and adding messages to support the extra hardware seems natural to serve all these needs.
However, it's hard to, for example, encode a 'long' integer value using the recommended Firmata approach. The Accelstepper library I'm using employs a 'long' as its representation of stepper position.
FIrmata recommends the use of the Sysex extension to achieve build-specific messaging, but data must be sent in a 7-bit encoding, as one of the bits is used to flag Firmata control messages (including the start and end of Sysex). With a 4-byte 'long' it's hard to know exactly how to shove it into 7-bits in a comprehensible and debuggable way.
Enter base85 (
http://en.wikipedia.org/wiki/Ascii85). This has a canonical (and efficient) implementation as part of the git version control utility. The code is accessible at
https://github.com/git/git/blob/master/base85.c The transform can be reversed using mainstream utilities for debugging.
I would like to know if there are any reasons why the git code for base85 linked above should not be ported to Arduino in order to facilitate the encoding of arbitrary bytestreams (including the 4 bytes of my long) into the 7-bit data space of Firmata Sysex messages. This would mean that generic sendInt, sendFloat, receiveInt, receiveFloat operations could be defined on each side of the Firmata link, using the base85 encoding on the component bytes of each type to create a straightforward wrapper for data.
I've arrived at code which almost compiles, but hoping to get insights from others on the practicality of the approach. Also there may be a much easier model of encoding and decoding bytes into a 7-bit transport, or a better base85 code reference which I should adopt instead.