Following on from the first part of the Blync hack, I want to be able to control the color from a terminal.

Let’s create a simple protocol

CommandInput in byteOutput in byte
Default mode0..7: 0 (AUTO)no output
Get current RGB value0..7: 1 (GET)0..7: Red as [0..255]
8..15: Green as [0..255]
16..23: Blue as [0..255]
Set RGB value0..7: 2 (SET)
8..15: Red as [0..255]
16..23: Green as [0..255]
24..31: Blue as [0..255]
0..7: 1 (ACK)

Here is the demo code in Python using pyserial.

pip install pyserial
#!/usr/bin/env python3
from time import sleep
import serial

CMD_AUTO = 0
CMD_GET = 1
CMD_SET = 2
CMD_ACK = 1

if __name__ == "__main__":
    ser = serial.Serial('/dev/ttyACM0', 9600)

    for i in range(0, 256):
        ser.write(bytearray([CMD_SET, i, i, i]))
        assert ser.read(1)[0] == CMD_ACK

    ser.write(bytearray([CMD_GET]))
    print(ser.read(3))

    ser.write(bytearray([CMD_AUTO]))

And here is the Arduino code.