S&B Volcano vaporizer remote control with Pi Pico W
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

state_connect.py 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python
  2. import uasyncio as asyncio
  3. from poll import cache_services_characteristics
  4. from state_wait_temp import draw_graph
  5. import machine
  6. class StateConnect:
  7. def __init__(self, lcd, state):
  8. self.lcd = lcd
  9. self.state = state
  10. self.lock = asyncio.Lock()
  11. def enter(self, val = None):
  12. self.step = False
  13. self.iteration = 0
  14. self.done = False
  15. self.client = None
  16. self.connector = asyncio.create_task(self.connect(val))
  17. def exit(self):
  18. self.connector.cancel()
  19. if self.lock.locked():
  20. self.lock.release()
  21. if self.state == False:
  22. machine.soft_reset()
  23. return self.client
  24. async def progress(self, n):
  25. async with self.lock:
  26. self.iteration = n
  27. async def connect(self, d):
  28. async with self.lock:
  29. self.done = False
  30. if self.state:
  31. client = await d[0].device.connect()
  32. async with self.lock:
  33. self.step = True
  34. success = await cache_services_characteristics(client, self.progress)
  35. if not success:
  36. raise RuntimeError("Error fetching characteristics")
  37. else:
  38. await d[0].disconnect()
  39. client = None
  40. async with self.lock:
  41. self.done = True
  42. self.client = (client, d[1])
  43. async def draw(self):
  44. self.lcd.text("Connecting to Bluetooth device", 0, 10, self.lcd.red)
  45. keys = self.lcd.buttons()
  46. if keys.once("y"):
  47. if self.state:
  48. return 5
  49. else:
  50. return 0
  51. async with self.lock:
  52. if self.done:
  53. if self.state:
  54. return 3
  55. else:
  56. return 0
  57. else:
  58. if self.state == False:
  59. self.lcd.textC("Disconnecting...", int(self.lcd.width / 2), int(self.lcd.height / 2), self.lcd.white)
  60. else:
  61. if self.step == False:
  62. self.lcd.textC("Connecting...", int(self.lcd.width / 2), int(self.lcd.height / 2), self.lcd.white)
  63. else:
  64. draw_graph(self.lcd, 0, self.iteration, 10)
  65. self.lcd.textC("Fetching parameters...", int(self.lcd.width / 2), int(self.lcd.height / 2) - 10, self.lcd.white, self.lcd.black)
  66. return -1