Created
April 13, 2023 00:39
-
-
Save cmrigney/449056ccc6e9d550ad027c41f05830c7 to your computer and use it in GitHub Desktop.
Personal hacked desk adjustment system, written in my version of Lox. Runs on a Pi Pico.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Personal hacked desk adjustment system, written in my version of Lox. Runs on a Pi Pico. | |
| var pico = systemImport("pico"); | |
| var CRNL = Buffer(Array(13, 10)).asString(); | |
| class DoubleClickTracker { | |
| init(pin, callback) { | |
| this.pin = pin; | |
| this.callback = callback; | |
| this.reset(); | |
| } | |
| reset() { | |
| this.lastActive = false; | |
| this.lastActiveTime = 0; | |
| } | |
| track() { | |
| var active = !this.pin.read(); | |
| // If active now, not last time, but last active was less than 1/2 second ago, then trigger | |
| if(active and !this.lastActive and (clock() - this.lastActiveTime < 0.6)) { | |
| this.callback(); | |
| this.reset(); | |
| } | |
| this.lastActive = active; | |
| if(active) { | |
| this.lastActiveTime = clock(); | |
| } | |
| } | |
| } | |
| var socketManager = pico.SocketManager(); | |
| fun reportChange(newState) { | |
| var client = pico.TCPClient("192.168.1.135", 8088, 3); // 3 second timeout | |
| if(client.connected()) { | |
| socketManager.clientSockets.push(client); | |
| client.write("GET /change?state=" + newState + " HTTP/1.1" + CRNL + CRNL); | |
| client.close(); | |
| } | |
| } | |
| var UpPinNum = 13; | |
| var UpReadPinNum = 12; | |
| var DownPinNum = 9; | |
| var DownReadPinNum = 8; | |
| var UpPin = pico.Pin(UpPinNum).output().off(); | |
| var UpReadPin = pico.Pin(UpReadPinNum).input().pullup(); | |
| var DownPin = pico.Pin(DownPinNum).output().off(); | |
| var DownReadPin = pico.Pin(DownReadPinNum).input().pullup(); | |
| fun goUp() { | |
| UpPin.on(); | |
| pico.sleep(14 * 1000); | |
| UpPin.off(); | |
| pico.sleep(200); | |
| DownPin.on(); | |
| pico.sleep(400); | |
| DownPin.off(); | |
| reportChange("stand"); | |
| } | |
| fun goDown() { | |
| DownPin.on(); | |
| pico.sleep(14 * 1000); | |
| DownPin.off(); | |
| reportChange("sit"); | |
| } | |
| var UpClickTracker = DoubleClickTracker(UpReadPin, goUp); | |
| var DownClickTracker = DoubleClickTracker(DownReadPin, goDown); | |
| pico.LEDPin.on(); | |
| fun error(msg) { | |
| pico.LEDPin.off(); | |
| logln(msg); | |
| pico.exit(); | |
| } | |
| if(!pico.isW()) { | |
| error("Only works on W"); | |
| } | |
| logln("Attempting to connect to Wifi"); | |
| if(!pico.connectToAP("<SSID>", "<Password>", 30000)) { | |
| error("Unable to connect to AP"); | |
| } | |
| var server = pico.HTTPServer(8080); | |
| server.use("/mem", fun (req, res) { | |
| res.json(.{ | |
| vm: getMemStats(), | |
| pico: pico.getPicoStats() | |
| }); | |
| }); | |
| server.use("/getup", fun (req, res) { | |
| res.json(.{ active: !UpReadPin.read() }); | |
| }); | |
| server.use("/getdown", fun (req, res) { | |
| res.json(.{ active: !DownReadPin.read() }); | |
| }); | |
| server.use("/up", fun (req, res) { | |
| goUp(); | |
| res.json(.{ status: "ok" }); | |
| }); | |
| server.use("/down", fun (req, res) { | |
| goDown(); | |
| res.json(.{ status: "ok" }); | |
| }); | |
| logln("Listening on port 8080"); | |
| server.listen(true); | |
| while(true) { | |
| UpClickTracker.track(); | |
| DownClickTracker.track(); | |
| pico.sleep(1); | |
| socketManager.poll(); | |
| if(server.poll()) { // did work | |
| pico.sleep(1); | |
| } | |
| else { | |
| // pico.lowPowerSleep(1000); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment