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 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. class LCD(framebuf.FrameBuffer):
  34. def __init__(self):
  35. self.pwm = PWM(Pin(13))
  36. self.pwm.freq(1000)
  37. self.brightness(0.0)
  38. self.width = 240
  39. self.height = 240
  40. self.cs = Pin(9, Pin.OUT)
  41. self.rst = Pin(12, Pin.OUT)
  42. self.cs(1)
  43. #self.spi = SPI(1)
  44. #self.spi = SPI(1, 1_000_000)
  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. def buttons(self):
  81. keys = {
  82. "a": self.keyA.value() == 0,
  83. "b": self.keyB.value() == 0,
  84. "x": self.keyX.value() == 0,
  85. "y": self.keyY.value() == 0,
  86. "up": self.up.value() == 0,
  87. "down": self.down.value() == 0,
  88. "left": self.left.value() == 0,
  89. "right": self.right.value() == 0,
  90. "enter": self.ctrl.value() == 0,
  91. }
  92. kc = KeyCheck(keys, self.keys_old)
  93. self.keys_old = keys.copy()
  94. return kc
  95. # Convert RGB888 to RGB565
  96. def color(self, R, G, B):
  97. return (((G & 0b00011100) << 3) + ((B & 0b11111000) >> 3) << 8) + (R & 0b11111000) + ((G & 0b11100000) >> 5)
  98. def brightness(self, v):
  99. self.pwm.duty_u16(int(v * 65535))
  100. def write_cmd(self, cmd):
  101. self.cs(1)
  102. self.dc(0)
  103. self.cs(0)
  104. self.spi.write(bytearray([cmd]))
  105. self.cs(1)
  106. def write_data(self, buf):
  107. self.cs(1)
  108. self.dc(1)
  109. self.cs(0)
  110. self.spi.write(bytearray([buf]))
  111. self.cs(1)
  112. def init_display(self):
  113. self.rst(1)
  114. self.rst(0)
  115. self.rst(1)
  116. self.write_cmd(0x36)
  117. self.write_data(0x70)
  118. self.write_cmd(0x3A)
  119. self.write_data(0x05)
  120. self.write_cmd(0xB2)
  121. self.write_data(0x0C)
  122. self.write_data(0x0C)
  123. self.write_data(0x00)
  124. self.write_data(0x33)
  125. self.write_data(0x33)
  126. self.write_cmd(0xB7)
  127. self.write_data(0x35)
  128. self.write_cmd(0xBB)
  129. self.write_data(0x19)
  130. self.write_cmd(0xC0)
  131. self.write_data(0x2C)
  132. self.write_cmd(0xC2)
  133. self.write_data(0x01)
  134. self.write_cmd(0xC3)
  135. self.write_data(0x12)
  136. self.write_cmd(0xC4)
  137. self.write_data(0x20)
  138. self.write_cmd(0xC6)
  139. self.write_data(0x0F)
  140. self.write_cmd(0xD0)
  141. self.write_data(0xA4)
  142. self.write_data(0xA1)
  143. self.write_cmd(0xE0)
  144. self.write_data(0xD0)
  145. self.write_data(0x04)
  146. self.write_data(0x0D)
  147. self.write_data(0x11)
  148. self.write_data(0x13)
  149. self.write_data(0x2B)
  150. self.write_data(0x3F)
  151. self.write_data(0x54)
  152. self.write_data(0x4C)
  153. self.write_data(0x18)
  154. self.write_data(0x0D)
  155. self.write_data(0x0B)
  156. self.write_data(0x1F)
  157. self.write_data(0x23)
  158. self.write_cmd(0xE1)
  159. self.write_data(0xD0)
  160. self.write_data(0x04)
  161. self.write_data(0x0C)
  162. self.write_data(0x11)
  163. self.write_data(0x13)
  164. self.write_data(0x2C)
  165. self.write_data(0x3F)
  166. self.write_data(0x44)
  167. self.write_data(0x51)
  168. self.write_data(0x2F)
  169. self.write_data(0x1F)
  170. self.write_data(0x1F)
  171. self.write_data(0x20)
  172. self.write_data(0x23)
  173. self.write_cmd(0x21)
  174. self.write_cmd(0x11)
  175. self.write_cmd(0x29)
  176. def show(self):
  177. self.write_cmd(0x2A)
  178. self.write_data(0x00)
  179. self.write_data(0x00)
  180. self.write_data(0x00)
  181. self.write_data(0xef)
  182. self.write_cmd(0x2B)
  183. self.write_data(0x00)
  184. self.write_data(0x00)
  185. self.write_data(0x00)
  186. self.write_data(0xEF)
  187. self.write_cmd(0x2C)
  188. self.cs(1)
  189. self.dc(1)
  190. self.cs(0)
  191. self.spi.write(self.buffer)
  192. self.cs(1)
  193. def circle(self, x, y, r, c):
  194. self.hline(x-r,y,r*2,c)
  195. for i in range(1,r):
  196. a = int(math.sqrt(r*r-i*i)) # Pythagoras!
  197. self.hline(x-a,y+i,a*2,c) # Lower half
  198. self.hline(x-a,y-i,a*2,c) # Upper half
  199. def ring(self, x, y, r, c):
  200. self.pixel(x-r,y,c)
  201. self.pixel(x+r,y,c)
  202. self.pixel(x,y-r,c)
  203. self.pixel(x,y+r,c)
  204. for i in range(1, r):
  205. a = int(math.sqrt(r*r-i*i))
  206. self.pixel(x-a,y-i,c)
  207. self.pixel(x+a,y-i,c)
  208. self.pixel(x-a,y+i,c)
  209. self.pixel(x+a,y+i,c)
  210. self.pixel(x-i,y-a,c)
  211. self.pixel(x+i,y-a,c)
  212. self.pixel(x-i,y+a,c)
  213. self.pixel(x+i,y+a,c)
  214. def arc(self, x_off, y_off, w, h, c,
  215. start_angle, end_angle,
  216. filled = True,
  217. num_segments = 64):
  218. start_segment = int(start_angle / 360 * num_segments)
  219. end_segment = int(end_angle / 360 * num_segments)
  220. gc.collect()
  221. point_list = array('h')
  222. point_list.append(0)
  223. point_list.append(0)
  224. for segment in range(start_segment, end_segment + 1):
  225. theta = 2.0 * 3.1415926 * segment / num_segments
  226. x = w * math.cos(theta) / 2
  227. y = h * math.sin(theta) / 2
  228. i = (segment - start_segment + 1) * 2
  229. point_list.append(int(x))
  230. point_list.append(int(y))
  231. self.poly(int(x_off), int(y_off), point_list, c, filled)
  232. def pie(self, x0, y0, w, c_border, c_circle, v):
  233. if v > 0.0:
  234. self.arc(int(x0), int(y0), int(w), int(w), c_circle, -90, int(v * 360) - 90)
  235. self.ring(int(x0), int(y0), int(w / 2), c_border)
  236. def textC(self, s, x, y, c, bgColor = None):
  237. xStart = x - int(len(s) * 8 / 2)
  238. yStart = y - 5
  239. if bgColor != None:
  240. self.rect(xStart, yStart - 1, len(s) * 8, 10, bgColor, True)
  241. self.text(s, xStart, yStart, c)
  242. def textLine(self, s, c, off = 0):
  243. charsPerLine = int(self.width / 8)
  244. lines = list(s[0+i:charsPerLine+i] for i in range(0, len(s), charsPerLine))
  245. n = 0
  246. for i, l in enumerate(lines):
  247. self.text(l, 0, (i + off) * 10, c)
  248. n += 1
  249. if i >= (self.height / 10):
  250. break
  251. return n
  252. def textBlock(self, s, c):
  253. lines = s.split("\n")
  254. off = 0
  255. for l in lines:
  256. off += self.textLine(l, c, off)