'Arduino, ambilight main loop for displaying LEDs and error handling

i have made ambilight on arduino and now im trying to figure how it works. This is main loop of the program which is displaying LEDS.

Can somebody tell me what does first loop (what is magic word), Hi, Lo, Checksum and If checksum does not match go back to wait.

void loop() { 
  // Wait for first byte of Magic Word
  for(i = 0; i < sizeof prefix; ++i) {
    waitLoop: while (!Serial.available()) ;;
    // Check next byte in Magic Word
    if(prefix[i] == Serial.read()) continue;
    // otherwise, start over
    i = 0;
    goto waitLoop;
  }
  
  // Hi, Lo, Checksum  
  while (!Serial.available()) ;;
  hi=Serial.read();
  while (!Serial.available()) ;;
  lo=Serial.read();
  while (!Serial.available()) ;;
  chk=Serial.read();
  
  // If checksum does not match go back to wait
  if (chk != (hi ^ lo ^ 0x55)) {
    i=0;
    goto waitLoop;
  }
  
  memset(leds, 0, NUM_LEDS * sizeof(struct CRGB));
  // Read the transmission data and set LED values
  for (uint8_t i = 0; i < NUM_LEDS; i++) {
    byte r, g, b;    
    while(!Serial.available());
    r = Serial.read();
    while(!Serial.available());
    g = Serial.read();
    while(!Serial.available());
    b = Serial.read();
    leds[i].r = r;
    leds[i].g = g;
    leds[i].b = b;
  }
  
  // Shows new values
  FastLED.show();
}


Solution 1:[1]

The code decoding what is generally called as the "Adalight protocol", it consists of a 3-byte prefix as the "magic word" {'A', 'd', 'a'} or "Ada"), followed by a uint16_t value in big endian format that represents the number of LEDs - 1, followed by 16-bit checksum. LED data follows, 3 bytes per LED, in order R, G, B (where 0 = off and 255 = max brightness).

By the way, wherever you copy your code from, it is not well written. You could find better implementation online.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hcheung