-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled_controller.ino
83 lines (72 loc) · 2.01 KB
/
led_controller.ino
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <FastLED.h>
#define NUM_LEDS 100
#define DATA_PIN D7
CRGB leds[NUM_LEDS];
#define LINE_BUF_SIZE 128 //Maximum input string length
char line[LINE_BUF_SIZE];
void setup() {
// Uncomment/edit one of the following lines for your leds arrangement.
// ## Clockless types ##
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); // GRB ordering is assumed
FastLED.setBrightness(128);
Serial.begin(115200);
Serial.println("UART to WS2812 LED Driver.");
}
void loop() {
read_line();
switch(line[0]){
case 0x00:
break;
case 'B':
unsigned long int brightness;
brightness = getArgument();
Serial.print("Setting brightness: ");
Serial.println(brightness);
FastLED.setBrightness(brightness);
break;
case 'C':
unsigned long int ledAndColor;
ledAndColor = getArgument();
int led = (ledAndColor & 0xFF000000) >> 24;
leds[led].red = (ledAndColor & 0x00FF0000) >> 16;
leds[led].green = (ledAndColor & 0x0000FF00) >> 8;
leds[led].blue = (ledAndColor & 0x000000FF);
break;
}
FastLED.show();
delay(1);
// // Now turn the LED off, then pause
// for(int i=0;i<NUM_LEDS;i++)
// leds[i] = CRGB::Black;
// FastLED.show();
// delay(500);
}
void read_line(){
String line_string;
memset(line, 0, sizeof(line));
long timeStart = millis();
while(!Serial.available()){
delay(1);
if(millis() - timeStart > 10){
return;
}
}
if(Serial.available()){
line_string = Serial.readStringUntil('\n');
if(line_string.length() < LINE_BUF_SIZE){
line_string.toCharArray(line, LINE_BUF_SIZE);
Serial.println(line_string);
}
else{
Serial.println("Input string too long.");
}
}
}
unsigned long int getArgument(){
unsigned long int number = 0x00;
for(int i = 0; i<8;i++){
number = number << 4;
number += (line[3+i] < '9') ? line[3+i] - '0' : line[3+i] - '7';
}
return number;
}