main.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import button
  2. import machine
  3. from machine import Pin
  4. import network
  5. import urequests as requests
  6. BTN_IDLE = 0
  7. BTN_FWD_1 = 1
  8. BTN_FWD_2 = 2
  9. BTN_FWD_RDY = 3
  10. BTN_REV_1 = 4
  11. BTN_REV_2 = 5
  12. BTN_REV_RDY = 6
  13. def check_forward_1(handler):
  14. if handler.total_change > 100:
  15. led_red2.on()
  16. handler.reset()
  17. return BTN_FWD_2
  18. return BTN_FWD_1
  19. def check_forward_2(handler):
  20. global button_triggered
  21. if handler.total_change > 150 and wifi.isconnected():
  22. led_green.on()
  23. button_triggered = False
  24. print('Button armed')
  25. return BTN_FWD_RDY
  26. return BTN_FWD_2
  27. def check_forward_trigger(handler):
  28. if (THE_BUTTON.pressed):
  29. # Ping overlay server
  30. requests.get('http://192.168.0.210:42069/rdp')
  31. led_red1.off()
  32. led_red2.off()
  33. led_green.off()
  34. activity_timeout.deinit()
  35. machine.lightsleep(5)
  36. THE_BUTTON.reset()
  37. return BTN_IDLE
  38. return BTN_FWD_RDY
  39. button_triggered = False
  40. def button_handler(pin):
  41. global button_triggered
  42. button_triggered = True
  43. def time_up(timer):
  44. global current_state, current_pos
  45. print('Timeout exceeded, resetting')
  46. current_state = BTN_IDLE
  47. current_pos = 0
  48. led_red1.off()
  49. led_red2.off()
  50. led_green.off()
  51. input_handler.reset()
  52. def dummy_cb(*args, **kwargs):
  53. return current_state
  54. def get_state_handler(state):
  55. handler_map = {
  56. BTN_FWD_1: check_forward_1,
  57. BTN_FWD_2: check_forward_2,
  58. BTN_FWD_RDY: check_forward_trigger
  59. }
  60. return handler_map.get(state, dummy_cb)
  61. btn_pin = Pin(CONFIG['pins']['play_dat'], Pin.IN, Pin.PULL_UP)
  62. THE_BUTTON = button.Button(btn_pin)
  63. activity_timeout = machine.Timer(-1)
  64. current_pos = input_handler.total_change
  65. current_state = BTN_IDLE
  66. while True:
  67. while current_state == BTN_IDLE:
  68. machine.lightsleep(CONFIG['logic']['sleep_time'] * 1000)
  69. if input_handler.total_change - current_pos > CONFIG['logic']['wake_threshold']:
  70. current_state = BTN_FWD_1
  71. elif current_pos - input_handler.total_change > CONFIG['logic']['wake_threshold']:
  72. current_state = BTN_REV_1
  73. wifi = network.WLAN(network.STA_IF)
  74. wifi.active(True)
  75. wifi.connect(CONFIG['wifi']['ssid'], CONFIG['wifi']['password'])
  76. led_red1.on()
  77. activity_timeout.init(mode=machine.Timer.ONE_SHOT, callback=time_up,
  78. period=CONFIG['logic']['active_time'] * 1000)
  79. input_handler.reset()
  80. while current_state != BTN_IDLE:
  81. machine.lightsleep(100)
  82. handler_func = get_state_handler(current_state)
  83. current_state = handler_func(input_handler)
  84. wifi.active(False)
  85. """
  86. from machine import Pin, Signal
  87. LED_PIN = Signal(13, Pin.OUT, invert=False)
  88. BUTTON_PIN = Pin(0, Pin.IN)
  89. # Turn off "ready" LED
  90. LED_PIN.off()
  91. # Read configuration from JSON file
  92. with open('config.json') as fd:
  93. import ujson
  94. config = ujson.load(fd)
  95. # Configure wireless networking
  96. import network
  97. wifi_host = network.WLAN(network.AP_IF)
  98. wifi_host.active(False)
  99. wifi_guest = network.WLAN(network.STA_IF)
  100. wifi_guest.active(True)
  101. if not wifi_guest.isconnected():
  102. wifi_guest.connect(config['wifi']['ssid'], config['wifi']['password'])
  103. while not wifi_guest.isconnected():
  104. pass
  105. print(wifi_guest.ifconfig())
  106. # Set up interrupt handler
  107. BUTTON_PIN.irq(button_handler, Pin.IRQ_RISING)
  108. # Turn on "ready" LED
  109. LED_PIN.on()
  110. # Enter main loop
  111. import time
  112. while True:
  113. if button_triggered:
  114. import urequests as requests
  115. LED_PIN.off()
  116. btn_id = config['buttonId']
  117. endpoint = 'http://192.168.0.212:42069/rdp?id={0}'.format(btn_id)
  118. res = requests.get(endpoint)
  119. # print(res.status_code)
  120. time.sleep(5)
  121. button_triggered = False
  122. LED_PIN.on()
  123. else:
  124. time.sleep(1)
  125. """