|
@@ -1,54 +1,105 @@
|
|
|
-from machine import Pin, Signal
|
|
|
-LED_PIN = Signal(13, Pin.OUT, invert=False)
|
|
|
-BUTTON_PIN = Pin(0, Pin.IN)
|
|
|
+import button
|
|
|
+import machine
|
|
|
+from machine import Pin
|
|
|
+import network
|
|
|
+import urequests as requests
|
|
|
+import utime
|
|
|
|
|
|
-button_triggered = False
|
|
|
-def button_handler(pin):
|
|
|
- global button_triggered
|
|
|
- button_triggered = True
|
|
|
+BTN_IDLE = 0
|
|
|
+BTN_FWD_1 = 1
|
|
|
+BTN_FWD_2 = 2
|
|
|
+BTN_FWD_RDY = 3
|
|
|
+BTN_REV_1 = 4
|
|
|
+BTN_REV_2 = 5
|
|
|
+BTN_REV_RDY = 6
|
|
|
|
|
|
-# Turn off "ready" LED
|
|
|
-LED_PIN.off()
|
|
|
+def full_reset():
|
|
|
+ global current_state
|
|
|
+ led_red1.off()
|
|
|
+ led_red2.off()
|
|
|
+ led_green.off()
|
|
|
+ activity_timeout.deinit()
|
|
|
+ THE_BUTTON.reset()
|
|
|
+ current_state = BTN_IDLE
|
|
|
+ input_handler.reset()
|
|
|
+ input_handler.enable()
|
|
|
+ wifi.active(False)
|
|
|
|
|
|
-# Read configuration from JSON file
|
|
|
-with open('config.json') as fd:
|
|
|
- import ujson
|
|
|
- config = ujson.load(fd)
|
|
|
+def check_forward_1(handler):
|
|
|
+ if handler.total_change > 100:
|
|
|
+ led_red2.on()
|
|
|
+ handler.reset()
|
|
|
+ return BTN_FWD_2
|
|
|
+ return BTN_FWD_1
|
|
|
|
|
|
-# Configure wireless networking
|
|
|
-import network
|
|
|
-wifi_host = network.WLAN(network.AP_IF)
|
|
|
-wifi_host.active(False)
|
|
|
+def check_forward_2(handler):
|
|
|
+ global button_triggered
|
|
|
+ if handler.total_change > 150 and wifi.isconnected():
|
|
|
+ led_green.on()
|
|
|
+ button_triggered = False
|
|
|
+ print('Button armed')
|
|
|
+ return BTN_FWD_RDY
|
|
|
+ return BTN_FWD_2
|
|
|
+
|
|
|
+def check_forward_trigger(handler):
|
|
|
+ if (THE_BUTTON.pressed):
|
|
|
+ # Ping overlay server
|
|
|
+ # requests.get('http://192.168.0.210:42069/rdp')
|
|
|
+ led_red1.off()
|
|
|
+ led_red2.off()
|
|
|
+ led_green.off()
|
|
|
+ activity_timeout.deinit()
|
|
|
+ machine.lightsleep(5)
|
|
|
+ THE_BUTTON.reset()
|
|
|
+ return BTN_IDLE
|
|
|
+ return BTN_FWD_RDY
|
|
|
|
|
|
-wifi_guest = network.WLAN(network.STA_IF)
|
|
|
-wifi_guest.active(True)
|
|
|
-if not wifi_guest.isconnected():
|
|
|
- wifi_guest.connect(config['wifi']['ssid'], config['wifi']['password'])
|
|
|
- while not wifi_guest.isconnected():
|
|
|
- pass
|
|
|
-print(wifi_guest.ifconfig())
|
|
|
+def time_up(timer):
|
|
|
+ print('Timeout exceeded, resetting')
|
|
|
+ full_reset()
|
|
|
|
|
|
-# Set up interrupt handler
|
|
|
-BUTTON_PIN.irq(button_handler, Pin.IRQ_RISING)
|
|
|
+btn_pin = Pin(CONFIG['pins']['play_dat'], Pin.IN, Pin.PULL_UP)
|
|
|
+THE_BUTTON = button.Button(btn_pin)
|
|
|
|
|
|
-# Turn on "ready" LED
|
|
|
-LED_PIN.on()
|
|
|
+activity_timeout = machine.Timer(-1)
|
|
|
+current_state = BTN_IDLE
|
|
|
|
|
|
-# Enter main loop
|
|
|
-import time
|
|
|
+wifi = network.WLAN(network.STA_IF)
|
|
|
while True:
|
|
|
- if button_triggered:
|
|
|
- import urequests as requests
|
|
|
+ while current_state == BTN_IDLE:
|
|
|
+ machine.lightsleep(CONFIG['logic']['sleep_time'] * 1000)
|
|
|
+ if input_handler.total_change > CONFIG['logic']['wake_threshold']:
|
|
|
+ print('Entering BTN_FWD_1')
|
|
|
+ current_state = BTN_FWD_1
|
|
|
|
|
|
- LED_PIN.off()
|
|
|
+ led_red1.on()
|
|
|
+ wifi.active(True)
|
|
|
+ wifi.connect(CONFIG['wifi']['ssid'], CONFIG['wifi']['password'])
|
|
|
+ input_handler.disable()
|
|
|
+ start_time = utime.ticks_ms()
|
|
|
+ activity_timeout.init(mode=machine.Timer.ONE_SHOT, callback=time_up,
|
|
|
+ period=CONFIG['logic']['active_time'] * 1000)
|
|
|
+ while current_state == BTN_FWD_1:
|
|
|
+ machine.lightsleep(100)
|
|
|
+ if utime.ticks_diff(utime.ticks_ms(), start_time) > 1500:
|
|
|
+ print('Entering BTN_FWD_2')
|
|
|
+ current_state = BTN_FWD_2
|
|
|
|
|
|
- btn_id = config['buttonId']
|
|
|
- endpoint = 'http://192.168.0.212:42069/rdp?id={0}'.format(btn_id)
|
|
|
- res = requests.get(endpoint)
|
|
|
-# print(res.status_code)
|
|
|
+ led_red2.on()
|
|
|
+ start_time = utime.ticks_ms()
|
|
|
+ while current_state == BTN_FWD_2:
|
|
|
+ machine.lightsleep(100)
|
|
|
+ if (utime.ticks_diff(utime.ticks_ms(), start_time) > 1500) and wifi.isconnected():
|
|
|
+ print('Entering BTN_FWD_RDY')
|
|
|
+ current_state = BTN_FWD_RDY
|
|
|
|
|
|
- time.sleep(5)
|
|
|
- button_triggered = False
|
|
|
- LED_PIN.on()
|
|
|
- else:
|
|
|
- time.sleep(1)
|
|
|
+ led_green.on()
|
|
|
+ THE_BUTTON.reset()
|
|
|
+ while current_state == BTN_FWD_RDY:
|
|
|
+ machine.lightsleep(100)
|
|
|
+ if THE_BUTTON.pressed:
|
|
|
+ print('Launching dance party')
|
|
|
+ requests.get('http://192.168.0.210:42069/rdp')
|
|
|
+ current_state = BTN_IDLE
|
|
|
+
|
|
|
+ full_reset()
|