Просмотр исходного кода

- LED indicator now event driven
- explicit kill function for LED module
- names of things

etisab 10 лет назад
Родитель
Сommit
44c15aeea8
4 измененных файлов с 55 добавлено и 13 удалено
  1. 11 3
      rebuild/init.lua
  2. 6 0
      rebuild/led.lua
  3. 21 10
      rebuild/main.lua
  4. 17 0
      rebuild/yo.lua

+ 11 - 3
rebuild/init.lua

@@ -1,4 +1,4 @@
-DEBUG = true
+DEBUG = false
 
 function debug_message(message)
   if DEBUG then
@@ -10,8 +10,16 @@ end
 if DEBUG then
   debug_message('1 second startup delay on timer 0 ...')
   tmr.alarm(0, 1000, 0, function()
-    dofile('main.lua')
+    if file.exists('main.lc') then
+      dofile('main.lc')
+    else
+      dofile('main.lua')
+    end
   end)
 else
-  dofile('main.lua')
+  if file.exists('main.lc') then
+    dofile('main.lc')
+  else
+    dofile('main.lua')
+  end
 end

+ 6 - 0
rebuild/led.lua

@@ -109,4 +109,10 @@ function next_pattern()
   end
 end
 
+function kill()
+  Q = {}
+  tmr.unregister(TIMER)
+  pwm.setduty(LED_PIN, 0)
+end
+
 init(TIMER)

+ 21 - 10
rebuild/main.lua

@@ -4,13 +4,17 @@ TIMERS = {
   led = 3
 }
 
+local button_pin = 6
+local led_pin = 1
 local yo = require('yo')
 local server = require('server')
 local led = require('led')
 local read = require('read')
-local button_pin = 6
 
-function wifi_setup(func, ...)
+function setup_mode(func, ...)
+  led.kill()
+  led.q_heart_beat()
+
   wifi.setmode(wifi.STATIONAP)
   wifi.ap.config({
     ssid = "YoButton-" .. node.chipid(),
@@ -21,34 +25,39 @@ function wifi_setup(func, ...)
   wifi.ap.dhcp.start()
   wifi.sleeptype(wifi.NONE_SLEEP)
 
-  --wifi_setup inactivity timeout: 5 minutes
+  --setup_mode inactivity timeout: 5 minutes
   tmr.alarm(TIMERS.setup_timeout, 60*5*1000, tmr.ALARM_SINGLE, function()
-    wifi_default(function()
-      debug_message('wifi_setup: timeout')
+    default_mode(function()
+      debug_message('setup_mode: timeout')
       return nil  --nil function to use decorator side effects: code smell
     end)
   end)
 
   func(...)
-
 end
 
-function wifi_default(func, ...)
+function default_mode(func, ...)
+  led.kill()
+  led.q_fade_in()
   server.stop()
+
   wifi.setmode(wifi.STATION)
   wifi.sleeptype(wifi.NONE_SLEEP)
 
-  func(...)
+  local success = func(...)
+
+  debug_message(success)
 
   wifi.sleeptype(wifi.MODEM_SLEEP)
 end
 
+
 function handle_short_press()
-  wifi_default(yo.yo, read.yo_recipient(), read.api_key())
+  default_mode(yo.yo, read.yo_recipient(), read.api_key())
 end
 
 function handle_long_press()
-  wifi_setup(server.start)
+  setup_mode(server.start)
 end
 
 function handle_button_flip()
@@ -95,6 +104,8 @@ function debounce(delay, func)
 end
 
 wifi.setmode(wifi.STATION)
+pwm.setduty(led_pin, 0)
+
 handle_short_press = debounce(3000000, handle_short_press) --3 seconds
 gpio.mode(button_pin, gpio.INT, gpio.FLOAT)
 gpio.trig(button_pin, "both", debounce(50000, handle_button_flip)) --50 ms

+ 17 - 0
rebuild/yo.lua

@@ -1,5 +1,6 @@
 local http = http
 local string = string
+local led = require('led')
 local assert = assert
 local type = type
 local debug_message = debug_message
@@ -14,6 +15,7 @@ function yo(yo_user, api_token)
 
   local content_string = "api_token=" .. api_token .. "&username=" .. string.upper(yo_user) .. '\r\n\r\n'
   local content_length = string.len(content_string)
+  local succeeded = false
 
   debug_message('yo.yo: sending Yo')
   http.post(
@@ -24,6 +26,21 @@ function yo(yo_user, api_token)
     function(status_code, response_data)
       debug_message('yo.yo: status code: ' .. status_code)
       debug_message('yo.yo: response data: ' .. (response_data or 'nil'))
+
+      if status_code >= 200 and status_code < 300 then
+        succeeded = true
+      else
+        succeeded = false
+      end
+      yo_sent(succeeded)
     end
   )
 end
+
+function yo_sent(succeeded)
+  if succeeded then
+    led.q_fade_out()
+  else
+    led.q_triple_blink()
+  end
+end