S&B Volcano vaporizer remote control with Pi Pico W
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

state_wait_temp.py 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env python
  2. import uasyncio as asyncio
  3. from poll import set_target_temp, get_current_temp
  4. def draw_graph(lcd, min, val, max):
  5. if max == min:
  6. lcd.text("{} -> {} -> {}".format(min, val, max), 0, int(lcd.height / 2) - 5, lcd.white)
  7. return
  8. w = lcd.width - 10
  9. ratio = (val - min) / (max - min)
  10. #wfull = int(w * ratio)
  11. #wempty = w - wfull
  12. #lcd.rect(4, int(lcd.height / 2) - 5, wfull + 1, 50, lcd.green, True)
  13. #lcd.rect(4 + wfull, int(lcd.height / 2) - 5, wempty + 2, 50, lcd.green, False)
  14. lcd.pie(lcd.width / 2, lcd.height / 2, w, lcd.red, lcd.green, ratio)
  15. lcd.text("{}".format(val), int(lcd.width / 2), 125, lcd.white)
  16. class StateWaitTemp:
  17. def __init__(self, lcd):
  18. self.lcd = lcd
  19. self.lock = asyncio.Lock()
  20. def enter(self, val = None):
  21. self.value = val
  22. self.poller = asyncio.create_task(self.poll())
  23. self.temp = 0.0
  24. self.min = 0.0
  25. self.max = 100.0
  26. def exit(self):
  27. self.poller.cancel()
  28. if self.lock.locked():
  29. self.lock.release()
  30. return (self.value[0], self.value[1], self.value[2])
  31. async def poll(self):
  32. device, workflow, index = self.value
  33. async with self.lock:
  34. self.temp = 0.0
  35. self.min = 0.0
  36. self.max = workflow["steps"][index][0]
  37. temp = await get_current_temp(device)
  38. print("initial temp: {}".format(temp))
  39. async with self.lock:
  40. self.temp = temp
  41. self.min = temp
  42. print("Setting temperature: {}".format(self.max))
  43. await set_target_temp(device, self.max)
  44. while temp < self.max:
  45. temp = await get_current_temp(device)
  46. print("now at {}".format(temp))
  47. async with self.lock:
  48. self.temp = temp
  49. async def draw(self):
  50. device, workflow, index = self.value
  51. self.lcd.text("Running Workflow - Heat {}".format(workflow["steps"][index][0]), 0, 10, self.lcd.red)
  52. keys = self.lcd.buttons()
  53. if keys.once("y"):
  54. print("user abort")
  55. return 4 # heat off
  56. async with self.lock:
  57. if self.temp == 0.0:
  58. self.lcd.text("Setting temperature...", 0, int(self.lcd.height / 2) - 5, self.lcd.white)
  59. else:
  60. draw_graph(self.lcd, self.min, self.temp, self.max)
  61. if self.temp >= self.max:
  62. print("switch, {} >= {}".format(self.temp, self.max))
  63. return 7 # wait for time
  64. return -1 # stay in this state