Update for capacitive touch
parent
dd9dcbd46e
commit
822539db4a
|
@ -1,44 +1,55 @@
|
||||||
#include <WS2812.h>
|
#include <WS2812.h>
|
||||||
|
#include <CapacitiveSensor.h>
|
||||||
|
|
||||||
#define LEDCount 37
|
#define LEDCount 37
|
||||||
#define outputPin 11
|
#define outputPin 11
|
||||||
WS2812 LED(LEDCount);
|
WS2812 LED(LEDCount);
|
||||||
cRGB value;
|
cRGB value;
|
||||||
|
CapacitiveSensor capv_btn1 = CapacitiveSensor(4, 2);
|
||||||
|
bool capv_btn1_state = false;
|
||||||
|
int capv_btn1_avg = 0;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
//Serial.begin(230400);
|
//Serial.begin(230400);
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
LED.setOutput(outputPin);
|
LED.setOutput(outputPin);
|
||||||
|
capv_btn1.set_CS_AutocaL_Millis(0xFFFFFFFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
//int capv_btn1_v = capv_btn1.capacitiveSensor(1);
|
||||||
|
capv_btn1_avg = (capv_btn1.capacitiveSensor(1) + capv_btn1_avg * 7) / 8;
|
||||||
|
int capv_btn1_v = capv_btn1_avg;
|
||||||
|
if (capv_btn1_v > 60 && !capv_btn1_state) {
|
||||||
|
Serial.write(0x17);
|
||||||
|
Serial.write(0xff);
|
||||||
|
capv_btn1_avg += 600;
|
||||||
|
capv_btn1_state = true;
|
||||||
|
}
|
||||||
|
else if (capv_btn1_v < 25 && capv_btn1_state) {
|
||||||
|
Serial.write(0x17);
|
||||||
|
Serial.write(0x00);
|
||||||
|
capv_btn1_state = false;
|
||||||
|
}
|
||||||
|
|
||||||
if (Serial.available() > 0) {
|
if (Serial.available() > 0) {
|
||||||
byte command = Serial.read();
|
byte command = Serial.read();
|
||||||
//Serial.write(command);
|
|
||||||
if (command == 0x11) {
|
if (command == 0x11) {
|
||||||
LED.sync();
|
LED.sync();
|
||||||
|
//Serial.write(0x1a);
|
||||||
Serial.write(0x1a);
|
|
||||||
}
|
}
|
||||||
else if (command == 0x12) {
|
else if (command == 0x12) {
|
||||||
while (Serial.available() < 4) {
|
while (Serial.available() < 4) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int pos = Serial.read();
|
int pos = Serial.read();
|
||||||
value.r = Serial.read();
|
value.r = Serial.read();
|
||||||
value.g = Serial.read();
|
value.g = Serial.read();
|
||||||
value.b = Serial.read();
|
value.b = Serial.read();
|
||||||
|
|
||||||
LED.set_crgb_at(pos, value);
|
LED.set_crgb_at(pos, value);
|
||||||
|
//Serial.write(0x1b);
|
||||||
//Serial.write(pos);
|
|
||||||
//Serial.write(value.r);
|
|
||||||
//Serial.write(value.g);
|
|
||||||
//Serial.write(value.b);
|
|
||||||
Serial.write(0x1b);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Serial.write(0x1e);
|
//Serial.write(0x1e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
51
index.js
51
index.js
|
@ -5,6 +5,9 @@ var serialPort = new SerialPort(process.argv[2], {
|
||||||
baudrate: 115200
|
baudrate: 115200
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var lightstate = false;
|
||||||
|
|
||||||
|
var tT;
|
||||||
var leds = 37;
|
var leds = 37;
|
||||||
|
|
||||||
var toHue = function (s) {
|
var toHue = function (s) {
|
||||||
|
@ -94,7 +97,7 @@ var slide = function (color, cb, time) {
|
||||||
i++;
|
i++;
|
||||||
if (i < leds) {
|
if (i < leds) {
|
||||||
var c = color(i);
|
var c = color(i);
|
||||||
setTimeout(function () {
|
tT = setTimeout(function () {
|
||||||
writeLED(i, c[0], c[1], c[2], function () {
|
writeLED(i, c[0], c[1], c[2], function () {
|
||||||
update(loop);
|
update(loop);
|
||||||
});
|
});
|
||||||
|
@ -113,7 +116,28 @@ var fade = function (color, cb) {
|
||||||
|
|
||||||
serialPort.on("open", function () {
|
serialPort.on("open", function () {
|
||||||
serialPort.on("data", function (d) {
|
serialPort.on("data", function (d) {
|
||||||
//console.log(d);
|
var i = 0;
|
||||||
|
var read = () => {
|
||||||
|
if (d.length == i)
|
||||||
|
return null;
|
||||||
|
return d[i++];
|
||||||
|
};
|
||||||
|
var cmd;
|
||||||
|
while (cmd = read()) {
|
||||||
|
if (cmd == 0x17) {
|
||||||
|
var n = read();
|
||||||
|
if (n === null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
var state = n == 0xff;
|
||||||
|
// state
|
||||||
|
//state slide(() => { return [0, 0, 0]; }, () => {}, 20);
|
||||||
|
if (state) {
|
||||||
|
lightstate = !lightstate;
|
||||||
|
lightstate ? setLightness(0) : setLightness(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
/*
|
/*
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
|
@ -185,7 +209,6 @@ app.get("/set/:i", function (req, res) {
|
||||||
}
|
}
|
||||||
if (Math.floor(toHue(state) / sp * 2) == Math.floor(i / sp * 2)) {
|
if (Math.floor(toHue(state) / sp * 2) == Math.floor(i / sp * 2)) {
|
||||||
i = toHue(state);
|
i = toHue(state);
|
||||||
console.log(i);
|
|
||||||
clearInterval(h);
|
clearInterval(h);
|
||||||
}
|
}
|
||||||
flash(function (la) {
|
flash(function (la) {
|
||||||
|
@ -197,11 +220,13 @@ app.get("/set/:i", function (req, res) {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/lightness/set/:i", function (req, res) {
|
var lightnessT;
|
||||||
res.status(200).end();
|
var setLightness = (l) => {
|
||||||
|
clearTimeout(lightnessT);
|
||||||
|
clearTimeout(tT);
|
||||||
|
|
||||||
var from = lightness;
|
var from = lightness;
|
||||||
var to = req.params.i * 1;
|
var to = l * 1;
|
||||||
|
|
||||||
var a = function () {
|
var a = function () {
|
||||||
flash(function (la) {
|
flash(function (la) {
|
||||||
|
@ -209,17 +234,28 @@ app.get("/lightness/set/:i", function (req, res) {
|
||||||
}, function () {
|
}, function () {
|
||||||
lightness = lightness + (to - from) * 0.02;
|
lightness = lightness + (to - from) * 0.02;
|
||||||
if ((to - from) < 0 ? lightness > to : lightness < to) {
|
if ((to - from) < 0 ? lightness > to : lightness < to) {
|
||||||
setTimeout(function () {
|
lightnessT = setTimeout(function () {
|
||||||
a();
|
a();
|
||||||
}, 16);
|
}, 16);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
lightness = to;
|
lightness = to;
|
||||||
|
lightnessT = setTimeout(() => {
|
||||||
|
flash(() => {
|
||||||
|
return hslToRgb(toHue(state) / 256, 1, lightness);
|
||||||
|
}, () => {
|
||||||
|
});
|
||||||
|
}, 16);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
a();
|
a();
|
||||||
|
};
|
||||||
|
app.get("/lightness/set/:i", function (req, res) {
|
||||||
|
res.status(200).end();
|
||||||
|
|
||||||
|
setLightness(req.params.i);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/demo1/start", function (req, res) {
|
app.get("/demo1/start", function (req, res) {
|
||||||
|
@ -336,5 +372,4 @@ app.get("/demo2/stop", function (req, res) {
|
||||||
|
|
||||||
demo2 = false;
|
demo2 = false;
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(8026);
|
app.listen(8026);
|
||||||
|
|
|
@ -11,6 +11,6 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"express": "^4.12.4",
|
"express": "^4.12.4",
|
||||||
"serialport": "^2.0.5"
|
"serialport": "^3.1.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue