S&B Volcano vaporizer remote control with Pi Pico W
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

state_connect.py 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.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. await cache_services_characteristics(client, self.progress)
  35. else:
  36. await d[0].disconnect()
  37. client = None
  38. async with self.lock:
  39. self.done = True
  40. self.client = (client, d[1])
  41. async def draw(self):
  42. self.lcd.text("Connecting to Bluetooth device", 0, 10, self.lcd.red)
  43. keys = self.lcd.buttons()
  44. if keys.once("y"):
  45. if self.state:
  46. return 5
  47. else:
  48. return 0
  49. async with self.lock:
  50. if self.done:
  51. if self.state:
  52. return 3
  53. else:
  54. return 0
  55. else:
  56. if self.state == False:
  57. self.lcd.textC("Disconnecting...", int(self.lcd.width / 2), int(self.lcd.height / 2), self.lcd.white)
  58. else:
  59. if self.step == False:
  60. self.lcd.textC("Connecting...", int(self.lcd.width / 2), int(self.lcd.height / 2), self.lcd.white)
  61. else:
  62. draw_graph(self.lcd, 0, int(self.iteration * 10), 10)
  63. self.lcd.textC("Fetching parameters...", int(self.lcd.width / 2), int(self.lcd.height / 2) - 10, self.lcd.white)
  64. return -1