diff --git a/USBStrip/USBStrip.ino b/USBStrip/USBStrip.ino index efc0934..c64a458 100644 --- a/USBStrip/USBStrip.ino +++ b/USBStrip/USBStrip.ino @@ -1,44 +1,55 @@ #include +#include #define LEDCount 37 #define outputPin 11 -WS2812 LED(LEDCount); +WS2812 LED(LEDCount); cRGB value; +CapacitiveSensor capv_btn1 = CapacitiveSensor(4, 2); +bool capv_btn1_state = false; +int capv_btn1_avg = 0; void setup() { //Serial.begin(230400); Serial.begin(115200); LED.setOutput(outputPin); + capv_btn1.set_CS_AutocaL_Millis(0xFFFFFFFF); } 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) { byte command = Serial.read(); - //Serial.write(command); if (command == 0x11) { LED.sync(); - - Serial.write(0x1a); + //Serial.write(0x1a); } else if (command == 0x12) { while (Serial.available() < 4) { } - int pos = Serial.read(); value.r = Serial.read(); value.g = Serial.read(); value.b = Serial.read(); - LED.set_crgb_at(pos, value); - - //Serial.write(pos); - //Serial.write(value.r); - //Serial.write(value.g); - //Serial.write(value.b); - Serial.write(0x1b); + //Serial.write(0x1b); } else { - Serial.write(0x1e); + //Serial.write(0x1e); } } } diff --git a/index.js b/index.js index ba59116..d2b9377 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,9 @@ var serialPort = new SerialPort(process.argv[2], { baudrate: 115200 }); +var lightstate = false; + +var tT; var leds = 37; var toHue = function (s) { @@ -94,7 +97,7 @@ var slide = function (color, cb, time) { i++; if (i < leds) { var c = color(i); - setTimeout(function () { + tT = setTimeout(function () { writeLED(i, c[0], c[1], c[2], function () { update(loop); }); @@ -113,7 +116,28 @@ var fade = function (color, cb) { serialPort.on("open", function () { 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 () { @@ -185,7 +209,6 @@ app.get("/set/:i", function (req, res) { } if (Math.floor(toHue(state) / sp * 2) == Math.floor(i / sp * 2)) { i = toHue(state); - console.log(i); clearInterval(h); } flash(function (la) { @@ -197,11 +220,13 @@ app.get("/set/:i", function (req, res) { }); -app.get("/lightness/set/:i", function (req, res) { - res.status(200).end(); +var lightnessT; +var setLightness = (l) => { + clearTimeout(lightnessT); + clearTimeout(tT); var from = lightness; - var to = req.params.i * 1; + var to = l * 1; var a = function () { flash(function (la) { @@ -209,17 +234,28 @@ app.get("/lightness/set/:i", function (req, res) { }, function () { lightness = lightness + (to - from) * 0.02; if ((to - from) < 0 ? lightness > to : lightness < to) { - setTimeout(function () { + lightnessT = setTimeout(function () { a(); }, 16); } else { lightness = to; + lightnessT = setTimeout(() => { + flash(() => { + return hslToRgb(toHue(state) / 256, 1, lightness); + }, () => { + }); + }, 16); } }); }; a(); +}; +app.get("/lightness/set/:i", function (req, res) { + res.status(200).end(); + + setLightness(req.params.i); }); app.get("/demo1/start", function (req, res) { @@ -336,5 +372,4 @@ app.get("/demo2/stop", function (req, res) { demo2 = false; }); - app.listen(8026); diff --git a/package.json b/package.json index 17cebf6..c09b455 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,6 @@ }, "dependencies": { "express": "^4.12.4", - "serialport": "^2.0.5" + "serialport": "^3.1.1" } }