From edf2653340f032b5c368d8d83899d084af639920 Mon Sep 17 00:00:00 2001 From: "DESKTOP-B25GA9E\\W35" <1733709035@qq.com> Date: Wed, 20 Sep 2023 17:15:29 +0800 Subject: [PATCH] add auto close and change staus --- .vscode/settings.json | 3 +- GPIO.lua | 64 ++++++++++++++++++++++- eus_params.lua => SaveData/eus_params.lua | 0 UDPService.lua | 8 --- 4 files changed, 64 insertions(+), 11 deletions(-) rename eus_params.lua => SaveData/eus_params.lua (100%) diff --git a/.vscode/settings.json b/.vscode/settings.json index 1f4708c..c2c21ee 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -6,6 +6,7 @@ "i2c", "gpio", "enduser_setup", - "file" + "file", + "tmr" ] } \ No newline at end of file diff --git a/GPIO.lua b/GPIO.lua index a6d1bd2..1170f87 100644 --- a/GPIO.lua +++ b/GPIO.lua @@ -1,3 +1,5 @@ +Is_AutoClose = true; +LightArr = {[1]=false,[2]=false,[3]=false,[4]=false} --Init GPIO params function InitGPIO() gpio.mode(0,gpio.OUTPUT); @@ -14,14 +16,72 @@ end --Push gpio to high function GPIOOn(gpioId) if gpioId>=0 and gpioId<=4 then - gpio.write(gpioId,gpio.HIGH) + gpio.write(gpioId,gpio.HIGH); + LightArr[gpioId] = true; + PageLight(LightArr); + if Is_AutoClose then + AutoClose(gpioId); + end + end + if gpioId == 99 then + gpio.write(0,gpio.HIGH); + gpio.write(1,gpio.HIGH); + gpio.write(2,gpio.HIGH); + gpio.write(3,gpio.HIGH); + gpio.write(4,gpio.HIGH); + LightArr = {[1]=true,[2]=true,[3]=true,[4]=true} + PageLight(LightArr); + if Is_AutoClose then + AutoCloseAll(); + end + end + if gpioId == 10 then + Is_AutoClose = true; end end --Push gpio to low function GPIOOff(gpioId) if gpioId>=0 and gpioId<=4 then - gpio.write(gpioId,gpio.LOW) + gpio.write(gpioId,gpio.LOW); + LightArr[gpioId] = false; + PageLight(LightArr); + end + if gpioId == 99 then + gpio.write(0,gpio.LOW); + gpio.write(1,gpio.LOW); + gpio.write(2,gpio.LOW); + gpio.write(3,gpio.LOW); + gpio.write(4,gpio.LOW); + LightArr = {[1]=false,[2]=false,[3]=false,[4]=false} + PageLight(LightArr); end + if gpioId == 10 then + Is_AutoClose = false; + end +end + +function AutoClose(gpioId) + local mytimer = tmr.create() + mytimer:register(1000, tmr.ALARM_SINGLE, function() + gpio.write(gpioId,gpio.LOW); + LightArr[gpioId] = false; + PageLight(LightArr); + end) + mytimer:start() +end + +function AutoCloseAll() + local mytimer = tmr.create() + mytimer:register(1000, tmr.ALARM_SINGLE, function() + gpio.write(0,gpio.LOW); + gpio.write(1,gpio.LOW); + gpio.write(2,gpio.LOW); + gpio.write(3,gpio.LOW); + gpio.write(4,gpio.LOW); + LightArr = {[1]=false,[2]=false,[3]=false,[4]=false} + PageLight(LightArr); + end) + mytimer:start() end InitGPIO(); print("gpio1-5 init over,turn all gpio to output,low") diff --git a/eus_params.lua b/SaveData/eus_params.lua similarity index 100% rename from eus_params.lua rename to SaveData/eus_params.lua diff --git a/UDPService.lua b/UDPService.lua index 7264b86..f2d5103 100644 --- a/UDPService.lua +++ b/UDPService.lua @@ -1,8 +1,4 @@ --- UDPSocket = nil; --- UDPIP = nil; --- UDPPort = nil; require("GPIO"); -LightArr = {[1]=false,[2]=false,[3]=false,[4]=false} function StartUDPService() if UDPSocket == nil then UDPSocket = net.createUDPSocket(); @@ -28,14 +24,10 @@ function UDPService(s, data, port, ip) print(tonumber(commandSet[2]).."-"..commandSet[3]) if commandSet[3]=="On" and tonumber(commandSet[2])~=nil then GPIOOn(tonumber(commandSet[2])); - LightArr[tonumber(commandSet[2])] = true; s:send(port, ip, "Light "..commandSet[2].." "..commandSet[3]); - PageLight(LightArr); elseif commandSet[3]=="Off" and tonumber(commandSet[2])~=nil then GPIOOff(tonumber(commandSet[2])); - LightArr[tonumber(commandSet[2])] = false; s:send(port, ip, "Light "..commandSet[2].." "..commandSet[3]); - PageLight(LightArr); else s:send(port, ip, "CommandFaild"); end