summaryrefslogtreecommitdiff
path: root/adaledstripe.ino
blob: ef39c1e84d3a80ef546042f2e427e3316ce7fb19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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);
}