1
0
Fork 0
USBStrip/USBStrip/USBStrip.ino

63 lines
1.4 KiB
Arduino
Raw Normal View History

2016-05-23 23:34:10 +08:00
// TODO: Rewrite everything
2015-12-16 14:12:34 +08:00
#include <WS2812.h>
2016-05-23 23:34:10 +08:00
// TODO: switch to FastLED.io
2016-05-22 19:53:35 +08:00
#include <CapacitiveSensor.h>
2015-12-16 14:12:34 +08:00
#define LEDCount 37
2016-01-17 14:20:14 +08:00
#define outputPin 11
2016-05-22 19:53:35 +08:00
WS2812 LED(LEDCount);
2015-12-16 14:12:34 +08:00
cRGB value;
2016-05-22 19:53:35 +08:00
CapacitiveSensor capv_btn1 = CapacitiveSensor(4, 2);
bool capv_btn1_state = false;
int capv_btn1_avg = 0;
2015-12-16 14:12:34 +08:00
void setup() {
2016-01-17 14:20:14 +08:00
//Serial.begin(230400);
Serial.begin(115200);
2015-12-16 14:12:34 +08:00
LED.setOutput(outputPin);
2016-05-22 19:53:35 +08:00
capv_btn1.set_CS_AutocaL_Millis(0xFFFFFFFF);
2015-12-16 14:12:34 +08:00
}
2016-05-23 23:34:10 +08:00
int capv_btn1_v;
2015-12-16 14:12:34 +08:00
void loop() {
2016-05-22 19:53:35 +08:00
//int capv_btn1_v = capv_btn1.capacitiveSensor(1);
2016-05-23 23:34:10 +08:00
capv_btn1_avg = (capv_btn1.capacitiveSensor(1) + capv_btn1_avg * 20) / 21;
capv_btn1_v = capv_btn1_avg;
if (capv_btn1_v > 70 && !capv_btn1_state) {
2016-05-22 19:53:35 +08:00
Serial.write(0x17);
Serial.write(0xff);
2016-05-23 23:34:10 +08:00
capv_btn1_avg += 1000000;
2016-05-22 19:53:35 +08:00
capv_btn1_state = true;
2016-05-23 23:34:10 +08:00
digitalWrite(13, HIGH);
2016-05-22 19:53:35 +08:00
}
2016-05-23 23:34:10 +08:00
else if (capv_btn1_v < 30 && capv_btn1_state) {
2016-05-22 19:53:35 +08:00
Serial.write(0x17);
Serial.write(0x00);
capv_btn1_state = false;
2016-05-23 23:34:10 +08:00
digitalWrite(13, LOW);
2016-05-22 19:53:35 +08:00
}
2016-01-17 14:20:14 +08:00
if (Serial.available() > 0) {
byte command = Serial.read();
2015-12-16 14:12:34 +08:00
if (command == 0x11) {
LED.sync();
2016-05-23 23:34:10 +08:00
Serial.write(0x1a);
2015-12-16 14:12:34 +08:00
}
else if (command == 0x12) {
2016-01-17 14:20:14 +08:00
while (Serial.available() < 4) {
2015-12-16 14:12:34 +08:00
}
2016-01-17 14:20:14 +08:00
int pos = Serial.read();
value.r = Serial.read();
value.g = Serial.read();
value.b = Serial.read();
2015-12-16 14:12:34 +08:00
LED.set_crgb_at(pos, value);
2016-05-23 23:34:10 +08:00
Serial.write(0x1b);
2015-12-16 14:12:34 +08:00
}
else {
2016-05-23 23:34:10 +08:00
Serial.write(0x1e);
2015-12-16 14:12:34 +08:00
}
}
}