main.py 3.5 KB

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