2020-03-22 16:40:18 +01:00
|
|
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
# Copyright (C) 2020 Daniel Thompson
|
|
|
|
|
2020-05-14 22:44:05 +02:00
|
|
|
"""RGB565 drawing library
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
"""
|
|
|
|
|
2020-04-08 22:50:42 +02:00
|
|
|
import array
|
2020-02-19 20:47:51 +01:00
|
|
|
import fonts.sans24
|
2020-12-27 10:07:06 +01:00
|
|
|
import math
|
2020-02-19 20:47:51 +01:00
|
|
|
import micropython
|
|
|
|
|
2020-12-29 13:30:20 +01:00
|
|
|
from micropython import const
|
|
|
|
|
|
|
|
R = const(0b11111_000000_00000)
|
|
|
|
G = const(0b00000_111111_00000)
|
|
|
|
B = const(0b00000_000000_11111)
|
|
|
|
|
2020-02-19 20:47:51 +01:00
|
|
|
@micropython.viper
|
2020-02-23 21:52:09 +01:00
|
|
|
def _bitblit(bitbuf, pixels, bgfg: int, count: int):
|
2020-04-10 21:23:12 +02:00
|
|
|
mv = ptr16(bitbuf)
|
2020-02-19 20:47:51 +01:00
|
|
|
px = ptr8(pixels)
|
|
|
|
|
2020-04-10 21:23:12 +02:00
|
|
|
# Extract and byte-swap
|
|
|
|
bg = ((bgfg >> 24) & 0xff) + ((bgfg >> 8) & 0xff00)
|
|
|
|
fg = ((bgfg >> 8) & 0xff) + ((bgfg & 0xff) << 8)
|
2020-02-19 20:47:51 +01:00
|
|
|
|
|
|
|
bitselect = 0x80
|
|
|
|
pxp = 0
|
|
|
|
mvp = 0
|
|
|
|
|
|
|
|
for bit in range(count):
|
|
|
|
# Draw the pixel
|
|
|
|
active = px[pxp] & bitselect
|
2020-04-10 21:23:12 +02:00
|
|
|
mv[mvp] = fg if active else bg
|
2020-02-19 20:47:51 +01:00
|
|
|
mvp += 1
|
|
|
|
|
|
|
|
# Advance to the next bit
|
|
|
|
bitselect >>= 1
|
|
|
|
if not bitselect:
|
|
|
|
bitselect = 0x80
|
|
|
|
pxp += 1
|
|
|
|
|
2020-04-06 22:45:44 +02:00
|
|
|
@micropython.viper
|
2020-05-18 23:17:51 +02:00
|
|
|
def _clut8_rgb565(i: int) -> int:
|
|
|
|
if i < 216:
|
|
|
|
rgb565 = (( i % 6) * 0x33) >> 3
|
|
|
|
rg = i // 6
|
|
|
|
rgb565 += ((rg % 6) * (0x33 << 3)) & 0x07e0
|
|
|
|
rgb565 += ((rg // 6) * (0x33 << 8)) & 0xf800
|
|
|
|
elif i < 252:
|
|
|
|
i -= 216
|
|
|
|
rgb565 = (0x7f + (( i % 3) * 0x33)) >> 3
|
|
|
|
rg = i // 3
|
|
|
|
rgb565 += ((0x4c << 3) + ((rg % 4) * (0x33 << 3))) & 0x07e0
|
|
|
|
rgb565 += ((0x7f << 8) + ((rg // 4) * (0x33 << 8))) & 0xf800
|
|
|
|
else:
|
|
|
|
i -= 252
|
|
|
|
gr6 = (0x2c + (0x10 * i)) >> 2
|
|
|
|
gr5 = gr6 >> 1
|
|
|
|
rgb565 = (gr5 << 11) + (gr6 << 5) + gr5
|
|
|
|
|
|
|
|
return rgb565
|
2020-04-06 22:45:44 +02:00
|
|
|
|
2020-03-09 01:00:13 +01:00
|
|
|
@micropython.viper
|
|
|
|
def _fill(mv, color: int, count: int, offset: int):
|
2020-04-08 22:50:42 +02:00
|
|
|
p = ptr16(mv)
|
2020-04-10 21:20:01 +02:00
|
|
|
color = (color >> 8) + ((color & 0xff) << 8)
|
2020-03-09 01:00:13 +01:00
|
|
|
|
2020-04-08 22:50:42 +02:00
|
|
|
for x in range(offset, offset+count):
|
|
|
|
p[x] = color
|
2020-03-09 01:00:13 +01:00
|
|
|
|
2020-02-23 21:52:09 +01:00
|
|
|
def _bounding_box(s, font):
|
2020-12-28 15:28:54 +01:00
|
|
|
if not s:
|
|
|
|
return (0, font.height())
|
|
|
|
|
2021-02-04 23:06:49 +01:00
|
|
|
get_ch = font.get_ch
|
2021-06-04 11:46:25 +02:00
|
|
|
w = len(s)
|
2020-02-19 20:47:51 +01:00
|
|
|
for ch in s:
|
2021-02-04 23:06:49 +01:00
|
|
|
(_, h, wc) = get_ch(ch)
|
|
|
|
w += wc
|
2020-02-19 20:47:51 +01:00
|
|
|
|
|
|
|
return (w, h)
|
|
|
|
|
2020-03-09 22:29:35 +01:00
|
|
|
@micropython.native
|
2020-02-23 21:52:09 +01:00
|
|
|
def _draw_glyph(display, glyph, x, y, bgfg):
|
2020-02-19 20:47:51 +01:00
|
|
|
(px, h, w) = glyph
|
|
|
|
|
2021-01-17 18:44:21 +01:00
|
|
|
buf = display.linebuffer[0:2*(w+1)]
|
2021-03-29 23:16:43 +02:00
|
|
|
buf[2*w] = bgfg >> 24
|
|
|
|
buf[2*w + 1] = (bgfg >> 16) & 0xff
|
2020-02-19 20:47:51 +01:00
|
|
|
bytes_per_row = (w + 7) // 8
|
|
|
|
|
2020-03-09 22:29:35 +01:00
|
|
|
display.set_window(x, y, w+1, h)
|
2021-01-17 17:03:42 +01:00
|
|
|
quick_write = display.quick_write
|
|
|
|
|
|
|
|
display.quick_start()
|
2020-02-19 20:47:51 +01:00
|
|
|
for row in range(h):
|
2020-02-23 21:52:09 +01:00
|
|
|
_bitblit(buf, px[row*bytes_per_row:], bgfg, w)
|
2021-01-17 17:03:42 +01:00
|
|
|
quick_write(buf)
|
|
|
|
display.quick_end()
|
2020-02-19 20:47:51 +01:00
|
|
|
|
|
|
|
class Draw565(object):
|
2020-02-23 21:52:09 +01:00
|
|
|
"""Drawing library for RGB565 displays.
|
|
|
|
|
|
|
|
A full framebufer is not required although the library will
|
|
|
|
'borrow' a line buffer from the underlying display driver.
|
2020-05-14 22:44:05 +02:00
|
|
|
|
|
|
|
.. automethod:: __init__
|
2020-02-23 21:52:09 +01:00
|
|
|
"""
|
|
|
|
|
2020-02-19 20:47:51 +01:00
|
|
|
def __init__(self, display):
|
2020-02-23 21:52:09 +01:00
|
|
|
"""Initialise the library.
|
|
|
|
|
|
|
|
Defaults to white-on-black for monochrome drawing operations
|
2020-05-12 00:07:35 +02:00
|
|
|
and 24pt Sans Serif text.
|
2020-02-23 21:52:09 +01:00
|
|
|
"""
|
2020-02-19 20:47:51 +01:00
|
|
|
self._display = display
|
2020-04-05 10:29:06 +02:00
|
|
|
self.reset()
|
|
|
|
|
|
|
|
def reset(self):
|
2020-05-12 00:07:35 +02:00
|
|
|
"""Restore the default colours and font.
|
|
|
|
|
|
|
|
Default colours are white-on-block (white foreground, black
|
|
|
|
background) and the default font is 24pt Sans Serif."""
|
2020-02-19 20:47:51 +01:00
|
|
|
self.set_color(0xffff)
|
|
|
|
self.set_font(fonts.sans24)
|
|
|
|
|
2020-03-09 01:00:13 +01:00
|
|
|
def fill(self, bg=None, x=0, y=0, w=None, h=None):
|
2020-05-14 22:44:05 +02:00
|
|
|
"""Draw a solid colour rectangle.
|
2020-03-09 01:00:13 +01:00
|
|
|
|
|
|
|
If no arguments a provided the whole display will be filled with
|
2020-05-12 00:07:35 +02:00
|
|
|
the background colour (typically black).
|
|
|
|
|
|
|
|
:param bg: Background colour (in RGB565 format)
|
|
|
|
:param x: X coordinate of the left-most pixels of the rectangle
|
|
|
|
:param y: Y coordinate of the top-most pixels of the rectangle
|
|
|
|
:param w: Width of the rectangle, defaults to None (which means select
|
|
|
|
the right-most pixel of the display)
|
|
|
|
:param h: Height of the rectangle, defaults to None (which means select
|
|
|
|
the bottom-most pixel of the display)
|
2020-03-09 01:00:13 +01:00
|
|
|
"""
|
2020-06-22 23:20:34 +02:00
|
|
|
display = self._display
|
|
|
|
quick_write = display.quick_write
|
|
|
|
|
2020-03-09 01:00:13 +01:00
|
|
|
if bg is None:
|
|
|
|
bg = self._bgfg >> 16
|
2020-06-22 23:20:34 +02:00
|
|
|
if w is None:
|
|
|
|
w = display.width - x
|
|
|
|
if h is None:
|
|
|
|
h = display.height - y
|
|
|
|
|
|
|
|
display.set_window(x, y, w, h)
|
|
|
|
|
|
|
|
remaining = w * h
|
|
|
|
|
|
|
|
# Populate the line buffer
|
|
|
|
buf = display.linebuffer
|
2021-01-17 18:44:21 +01:00
|
|
|
sz = len(buf) // 2
|
2020-06-22 23:20:34 +02:00
|
|
|
_fill(buf, bg, min(sz, remaining), 0)
|
|
|
|
|
|
|
|
display.quick_start()
|
|
|
|
while remaining >= sz:
|
|
|
|
quick_write(buf)
|
|
|
|
remaining -= sz
|
|
|
|
if remaining:
|
2021-01-17 18:44:21 +01:00
|
|
|
quick_write(buf[0:2*remaining])
|
2020-06-22 23:20:34 +02:00
|
|
|
display.quick_end()
|
2020-03-09 01:00:13 +01:00
|
|
|
|
2020-04-10 21:22:51 +02:00
|
|
|
@micropython.native
|
2020-05-23 14:44:33 +02:00
|
|
|
def blit(self, image, x, y, fg=0xffff, c1=0x4a69, c2=0x7bef):
|
2020-05-12 00:07:35 +02:00
|
|
|
"""Decode and draw an encoded image.
|
|
|
|
|
|
|
|
:param image: Image data in either 1-bit RLE or 2-bit RLE formats. The
|
|
|
|
format will be autodetected
|
|
|
|
:param x: X coordinate for the left-most pixels in the image
|
|
|
|
:param y: Y coordinate for the top-most pixels in the image
|
|
|
|
"""
|
2020-04-10 21:22:51 +02:00
|
|
|
if len(image) == 3:
|
|
|
|
# Legacy 1-bit image
|
2020-05-23 14:44:33 +02:00
|
|
|
self.rleblit(image, (x, y), fg)
|
2020-04-10 21:22:51 +02:00
|
|
|
else: #elif image[0] == 2:
|
|
|
|
# 2-bit RLE image, (255x255, v1)
|
2020-05-23 14:44:33 +02:00
|
|
|
self._rle2bit(image, x, y, fg, c1, c2)
|
2020-04-10 21:22:51 +02:00
|
|
|
|
2020-03-09 01:00:13 +01:00
|
|
|
@micropython.native
|
|
|
|
def rleblit(self, image, pos=(0, 0), fg=0xffff, bg=0):
|
2020-05-12 00:07:35 +02:00
|
|
|
"""Decode and draw a 1-bit RLE image.
|
|
|
|
|
|
|
|
.. deprecated:: M2
|
|
|
|
Use :py:meth:`~.blit` instead.
|
|
|
|
"""
|
2020-03-09 01:00:13 +01:00
|
|
|
display = self._display
|
2020-04-08 22:50:42 +02:00
|
|
|
write_data = display.write_data
|
2020-03-09 01:00:13 +01:00
|
|
|
(sx, sy, rle) = image
|
|
|
|
|
|
|
|
display.set_window(pos[0], pos[1], sx, sy)
|
|
|
|
|
2021-01-17 18:44:21 +01:00
|
|
|
buf = display.linebuffer[0:2*sx]
|
2020-03-09 01:00:13 +01:00
|
|
|
bp = 0
|
|
|
|
color = bg
|
|
|
|
|
|
|
|
for rl in rle:
|
|
|
|
while rl:
|
|
|
|
count = min(sx - bp, rl)
|
|
|
|
_fill(buf, color, count, bp)
|
|
|
|
bp += count
|
|
|
|
rl -= count
|
|
|
|
|
|
|
|
if bp >= sx:
|
2020-04-08 22:50:42 +02:00
|
|
|
write_data(buf)
|
2020-03-09 01:00:13 +01:00
|
|
|
bp = 0
|
|
|
|
|
|
|
|
if color == bg:
|
|
|
|
color = fg
|
|
|
|
else:
|
|
|
|
color = bg
|
|
|
|
|
2020-04-06 22:45:44 +02:00
|
|
|
@micropython.native
|
2020-05-23 14:44:33 +02:00
|
|
|
def _rle2bit(self, image, x, y, fg, c1, c2):
|
2020-04-06 22:45:44 +02:00
|
|
|
"""Decode and draw a 2-bit RLE image."""
|
|
|
|
display = self._display
|
2020-04-08 22:50:42 +02:00
|
|
|
quick_write = display.quick_write
|
2020-04-10 21:22:51 +02:00
|
|
|
sx = image[1]
|
|
|
|
sy = image[2]
|
|
|
|
rle = memoryview(image)[3:]
|
2020-04-06 22:45:44 +02:00
|
|
|
|
|
|
|
display.set_window(x, y, sx, sy)
|
|
|
|
|
2020-05-17 10:47:15 +02:00
|
|
|
if sx <= (len(display.linebuffer) // 4) and not bool(sy & 1):
|
2020-04-10 21:22:51 +02:00
|
|
|
sx *= 2
|
2020-05-17 10:47:15 +02:00
|
|
|
sy //= 2
|
2020-04-10 21:22:51 +02:00
|
|
|
|
2020-05-23 14:44:33 +02:00
|
|
|
palette = array.array('H', (0, c1, c2, fg))
|
2020-04-08 22:50:42 +02:00
|
|
|
next_color = 1
|
|
|
|
rl = 0
|
2021-01-17 18:44:21 +01:00
|
|
|
buf = display.linebuffer[0:2*sx]
|
2020-04-06 22:45:44 +02:00
|
|
|
bp = 0
|
|
|
|
|
2020-04-08 22:50:42 +02:00
|
|
|
display.quick_start()
|
2020-04-06 22:45:44 +02:00
|
|
|
for op in rle:
|
|
|
|
if rl == 0:
|
|
|
|
px = op >> 6
|
|
|
|
rl = op & 0x3f
|
|
|
|
if 0 == rl:
|
|
|
|
rl = -1
|
2020-04-08 22:50:42 +02:00
|
|
|
continue
|
|
|
|
if rl >= 63:
|
|
|
|
continue
|
2020-04-06 22:45:44 +02:00
|
|
|
elif rl > 0:
|
|
|
|
rl += op
|
2020-04-08 22:50:42 +02:00
|
|
|
if op >= 255:
|
|
|
|
continue
|
2020-04-06 22:45:44 +02:00
|
|
|
else:
|
2020-05-18 23:17:51 +02:00
|
|
|
palette[next_color] = _clut8_rgb565(op)
|
2020-04-08 22:50:42 +02:00
|
|
|
if next_color < 3:
|
|
|
|
next_color += 1
|
|
|
|
else:
|
2020-04-06 22:45:44 +02:00
|
|
|
next_color = 1
|
|
|
|
rl = 0
|
2020-04-08 22:50:42 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
while rl:
|
|
|
|
count = min(sx - bp, rl)
|
|
|
|
_fill(buf, palette[px], count, bp)
|
|
|
|
bp += count
|
|
|
|
rl -= count
|
|
|
|
|
|
|
|
if bp >= sx:
|
|
|
|
quick_write(buf)
|
|
|
|
bp = 0
|
|
|
|
display.quick_end()
|
2020-04-06 22:45:44 +02:00
|
|
|
|
2020-02-19 20:47:51 +01:00
|
|
|
def set_color(self, color, bg=0):
|
2020-05-12 00:07:35 +02:00
|
|
|
"""Set the foreground and background colours.
|
2020-02-23 21:52:09 +01:00
|
|
|
|
2020-05-12 00:07:35 +02:00
|
|
|
The supplied colour will be used for all monochrome drawing operations.
|
|
|
|
If no background colour is provided then the background will be set
|
2020-02-23 21:52:09 +01:00
|
|
|
to black.
|
2020-05-12 00:07:35 +02:00
|
|
|
|
|
|
|
:param color: Foreground colour
|
|
|
|
:param bg: Background colour, defaults to black
|
2020-02-23 21:52:09 +01:00
|
|
|
"""
|
2020-02-19 20:47:51 +01:00
|
|
|
self._bgfg = (bg << 16) + color
|
|
|
|
|
|
|
|
def set_font(self, font):
|
2020-05-12 00:07:35 +02:00
|
|
|
"""Set the font used for rendering text.
|
|
|
|
|
|
|
|
:param font: A font module generated using ``font_to_py.py``.
|
|
|
|
"""
|
2020-02-19 20:47:51 +01:00
|
|
|
self._font = font
|
|
|
|
|
2020-12-28 15:29:09 +01:00
|
|
|
def string(self, s, x, y, width=None, right=False):
|
2020-02-23 21:52:09 +01:00
|
|
|
"""Draw a string at the supplied position.
|
|
|
|
|
2020-05-12 00:07:35 +02:00
|
|
|
:param s: String to render
|
|
|
|
:param x: X coordinate for the left-most pixels in the image
|
|
|
|
:param y: Y coordinate for the top-most pixels in the image
|
|
|
|
:param width: If no width is provided then the text will be left
|
|
|
|
justified, otherwise the text will be centred within the
|
|
|
|
provided width and, importantly, the remaining width will
|
|
|
|
be filled with the background colour (to ensure that if
|
|
|
|
we update one string with a narrower one there is no
|
|
|
|
need to "undraw" it)
|
2020-12-28 15:29:09 +01:00
|
|
|
:param right: If True (and width is set) then right justify rather than
|
|
|
|
centre the text
|
2020-02-23 21:52:09 +01:00
|
|
|
"""
|
2020-02-19 20:47:51 +01:00
|
|
|
display = self._display
|
|
|
|
bgfg = self._bgfg
|
|
|
|
font = self._font
|
2020-07-31 14:09:33 +02:00
|
|
|
bg = self._bgfg >> 16
|
2020-02-19 20:47:51 +01:00
|
|
|
|
|
|
|
if width:
|
2020-02-23 21:52:09 +01:00
|
|
|
(w, h) = _bounding_box(s, font)
|
2020-12-28 15:29:09 +01:00
|
|
|
if right:
|
|
|
|
leftpad = width - w
|
|
|
|
rightpad = 0
|
|
|
|
else:
|
|
|
|
leftpad = (width - w) // 2
|
|
|
|
rightpad = width - w - leftpad
|
2020-07-31 14:09:33 +02:00
|
|
|
self.fill(bg, x, y, leftpad, h)
|
2020-02-19 20:47:51 +01:00
|
|
|
x += leftpad
|
|
|
|
|
|
|
|
for ch in s:
|
|
|
|
glyph = font.get_ch(ch)
|
2020-02-23 21:52:09 +01:00
|
|
|
_draw_glyph(display, glyph, x, y, bgfg)
|
2020-02-19 20:47:51 +01:00
|
|
|
x += glyph[2] + 1
|
|
|
|
|
|
|
|
if width:
|
2020-07-31 14:09:33 +02:00
|
|
|
self.fill(bg, x, y, rightpad, h)
|
2020-04-11 21:14:30 +02:00
|
|
|
|
2021-02-01 11:12:38 +01:00
|
|
|
def bounding_box(self, s):
|
|
|
|
"""Return the bounding box of a string.
|
|
|
|
|
|
|
|
:param s: A string
|
|
|
|
:returns: Tuple of (width, height)
|
|
|
|
"""
|
|
|
|
return _bounding_box(s, self._font)
|
|
|
|
|
2020-04-11 21:14:30 +02:00
|
|
|
def wrap(self, s, width):
|
2020-05-12 00:07:35 +02:00
|
|
|
"""Chunk a string so it can rendered within a specified width.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
draw = wasp.watch.drawable
|
|
|
|
chunks = draw.wrap(long_string, 240)
|
|
|
|
|
|
|
|
# line(1) will provide the first line
|
|
|
|
# line(len(chunks)-1) will provide the last line
|
|
|
|
def line(n):
|
|
|
|
return long_string[chunks[n-1]:chunks[n]]
|
|
|
|
|
|
|
|
:param s: String to be chunked
|
|
|
|
:param width: Width to wrap the text into
|
|
|
|
:returns: List of chunk boundaries
|
|
|
|
"""
|
2020-04-11 21:14:30 +02:00
|
|
|
font = self._font
|
|
|
|
max = len(s)
|
|
|
|
chunks = [ 0, ]
|
|
|
|
end = 0
|
|
|
|
|
|
|
|
while end < max:
|
|
|
|
start = end
|
|
|
|
l = 0
|
|
|
|
|
|
|
|
for i in range(start, max+1):
|
2021-07-22 21:03:59 +02:00
|
|
|
if i >= max:
|
|
|
|
end = i
|
2020-04-11 21:14:30 +02:00
|
|
|
break
|
|
|
|
ch = s[i]
|
2021-03-22 09:29:06 +01:00
|
|
|
(_, h, w) = font.get_ch(ch)
|
|
|
|
l += w + 1
|
|
|
|
if l > width:
|
2021-07-22 21:03:59 +02:00
|
|
|
if end <= start:
|
|
|
|
end = i
|
2021-03-22 09:29:06 +01:00
|
|
|
break
|
|
|
|
|
|
|
|
# Break the line immediately if requested
|
2020-04-11 21:14:30 +02:00
|
|
|
if ch == '\n':
|
|
|
|
end = i+1
|
|
|
|
break
|
2021-03-22 09:29:06 +01:00
|
|
|
|
|
|
|
# Remember the right-most place we can cleanly break the line
|
2020-04-11 21:14:30 +02:00
|
|
|
if ch == ' ':
|
|
|
|
end = i+1
|
|
|
|
chunks.append(end)
|
|
|
|
|
|
|
|
return chunks
|
2020-12-11 09:12:09 +01:00
|
|
|
|
2020-12-27 09:47:05 +01:00
|
|
|
def line(self, x0, y0, x1, y1, width=1, color=None):
|
2020-12-27 09:38:54 +01:00
|
|
|
"""Draw a line between points (x0, y0) and (x1, y1).
|
2020-12-11 09:12:09 +01:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
draw = wasp.watch.drawable
|
|
|
|
draw.line(0, 120, 240, 240, 0xf800)
|
|
|
|
|
2020-12-27 09:38:54 +01:00
|
|
|
:param x0: X coordinate of the start of the line
|
|
|
|
:param y0: Y coordinate of the start of the line
|
|
|
|
:param x1: X coordinate of the end of the line
|
|
|
|
:param y1: Y coordinate of the end of the line
|
2020-12-27 09:47:05 +01:00
|
|
|
:param width: Width of the line in pixels
|
2020-12-27 09:38:54 +01:00
|
|
|
:param color: Colour to draw line, defaults to the foreground colour
|
2020-12-11 09:12:09 +01:00
|
|
|
"""
|
2020-12-27 09:38:54 +01:00
|
|
|
if color is None:
|
|
|
|
color = self._bgfg & 0xffff
|
2020-12-27 09:47:05 +01:00
|
|
|
px = bytes(((color >> 8) & 0xFF, color & 0xFF)) * (width * width)
|
2020-12-11 09:12:09 +01:00
|
|
|
write_data = self._display.write_data
|
|
|
|
set_window = self._display.set_window
|
|
|
|
|
2020-12-27 09:47:05 +01:00
|
|
|
dw = (width - 1) // 2
|
|
|
|
x0 -= dw
|
|
|
|
y0 -= dw
|
|
|
|
x1 -= dw
|
|
|
|
y1 -= dw
|
|
|
|
|
2020-12-11 09:12:09 +01:00
|
|
|
dx = abs(x1 - x0)
|
|
|
|
sx = 1 if x0 < x1 else -1
|
|
|
|
dy = -abs(y1 - y0)
|
|
|
|
sy = 1 if y0 < y1 else -1
|
|
|
|
err = dx + dy
|
|
|
|
if dx == 0 or dy == 0:
|
|
|
|
if x1 < x0 or y1 < y0:
|
|
|
|
x0, x1 = x1, x0
|
|
|
|
y0, y1 = y1, y0
|
2020-12-28 15:27:10 +01:00
|
|
|
w = width if dx == 0 else (dx + width)
|
|
|
|
h = width if dy == 0 else (-dy + width)
|
2020-12-11 09:12:09 +01:00
|
|
|
self.fill(color, x0, y0, w, h)
|
|
|
|
return
|
|
|
|
while True:
|
2020-12-27 09:47:05 +01:00
|
|
|
set_window(x0, y0, width, width)
|
2020-12-11 09:12:09 +01:00
|
|
|
write_data(px)
|
|
|
|
if x0 == x1 and y0 == y1:
|
|
|
|
break
|
|
|
|
e2 = 2 * err
|
|
|
|
if e2 >= dy:
|
|
|
|
err += dy
|
|
|
|
x0 += sx
|
|
|
|
if e2 <= dx:
|
|
|
|
err += dx;
|
|
|
|
y0 += sy;
|
|
|
|
|
2020-12-27 10:07:06 +01:00
|
|
|
def polar(self, x, y, theta, r0, r1, width=1, color=None):
|
|
|
|
"""Draw a line using polar coordinates.
|
2020-12-11 09:12:09 +01:00
|
|
|
|
2020-12-27 10:07:06 +01:00
|
|
|
The coordinate system is tuned for clock applications so it
|
|
|
|
adopts navigational conventions rather than mathematical ones.
|
|
|
|
Specifically the reference direction is drawn vertically
|
|
|
|
upwards and the angle is measures clockwise in degrees.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
draw = wasp.watch.drawable
|
|
|
|
draw.line(360 / 12, 16, 64)
|
|
|
|
|
|
|
|
:param theta: Angle, in degrees
|
|
|
|
:param r0: Radius of the start of the line
|
|
|
|
:param y0: Radius of the end of the line
|
|
|
|
:param x: X coordinate of the origin
|
|
|
|
:param y: Y coordinate of the origin
|
|
|
|
:param width: Width of the line in pixels
|
|
|
|
:param color: Colour to draw line in, defaults to the foreground colour
|
|
|
|
"""
|
|
|
|
to_radians = math.pi / 180
|
|
|
|
xdelta = math.sin(theta * to_radians)
|
|
|
|
ydelta = math.cos(theta * to_radians)
|
|
|
|
|
|
|
|
x0 = x + int(xdelta * r0)
|
|
|
|
x1 = x + int(xdelta * r1)
|
|
|
|
y0 = y - int(ydelta * r0)
|
|
|
|
y1 = y - int(ydelta * r1)
|
|
|
|
|
|
|
|
self.line(x0, y0, x1, y1, width, color)
|
2020-12-29 13:30:20 +01:00
|
|
|
|
|
|
|
def lighten(self, color, step=1):
|
|
|
|
"""Get a lighter shade from the same palette.
|
|
|
|
|
|
|
|
The approach is somewhat unsophisticated. It is essentially just a
|
|
|
|
saturating add for each of the RGB fields.
|
|
|
|
|
|
|
|
:param color: Shade to lighten
|
|
|
|
:returns: New colour
|
|
|
|
"""
|
|
|
|
r = (color & R) + (step << 11)
|
|
|
|
if r > R:
|
|
|
|
r = R
|
|
|
|
|
|
|
|
g = (color & G) + (step << 6)
|
|
|
|
if g > G:
|
|
|
|
g = G
|
|
|
|
|
|
|
|
b = (color & B) + step
|
|
|
|
if b > B:
|
|
|
|
b = B
|
|
|
|
|
|
|
|
return (r | g | b)
|
|
|
|
|
|
|
|
def darken(self, color, step=1):
|
|
|
|
"""Get a darker shade from the same palette.
|
|
|
|
|
|
|
|
The approach is somewhat unsophisticated. It is essentially just a
|
|
|
|
desaturating subtract for each of the RGB fields.
|
|
|
|
|
|
|
|
:param color: Shade to darken
|
|
|
|
:returns: New colour
|
|
|
|
"""
|
|
|
|
rm = color & R
|
|
|
|
rs = step << 11
|
|
|
|
r = rm - rs if rm > rs else 0
|
|
|
|
|
|
|
|
gm = color & G
|
|
|
|
gs = step << 6
|
|
|
|
g = gm - gs if gm > gs else 0
|
|
|
|
|
|
|
|
bm = color & B
|
|
|
|
b = bm - step if bm > step else 0
|
|
|
|
|
|
|
|
return (r | g | b)
|