summaryrefslogtreecommitdiff
path: root/wasp/draw565.py
diff options
context:
space:
mode:
authorKozova1 <mug66kk@gmail.com>2020-12-11 08:12:09 (GMT)
committerDaniel Thompson <daniel@redfelineninja.org.uk>2020-12-12 18:18:01 (GMT)
commitc17abd1728ff02e83a348688d288869c42097232 (patch)
tree0bc8954199b59367e8ecaecd8e3434aa8918ef0f /wasp/draw565.py
parent339b8357824b35cf1a4fb03732acdccd7ef5805e (diff)
draw565: Added line drawing function
This is the API: drawable.line(x1, y1, x2, y2, color) The function has optimizations for the case of vertical or horizontal lines. Signed-off-by: Kozova1 <mug66kk@gmail.com> [daniel@redfelineninja.org.uk: Minor update to commit message] Signed-off-by: Daniel Thompson <daniel@redfelineninja.org.uk>
Diffstat (limited to 'wasp/draw565.py')
-rw-r--r--wasp/draw565.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/wasp/draw565.py b/wasp/draw565.py
index 5e38284..77bd286 100644
--- a/wasp/draw565.py
+++ b/wasp/draw565.py
@@ -355,3 +355,52 @@ class Draw565(object):
chunks.append(end)
return chunks
+
+ def line(self, x0=0, y0=0, x1=0, y1=0, color=0xffff):
+ """Draw a line between points (x0;y0) and (x1;y1) with color.
+
+ Example:
+
+ .. code-block:: python
+
+ draw = wasp.watch.drawable
+ draw.line(0, 120, 240, 240, 0xf800)
+
+ :param x0: X coordinate of the start of the line, defaults to 0
+ :param x1: Y coordinate of the end of the line, defaults to 0
+ :param y0: Y coordinate of the start of the line, defaults to 0
+ :param y1: Y coordinate of the end of the line, defaults to 0
+ :param color: Color to draw line in, in RGB565 format, defaults to 0xffff
+ """
+
+ px = bytes(((color >> 8) & 0xFF, color & 0xFF))
+ write_data = self._display.write_data
+ set_window = self._display.set_window
+
+ 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
+ w = 1 if dx == 0 else dx
+ h = 1 if dy == 0 else -dy
+ self.fill(color, x0, y0, w, h)
+ return
+ while True:
+ set_window(x0, y0, 1, 1)
+ 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;
+
+