Last active
January 9, 2017 01:25
-
-
Save geekscape/f2ecf433545a9fd177095581274ee557 to your computer and use it in GitHub Desktop.
Arduino WS2812B LED serial interface command interpreter
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
// c000011 | |
// f | |
// c000000 | |
// f | |
// c100000 | |
// p0,2,4,6,9,11,13,15,16,18,20,22,25,27,29,31,32,34,36,38,41,43,45,47,48,50,52,54,57,59,61,63 | |
// c001000 | |
// p1,3,5,7,8,10,12,14,17,19,21,23,24,26,28,30,33,35,37,39,40,42,44,46,49,51,53,55,56,58,60,62 | |
// c100000 | |
// p0,1,2,3,4,5,6,7,16,17,18,19,20,21,22,23,32,33,34,35,36,37,38,39,48,49,50,51,52,53,54,55 | |
// c000010 | |
// p8,9,10,11,12,13,14,15,24,25,26,27,28,29,30,31,40,41,42,43,44,45,46,47,56,57,58,59,60,61,62,63 | |
#include <string.h> | |
#include <Adafruit_NeoPixel.h> | |
const byte PIN_LEDS = 9; | |
const byte LED_COUNT = 64; | |
const byte COMMAND_SIZE = 161; | |
Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_COUNT, PIN_LEDS, NEO_GRB + NEO_KHZ800); | |
char command[COMMAND_SIZE]; | |
int count = 0; | |
uint32_t brightness = 256; | |
uint32_t fg_color[3] = { 255, 255, 255 }; | |
uint32_t color_brightness( | |
uint32_t color[3]) { | |
return(leds.Color( | |
(uint32_t) (color[0] * brightness / 256.0 + 0.1), | |
(uint32_t) (color[1] * brightness / 256.0 + 0.1), | |
(uint32_t) (color[2] * brightness / 256.0 + 0.1) | |
) | |
); | |
} | |
void setup(void) { | |
leds.begin(); | |
leds.show(); | |
Serial.begin(115200); | |
while (! Serial) { // Wait for Serial port | |
}; | |
delay(3000); // Waut for Serial monitor window to be opened | |
Serial.println("[LED command 0.1]"); | |
Serial.println(); | |
Serial.println("Brightness: bN (N between 0 and 256)"); | |
Serial.println("Colour: cRRGGBB (RRGGBB 6 digit hexadecimal code)"); | |
Serial.println("Fill: f"); | |
Serial.println("Pixels: pN,N ... (N pixel index list, comma separated)"); | |
Serial.println(); | |
Serial.println("Each input line must end with Carriage Return [Enter]"); | |
} | |
void loop(void) { | |
while (Serial.available()) { | |
int ch = Serial.read(); | |
if (ch == '\n') { // Ignore newline | |
} | |
else if (ch == '\r') { | |
if (count > 0) { | |
execute(command); | |
command[0] = 0; | |
count = 0; | |
} | |
} | |
else if (count >= COMMAND_SIZE - 1) { | |
Serial.print("Maximum input line is "); | |
Serial.print(COMMAND_SIZE - 1); | |
Serial.println(" characters"); | |
command[0] = 0; | |
count = 0; | |
} | |
else { | |
command[count ++] = ch; | |
command[count] = 0; | |
} | |
} | |
} | |
void execute(char command[]) { | |
char *c = & command[1]; | |
char *token; | |
switch (command[0]) { | |
case 'b': | |
brightness = atoi(& command[1]); | |
if (brightness > 256) brightness = 256; | |
Serial.print("Brightness: "); | |
Serial.println(brightness); | |
break; | |
case 'c': | |
if (strlen(command) == 7) { | |
Serial.print("Color: "); | |
Serial.println(& command[1]); | |
fg_color[2] = strtol(& command[5], NULL, 16); command[5] = 0; | |
fg_color[1] = strtol(& command[3], NULL, 16); command[3] = 0; | |
fg_color[0] = strtol(& command[1], NULL, 16); command[1] = 0; | |
} | |
else { | |
Serial.println("Color command must be 'cRRGGBB'"); | |
} | |
break; | |
case 'f': | |
for (int index = 0; index < LED_COUNT; index ++) { | |
leds.setPixelColor(index, color_brightness(fg_color)); | |
} | |
leds.show(); | |
break; | |
case 'p': | |
while ((token = strtok(c, ",")) != NULL) { | |
c = NULL; | |
leds.setPixelColor(atoi(token), color_brightness(fg_color)); | |
} | |
leds.show(); | |
break; | |
default: | |
Serial.println("Unknown command"); | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment