main.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import button
  2. import machine
  3. from machine import Pin
  4. import network
  5. import urequests as requests
  6. import utime
  7. BTN_IDLE = 0
  8. BTN_FWD_1 = 1
  9. BTN_FWD_2 = 2
  10. BTN_FWD_RDY = 3
  11. BTN_REV_1 = 4
  12. BTN_REV_2 = 5
  13. BTN_REV_RDY = 6
  14. def full_reset():
  15. global current_state
  16. led_red1.off()
  17. led_red2.off()
  18. led_green.off()
  19. activity_timeout.deinit()
  20. THE_BUTTON.reset()
  21. current_state = BTN_IDLE
  22. input_handler.reset()
  23. input_handler.enable()
  24. wifi.active(False)
  25. def check_forward_1(handler):
  26. if handler.total_change > 100:
  27. led_red2.on()
  28. handler.reset()
  29. return BTN_FWD_2
  30. return BTN_FWD_1
  31. def check_forward_2(handler):
  32. global button_triggered
  33. if handler.total_change > 150 and wifi.isconnected():
  34. led_green.on()
  35. button_triggered = False
  36. print('Button armed')
  37. return BTN_FWD_RDY
  38. return BTN_FWD_2
  39. def check_forward_trigger(handler):
  40. if (THE_BUTTON.pressed):
  41. # Ping overlay server
  42. # requests.get('http://192.168.0.210:42069/rdp')
  43. led_red1.off()
  44. led_red2.off()
  45. led_green.off()
  46. activity_timeout.deinit()
  47. machine.lightsleep(5)
  48. THE_BUTTON.reset()
  49. return BTN_IDLE
  50. return BTN_FWD_RDY
  51. def time_up(timer):
  52. print('Timeout exceeded, resetting')
  53. full_reset()
  54. btn_pin = Pin(CONFIG['pins']['play_dat'], Pin.IN, Pin.PULL_UP)
  55. THE_BUTTON = button.Button(btn_pin)
  56. activity_timeout = machine.Timer(-1)
  57. current_state = BTN_IDLE
  58. wifi = network.WLAN(network.STA_IF)
  59. while True:
  60. while current_state == BTN_IDLE:
  61. machine.lightsleep(CONFIG['logic']['sleep_time'] * 1000)
  62. if input_handler.total_change > CONFIG['logic']['wake_threshold']:
  63. print('Entering BTN_FWD_1')
  64. current_state = BTN_FWD_1
  65. led_red1.on()
  66. input_handler.disable()
  67. wifi.active(True)
  68. wifi.connect(CONFIG['wifi']['ssid'], CONFIG['wifi']['password'])
  69. start_time = utime.ticks_ms()
  70. activity_timeout.init(mode=machine.Timer.ONE_SHOT, callback=time_up,
  71. period=CONFIG['logic']['active_time'] * 1000)
  72. while current_state == BTN_FWD_1:
  73. machine.lightsleep(100)
  74. if utime.ticks_diff(utime.ticks_ms(), start_time) > 1500:
  75. print('Entering BTN_FWD_2')
  76. current_state = BTN_FWD_2
  77. led_red2.on()
  78. start_time = utime.ticks_ms()
  79. while current_state == BTN_FWD_2:
  80. machine.lightsleep(100)
  81. if (utime.ticks_diff(utime.ticks_ms(), start_time) > 1500) and wifi.isconnected():
  82. print('Entering BTN_FWD_RDY')
  83. current_state = BTN_FWD_RDY
  84. led_green.on()
  85. THE_BUTTON.reset()
  86. while current_state == BTN_FWD_RDY:
  87. machine.lightsleep(100)
  88. if THE_BUTTON.pressed:
  89. print('Launching dance party')
  90. requests.get('http://192.168.0.210:42069/rdp')
  91. current_state = BTN_IDLE
  92. full_reset()