import button import machine from machine import Pin import network import urequests as requests import utime 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 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) def check_forward_1(handler): if handler.total_change > 100: led_red2.on() handler.reset() return BTN_FWD_2 return BTN_FWD_1 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 def time_up(timer): print('Timeout exceeded, resetting') full_reset() btn_pin = Pin(CONFIG['pins']['play_dat'], Pin.IN, Pin.PULL_UP) THE_BUTTON = button.Button(btn_pin) activity_timeout = machine.Timer(-1) current_state = BTN_IDLE wifi = network.WLAN(network.STA_IF) while True: 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_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 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 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()