Selaa lähdekoodia

Merge pull request #3 from epukaza/esp_dev

Esp dev
etisab 10 vuotta sitten
vanhempi
commit
022c3cfb0b
4 muutettua tiedostoa jossa 294 lisäystä ja 18 poistoa
  1. 1 0
      proto_api_keys/button20/apikey.txt
  2. 199 0
      rebuild/led.lua
  3. 52 0
      rebuild/main.lua
  4. 42 18
      rebuild/server.lua

+ 1 - 0
proto_api_keys/button20/apikey.txt

@@ -0,0 +1 @@
+4e6542a8-bb75-46bd-a9f5-3a76bc754a60

+ 199 - 0
rebuild/led.lua

@@ -0,0 +1,199 @@
+--pin definitions, should we move it out to another module?
+local led_pin = 1 --GPIO 5  
+local override_pin = 2 --GPIO 4
+
+--pattern definitions
+local STOPPED = 0
+local HEARTBEAT = 1
+local FADE_IN = 2
+local FADE_OUT = 3
+local TRIPLE_BLINK = 4
+
+--private constants
+local heartbeat_times = {40, 200, 40, 900} --format is alternating on and off times in ms
+local triple_blink_times = {100, 20, 100, 20, 100, 20, 100} --format is alternating off and on times in ms
+local timer_id = nil --"constant"
+local led_pwm_frequency = 500 --units: hz
+local led_max_brightness = 1023
+
+--state variables
+local current_pattern
+local pattern_queue = {}
+local heartbeat_index = 1
+local triple_blink_index = 1
+
+--function declarations
+local init = nil
+local stop = nil
+local do_pattern = nil
+local fade_in_update = nil
+local fade_out_update = nil
+local heartbeat_update = nil
+local triple_blink_update = nil
+local end_pattern = nil
+local override_enable = nil
+local override_disable = nil
+local invalid_timer = nil
+
+-----------------------public functions------------------------------
+function init(timer)
+  --handle timer, take exclusive control
+  timer_id = timer
+  tmr.unregister(timer_id)
+
+  --init pins
+  gpio.mode(override_pin, gpio.OUTPUT)
+  pwm.setup(led_pin, led_pwm_frequency, 0)
+  pwm.start(led_pin)
+  pwm.stop(led_pin)
+  
+  stop()
+end
+
+function stop()
+  if(invalid_timer()) then
+    print "Error, call led.init(timer_id) before calling any member functions"
+    return
+  end
+  tmr.unregister(timer_id)
+  --deassert control
+  override_disable()
+  --stop pwm
+  --pwm.stop(led_pin)
+  current_pattern = STOPPED
+  pattern_queue = {}
+end
+
+function do_pattern(pattern)
+  if(invalid_timer()) then
+    print "Error, call led.init(timer_id) before calling any member functions"
+    return
+  end
+
+  --TODO: CONFIRM VALID PATTERN
+  --if(not is_valid_pattern(pattern))
+
+  if(pattern == current_pattern) then
+    --do nothing
+    return
+  end
+
+  if not (current_pattern == STOPPED) then
+    --put pattern in queue
+    table.insert(pattern_queue, pattern)
+  else
+    --start pattern!
+    current_pattern = pattern
+    override_enable()
+
+    --if train could be optimized with else's but this is more readable
+    if(current_pattern == FADE_IN) then
+      pwm.setduty(led_pin, 0)
+      pwm.start(led_pin)
+      fade_in_update()
+    end
+    if(current_pattern == FADE_OUT) then
+      pwm.setduty(led_pin, led_max_brightness)
+      pwm.start(led_pin)
+      fade_out_update()
+    end
+    if(current_pattern == HEARTBEAT) then
+      pwm.setduty(led_pin, 0)
+      pwm.start(led_pin)
+      heartbeat_index = 1
+      heartbeat_update()
+    end
+    if(current_pattern == TRIPLE_BLINK) then
+      pwm.setduty(led_pin, led_max_brightness)
+      pwm.start(led_pin)
+      triple_blink_index = 1
+      triple_blink_update()
+    end
+  end
+end
+
+-----------------------private functions-------------------------
+function fade_in_update()
+  local current_brightness = pwm.getduty(led_pin)
+  current_brightness = current_brightness + 1
+  if(current_brightness > led_max_brightness) then
+    current_brightness = led_max_brightness
+  end
+  pwm.setduty(led_pin, current_brightness)
+  if current_brightness < led_max_brightness then
+    tmr.alarm(timer_id, 2, tmr.ALARM_SINGLE, fade_in_update)
+  else
+    end_pattern()
+  end
+end
+
+function fade_out_update()
+  local current_brightness = pwm.getduty(led_pin)
+  current_brightness = current_brightness - 1
+  if(current_brightness < 0) then
+    current_brightness = 0
+  end
+  pwm.setduty(led_pin, current_brightness)
+  if current_brightness > 0 then
+    tmr.alarm(timer_id, 2, tmr.ALARM_SINGLE, fade_out_update)
+  else
+    end_pattern()
+  end
+end
+
+function heartbeat_update()
+  local current_brightness = pwm.getduty(led_pin)
+  current_brightness = led_max_brightness - current_brightness
+  pwm.setduty(led_pin, current_brightness)
+  tmr.alarm(timer_id, heartbeat_times[heartbeat_index], tmr.ALARM_SINGLE, heartbeat_update)
+  heartbeat_index = heartbeat_index + 1
+  if heartbeat_index > table.getn(heartbeat_times) then
+    heartbeat_index = 1
+  end
+  --pattern does not end
+end
+
+function triple_blink_update()
+  local current_brightness = pwm.getduty(led_pin)
+  current_brightness = led_max_brightness - current_brightness
+  pwm.setduty(led_pin, current_brightness)
+  tmr.alarm(timer_id, triple_blink_times[triple_blink_index], tmr.ALARM_SINGLE, triple_blink_update)
+  triple_blink_index = triple_blink_index + 1
+  if triple_blink_index > table.getn(triple_blink_times) then
+    end_pattern()
+  end
+end
+
+function end_pattern()
+  if table.getn(pattern_queue) == 0 then
+    stop()
+  else
+    p = pattern_queue[1]
+    table.remove(pattern_queue, 1)
+    current_pattern = STOPPED
+    tmr.unregister(timer_id)
+    do_pattern(p)
+  end
+end
+
+function override_enable()
+  gpio.write(override_pin, gpio.HIGH)
+end
+
+function override_disable()
+  gpio.write(override_pin, gpio.LOW)
+end
+
+function invalid_timer()
+  return (timer_id == nil or timer_id > 6)
+end
+
+return {
+  init = init,
+  stop = stop,
+  do_pattern = do_pattern,
+  HEARTBEAT = HEARTBEAT,
+  FADE_IN = FADE_IN,
+  FADE_OUT = FADE_OUT,
+  TRIPLE_BLINK = TRIPLE_BLINK,
+}

