Advertisement
Guest User

Untitled

a guest
Nov 17th, 2022
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 41.96 KB | None | 0 0
  1. main.py
  2. -----------------------------------------------------------------------
  3.  
  4. import machine
  5. from machine import Pin
  6. import st7789py as st7789
  7. from fonts import vga2_8x8 as font1
  8. from fonts import vga1_16x32 as font2
  9. import random
  10. import time
  11. import framebuf
  12.  
  13.  
  14. spi1_sck=18
  15. spi1_mosi=19
  16. st7789_res = 16
  17. st7789_dc = 17
  18. disp_width = 240
  19. disp_height = 240
  20. CENTER_Y = int(disp_width/2)
  21. CENTER_X = int(disp_height/2)
  22.  
  23. spi = machine.SPI(0, baudrate=40000000, polarity=1)
  24. display = st7789.ST7789(spi, disp_width, disp_width,
  25. reset=machine.Pin(st7789_res, machine.Pin.OUT),
  26. dc=machine.Pin(st7789_dc, machine.Pin.OUT),
  27. xstart=0, ystart=0, rotation=1)
  28.  
  29. upButton = Pin(14, Pin.IN, Pin.PULL_DOWN)
  30. rightButton = Pin(12, Pin.IN, Pin.PULL_DOWN)
  31. downButton = Pin(13, Pin.IN, Pin.PULL_DOWN)
  32. leftButton = Pin(11, Pin.IN, Pin.PULL_DOWN)
  33.  
  34. pixelRatio = 40
  35. pixelSize = 240 / pixelRatio
  36.  
  37. imageToDisplay = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  38. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  39. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  40. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  41. 0,0,0,0,0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E)
  80.  
  81.  
  82. def updateScreen():
  83.  
  84. currentPixelX = 0
  85. currentPixelY = 0
  86.  
  87.  
  88. for i in range(pixelRatio*pixelRatio):
  89. if(imageToDisplay[i] == 1):
  90. display.fill_rect(currentPixelX, currentPixelY, int(pixelSize), int(pixelSize), 0xFFFFFF)
  91. if(imageToDisplay[i] == 0):
  92. display.fill_rect(currentPixelX, currentPixelY, int(pixelSize), int(pixelSize), 0x000000)
  93.  
  94.  
  95. currentPixelX += int(pixelSize)
  96.  
  97. if(currentPixelX == 240):
  98. currentPixelX = 0
  99. currentPixelY += int(pixelSize)
  100.  
  101. while True:
  102. updateScreen()
  103.  
  104.  
  105.  
  106. ------------------------------------------------------------------------------------------------------------------------
  107. st7789py.py
  108. ------------------------------------------------------------------------------------------------------------------------
  109. """
  110. Copyright (c) 2020, 2021 Russ Hughes
  111.  
  112. This file incorporates work covered by the following copyright and
  113. permission notice and is licensed under the same terms:
  114.  
  115. The MIT License (MIT)
  116.  
  117. Copyright (c) 2019 Ivan Belokobylskiy
  118.  
  119. Permission is hereby granted, free of charge, to any person obtaining a copy
  120. of this software and associated documentation files (the "Software"), to deal
  121. in the Software without restriction, including without limitation the rights
  122. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  123. copies of the Software, and to permit persons to whom the Software is
  124. furnished to do so, subject to the following conditions:
  125.  
  126. The above copyright notice and this permission notice shall be included in
  127. all copies or substantial portions of the Software.
  128.  
  129. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  130. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  131. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  132. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  133. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  134. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  135. THE SOFTWARE.
  136.  
  137. The driver is based on devbis' st7789py_mpy module from
  138. https://github.com/devbis/st7789py_mpy.
  139.  
  140. This driver adds support for:
  141.  
  142. - 320x240, 240x240 and 135x240 pixel displays
  143. - Display rotation
  144. - Hardware based scrolling
  145. - Drawing text using 8 and 16 bit wide bitmap fonts with heights that are
  146. multiples of 8. Included are 12 bitmap fonts derived from classic pc
  147. BIOS text mode fonts.
  148. - Drawing text using converted TrueType fonts.
  149. - Drawing converted bitmaps
  150.  
  151. """
  152.  
  153. import time
  154. from micropython import const
  155. import ustruct as struct
  156.  
  157. # commands
  158. ST7789_NOP = const(0x00)
  159. ST7789_SWRESET = const(0x01)
  160. ST7789_RDDID = const(0x04)
  161. ST7789_RDDST = const(0x09)
  162.  
  163. ST7789_SLPIN = const(0x10)
  164. ST7789_SLPOUT = const(0x11)
  165. ST7789_PTLON = const(0x12)
  166. ST7789_NORON = const(0x13)
  167.  
  168. ST7789_INVOFF = const(0x20)
  169. ST7789_INVON = const(0x21)
  170. ST7789_DISPOFF = const(0x28)
  171. ST7789_DISPON = const(0x29)
  172. ST7789_CASET = const(0x2A)
  173. ST7789_RASET = const(0x2B)
  174. ST7789_RAMWR = const(0x2C)
  175. ST7789_RAMRD = const(0x2E)
  176.  
  177. ST7789_PTLAR = const(0x30)
  178. ST7789_VSCRDEF = const(0x33)
  179. ST7789_COLMOD = const(0x3A)
  180. ST7789_MADCTL = const(0x36)
  181. ST7789_VSCSAD = const(0x37)
  182.  
  183. ST7789_MADCTL_MY = const(0x80)
  184. ST7789_MADCTL_MX = const(0x40)
  185. ST7789_MADCTL_MV = const(0x20)
  186. ST7789_MADCTL_ML = const(0x10)
  187. ST7789_MADCTL_BGR = const(0x08)
  188. ST7789_MADCTL_MH = const(0x04)
  189. ST7789_MADCTL_RGB = const(0x00)
  190.  
  191. ST7789_RDID1 = const(0xDA)
  192. ST7789_RDID2 = const(0xDB)
  193. ST7789_RDID3 = const(0xDC)
  194. ST7789_RDID4 = const(0xDD)
  195.  
  196. COLOR_MODE_65K = const(0x50)
  197. COLOR_MODE_262K = const(0x60)
  198. COLOR_MODE_12BIT = const(0x03)
  199. COLOR_MODE_16BIT = const(0x05)
  200. COLOR_MODE_18BIT = const(0x06)
  201. COLOR_MODE_16M = const(0x07)
  202.  
  203. # Color definitions
  204. BLACK = const(0x0000)
  205. BLUE = const(0x001F)
  206. RED = const(0xF800)
  207. GREEN = const(0x07E0)
  208. CYAN = const(0x07FF)
  209. MAGENTA = const(0xF81F)
  210. YELLOW = const(0xFFE0)
  211. WHITE = const(0xFFFF)
  212.  
  213. _ENCODE_PIXEL = ">H"
  214. _ENCODE_POS = ">HH"
  215. _DECODE_PIXEL = ">BBB"
  216.  
  217. _BUFFER_SIZE = const(256)
  218.  
  219. _BIT7 = const(0x80)
  220. _BIT6 = const(0x40)
  221. _BIT5 = const(0x20)
  222. _BIT4 = const(0x10)
  223. _BIT3 = const(0x08)
  224. _BIT2 = const(0x04)
  225. _BIT1 = const(0x02)
  226. _BIT0 = const(0x01)
  227.  
  228. # Rotation tables (width, height, xstart, ystart)[rotation % 4]
  229.  
  230. WIDTH_320 = [(240, 320, 0, 0),
  231. (320, 240, 0, 0),
  232. (240, 320, 0, 0),
  233. (320, 240, 0, 0)]
  234.  
  235. WIDTH_240 = [(240, 240, 0, 0),
  236. (240, 240, 0, 0),
  237. (240, 240, 0, 80),
  238. (240, 240, 80, 0)]
  239.  
  240. WIDTH_135 = [(135, 240, 52, 40),
  241. (240, 135, 40, 53),
  242. (135, 240, 53, 40),
  243. (240, 135, 40, 52)]
  244.  
  245. # MADCTL ROTATIONS[rotation % 4]
  246. ROTATIONS = [0x00, 0x60, 0xc0, 0xa0]
  247.  
  248.  
  249. def color565(red, green=0, blue=0):
  250. """
  251. Convert red, green and blue values (0-255) into a 16-bit 565 encoding.
  252. """
  253. try:
  254. red, green, blue = red # see if the first var is a tuple/list
  255. except TypeError:
  256. pass
  257. return (red & 0xf8) << 8 | (green & 0xfc) << 3 | blue >> 3
  258.  
  259.  
  260. def _encode_pos(x, y):
  261. """Encode a postion into bytes."""
  262. return struct.pack(_ENCODE_POS, x, y)
  263.  
  264.  
  265. def _encode_pixel(color):
  266. """Encode a pixel color into bytes."""
  267. return struct.pack(_ENCODE_PIXEL, color)
  268.  
  269.  
  270. class ST7789():
  271. """
  272. ST7789 driver class
  273.  
  274. Args:
  275. spi (spi): spi object **Required**
  276. width (int): display width **Required**
  277. height (int): display height **Required**
  278. reset (pin): reset pin
  279. dc (pin): dc pin **Required**
  280. cs (pin): cs pin
  281. backlight(pin): backlight pin
  282. rotation (int): display rotation
  283. - 0-Portrait
  284. - 1-Landscape
  285. - 2-Inverted Portrait
  286. - 3-Inverted Landscape
  287. """
  288. def __init__(self, spi, width, height, reset=None, dc=None,
  289. cs=None, backlight=None, xstart=-1, ystart =-1, rotation=0):
  290. """
  291. Initialize display.
  292. """
  293. if height != 240 or width not in [320, 240, 135]:
  294. raise ValueError(
  295. "Unsupported display. 320x240, 240x240 and 135x240 are supported."
  296. )
  297.  
  298. if dc is None:
  299. raise ValueError("dc pin is required.")
  300.  
  301. self._display_width = self.width = width
  302. self._display_height = self.height = height
  303. self.xstart = 0
  304. self.ystart = 0
  305. self.spi = spi
  306. self.reset = reset
  307. self.dc = dc
  308. self.cs = cs
  309. self.backlight = backlight
  310. self._rotation = rotation % 4
  311.  
  312. self.xstart = xstart
  313. self.ystart = ystart
  314.  
  315. self.hard_reset()
  316. self.soft_reset()
  317. self.sleep_mode(False)
  318.  
  319. self._set_color_mode(COLOR_MODE_65K | COLOR_MODE_16BIT)
  320. time.sleep_ms(50)
  321. self.rotation(self._rotation)
  322. self.inversion_mode(True)
  323. time.sleep_ms(10)
  324. self._write(ST7789_NORON)
  325. time.sleep_ms(10)
  326. if backlight is not None:
  327. backlight.value(1)
  328. self.fill(0)
  329. self._write(ST7789_DISPON)
  330. time.sleep_ms(500)
  331.  
  332. def _write(self, command=None, data=None):
  333. """SPI write to the device: commands and data."""
  334. if self.cs:
  335. self.cs.off()
  336.  
  337. if command is not None:
  338. self.dc.off()
  339. self.spi.write(bytes([command]))
  340. if data is not None:
  341. self.dc.on()
  342. self.spi.write(data)
  343. if self.cs:
  344. self.cs.on()
  345.  
  346. def hard_reset(self):
  347. """
  348. Hard reset display.
  349. """
  350. if self.cs:
  351. self.cs.off()
  352. if self.reset:
  353. self.reset.on()
  354. time.sleep_ms(50)
  355. if self.reset:
  356. self.reset.off()
  357. time.sleep_ms(50)
  358. if self.reset:
  359. self.reset.on()
  360. time.sleep_ms(150)
  361. if self.cs:
  362. self.cs.on()
  363.  
  364. def soft_reset(self):
  365. """
  366. Soft reset display.
  367. """
  368. self._write(ST7789_SWRESET)
  369. time.sleep_ms(150)
  370.  
  371. def sleep_mode(self, value):
  372. """
  373. Enable or disable display sleep mode.
  374.  
  375. Args:
  376. value (bool): if True enable sleep mode. if False disable sleep
  377. mode
  378. """
  379. if value:
  380. self._write(ST7789_SLPIN)
  381. else:
  382. self._write(ST7789_SLPOUT)
  383.  
  384. def inversion_mode(self, value):
  385. """
  386. Enable or disable display inversion mode.
  387.  
  388. Args:
  389. value (bool): if True enable inversion mode. if False disable
  390. inversion mode
  391. """
  392. if value:
  393. self._write(ST7789_INVON)
  394. else:
  395. self._write(ST7789_INVOFF)
  396.  
  397. def _set_color_mode(self, mode):
  398. """
  399. Set display color mode.
  400.  
  401. Args:
  402. mode (int): color mode
  403. COLOR_MODE_65K, COLOR_MODE_262K, COLOR_MODE_12BIT,
  404. COLOR_MODE_16BIT, COLOR_MODE_18BIT, COLOR_MODE_16M
  405. """
  406. self._write(ST7789_COLMOD, bytes([mode & 0x77]))
  407.  
  408. def rotation(self, rotation):
  409. """
  410. Set display rotation.
  411.  
  412. Args:
  413. rotation (int):
  414. - 0-Portrait
  415. - 1-Landscape
  416. - 2-Inverted Portrait
  417. - 3-Inverted Landscape
  418. """
  419.  
  420. rotation %= 4
  421. self._rotation = rotation
  422. madctl = ROTATIONS[rotation]
  423.  
  424. if self._display_width == 320:
  425. table = WIDTH_320
  426. elif self._display_width == 240:
  427. table = WIDTH_240
  428. elif self._display_width == 135:
  429. table = WIDTH_135
  430. else:
  431. raise ValueError(
  432. "Unsupported display. 320x240, 240x240 and 135x240 are supported."
  433. )
  434.  
  435. self.width, self.height, self.xstart, self.ystart = table[rotation]
  436. self._write(ST7789_MADCTL, bytes([madctl]))
  437.  
  438. def _set_columns(self, start, end):
  439. """
  440. Send CASET (column address set) command to display.
  441.  
  442. Args:
  443. start (int): column start address
  444. end (int): column end address
  445. """
  446. if start <= end <= self.width:
  447. self._write(ST7789_CASET, _encode_pos(
  448. start+self.xstart, end + self.xstart))
  449.  
  450. def _set_rows(self, start, end):
  451. """
  452. Send RASET (row address set) command to display.
  453.  
  454. Args:
  455. start (int): row start address
  456. end (int): row end address
  457. """
  458. if start <= end <= self.height:
  459. self._write(ST7789_RASET, _encode_pos(
  460. start+self.ystart, end+self.ystart))
  461.  
  462. def _set_window(self, x0, y0, x1, y1):
  463. """
  464. Set window to column and row address.
  465.  
  466. Args:
  467. x0 (int): column start address
  468. y0 (int): row start address
  469. x1 (int): column end address
  470. y1 (int): row end address
  471. """
  472. self._set_columns(x0, x1)
  473. self._set_rows(y0, y1)
  474. self._write(ST7789_RAMWR)
  475.  
  476. def vline(self, x, y, length, color):
  477. """
  478. Draw vertical line at the given location and color.
  479.  
  480. Args:
  481. x (int): x coordinate
  482. Y (int): y coordinate
  483. length (int): length of line
  484. color (int): 565 encoded color
  485. """
  486. self.fill_rect(x, y, 1, length, color)
  487.  
  488. def hline(self, x, y, length, color):
  489. """
  490. Draw horizontal line at the given location and color.
  491.  
  492. Args:
  493. x (int): x coordinate
  494. Y (int): y coordinate
  495. length (int): length of line
  496. color (int): 565 encoded color
  497. """
  498. self.fill_rect(x, y, length, 1, color)
  499.  
  500. def pixel(self, x, y, color):
  501. """
  502. Draw a pixel at the given location and color.
  503.  
  504. Args:
  505. x (int): x coordinate
  506. Y (int): y coordinate
  507. color (int): 565 encoded color
  508. """
  509. self._set_window(x, y, x, y)
  510. self._write(None, _encode_pixel(color))
  511.  
  512. def blit_buffer(self, buffer, x, y, width, height):
  513. """
  514. Copy buffer to display at the given location.
  515.  
  516. Args:
  517. buffer (bytes): Data to copy to display
  518. x (int): Top left corner x coordinate
  519. Y (int): Top left corner y coordinate
  520. width (int): Width
  521. height (int): Height
  522. """
  523. self._set_window(x, y, x + width - 1, y + height - 1)
  524. self._write(None, buffer)
  525.  
  526. def rect(self, x, y, w, h, color):
  527. """
  528. Draw a rectangle at the given location, size and color.
  529.  
  530. Args:
  531. x (int): Top left corner x coordinate
  532. y (int): Top left corner y coordinate
  533. width (int): Width in pixels
  534. height (int): Height in pixels
  535. color (int): 565 encoded color
  536. """
  537. self.hline(x, y, w, color)
  538. self.vline(x, y, h, color)
  539. self.vline(x + w - 1, y, h, color)
  540. self.hline(x, y + h - 1, w, color)
  541.  
  542. def fill_rect(self, x, y, width, height, color):
  543. """
  544. Draw a rectangle at the given location, size and filled with color.
  545.  
  546. Args:
  547. x (int): Top left corner x coordinate
  548. y (int): Top left corner y coordinate
  549. width (int): Width in pixels
  550. height (int): Height in pixels
  551. color (int): 565 encoded color
  552. """
  553. self._set_window(x, y, x + width - 1, y + height - 1)
  554. chunks, rest = divmod(width * height, _BUFFER_SIZE)
  555. pixel = _encode_pixel(color)
  556. self.dc.on()
  557. if chunks:
  558. data = pixel * _BUFFER_SIZE
  559. for _ in range(chunks):
  560. self._write(None, data)
  561. if rest:
  562. self._write(None, pixel * rest)
  563.  
  564. def fill(self, color):
  565. """
  566. Fill the entire FrameBuffer with the specified color.
  567.  
  568. Args:
  569. color (int): 565 encoded color
  570. """
  571. self.fill_rect(0, 0, self.width, self.height, color)
  572.  
  573. def line(self, x0, y0, x1, y1, color):
  574. """
  575. Draw a single pixel wide line starting at x0, y0 and ending at x1, y1.
  576.  
  577. Args:
  578. x0 (int): Start point x coordinate
  579. y0 (int): Start point y coordinate
  580. x1 (int): End point x coordinate
  581. y1 (int): End point y coordinate
  582. color (int): 565 encoded color
  583. """
  584. steep = abs(y1 - y0) > abs(x1 - x0)
  585. if steep:
  586. x0, y0 = y0, x0
  587. x1, y1 = y1, x1
  588. if x0 > x1:
  589. x0, x1 = x1, x0
  590. y0, y1 = y1, y0
  591. dx = x1 - x0
  592. dy = abs(y1 - y0)
  593. err = dx // 2
  594. ystep = 1 if y0 < y1 else -1
  595. while x0 <= x1:
  596. if steep:
  597. self.pixel(y0, x0, color)
  598. else:
  599. self.pixel(x0, y0, color)
  600. err -= dy
  601. if err < 0:
  602. y0 += ystep
  603. err += dx
  604. x0 += 1
  605.  
  606. def vscrdef(self, tfa, vsa, bfa):
  607. """
  608. Set Vertical Scrolling Definition.
  609.  
  610. To scroll a 135x240 display these values should be 40, 240, 40.
  611. There are 40 lines above the display that are not shown followed by
  612. 240 lines that are shown followed by 40 more lines that are not shown.
  613. You could write to these areas off display and scroll them into view by
  614. changing the TFA, VSA and BFA values.
  615.  
  616. Args:
  617. tfa (int): Top Fixed Area
  618. vsa (int): Vertical Scrolling Area
  619. bfa (int): Bottom Fixed Area
  620. """
  621. struct.pack(">HHH", tfa, vsa, bfa)
  622. self._write(ST7789_VSCRDEF, struct.pack(">HHH", tfa, vsa, bfa))
  623.  
  624. def vscsad(self, vssa):
  625. """
  626. Set Vertical Scroll Start Address of RAM.
  627.  
  628. Defines which line in the Frame Memory will be written as the first
  629. line after the last line of the Top Fixed Area on the display
  630.  
  631. Example:
  632.  
  633. for line in range(40, 280, 1):
  634. tft.vscsad(line)
  635. utime.sleep(0.01)
  636.  
  637. Args:
  638. vssa (int): Vertical Scrolling Start Address
  639.  
  640. """
  641. self._write(ST7789_VSCSAD, struct.pack(">H", vssa))
  642.  
  643. def _text8(self, font, text, x0, y0, color=WHITE, background=BLACK):
  644. """
  645. Internal method to write characters with width of 8 and
  646. heights of 8 or 16.
  647.  
  648. Args:
  649. font (module): font module to use
  650. text (str): text to write
  651. x0 (int): column to start drawing at
  652. y0 (int): row to start drawing at
  653. color (int): 565 encoded color to use for characters
  654. background (int): 565 encoded color to use for background
  655. """
  656. for char in text:
  657. ch = ord(char)
  658. if (font.FIRST <= ch < font.LAST
  659. and x0+font.WIDTH <= self.width
  660. and y0+font.HEIGHT <= self.height):
  661.  
  662. if font.HEIGHT == 8:
  663. passes = 1
  664. size = 8
  665. each = 0
  666. else:
  667. passes = 2
  668. size = 16
  669. each = 8
  670.  
  671. for line in range(passes):
  672. idx = (ch-font.FIRST)*size+(each*line)
  673. buffer = struct.pack(
  674. '>64H',
  675. color if font.FONT[idx] & _BIT7 else background,
  676. color if font.FONT[idx] & _BIT6 else background,
  677. color if font.FONT[idx] & _BIT5 else background,
  678. color if font.FONT[idx] & _BIT4 else background,
  679. color if font.FONT[idx] & _BIT3 else background,
  680. color if font.FONT[idx] & _BIT2 else background,
  681. color if font.FONT[idx] & _BIT1 else background,
  682. color if font.FONT[idx] & _BIT0 else background,
  683. color if font.FONT[idx+1] & _BIT7 else background,
  684. color if font.FONT[idx+1] & _BIT6 else background,
  685. color if font.FONT[idx+1] & _BIT5 else background,
  686. color if font.FONT[idx+1] & _BIT4 else background,
  687. color if font.FONT[idx+1] & _BIT3 else background,
  688. color if font.FONT[idx+1] & _BIT2 else background,
  689. color if font.FONT[idx+1] & _BIT1 else background,
  690. color if font.FONT[idx+1] & _BIT0 else background,
  691. color if font.FONT[idx+2] & _BIT7 else background,
  692. color if font.FONT[idx+2] & _BIT6 else background,
  693. color if font.FONT[idx+2] & _BIT5 else background,
  694. color if font.FONT[idx+2] & _BIT4 else background,
  695. color if font.FONT[idx+2] & _BIT3 else background,
  696. color if font.FONT[idx+2] & _BIT2 else background,
  697. color if font.FONT[idx+2] & _BIT1 else background,
  698. color if font.FONT[idx+2] & _BIT0 else background,
  699. color if font.FONT[idx+3] & _BIT7 else background,
  700. color if font.FONT[idx+3] & _BIT6 else background,
  701. color if font.FONT[idx+3] & _BIT5 else background,
  702. color if font.FONT[idx+3] & _BIT4 else background,
  703. color if font.FONT[idx+3] & _BIT3 else background,
  704. color if font.FONT[idx+3] & _BIT2 else background,
  705. color if font.FONT[idx+3] & _BIT1 else background,
  706. color if font.FONT[idx+3] & _BIT0 else background,
  707. color if font.FONT[idx+4] & _BIT7 else background,
  708. color if font.FONT[idx+4] & _BIT6 else background,
  709. color if font.FONT[idx+4] & _BIT5 else background,
  710. color if font.FONT[idx+4] & _BIT4 else background,
  711. color if font.FONT[idx+4] & _BIT3 else background,
  712. color if font.FONT[idx+4] & _BIT2 else background,
  713. color if font.FONT[idx+4] & _BIT1 else background,
  714. color if font.FONT[idx+4] & _BIT0 else background,
  715. color if font.FONT[idx+5] & _BIT7 else background,
  716. color if font.FONT[idx+5] & _BIT6 else background,
  717. color if font.FONT[idx+5] & _BIT5 else background,
  718. color if font.FONT[idx+5] & _BIT4 else background,
  719. color if font.FONT[idx+5] & _BIT3 else background,
  720. color if font.FONT[idx+5] & _BIT2 else background,
  721. color if font.FONT[idx+5] & _BIT1 else background,
  722. color if font.FONT[idx+5] & _BIT0 else background,
  723. color if font.FONT[idx+6] & _BIT7 else background,
  724. color if font.FONT[idx+6] & _BIT6 else background,
  725. color if font.FONT[idx+6] & _BIT5 else background,
  726. color if font.FONT[idx+6] & _BIT4 else background,
  727. color if font.FONT[idx+6] & _BIT3 else background,
  728. color if font.FONT[idx+6] & _BIT2 else background,
  729. color if font.FONT[idx+6] & _BIT1 else background,
  730. color if font.FONT[idx+6] & _BIT0 else background,
  731. color if font.FONT[idx+7] & _BIT7 else background,
  732. color if font.FONT[idx+7] & _BIT6 else background,
  733. color if font.FONT[idx+7] & _BIT5 else background,
  734. color if font.FONT[idx+7] & _BIT4 else background,
  735. color if font.FONT[idx+7] & _BIT3 else background,
  736. color if font.FONT[idx+7] & _BIT2 else background,
  737. color if font.FONT[idx+7] & _BIT1 else background,
  738. color if font.FONT[idx+7] & _BIT0 else background
  739. )
  740. self.blit_buffer(buffer, x0, y0+8*line, 8, 8)
  741.  
  742. x0 += 8
  743.  
  744. def _text16(self, font, text, x0, y0, color=WHITE, background=BLACK):
  745. """
  746. Internal method to draw characters with width of 16 and heights of 16
  747. or 32.
  748.  
  749. Args:
  750. font (module): font module to use
  751. text (str): text to write
  752. x0 (int): column to start drawing at
  753. y0 (int): row to start drawing at
  754. color (int): 565 encoded color to use for characters
  755. background (int): 565 encoded color to use for background
  756. """
  757. for char in text:
  758. ch = ord(char)
  759. if (font.FIRST <= ch < font.LAST
  760. and x0+font.WIDTH <= self.width
  761. and y0+font.HEIGHT <= self.height):
  762.  
  763. each = 16
  764. if font.HEIGHT == 16:
  765. passes = 2
  766. size = 32
  767. else:
  768. passes = 4
  769. size = 64
  770.  
  771. for line in range(passes):
  772. idx = (ch-font.FIRST)*size+(each*line)
  773. buffer = struct.pack(
  774. '>128H',
  775. color if font.FONT[idx] & _BIT7 else background,
  776. color if font.FONT[idx] & _BIT6 else background,
  777. color if font.FONT[idx] & _BIT5 else background,
  778. color if font.FONT[idx] & _BIT4 else background,
  779. color if font.FONT[idx] & _BIT3 else background,
  780. color if font.FONT[idx] & _BIT2 else background,
  781. color if font.FONT[idx] & _BIT1 else background,
  782. color if font.FONT[idx] & _BIT0 else background,
  783. color if font.FONT[idx+1] & _BIT7 else background,
  784. color if font.FONT[idx+1] & _BIT6 else background,
  785. color if font.FONT[idx+1] & _BIT5 else background,
  786. color if font.FONT[idx+1] & _BIT4 else background,
  787. color if font.FONT[idx+1] & _BIT3 else background,
  788. color if font.FONT[idx+1] & _BIT2 else background,
  789. color if font.FONT[idx+1] & _BIT1 else background,
  790. color if font.FONT[idx+1] & _BIT0 else background,
  791. color if font.FONT[idx+2] & _BIT7 else background,
  792. color if font.FONT[idx+2] & _BIT6 else background,
  793. color if font.FONT[idx+2] & _BIT5 else background,
  794. color if font.FONT[idx+2] & _BIT4 else background,
  795. color if font.FONT[idx+2] & _BIT3 else background,
  796. color if font.FONT[idx+2] & _BIT2 else background,
  797. color if font.FONT[idx+2] & _BIT1 else background,
  798. color if font.FONT[idx+2] & _BIT0 else background,
  799. color if font.FONT[idx+3] & _BIT7 else background,
  800. color if font.FONT[idx+3] & _BIT6 else background,
  801. color if font.FONT[idx+3] & _BIT5 else background,
  802. color if font.FONT[idx+3] & _BIT4 else background,
  803. color if font.FONT[idx+3] & _BIT3 else background,
  804. color if font.FONT[idx+3] & _BIT2 else background,
  805. color if font.FONT[idx+3] & _BIT1 else background,
  806. color if font.FONT[idx+3] & _BIT0 else background,
  807. color if font.FONT[idx+4] & _BIT7 else background,
  808. color if font.FONT[idx+4] & _BIT6 else background,
  809. color if font.FONT[idx+4] & _BIT5 else background,
  810. color if font.FONT[idx+4] & _BIT4 else background,
  811. color if font.FONT[idx+4] & _BIT3 else background,
  812. color if font.FONT[idx+4] & _BIT2 else background,
  813. color if font.FONT[idx+4] & _BIT1 else background,
  814. color if font.FONT[idx+4] & _BIT0 else background,
  815. color if font.FONT[idx+5] & _BIT7 else background,
  816. color if font.FONT[idx+5] & _BIT6 else background,
  817. color if font.FONT[idx+5] & _BIT5 else background,
  818. color if font.FONT[idx+5] & _BIT4 else background,
  819. color if font.FONT[idx+5] & _BIT3 else background,
  820. color if font.FONT[idx+5] & _BIT2 else background,
  821. color if font.FONT[idx+5] & _BIT1 else background,
  822. color if font.FONT[idx+5] & _BIT0 else background,
  823. color if font.FONT[idx+6] & _BIT7 else background,
  824. color if font.FONT[idx+6] & _BIT6 else background,
  825. color if font.FONT[idx+6] & _BIT5 else background,
  826. color if font.FONT[idx+6] & _BIT4 else background,
  827. color if font.FONT[idx+6] & _BIT3 else background,
  828. color if font.FONT[idx+6] & _BIT2 else background,
  829. color if font.FONT[idx+6] & _BIT1 else background,
  830. color if font.FONT[idx+6] & _BIT0 else background,
  831. color if font.FONT[idx+7] & _BIT7 else background,
  832. color if font.FONT[idx+7] & _BIT6 else background,
  833. color if font.FONT[idx+7] & _BIT5 else background,
  834. color if font.FONT[idx+7] & _BIT4 else background,
  835. color if font.FONT[idx+7] & _BIT3 else background,
  836. color if font.FONT[idx+7] & _BIT2 else background,
  837. color if font.FONT[idx+7] & _BIT1 else background,
  838. color if font.FONT[idx+7] & _BIT0 else background,
  839. color if font.FONT[idx+8] & _BIT7 else background,
  840. color if font.FONT[idx+8] & _BIT6 else background,
  841. color if font.FONT[idx+8] & _BIT5 else background,
  842. color if font.FONT[idx+8] & _BIT4 else background,
  843. color if font.FONT[idx+8] & _BIT3 else background,
  844. color if font.FONT[idx+8] & _BIT2 else background,
  845. color if font.FONT[idx+8] & _BIT1 else background,
  846. color if font.FONT[idx+8] & _BIT0 else background,
  847. color if font.FONT[idx+9] & _BIT7 else background,
  848. color if font.FONT[idx+9] & _BIT6 else background,
  849. color if font.FONT[idx+9] & _BIT5 else background,
  850. color if font.FONT[idx+9] & _BIT4 else background,
  851. color if font.FONT[idx+9] & _BIT3 else background,
  852. color if font.FONT[idx+9] & _BIT2 else background,
  853. color if font.FONT[idx+9] & _BIT1 else background,
  854. color if font.FONT[idx+9] & _BIT0 else background,
  855. color if font.FONT[idx+10] & _BIT7 else background,
  856. color if font.FONT[idx+10] & _BIT6 else background,
  857. color if font.FONT[idx+10] & _BIT5 else background,
  858. color if font.FONT[idx+10] & _BIT4 else background,
  859. color if font.FONT[idx+10] & _BIT3 else background,
  860. color if font.FONT[idx+10] & _BIT2 else background,
  861. color if font.FONT[idx+10] & _BIT1 else background,
  862. color if font.FONT[idx+10] & _BIT0 else background,
  863. color if font.FONT[idx+11] & _BIT7 else background,
  864. color if font.FONT[idx+11] & _BIT6 else background,
  865. color if font.FONT[idx+11] & _BIT5 else background,
  866. color if font.FONT[idx+11] & _BIT4 else background,
  867. color if font.FONT[idx+11] & _BIT3 else background,
  868. color if font.FONT[idx+11] & _BIT2 else background,
  869. color if font.FONT[idx+11] & _BIT1 else background,
  870. color if font.FONT[idx+11] & _BIT0 else background,
  871. color if font.FONT[idx+12] & _BIT7 else background,
  872. color if font.FONT[idx+12] & _BIT6 else background,
  873. color if font.FONT[idx+12] & _BIT5 else background,
  874. color if font.FONT[idx+12] & _BIT4 else background,
  875. color if font.FONT[idx+12] & _BIT3 else background,
  876. color if font.FONT[idx+12] & _BIT2 else background,
  877. color if font.FONT[idx+12] & _BIT1 else background,
  878. color if font.FONT[idx+12] & _BIT0 else background,
  879. color if font.FONT[idx+13] & _BIT7 else background,
  880. color if font.FONT[idx+13] & _BIT6 else background,
  881. color if font.FONT[idx+13] & _BIT5 else background,
  882. color if font.FONT[idx+13] & _BIT4 else background,
  883. color if font.FONT[idx+13] & _BIT3 else background,
  884. color if font.FONT[idx+13] & _BIT2 else background,
  885. color if font.FONT[idx+13] & _BIT1 else background,
  886. color if font.FONT[idx+13] & _BIT0 else background,
  887. color if font.FONT[idx+14] & _BIT7 else background,
  888. color if font.FONT[idx+14] & _BIT6 else background,
  889. color if font.FONT[idx+14] & _BIT5 else background,
  890. color if font.FONT[idx+14] & _BIT4 else background,
  891. color if font.FONT[idx+14] & _BIT3 else background,
  892. color if font.FONT[idx+14] & _BIT2 else background,
  893. color if font.FONT[idx+14] & _BIT1 else background,
  894. color if font.FONT[idx+14] & _BIT0 else background,
  895. color if font.FONT[idx+15] & _BIT7 else background,
  896. color if font.FONT[idx+15] & _BIT6 else background,
  897. color if font.FONT[idx+15] & _BIT5 else background,
  898. color if font.FONT[idx+15] & _BIT4 else background,
  899. color if font.FONT[idx+15] & _BIT3 else background,
  900. color if font.FONT[idx+15] & _BIT2 else background,
  901. color if font.FONT[idx+15] & _BIT1 else background,
  902. color if font.FONT[idx+15] & _BIT0 else background
  903. )
  904. self.blit_buffer(buffer, x0, y0+8*line, 16, 8)
  905. x0 += font.WIDTH
  906.  
  907. def text(self, font, text, x0, y0, color=WHITE, background=BLACK):
  908. """
  909. Draw text on display in specified font and colors. 8 and 16 bit wide
  910. fonts are supported.
  911.  
  912. Args:
  913. font (module): font module to use.
  914. text (str): text to write
  915. x0 (int): column to start drawing at
  916. y0 (int): row to start drawing at
  917. color (int): 565 encoded color to use for characters
  918. background (int): 565 encoded color to use for background
  919. """
  920. if font.WIDTH == 8:
  921. self._text8(font, text, x0, y0, color, background)
  922. else:
  923. self._text16(font, text, x0, y0, color, background)
  924.  
  925. def bitmap(self, bitmap, x, y, index=0):
  926. """
  927. Draw a bitmap on display at the specified column and row
  928.  
  929. Args:
  930. bitmap (bitmap_module): The module containing the bitmap to draw
  931. x (int): column to start drawing at
  932. y (int): row to start drawing at
  933. index (int): Optional index of bitmap to draw from multiple bitmap
  934. module
  935.  
  936. """
  937. bitmap_size = bitmap.HEIGHT * bitmap.WIDTH
  938. buffer_len = bitmap_size * 2
  939. buffer = bytearray(buffer_len)
  940. bs_bit = bitmap.BPP * bitmap_size * index if index > 0 else 0
  941.  
  942. for i in range(0, buffer_len, 2):
  943. color_index = 0
  944. for _ in range(bitmap.BPP):
  945. color_index <<= 1
  946. color_index |= (bitmap.BITMAP[bs_bit // 8]
  947. & 1 << (7 - (bs_bit % 8))) > 0
  948. bs_bit += 1
  949.  
  950. color = bitmap.PALETTE[color_index]
  951. buffer[i] = color & 0xff00 >> 8
  952. buffer[i + 1] = color_index & 0xff
  953.  
  954. to_col = x + bitmap.WIDTH - 1
  955. to_row = y + bitmap.HEIGHT - 1
  956. if self.width > to_col and self.height > to_row:
  957. self._set_window(x, y, to_col, to_row)
  958. self._write(None, buffer)
  959.  
  960. # @micropython.native
  961. def write(self, font, string, x, y, fg=WHITE, bg=BLACK):
  962. """
  963. Write a string using a converted true-type font on the display starting
  964. at the specified column and row
  965.  
  966. Args:
  967. font (font): The module containing the converted true-type font
  968. s (string): The string to write
  969. x (int): column to start writing
  970. y (int): row to start writing
  971. fg (int): foreground color, optional, defaults to WHITE
  972. bg (int): background color, optional, defaults to BLACK
  973. """
  974. buffer_len = font.HEIGHT * font.MAX_WIDTH * 2
  975. buffer = bytearray(buffer_len)
  976. fg_hi = (fg & 0xff00) >> 8
  977. fg_lo = fg & 0xff
  978.  
  979. bg_hi = (bg & 0xff00) >> 8
  980. bg_lo = bg & 0xff
  981.  
  982. for character in string:
  983. try:
  984. char_index = font.MAP.index(character)
  985. offset = char_index * font.OFFSET_WIDTH
  986. bs_bit = font.OFFSETS[offset]
  987. if font.OFFSET_WIDTH > 1:
  988. bs_bit = (bs_bit << 8) + font.OFFSETS[offset + 1]
  989.  
  990. if font.OFFSET_WIDTH > 2:
  991. bs_bit = (bs_bit << 8) + font.OFFSETS[offset + 2]
  992.  
  993. char_width = font.WIDTHS[char_index]
  994. buffer_needed = char_width * font.HEIGHT * 2
  995.  
  996. for i in range(0, buffer_needed, 2):
  997. if font.BITMAPS[bs_bit // 8] & 1 << (7 - (bs_bit % 8)) > 0:
  998. buffer[i] = fg_hi
  999. buffer[i + 1] = fg_lo
  1000. else:
  1001. buffer[i] = bg_hi
  1002. buffer[i + 1] = bg_lo
  1003.  
  1004. bs_bit += 1
  1005.  
  1006. to_col = x + char_width - 1
  1007. to_row = y + font.HEIGHT - 1
  1008. if self.width > to_col and self.height > to_row:
  1009. self._set_window(x, y, to_col, to_row)
  1010. self._write(None, buffer[:buffer_needed])
  1011.  
  1012. x += char_width
  1013.  
  1014. except ValueError:
  1015. pass
  1016.  
  1017. def write_width(self, font, string):
  1018. """
  1019. Returns the width in pixels of the string if it was written with the
  1020. specified font
  1021.  
  1022. Args:
  1023. font (font): The module containing the converted true-type font
  1024. string (string): The string to measure
  1025. """
  1026. width = 0
  1027. for character in string:
  1028. try:
  1029. char_index = font.MAP.index(character)
  1030. width += font.WIDTHS[char_index]
  1031.  
  1032. except ValueError:
  1033. pass
  1034.  
  1035. return width
  1036.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement