S&B Volcano vaporizer remote control with Pi Pico W
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

state_connect.py 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python
  2. import uasyncio as asyncio
  3. from poll import cache_services_characteristics
  4. class StateConnect:
  5. def __init__(self, lcd, state):
  6. self.lcd = lcd
  7. self.state = state
  8. self.lock = asyncio.Lock()
  9. def enter(self, val = None):
  10. self.step = False
  11. self.done = False
  12. self.client = None
  13. self.connector = asyncio.create_task(self.connect(val))
  14. def exit(self):
  15. self.connector.cancel()
  16. if self.lock.locked():
  17. self.lock.release()
  18. return self.client
  19. async def connect(self, d):
  20. async with self.lock:
  21. self.done = False
  22. if self.state:
  23. client = await d[0].device.connect()
  24. async with self.lock:
  25. self.step = True
  26. await cache_services_characteristics(client)
  27. else:
  28. await d[0].disconnect()
  29. client = None
  30. async with self.lock:
  31. self.done = True
  32. self.client = (client, d[1])
  33. async def draw(self):
  34. self.lcd.text("Connecting to Bluetooth device", 0, 10, self.lcd.red)
  35. keys = self.lcd.buttons()
  36. if keys.once("y"):
  37. print("user abort")
  38. if self.state:
  39. return 5 # disconnect
  40. else:
  41. return 0 # scan
  42. async with self.lock:
  43. if self.done:
  44. if self.state:
  45. return 3 # heater on
  46. else:
  47. return 0 # scan
  48. else:
  49. if self.state == False:
  50. self.lcd.text("Disconnecting...", 0, int(self.lcd.height / 2) - 5, self.lcd.white)
  51. else:
  52. if self.step == False:
  53. self.lcd.text("Connecting...", 0, int(self.lcd.height / 2) - 5, self.lcd.white)
  54. else:
  55. self.lcd.text("Fetching parameters...", 0, int(self.lcd.height / 2) - 5, self.lcd.white)
  56. return -1 # stay in this state