diff options
| author | schneefux <schneefux+commit@schneefux.xyz> | 2016-02-24 10:47:01 +0100 |
|---|---|---|
| committer | schneefux <schneefux+commit@schneefux.xyz> | 2016-02-24 10:47:01 +0100 |
| commit | ca34afc190cee987b52318551d039e2c607874b7 (patch) | |
| tree | 11af25a0c8085b4f10a875b4e158e55544800710 | |
| parent | 6b77d313c8fef618fedc008f3e1e7ad00f45f8ec (diff) | |
| download | mpd-rgb-vis-ca34afc190cee987b52318551d039e2c607874b7.tar.gz mpd-rgb-vis-ca34afc190cee987b52318551d039e2c607874b7.zip | |
add Arduino client sketch
| -rw-r--r-- | adastrip.ino | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/adastrip.ino b/adastrip.ino new file mode 100644 index 0000000..916d817 --- /dev/null +++ b/adastrip.ino @@ -0,0 +1,56 @@ +// +// Arduino serial-interface for controlling 1 analog led strip. The serial interface is based on the +// Adalight protocol. The protocol is as follows: +// [prefix][led count][checksum][red, green, blue]* +// prefix := 'Ada' +// led count := number of leds - 1 (uint16_t big endian), unused in this case +// checksum := led count high bit XOR led count low bit XOR 0x55 (uint8_t) +// red, green, blue := channel intensity [0-255] (uint8_t) +// + +/// The prefix for start sending led-data +static const uint8_t prefix[] = {'A', 'd', 'a'}; + +void setup() +{ + // Open the serial-connection + Serial.begin(115200); + // Send 'Ada' string to host + Serial.print("Ada\n"); +} + +void loop() +{ + int r, g, b; + // Wait for first byte of 'Ada' + for(int i = 0; i < sizeof(prefix); ++i) + { + while (!Serial.available()); + + // Check next byte in Magic Word + if(prefix[i] != Serial.read()) + return; + } + + // Wait for highByte, lowByte, Checksum to be available + while(Serial.available() < 3); + // Read high and low byte and the checksum + int highByte = Serial.read(); + int lowByte = Serial.read(); + int checksum = Serial.read(); + + if (checksum != (highByte ^ lowByte ^ 0x55)) + { + // Checksum check failed + return; + } + // Wait for the next color spec to be available + while(Serial.available() < 3); + // Read the color spec + r = Serial.read(); + g = Serial.read(); + b = Serial.read(); + analogWrite(3, r); + analogWrite(6, g); + analogWrite(5, b); +} |
