Last active
October 5, 2022 14:45
-
-
Save hpwit/71154f396e4d0f1a13f2b6e21dc80fe9 to your computer and use it in GitHub Desktop.
code prismarl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Arduino interface for the use of WS2812 strip LEDs | |
* Uses Adalight protocol and is compatible with Boblight, Prismatik etc... | |
* "Magic Word" for synchronisation is 'Ada' followed by LED High, Low and Checksum | |
* @author: Wifsimster <wifsimster@gmail.com> | |
* @library: FastLED v3.001 | |
* @date: 11/22/2015 | |
*/ | |
#include "FastLED.h" | |
#define NUM_LEDS 240 | |
#define DATA_PIN 6 | |
// Baudrate, higher rate allows faster refresh rate and more LEDs (defined in /etc/boblight.conf) | |
#define serialRate 115200 | |
// Adalight sends a "Magic Word" (defined in /etc/boblight.conf) before sending the pixel data | |
uint8_t prefix[] = {'A', 'd', 'a'}; | |
// Initialise LED-array | |
CRGB leds[NUM_LEDS]; | |
void setup() { | |
// Use NEOPIXEL to keep true colors | |
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); | |
// Initial RGB flash | |
LEDS.showColor(CRGB(255, 0, 0)); | |
delay(500); | |
LEDS.showColor(CRGB(0, 255, 0)); | |
delay(500); | |
LEDS.showColor(CRGB(0, 0, 255)); | |
delay(500); | |
LEDS.showColor(CRGB(0, 0, 0)); | |
Serial.begin(serialRate); | |
// Send "Magic Word" string to host | |
Serial.print("Ada\n"); | |
} | |
uint8_t readCharFromSerial() | |
{ | |
while (!Serial.available()) ; //not sure you need it for each call | |
return Serial.read(); | |
} | |
void waitForLMagicWord(uint8_t *magicWord) | |
{ | |
bool finished = false; | |
int i=0; | |
while (!finished) | |
{ | |
if(magicWord[i]==readCharFromSerial()) | |
{ | |
i++; | |
if(i==sizeof(magicWord)/sizeof(magicWord[0])) //indeed /sizeof(magicWord[0]) not necessary but to be cleaner :) | |
{ | |
finished=true; | |
} | |
} | |
else | |
{ | |
i=0; | |
} | |
} | |
} | |
void loop() { | |
// Wait for first byte of Magic Word | |
waitForLMagicWord(prefix); | |
// Hi, Lo, Checksum | |
uint8_t hi=readCharFromSerial(); | |
uint8_t lo=readCharFromSerial(); | |
uint8_t chk=readCharFromSerial(); | |
// If checksum does not match go back to wait | |
if (chk == (hi ^ lo ^ 0x55)) { | |
//as you rewrite every leds this should not be necessary | |
// memset(leds, 0, NUM_LEDS * sizeof(struct CRGB)); | |
// Read the transmission data and set LED values | |
for (int i = 0; i < NUM_LEDS; i++) { //int because of the issue of more than 255 leds | |
byte r, g, b; | |
r = readCharFromSerial(); | |
g = readCharFromSerial(); | |
b = readCharFromSerial(); | |
leds[i].r = r; | |
leds[i].g = g; | |
leds[i].b = b; | |
} | |
// Shows new values | |
FastLED.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment