Last active
August 29, 2015 14:10
-
-
Save electricimp/b1bfd835ffa3a2812633 to your computer and use it in GitHub Desktop.
Gists for the Shelf Life blog post
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
// Keypad Agent Code: | |
local lastdatereceived="" | |
local lastindex=0 | |
function gotnewdate(thedate){ | |
server.log (thedate) | |
lastdatereceived=thedate | |
lastindex=lastindex+1 | |
} | |
device.on("newdate",gotnewdate) | |
function httphandler(request,response){ | |
response.send(200,"index"+lastindex+ "date"+lastdatereceived ) | |
} | |
http.onrequest(httphandler) |
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
// Keypad Device Code: | |
hardware.pin1.configure(DIGITAL_OUT,0) | |
hardware.pin2.configure(DIGITAL_OUT,0) | |
hardware.pin5.configure(DIGITAL_OUT,0) | |
hardware.pin7.configure(DIGITAL_IN) | |
hardware.pin8.configure(DIGITAL_IN) | |
hardware.pin9.configure(DIGITAL_IN) | |
local thedate="" | |
function scanrows(){ | |
if(hardware.pin7.read()==1)return 1 | |
if(hardware.pin8.read()==1)return 4 | |
if(hardware.pin9.read()==1)return 7 | |
return 0 | |
} | |
function scancolumb1(){ | |
hardware.pin1.write(1) | |
local row=scanrows() | |
hardware.pin1.write(0) | |
return row | |
} | |
function scancolumb2(){ | |
hardware.pin2.write(1) | |
local row=scanrows() | |
hardware.pin2.write(0) | |
return row | |
} | |
function scancolumb3(){ | |
hardware.pin5.write(1) | |
local row=scanrows() | |
hardware.pin5.write(0) | |
return row | |
} | |
function scankeypad(){ | |
local columb1=scancolumb1() | |
local columb2=scancolumb2() | |
if(columb2>0)columb2=columb2+1 | |
local columb3=scancolumb3() | |
if(columb3>0)columb3=columb3+2 | |
local key=columb1+columb2+columb3 | |
if(key!=0){ | |
server.log("detected key press"+key) | |
thedate=thedate+key | |
if(thedate.len()==6){ | |
agent.send("newdate",thedate) | |
thedate="" | |
} | |
} | |
imp.wakeup(0.25,scankeypad) | |
} scankeypad() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment