firmata/arduino — pull request #64 by soundanalogous (2013-06-17 03:03:28+00:00)
Title: For Review and Discussion: Alternate version of Shift with config command

This is an alternative to the other shift implementation I have proposed. Please provide feedback. I'd like to settle on an implementation soon (either this config version or the other non-config version with no latch support or I'm open to other implementations as well if anyone else would like to submit a pull request).

There has been some discussion around supporting fractional byte devices. This pull does not include such functionality, but tigercat was going to work on an implementation.

This version uses a SHIFT_CONFIG command to set the clock pin, bitOrder and optional latchPin (or load for some shift-in hardware). There are a few different shift types / latch configurations:

// shift types (specified in bits 3:5 of byte 2)
SHIFT_OUT // shift out with no latch
SHIFT_IN // shift out with no latch/load
LATCH_L_SHIFT_OUT // set latch low then shift out
LATCH_L_SHIFT_OUT_LATCH_H // set latch low, shift out, then set latch high
SHIFT_OUT_LATCH_H // shift out then set latch high
TOGGLE_LOAD_SHIFT_IN // toggle load pin low, then high and shift in

The protocol is as follows:
// shift config (send when instantiating a new shift-based hardware module)
0 START_SYSEX
1 SHIFT_DATA (0x75)
2 SHIFT_CONFIG (bytes 0:2 shift command, bytes 3:5 shift type, byte 6 unused)
3 dataPin
4 clockPin
5 bitOrder
6 latchPin [optional]
7 END_SYSEX

// shift out
0 START_SYSEX
1 SHIFT_DATA (0x75)
2 SHIFT_OUT (bytes 0:2 shift command, bytes 3:5 shift type, byte 6 unused)
3 dataPin
n ... (shift out data)
n+1 END_SYSEX

// shift in
0 START_SYSEX
1 SHIFT_DATA (0x75)
2 SHIFT_IN (bytes 0:2 shift command, bytes 3:5 shift type, byte 6 unused)
3 dataPin
4 numBytes (number of bytes to shift in. Default to 1)
5 END_SYSEX