+ 52 - 0
rebuild/main.lua

@@ -1,4 +1,7 @@
 DEBUG = true
+TIMERS = {
+  interrupt = 1
+}
 function debug_message(message)
   if DEBUG then
     print(message)
@@ -9,6 +12,7 @@ end
 local yo = require('yo')
 local server = require('server')
 local read = require('read')
+local button_pin = 6
 
 function wifi_setup(func, ...)
   wifi.setmode(wifi.STATIONAP)
@@ -43,3 +47,51 @@ end
 function long_press()
   wifi_setup(server.start)
 end
+
+function short_or_long_press()
+  local long_press_time = 3000 -- 3 seconds
+  local level = gpio.read(button_pin)
+  debug_message('short_or_long_press: ' .. level)
+
+  if level == 1 then -- button depressed
+    debug_message('short_or_long_press: pressed: start long press timer')
+    tmr.alarm(TIMERS.interrupt, long_press_time, 0, function()
+      debug_message('short_or_long_press: long press!')
+      if server.is_serving() then
+        debug_message('short_or_long_press: toggle setup OFF')
+        short_press()
+      else
+        debug_message('short_or_long_press: toggle setup ON')
+        long_press()
+      end
+    end)
+  else -- button released
+    debug_message('short_or_long_press: released: end long press timer')
+    tmr.stop(TIMERS.interrupt)
+    if not server.is_serving() then
+      debug_message('short_or_long_press: short press!')
+      short_press()
+    end
+  end
+end
+
+function debounce(func)
+  local last = 0 --units: microseconds
+  local delay = 50000 --units: microseconds
+
+  return function(...)
+    local now = tmr.now()
+    if now - last < delay then
+      tmr.stop(TIMERS.interrupt)
+      debug_message("debounce: prevented extra push")
+      return
+    end
+
+    last = now
+    debug_message("debounce: succeed")
+    return func(...)
+  end
+end
+
+gpio.mode(button_pin, gpio.INT, gpio.FLOAT)
+gpio.trig(button_pin, "both", debounce(short_or_long_press))

