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

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