Last active
November 12, 2019 15:29
-
-
Save danbee/afe3376289462ddb90cc to your computer and use it in GitHub Desktop.
This file contains 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
wifi.setmode(wifi.STATION) | |
wifi.sta.config("SSID","password") | |
-- Send POST when buttons A or B pressed | |
PIN_BUTTON_A = 3 -- GPIO0 | |
PIN_BUTTON_B = 4 -- GPIO1 | |
TIME_ALARM = 25 -- 0.025 second, 40 Hz | |
gpio.mode(PIN_BUTTON_A, gpio.INPUT, gpio.PULLUP) | |
gpio.mode(PIN_BUTTON_B, gpio.INPUT, gpio.PULLUP) | |
button_state_a = 1 | |
button_state_b = 1 | |
button_time_a = 0 | |
button_time_b = 0 | |
undo_triggered_a = 0 | |
undo_triggered_b = 0 | |
--local conn = nil | |
--conn=net.createConnection(net.TCP, 0) | |
--conn:connect(80,"23.23.123.101") | |
function addPoint(button) | |
conn=net.createConnection(net.TCP, 0) | |
conn:connect(80, "0.0.0.0") -- IP of the hosted app. The library does not do DNS | |
conn:send("PUT /" .. button .."_scores HTTP/1.1\r\nHost: app-name.herokuapp.com\r\nConnection: keep-alive\r\nAccept: */*\r\nContent-Length: 0\r\n\r\n", function(sck) sck:close(); end) | |
end | |
function undoPoint(button) | |
conn=net.createConnection(net.TCP, 0) | |
conn:connect(80, "0.0.0.0") -- IP of the hosted app. The library does not do DNS | |
conn:send("PUT /" .. button .."_undo HTTP/1.1\r\nHost: app-name.herokuapp.com\r\nConnection: keep-alive\r\nAccept: */*\r\nContent-Length: 0\r\n\r\n", function(sck) sck:close(); end) | |
end | |
function buttonHandler() | |
-- button a | |
button_state_new_a = gpio.read(PIN_BUTTON_A) | |
if button_state_a == 1 and button_state_new_a == 0 then -- button down | |
button_time_a = tmr.time() | |
elseif button_state_a == 0 and button_state_new_a == 1 then -- button up | |
if tmr.time() > (button_time_a + 1) then -- button had been down for 1s | |
undo_triggered_a = 0 | |
else -- button had been down for less than 1s | |
addPoint('red') | |
end | |
elseif button_state_new_a == 0 and undo_triggered_a == 0 and tmr.time() > (button_time_a + 1) then | |
undoPoint('red') | |
undo_triggered_a = 1 | |
end | |
button_state_a = button_state_new_a | |
-- button b | |
button_state_new_b = gpio.read(PIN_BUTTON_B) | |
if button_state_b == 1 and button_state_new_b == 0 then -- button down | |
button_time_b = tmr.time() | |
elseif button_state_b == 0 and button_state_new_b == 1 then -- button up | |
if tmr.time() > (button_time_b + 1) then -- button had been down for 1s | |
undo_triggered_b = 0 | |
else -- button had been down for less than 1s | |
addPoint('blue') | |
end | |
elseif button_state_new_b == 0 and undo_triggered_b == 0 and tmr.time() > (button_time_b + 1) then | |
undoPoint('blue') | |
undo_triggered_b = 1 | |
end | |
button_state_b = button_state_new_b | |
end | |
tmr.alarm(1, TIME_ALARM, 1, buttonHandler) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment