dancefloor-monorepo/arduino/arduino.ino
Jake Hillion 8b89bdeee6 Better python
- Improved arduino types
- A more final python server
2020-04-28 13:39:58 +01:00

116 lines
1.8 KiB
C++

#include "FastLED.h"
#define NUM_STRIPS 2
#define NUM_LEDS_PER_STRIP 180
#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);
Serial.begin(9600);
}
uint8_t hex;
uint8_t stored[3];
int rgb;
int led;
void loop() {
if (Serial.available() > 0) {
int data;
switch (data = Serial.read()) {
case 'r':
resetState();
break;
case 'q':
complete();
break;
case 'l': // end of LED
nextLed();
break;
case 'n':
nextColour();
break;
default:
hex = (hex << 4) + charMap(data);
break;
}
}
}
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;
}
led++;
rgb = hex = 0;
}
void complete() {
FastLED.show();
led = rgb = hex = 0;
}
void resetState() {
led = rgb = hex = 0;
}
uint8_t charMap(char c) {
switch (c) {
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;
default:
return 0;
}
}