123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- import button
- import machine
- from machine import Pin
- import network
- import urequests as requests
- 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 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
- button_triggered = False
- def button_handler(pin):
- global button_triggered
- button_triggered = True
- def time_up(timer):
- global current_state, current_pos
- print('Timeout exceeded, resetting')
- current_state = BTN_IDLE
- current_pos = 0
- led_red1.off()
- led_red2.off()
- led_green.off()
- input_handler.reset()
- def dummy_cb(*args, **kwargs):
- return current_state
- def get_state_handler(state):
- handler_map = {
- BTN_FWD_1: check_forward_1,
- BTN_FWD_2: check_forward_2,
- BTN_FWD_RDY: check_forward_trigger
- }
- return handler_map.get(state, dummy_cb)
- btn_pin = Pin(CONFIG['pins']['play_dat'], Pin.IN, Pin.PULL_UP)
- THE_BUTTON = button.Button(btn_pin)
- activity_timeout = machine.Timer(-1)
- current_pos = input_handler.total_change
- current_state = BTN_IDLE
- while True:
- while current_state == BTN_IDLE:
- machine.lightsleep(CONFIG['logic']['sleep_time'] * 1000)
- if input_handler.total_change - current_pos > CONFIG['logic']['wake_threshold']:
- current_state = BTN_FWD_1
- elif current_pos - input_handler.total_change > CONFIG['logic']['wake_threshold']:
- current_state = BTN_REV_1
- wifi = network.WLAN(network.STA_IF)
- wifi.active(True)
- wifi.connect(CONFIG['wifi']['ssid'], CONFIG['wifi']['password'])
- led_red1.on()
- activity_timeout.init(mode=machine.Timer.ONE_SHOT, callback=time_up,
- period=CONFIG['logic']['active_time'] * 1000)
- input_handler.reset()
- while current_state != BTN_IDLE:
- machine.lightsleep(100)
- handler_func = get_state_handler(current_state)
- current_state = handler_func(input_handler)
- wifi.active(False)
- """
- from machine import Pin, Signal
- LED_PIN = Signal(13, Pin.OUT, invert=False)
- BUTTON_PIN = Pin(0, Pin.IN)
- # Turn off "ready" LED
- LED_PIN.off()
- # Read configuration from JSON file
- with open('config.json') as fd:
- import ujson
- config = ujson.load(fd)
- # Configure wireless networking
- import network
- wifi_host = network.WLAN(network.AP_IF)
- wifi_host.active(False)
- 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())
- # Set up interrupt handler
- BUTTON_PIN.irq(button_handler, Pin.IRQ_RISING)
- # Turn on "ready" LED
- LED_PIN.on()
- # Enter main loop
- import time
- while True:
- if button_triggered:
- import urequests as requests
- LED_PIN.off()
- 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)
- time.sleep(5)
- button_triggered = False
- LED_PIN.on()
- else:
- time.sleep(1)
- """
|