+ 42 - 18
rebuild/server.lua

@@ -5,31 +5,51 @@ local string = string
 local srv = nil
 local debug_message = debug_message
 
+
+local pairs = pairs
+local print = print
 module(...)
 
-local function connect(conn, data)
+local function connect(conn)
   local query_data
 
-  conn:on('receive',
-    function(cn, req_data)
-      query_data = get_http_req(req_data)
-      debug_message(query_data['METHOD'] .. ' ' .. ' ' .. query_data['User-Agent'])
+  conn:on('receive', function(cn, req_data)
+    debug_message('req_data:')
+    debug_message(req_data)
 
-      --TODO discriminate request types (POST --> update)
-      if query_data['METHOD'] == 'POST' then
-        write.new_settings(parse_post(req_data))
-      -- else
-        --TODO discriminate endpoints (/, /yo.css, /status, /favicon.ico)
-      end
+    --below methodology is flawed - this callback handles statelessly per chunk,
+    --however the client MAY send the POST payload in another chunk.
+    --temporarily work around by examining stringy properties of chunk,
+    --but it's totes insecure and will need updating.
 
-      send_index(cn)
-      cn:close()
+    if string.find(req_data, 's=.*&p=.*&r=.*&s=Update') then
+      new_settings = parse_post(req_data)
+      write.new_settings(new_settings)
     end
-  )
+
+    -- query_data = get_http_req(req_data)
+    -- debug_message(query_data['METHOD'] .. ' ' .. ' ' .. query_data['User-Agent'])
+    --TODO discriminate request types (POST --> update)
+    -- if query_data['METHOD'] == 'POST' then
+    --   debug_message('handling POST request')
+    --   new_settings = parse_post(req_data)
+    --   debug_message('new settings: ' .. (new_settings or 'nil'))
+      -- write.new_settings(new_settings)
+    -- else
+      --TODO discriminate endpoints (/, /yo.css, /status, /favicon.ico)
+    -- end
+
+    send_index(cn)
+  end)
+
+  conn:on('sent', function(cn)
+    cn:close()
+  end)
 end
 
 function parse_post(req_data)
   --TODO refactor this function
+  debug_message('server.parse_post')
   if req_data then
     local ssid_index = {req_data:find("s=")}
     local pass_index = {req_data:find("&p=")}
@@ -41,9 +61,9 @@ function parse_post(req_data)
       local new_password = string.gsub(string.sub(req_data, pass_index[2]+1, recipient_index[1]-1), "+", " ")
       local new_recipient = string.upper(string.sub(req_data, recipient_index[2]+1, submit_index[1]-1))
 
-      debug_message(new_ssid)
-      debug_message(new_password)
-      debug_message(new_recipient)
+      debug_message('new ssid: ' .. new_ssid)
+      debug_message('new password: ' .. new_password)
+      debug_message('new recipient: ' .. new_recipient)
 
       return {
         ssid = new_ssid,
@@ -104,7 +124,11 @@ function send_index(conn)
   debug_message('____________')
   debug_message(index)
   debug_message('____________')
-  conn:send('HTTP/1.1 200 OK\n\n' .. index)
+  conn:send('HTTP/1.1 200 OK\r\n\r\n' .. index)
+end
+
+function is_serving()
+  return srv ~= nil
 end
 
 function start()