diff options
Diffstat (limited to 'adaledstripe.ino')
| -rw-r--r-- | adaledstripe.ino | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/adaledstripe.ino b/adaledstripe.ino new file mode 100644 index 0000000..ef39c1e --- /dev/null +++ b/adaledstripe.ino @@ -0,0 +1,56 @@ +// +// Arduino serial-interface for controlling 1 analog led stripe. 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); +} |
