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

state_pump.py 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 time
  18. import uasyncio as asyncio
  19. from poll import set_state
  20. from state_wait_temp import draw_graph
  21. class StatePump:
  22. def __init__(self, lcd):
  23. self.lcd = lcd
  24. self.lock = asyncio.Lock()
  25. def enter(self, val = None):
  26. self.value = val
  27. self.done = False
  28. device, workflow, index = self.value
  29. self.start = None
  30. self.duration = workflow["steps"][index][2]
  31. self.pumper = asyncio.create_task(self.pump())
  32. def exit(self):
  33. self.pumper.cancel()
  34. if self.lock.locked():
  35. self.lock.release()
  36. return (self.value[0], self.value[1], self.value[2] + 1)
  37. async def pump(self):
  38. device, workflow, index = self.value
  39. await set_state(device, (None, True))
  40. async with self.lock:
  41. self.start = time.time()
  42. await asyncio.sleep_ms(int(self.duration * 1000))
  43. await set_state(device, (None, False))
  44. async with self.lock:
  45. self.done = True
  46. async def draw(self):
  47. device, workflow, index = self.value
  48. self.lcd.text("Running Workflow - Pump {}".format(workflow["steps"][index][2]), 0, 10, self.lcd.red)
  49. keys = self.lcd.buttons()
  50. if keys.once("y"):
  51. return 4
  52. async with self.lock:
  53. if self.start != None:
  54. now = time.time()
  55. if now - self.start <= self.duration:
  56. draw_graph(self.lcd, 0.0, now - self.start, self.duration)
  57. else:
  58. self.lcd.textC("Turning off pump...", int(self.lcd.width / 2), int(self.lcd.height / 2), self.lcd.white)
  59. else:
  60. self.lcd.textC("Turning on pump...", int(self.lcd.width / 2), int(self.lcd.height / 2), self.lcd.white)
  61. if self.done:
  62. if self.value[2] >= (len(workflow["steps"]) - 1):
  63. if workflow["notify"] != None:
  64. return 9
  65. else:
  66. return 4
  67. else:
  68. return 6
  69. return -1