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.

lcd.py 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. # https://www.waveshare.com/wiki/Pico-LCD-1.3
  18. # https://thepihut.com/blogs/raspberry-pi-tutorials/coding-graphics-with-micropython-on-raspberry-pi-pico-displays
  19. # https://api.arcade.academy/en/latest/_modules/arcade/draw_commands.html#draw_arc_filled
  20. from machine import Pin, SPI, PWM
  21. from array import array
  22. import framebuf
  23. import time
  24. import os
  25. import math
  26. import gc
  27. class KeyCheck:
  28. def __init__(self, new, old):
  29. self.new = new
  30. self.old = old
  31. def once(self, k):
  32. return self.new[k] and not self.old[k]
  33. def held(self, k):
  34. return self.new[k]
  35. class LCD(framebuf.FrameBuffer):
  36. def __init__(self):
  37. self.pwm = PWM(Pin(13))
  38. self.pwm.freq(1000)
  39. self.brightness(0.0)
  40. self.width = 240
  41. self.height = 240
  42. self.cs = Pin(9, Pin.OUT)
  43. self.rst = Pin(12, Pin.OUT)
  44. self.cs(1)
  45. self.spi = SPI(1, 100_000_000, polarity=0, phase=0, sck=Pin(10), mosi=Pin(11), miso=None)
  46. self.dc = Pin(8, Pin.OUT)
  47. self.dc(1)
  48. gc.collect()
  49. self.buffer = bytearray(self.height * self.width * 2)
  50. super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
  51. self.init_display()
  52. self.red = self.color(0xFF, 0x00, 0x00)
  53. self.green = self.color(0x00, 0xFF, 0x00)
  54. self.blue = self.color(0x00, 0x00, 0xFF)
  55. self.yellow = self.color(0xFF, 0xFF, 0x00)
  56. self.white = self.color(0xFF, 0xFF, 0xFF)
  57. self.black = self.color(0x00, 0x00, 0x00)
  58. self.fill(self.black)
  59. self.show()
  60. self.keyA = Pin(15, Pin.IN, Pin.PULL_UP)
  61. self.keyB = Pin(17, Pin.IN, Pin.PULL_UP)
  62. self.keyX = Pin(19, Pin.IN, Pin.PULL_UP)
  63. self.keyY = Pin(21, Pin.IN, Pin.PULL_UP)
  64. self.up = Pin( 2, Pin.IN, Pin.PULL_UP)
  65. self.down = Pin(18, Pin.IN, Pin.PULL_UP)
  66. self.left = Pin(16, Pin.IN, Pin.PULL_UP)
  67. self.right = Pin(20, Pin.IN, Pin.PULL_UP)
  68. self.ctrl = Pin( 3, Pin.IN, Pin.PULL_UP)
  69. self.keys_old = {
  70. "a": False,
  71. "b": False,
  72. "x": False,
  73. "y": False,
  74. "up": False,
  75. "down": False,
  76. "left": False,
  77. "right": False,
  78. "enter": False,
  79. }
  80. self.curr_brightness = 0.0
  81. try:
  82. with open("_cur_bright.txt", "r") as f:
  83. s = next(f).split()[0]
  84. self.curr_brightness = float(s)
  85. except:
  86. pass
  87. self.brightness(self.curr_brightness)
  88. def buttons(self):
  89. keys = {
  90. "a": self.keyA.value() == 0,
  91. "b": self.keyB.value() == 0,
  92. "x": self.keyX.value() == 0,
  93. "y": self.keyY.value() == 0,
  94. "up": self.up.value() == 0,
  95. "down": self.down.value() == 0,
  96. "left": self.left.value() == 0,
  97. "right": self.right.value() == 0,
  98. "enter": self.ctrl.value() == 0,
  99. }
  100. kc = KeyCheck(keys, self.keys_old)
  101. self.keys_old = keys.copy()
  102. return kc
  103. # Convert RGB888 to RGB565
  104. def color(self, R, G, B):
  105. return (((G & 0b00011100) << 3) + ((B & 0b11111000) >> 3) << 8) + (R & 0b11111000) + ((G & 0b11100000) >> 5)
  106. def store_brightness(self):
  107. old = -1.0
  108. try:
  109. with open("_cur_bright.txt", "r") as f:
  110. s = next(f).split()[0]
  111. old = float(s)
  112. except:
  113. pass
  114. if self.curr_brightness != old:
  115. with open("_cur_bright.txt", "w") as f:
  116. s = "{}\n".format(self.curr_brightness)
  117. f.write(s)
  118. def brightness(self, v):
  119. if v < 0.0:
  120. v = 0.0
  121. if v > 1.0:
  122. v = 1.0
  123. self.curr_brightness = v
  124. self.pwm.duty_u16(int(v * 65535))
  125. def write_cmd(self, cmd):
  126. self.cs(1)
  127. self.dc(0)
  128. self.cs(0)
  129. self.spi.write(bytearray([cmd]))
  130. self.cs(1)
  131. def write_data(self, buf):
  132. self.cs(1)
  133. self.dc(1)
  134. self.cs(0)
  135. self.spi.write(bytearray([buf]))
  136. self.cs(1)
  137. def init_display(self):
  138. self.rst(1)
  139. self.rst(0)
  140. self.rst(1)
  141. self.write_cmd(0x36)
  142. self.write_data(0x70)
  143. self.write_cmd(0x3A)
  144. self.write_data(0x05)
  145. self.write_cmd(0xB2)
  146. self.write_data(0x0C)
  147. self.write_data(0x0C)
  148. self.write_data(0x00)
  149. self.write_data(0x33)
  150. self.write_data(0x33)
  151. self.write_cmd(0xB7)
  152. self.write_data(0x35)
  153. self.write_cmd(0xBB)
  154. self.write_data(0x19)
  155. self.write_cmd(0xC0)
  156. self.write_data(0x2C)
  157. self.write_cmd(0xC2)
  158. self.write_data(0x01)
  159. self.write_cmd(0xC3)
  160. self.write_data(0x12)
  161. self.write_cmd(0xC4)
  162. self.write_data(0x20)
  163. self.write_cmd(0xC6)
  164. self.write_data(0x0F)
  165. self.write_cmd(0xD0)
  166. self.write_data(0xA4)
  167. self.write_data(0xA1)
  168. self.write_cmd(0xE0)
  169. self.write_data(0xD0)
  170. self.write_data(0x04)
  171. self.write_data(0x0D)
  172. self.write_data(0x11)
  173. self.write_data(0x13)
  174. self.write_data(0x2B)
  175. self.write_data(0x3F)
  176. self.write_data(0x54)
  177. self.write_data(0x4C)
  178. self.write_data(0x18)
  179. self.write_data(0x0D)
  180. self.write_data(0x0B)
  181. self.write_data(0x1F)
  182. self.write_data(0x23)
  183. self.write_cmd(0xE1)
  184. self.write_data(0xD0)
  185. self.write_data(0x04)
  186. self.write_data(0x0C)
  187. self.write_data(0x11)
  188. self.write_data(0x13)
  189. self.write_data(0x2C)
  190. self.write_data(0x3F)
  191. self.write_data(0x44)
  192. self.write_data(0x51)
  193. self.write_data(0x2F)
  194. self.write_data(0x1F)
  195. self.write_data(0x1F)
  196. self.write_data(0x20)
  197. self.write_data(0x23)
  198. self.write_cmd(0x21)
  199. self.write_cmd(0x11)
  200. self.write_cmd(0x29)
  201. def show(self):
  202. self.write_cmd(0x2A)
  203. self.write_data(0x00)
  204. self.write_data(0x00)
  205. self.write_data(0x00)
  206. self.write_data(0xef)
  207. self.write_cmd(0x2B)
  208. self.write_data(0x00)
  209. self.write_data(0x00)
  210. self.write_data(0x00)
  211. self.write_data(0xEF)
  212. self.write_cmd(0x2C)
  213. self.cs(1)
  214. self.dc(1)
  215. self.cs(0)
  216. self.spi.write(self.buffer)
  217. self.cs(1)
  218. def circle(self, x, y, r, c):
  219. self.hline(x-r,y,r*2,c)
  220. for i in range(1,r):
  221. a = int(math.sqrt(r*r-i*i)) # Pythagoras!
  222. self.hline(x-a,y+i,a*2,c) # Lower half
  223. self.hline(x-a,y-i,a*2,c) # Upper half
  224. def ring(self, x, y, r, c):
  225. self.pixel(x-r,y,c)
  226. self.pixel(x+r,y,c)
  227. self.pixel(x,y-r,c)
  228. self.pixel(x,y+r,c)
  229. for i in range(1, r):
  230. a = int(math.sqrt(r*r-i*i))
  231. self.pixel(x-a,y-i,c)
  232. self.pixel(x+a,y-i,c)
  233. self.pixel(x-a,y+i,c)
  234. self.pixel(x+a,y+i,c)
  235. self.pixel(x-i,y-a,c)
  236. self.pixel(x+i,y-a,c)
  237. self.pixel(x-i,y+a,c)
  238. self.pixel(x+i,y+a,c)
  239. def arc(self, x_off, y_off, w, h, c,
  240. start_angle, end_angle,
  241. filled = True,
  242. num_segments = 64):
  243. start_segment = int(start_angle / 360 * num_segments)
  244. end_segment = int(end_angle / 360 * num_segments)
  245. gc.collect()
  246. point_list = array('h')
  247. point_list.append(0)
  248. point_list.append(0)
  249. for segment in range(start_segment, end_segment + 1):
  250. theta = 2.0 * 3.1415926 * segment / num_segments
  251. x = w * math.cos(theta) / 2
  252. y = h * math.sin(theta) / 2
  253. i = (segment - start_segment + 1) * 2
  254. point_list.append(int(x))
  255. point_list.append(int(y))
  256. self.poly(int(x_off), int(y_off), point_list, c, filled)
  257. def pie(self, x0, y0, w, c_border, c_circle, v):
  258. if v > 0.0:
  259. self.arc(int(x0), int(y0), int(w), int(w), c_circle, -90, int(v * 360) - 90)
  260. self.ring(int(x0), int(y0), int(w / 2), c_border)
  261. def textC(self, s, x, y, c, bgColor = None):
  262. xStart = x - int(len(s) * 8 / 2)
  263. yStart = y - 5
  264. if bgColor != None:
  265. self.rect(xStart, yStart - 1, len(s) * 8, 10, bgColor, True)
  266. self.text(s, xStart, yStart, c)
  267. def textLine(self, s, c, off = 0):
  268. charsPerLine = int(self.width / 8)
  269. lines = list(s[0+i:charsPerLine+i] for i in range(0, len(s), charsPerLine))
  270. n = 0
  271. for i, l in enumerate(lines):
  272. self.text(l, 0, (i + off) * 10, c)
  273. n += 1
  274. if i >= (self.height / 10):
  275. break
  276. return n
  277. def textBlock(self, s, c):
  278. lines = s.split("\n")
  279. off = 0
  280. for l in lines:
  281. off += self.textLine(l, c, off)