Skip to content

Instantly share code, notes, and snippets.

@electricimp
Last active August 29, 2015 14:10
Show Gist options
  • Save electricimp/b1bfd835ffa3a2812633 to your computer and use it in GitHub Desktop.
Save electricimp/b1bfd835ffa3a2812633 to your computer and use it in GitHub Desktop.
Gists for the Shelf Life blog post
// Screen Change Agent Code:
local day = true
function changeday(data){
if (day) day = false;
else day = true;
}
device.on("changeday, changeday)
function httphandler(request, response) {
if (day) {
response.send(200,"day" )
} else {
response.send(200,"night" )
}
}
http.onrequest(httphandler)
// Screen Change Device Code:
button <- hardware.pin1;
function buttonPress() {
local state = button.read();
if (state == 1) {
server.log("Press");
}
else {
server.log("Release");
agent.send("changeday", true);
}
}
button.configure(DIGITAL_IN_PULLUP, buttonPress);
// 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)
// 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