S&B Volcano vaporizer remote control with Pi Pico W
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

state_connect.py 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.iteration = 0.0
  12. self.done = False
  13. self.client = None
  14. self.connector = asyncio.create_task(self.connect(val))
  15. def exit(self):
  16. self.connector.cancel()
  17. if self.lock.locked():
  18. self.lock.release()
  19. return self.client
  20. async def progress(self, n):
  21. async with self.lock:
  22. self.iteration = n
  23. async def connect(self, d):
  24. async with self.lock:
  25. self.done = False
  26. if self.state:
  27. client = await d[0].device.connect()
  28. async with self.lock:
  29. self.step = True
  30. await cache_services_characteristics(client, self.progress)
  31. else:
  32. await d[0].disconnect()
  33. client = None
  34. async with self.lock:
  35. self.done = True
  36. self.client = (client, d[1])
  37. async def draw(self):
  38. self.lcd.text("Connecting to Bluetooth device", 0, 10, self.lcd.red)
  39. keys = self.lcd.buttons()
  40. if keys.once("y"):
  41. if self.state:
  42. return 5
  43. else:
  44. return 0
  45. async with self.lock:
  46. if self.done:
  47. if self.state:
  48. return 3
  49. else:
  50. return 0
  51. else:
  52. if self.state == False:
  53. self.lcd.textC("Disconnecting...", int(self.lcd.width / 2), int(self.lcd.height / 2), self.lcd.white)
  54. else:
  55. if self.step == False:
  56. self.lcd.textC("Connecting...", int(self.lcd.width / 2), int(self.lcd.height / 2), self.lcd.white)
  57. else:
  58. self.lcd.pie(self.lcd.width / 2, self.lcd.height / 2, self.lcd.width - 30, self.lcd.red, self.lcd.green, self.iteration)
  59. self.lcd.textC("Fetching parameters...", int(self.lcd.width / 2), int(self.lcd.height / 2), self.lcd.white)
  60. return -1