S&B Volcano vaporizer remote control with Pi Pico W
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

state_connect.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python3
  2. # ----------------------------------------------------------------------------
  3. # Copyright (c) 2023 Thomas Buck (thomas@xythobuz.de)
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # See <http://www.gnu.org/licenses/>.
  16. # ----------------------------------------------------------------------------
  17. import uasyncio as asyncio
  18. from poll import cache_services_characteristics
  19. from state_wait_temp import draw_graph
  20. import machine
  21. class StateConnect:
  22. def __init__(self, lcd, state):
  23. self.lcd = lcd
  24. self.state = state
  25. self.lock = asyncio.Lock()
  26. def enter(self, val = None):
  27. self.step = False
  28. self.iteration = 0
  29. self.done = False
  30. self.client = None
  31. self.connector = asyncio.create_task(self.connect(val))
  32. def exit(self):
  33. self.connector.cancel()
  34. if self.lock.locked():
  35. self.lock.release()
  36. if self.state == False:
  37. machine.soft_reset()
  38. return self.client
  39. async def progress(self, n):
  40. async with self.lock:
  41. self.iteration = n
  42. async def connect(self, d):
  43. async with self.lock:
  44. self.done = False
  45. if self.state:
  46. client = await d[0].device.connect()
  47. async with self.lock:
  48. self.step = True
  49. success = await cache_services_characteristics(client, self.progress)
  50. if not success:
  51. raise RuntimeError("Error fetching characteristics")
  52. else:
  53. await d[0].disconnect()
  54. client = None
  55. async with self.lock:
  56. self.done = True
  57. self.client = (client, d[1])
  58. async def draw(self):
  59. self.lcd.text("Connecting to Bluetooth device", 0, 10, self.lcd.red)
  60. keys = self.lcd.buttons()
  61. if keys.once("y"):
  62. if self.state:
  63. return 5
  64. else:
  65. return 0
  66. async with self.lock:
  67. if self.state == False:
  68. self.lcd.textC("Disconnecting...", int(self.lcd.width / 2), int(self.lcd.height / 2), self.lcd.white)
  69. else:
  70. if self.step == False:
  71. self.lcd.textC("Connecting...", int(self.lcd.width / 2), int(self.lcd.height / 2), self.lcd.white)
  72. else:
  73. draw_graph(self.lcd, 0, self.iteration, 10)
  74. self.lcd.textC("Fetching parameters...", int(self.lcd.width / 2), int(self.lcd.height / 2) - 10, self.lcd.white, self.lcd.black)
  75. if self.done:
  76. if self.state:
  77. return 3
  78. else:
  79. return 0
  80. return -1