dancefloor-monorepo/arduino/arduino.ino

115 lines
1.8 KiB
Arduino
Raw Normal View History

#include "FastLED.h"
#define NUM_STRIPS 2
#define NUM_LEDS_PER_STRIP 180
2020-04-26 18:59:29 +01:00
#define NUM_LEDS_PER_SQUARE 12
#define NUM_SQUARES_PER_STRIP 15
#define PIN_0 12
#define PIN_1 13
CRGB leds[NUM_STRIPS][NUM_LEDS_PER_STRIP];
void setup() {
FastLED.addLeds<NEOPIXEL, PIN_0>(leds[0], NUM_LEDS_PER_STRIP);
FastLED.addLeds<NEOPIXEL, PIN_1>(leds[1], NUM_LEDS_PER_STRIP);
2020-04-26 18:59:29 +01:00
Serial.begin(9600);
}
2020-04-26 18:59:29 +01:00
int hex;
int rgb;
int led;
int stored[3];
void loop() {
2020-04-26 18:59:29 +01:00
if (Serial.available() > 0) {
int data;
2020-04-26 20:11:52 +01:00
switch (data = Serial.read()) {
2020-04-26 18:59:29 +01:00
case 'n':
nextColour();
break;
case 'l': // end of LED
nextLed();
break;
case 'q':
complete();
break;
case 'r':
resetState();
break;
2020-04-26 20:11:52 +01:00
default:
hex = (hex << 1) + charMap(data);
break;
}
}
}
2020-04-26 18:59:29 +01:00
void nextColour() {
stored[rgb++] = hex;
hex = 0;
}
void nextLed() {
CRGB color = CRGB(stored[0], stored[1], stored[2]);
int strip = led / NUM_SQUARES_PER_STRIP;
int start = (led * NUM_LEDS_PER_SQUARE) - (strip * NUM_LEDS_PER_STRIP);
for (int i = start; i < start + NUM_LEDS_PER_SQUARE; i++) {
leds[strip][i] = color;
}
2020-04-26 20:11:52 +01:00
2020-04-26 20:13:00 +01:00
led++;
2020-04-26 18:59:29 +01:00
rgb = hex = 0;
}
void complete() {
FastLED.show();
led = rgb = hex = 0;
}
void resetState() {
led = rgb = hex = 0;
}
int charMap(char that) {
switch (that) {
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'a':
return 10;
case 'b':
return 11;
case 'c':
return 12;
case 'd':
return 13;
case 'e':
return 14;
case 'f':
return 15;
}
return -1;
}