From c24681e696ac63e22b7bd84c39e860c8ab5a1f06 Mon Sep 17 00:00:00 2001 From: s-light Date: Thu, 16 Dec 2021 07:43:55 +0100 Subject: [PATCH 1/7] cartesian: fix basic update_line & add example --- .../widgets/cartesian.py | 1 + .../displayio_layout_cartesian_lineplot.py | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 examples/displayio_layout_cartesian_lineplot.py diff --git a/adafruit_displayio_layout/widgets/cartesian.py b/adafruit_displayio_layout/widgets/cartesian.py index 6628906..5955be7 100644 --- a/adafruit_displayio_layout/widgets/cartesian.py +++ b/adafruit_displayio_layout/widgets/cartesian.py @@ -508,3 +508,4 @@ def update_line(self, x: int, y: int) -> None: local_y, 1, ) + self.plot_line_point.append((local_x, local_y)) diff --git a/examples/displayio_layout_cartesian_lineplot.py b/examples/displayio_layout_cartesian_lineplot.py new file mode 100644 index 0000000..192b952 --- /dev/null +++ b/examples/displayio_layout_cartesian_lineplot.py @@ -0,0 +1,55 @@ +# SPDX-FileCopyrightText: 2021 Stefan Krüger +# +# SPDX-License-Identifier: MIT +############################# +""" +This is a basic demonstration of a Cartesian widget for line-ploting +""" + +import time +import board +import displayio +from adafruit_displayio_layout.widgets.cartesian import Cartesian + +# create the display on the PyPortal or Clue or PyBadge(for example) +display = board.DISPLAY +# otherwise change this to setup the display +# for display chip driver and pinout you have (e.g. ILI9341) + +# pybadge display: 160x128 +# Create a Cartesian widget +# https://circuitpython.readthedocs.io/projects/displayio-layout/en/latest/api.html#module-adafruit_displayio_layout.widgets.cartesian +my_plane = Cartesian( + x=30, # x position for the plane + y=2, # y plane position + width=128, # display width + height=105, # display height + xrange=(0, 100), # x range + yrange=(0, 100), # y range +) + +my_group = displayio.Group() +my_group.append(my_plane) +display.show(my_group) # add high level Group to the display + +data = [ + (0, 0), + (1, 10), + (20, 50), + (30, 60), + (40, 40), + (50, 80), + (60, 20), + (70, 60), + (80, 30), + (100, 100), +] + +print("examples/displayio_layout_cartesian_lineplot.py") +for x, y in data: + my_plane.update_line(x, y) + time.sleep(0.5) + print(my_plane.plot_line_point) + +while True: + pass From 5d60353e43c1b07141fab7c8f6835c2a8e07dae8 Mon Sep 17 00:00:00 2001 From: s-light Date: Thu, 16 Dec 2021 07:52:43 +0100 Subject: [PATCH 2/7] extract local calculation --- .../widgets/cartesian.py | 17 +++++----- .../displayio_layout_cartesian_lineplot.py | 32 +++++++++++++------ 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/adafruit_displayio_layout/widgets/cartesian.py b/adafruit_displayio_layout/widgets/cartesian.py index 5955be7..4d92965 100644 --- a/adafruit_displayio_layout/widgets/cartesian.py +++ b/adafruit_displayio_layout/widgets/cartesian.py @@ -457,6 +457,13 @@ def _draw_pointers(self, x: int, y: int) -> None: self.append(self._pointer) + def _calc_local_xy(self, x: int, y: int) -> (int, int): + local_x = int((x - self._xrange[0]) * self._factorx) + self._nudge_x + local_y = ( + int((self._yrange[0] - y) * self._factory) + self.height + self._nudge_y + ) + return (local_x, local_y) + def update_pointer(self, x: int, y: int) -> None: """updater_pointer function helper function to update pointer in the plane @@ -465,10 +472,7 @@ def update_pointer(self, x: int, y: int) -> None: :return: None rtype: None """ - local_x = int((x - self._xrange[0]) * self._factorx) + self._nudge_x - local_y = ( - int((self._yrange[0] - y) * self._factory) + self.height + self._nudge_y - ) + local_x, local_y = self._calc_local_xy(x, y) if local_x >= 0 or local_y <= 100: if self._update_line: @@ -489,10 +493,7 @@ def update_line(self, x: int, y: int) -> None: :return: None rtype: None """ - local_x = int((x - self._xrange[0]) * self._factorx) + self._nudge_x - local_y = ( - int((self._yrange[0] - y) * self._factory) + self.height + self._nudge_y - ) + local_x, local_y = self._calc_local_xy(x, y) if x < self._xrange[1] and y < self._yrange[1]: if local_x > 0 or local_y < 100: if self._update_line: diff --git a/examples/displayio_layout_cartesian_lineplot.py b/examples/displayio_layout_cartesian_lineplot.py index 192b952..ba1fd20 100644 --- a/examples/displayio_layout_cartesian_lineplot.py +++ b/examples/displayio_layout_cartesian_lineplot.py @@ -34,20 +34,34 @@ data = [ (0, 0), - (1, 10), - (20, 50), - (30, 60), + (10, 10), + (20, 20), + (30, 30), (40, 40), - (50, 80), - (60, 20), - (70, 60), - (80, 30), - (100, 100), + (50, 50), + (60, 60), + (70, 70), + (80, 80), + (90, 90), ] +# +# data = [ +# (0, 0), +# (10, 10), +# (20, 50), +# (30, 60), +# (40, 40), +# (50, 80), +# (60, 20), +# (70, 60), +# (80, 30), +# (100, 100), +# ] print("examples/displayio_layout_cartesian_lineplot.py") for x, y in data: - my_plane.update_line(x, y) + # my_plane.update_line(x, y) + my_plane.update_pointer(x, y) time.sleep(0.5) print(my_plane.plot_line_point) From af4c74cf3c1e900f30200d08b6391a54cfe02a41 Mon Sep 17 00:00:00 2001 From: s-light Date: Thu, 16 Dec 2021 09:44:06 +0100 Subject: [PATCH 3/7] debug _calc_local_xy --- .../widgets/cartesian.py | 8 ++- examples/displayio_layout_cartesian_dev.py | 56 +++++++++++++++++++ .../displayio_layout_cartesian_lineplot.py | 37 ++++-------- 3 files changed, 72 insertions(+), 29 deletions(-) create mode 100644 examples/displayio_layout_cartesian_dev.py diff --git a/adafruit_displayio_layout/widgets/cartesian.py b/adafruit_displayio_layout/widgets/cartesian.py index 4d92965..f6dc6ef 100644 --- a/adafruit_displayio_layout/widgets/cartesian.py +++ b/adafruit_displayio_layout/widgets/cartesian.py @@ -389,10 +389,12 @@ def _draw_ticks(self) -> None: if self._subticks: if i in subticks: + # calc subtick_line_height; force min lineheigt to 1. + subtick_line_height = max(1, self._tick_line_height // 2) rectangle_helper( text_dist, self._axes_line_thickness, - self._tick_line_height // 2, + subtick_line_height, 1, self._axesx_bitmap, 1, @@ -458,10 +460,13 @@ def _draw_pointers(self, x: int, y: int) -> None: self.append(self._pointer) def _calc_local_xy(self, x: int, y: int) -> (int, int): + # x_size = self._xrange[1], self._xrange[0] + local_x = int((x - self._xrange[0]) * self._factorx) + self._nudge_x local_y = ( int((self._yrange[0] - y) * self._factory) + self.height + self._nudge_y ) + print("({: >3}, {: >3}) -- ({: >3}, {: >3})".format(x, y, local_x, local_y)) return (local_x, local_y) def update_pointer(self, x: int, y: int) -> None: @@ -473,7 +478,6 @@ def update_pointer(self, x: int, y: int) -> None: rtype: None """ local_x, local_y = self._calc_local_xy(x, y) - if local_x >= 0 or local_y <= 100: if self._update_line: self._draw_pointers(local_x, local_y) diff --git a/examples/displayio_layout_cartesian_dev.py b/examples/displayio_layout_cartesian_dev.py new file mode 100644 index 0000000..072206c --- /dev/null +++ b/examples/displayio_layout_cartesian_dev.py @@ -0,0 +1,56 @@ +# SPDX-FileCopyrightText: 2021 Stefan Krüger +# +# SPDX-License-Identifier: MIT +############################# +""" +This is a basic demonstration of a Cartesian widget for line-ploting +""" + +import time +import board +import displayio +from adafruit_displayio_layout.widgets.cartesian import Cartesian + +# create the display on the PyPortal or Clue or PyBadge(for example) +display = board.DISPLAY +# otherwise change this to setup the display +# for display chip driver and pinout you have (e.g. ILI9341) + +# pybadge display: 160x128 +# Create a Cartesian widget +# https://circuitpython.readthedocs.io/projects/displayio-layout/en/latest/api.html#module-adafruit_displayio_layout.widgets.cartesian +my_plane = Cartesian( + x=11, # x position for the plane + y=0, # y plane position + width=140, # display width + height=120, # display height + xrange=(0, 10), # x range + yrange=(0, 10), # y range + major_tick_stroke=1, # ticks width in pixels + major_tick_length=2, # ticks length in pixels + axes_stroke=1, # axes lines width in pixels + axes_color=0x10A0A0, # axes line color + subticks=True, +) + +my_group = displayio.Group() +my_group.append(my_plane) +display.show(my_group) # add high level Group to the display + +data = [ + (0, 0), + (1, 1), + (3, 3), + (5, 5), + (7, 7), + (9, 9), +] + +print("examples/displayio_layout_cartesian_lineplot.py") +for x, y in data: + my_plane.update_line(x, y) + # my_plane.update_pointer(x, y) + time.sleep(2.5) + +while True: + pass diff --git a/examples/displayio_layout_cartesian_lineplot.py b/examples/displayio_layout_cartesian_lineplot.py index ba1fd20..bec782f 100644 --- a/examples/displayio_layout_cartesian_lineplot.py +++ b/examples/displayio_layout_cartesian_lineplot.py @@ -20,9 +20,9 @@ # Create a Cartesian widget # https://circuitpython.readthedocs.io/projects/displayio-layout/en/latest/api.html#module-adafruit_displayio_layout.widgets.cartesian my_plane = Cartesian( - x=30, # x position for the plane - y=2, # y plane position - width=128, # display width + x=20, # x position for the plane + y=0, # y plane position + width=130, # display width height=105, # display height xrange=(0, 100), # x range yrange=(0, 100), # y range @@ -35,35 +35,18 @@ data = [ (0, 0), (10, 10), - (20, 20), - (30, 30), - (40, 40), + (30, 10), (50, 50), - (60, 60), - (70, 70), - (80, 80), - (90, 90), + (70, 30), + (90, 80), + (95, 80), + (100, 100), ] -# -# data = [ -# (0, 0), -# (10, 10), -# (20, 50), -# (30, 60), -# (40, 40), -# (50, 80), -# (60, 20), -# (70, 60), -# (80, 30), -# (100, 100), -# ] print("examples/displayio_layout_cartesian_lineplot.py") for x, y in data: - # my_plane.update_line(x, y) - my_plane.update_pointer(x, y) - time.sleep(0.5) - print(my_plane.plot_line_point) + my_plane.update_line(x, y) + time.sleep(1) while True: pass From ff529e6c9e83d26174fc8602bc085cfe4f30ae65 Mon Sep 17 00:00:00 2001 From: s-light Date: Thu, 16 Dec 2021 10:19:10 +0100 Subject: [PATCH 4/7] analyse range checking --- .../widgets/cartesian.py | 89 ++++++++++++++++++- examples/displayio_layout_cartesian_dev.py | 1 + 2 files changed, 86 insertions(+), 4 deletions(-) diff --git a/adafruit_displayio_layout/widgets/cartesian.py b/adafruit_displayio_layout/widgets/cartesian.py index f6dc6ef..334d92a 100644 --- a/adafruit_displayio_layout/widgets/cartesian.py +++ b/adafruit_displayio_layout/widgets/cartesian.py @@ -464,11 +464,34 @@ def _calc_local_xy(self, x: int, y: int) -> (int, int): local_x = int((x - self._xrange[0]) * self._factorx) + self._nudge_x local_y = ( - int((self._yrange[0] - y) * self._factory) + self.height + self._nudge_y + int((self._yrange[0] - y) * self._factory) + + (self.height - 1) + + self._nudge_y + ) + print( + "({: >3}, {: >3}) --> ({: >3}, {: >3})".format( + x, + y, + local_x, + local_y, + ) ) - print("({: >3}, {: >3}) -- ({: >3}, {: >3})".format(x, y, local_x, local_y)) return (local_x, local_y) + def _check_local_xy_in_range(self, local_x, local_y): + in_range = False + if (0 <= local_x < self.width) and (0 <= local_y < self.height): + in_range = True + return in_range + + def _check_xy_in_range(self, x, y): + in_range = False + if (self._xrange[0] <= x <= self._xrange[1]) and ( + self._yrange[0] <= y <= self._yrange[1] + ): + in_range = True + return in_range + def update_pointer(self, x: int, y: int) -> None: """updater_pointer function helper function to update pointer in the plane @@ -497,14 +520,44 @@ def update_line(self, x: int, y: int) -> None: :return: None rtype: None """ + print( + "x:{: >4}; _xrange({: >4}, {: >4})\n" + "y:{: >4}; _yrange({: >4}, {: >4})\n" + "".format( + x, + self._xrange[0], + self._xrange[1], + y, + self._yrange[0], + self._yrange[1], + ) + ) local_x, local_y = self._calc_local_xy(x, y) - if x < self._xrange[1] and y < self._yrange[1]: - if local_x > 0 or local_y < 100: + print( + "local_x:{: >4}; _xrange({: >4}, {: >4})\n" + "local_y:{: >4}; _yrange({: >4}, {: >4})\n" + "".format( + local_x, + 0, + self.width, + local_y, + 0, + self.height, + ) + ) + if self._check_xy_in_range(x, y): + if self._check_local_xy_in_range(local_x, local_y): if self._update_line: self._set_plotter_line() self.plot_line_point.append((local_x, local_y)) self._update_line = False else: + print( + "line_start ({: >3}, {: >3})".format( + self.plot_line_point[-1][0], + self.plot_line_point[-1][1], + ) + ) bitmaptools.draw_line( self._screen_bitmap, self.plot_line_point[-1][0], @@ -514,3 +567,31 @@ def update_line(self, x: int, y: int) -> None: 1, ) self.plot_line_point.append((local_x, local_y)) + else: + raise ValueError( + "local_x or local_y out of range:\n" + "local_x:{: >4}; _xrange({: >4}, {: >4})\n" + "local_y:{: >4}; _yrange({: >4}, {: >4})\n" + "".format( + local_x, + 0, + self.width, + local_y, + 0, + self.height, + ) + ) + else: + raise ValueError( + "local_x or local_y out of range:\n" + "x:{: >4}; _xrange({: >4}, {: >4})\n" + "y:{: >4}; _yrange({: >4}, {: >4})\n" + "".format( + x, + self._xrange[0], + self._xrange[1], + y, + self._yrange[0], + self._yrange[1], + ) + ) diff --git a/examples/displayio_layout_cartesian_dev.py b/examples/displayio_layout_cartesian_dev.py index 072206c..49a7c0f 100644 --- a/examples/displayio_layout_cartesian_dev.py +++ b/examples/displayio_layout_cartesian_dev.py @@ -50,6 +50,7 @@ for x, y in data: my_plane.update_line(x, y) # my_plane.update_pointer(x, y) + # print(my_plane.plot_line_point) time.sleep(2.5) while True: From 096cf051645e3e7dd5c46a9921742604ffa22e80 Mon Sep 17 00:00:00 2001 From: s-light Date: Thu, 16 Dec 2021 11:06:26 +0100 Subject: [PATCH 5/7] fix range checking and add nice errormessages --- .../widgets/cartesian.py | 128 +++++++++++------- examples/displayio_layout_cartesian_dev.py | 17 ++- 2 files changed, 91 insertions(+), 54 deletions(-) diff --git a/adafruit_displayio_layout/widgets/cartesian.py b/adafruit_displayio_layout/widgets/cartesian.py index 334d92a..214cf3c 100644 --- a/adafruit_displayio_layout/widgets/cartesian.py +++ b/adafruit_displayio_layout/widgets/cartesian.py @@ -468,29 +468,35 @@ def _calc_local_xy(self, x: int, y: int) -> (int, int): + (self.height - 1) + self._nudge_y ) - print( - "({: >3}, {: >3}) --> ({: >3}, {: >3})".format( - x, - y, - local_x, - local_y, - ) - ) + # print( + # "({: >3}, {: >3}) --> ({: >3}, {: >3})".format( + # x, + # y, + # local_x, + # local_y, + # ) + # ) return (local_x, local_y) + def _check_local_x_in_range(self, local_x): + return 0 <= local_x < self.width + + def _check_local_y_in_range(self, local_y): + return 0 <= local_y < self.height + def _check_local_xy_in_range(self, local_x, local_y): - in_range = False - if (0 <= local_x < self.width) and (0 <= local_y < self.height): - in_range = True - return in_range + return self._check_local_x_in_range(local_x) and self._check_local_y_in_range( + local_y + ) + + def _check_x_in_range(self, x): + return self._xrange[0] <= x <= self._xrange[1] + + def _check_y_in_range(self, y): + return self._yrange[0] <= y <= self._yrange[1] def _check_xy_in_range(self, x, y): - in_range = False - if (self._xrange[0] <= x <= self._xrange[1]) and ( - self._yrange[0] <= y <= self._yrange[1] - ): - in_range = True - return in_range + return self._check_x_in_range(x) and self._check_y_in_range(y) def update_pointer(self, x: int, y: int) -> None: """updater_pointer function @@ -520,27 +526,30 @@ def update_line(self, x: int, y: int) -> None: :return: None rtype: None """ + print("") print( - "x:{: >4}; _xrange({: >4}, {: >4})\n" - "y:{: >4}; _yrange({: >4}, {: >4})\n" + "xy: ({: >4}, {: >4}) " + "_xrange: ({: >4}, {: >4}) " + "_yrange: ({: >4}, {: >4}) " "".format( x, + y, self._xrange[0], self._xrange[1], - y, self._yrange[0], self._yrange[1], ) ) local_x, local_y = self._calc_local_xy(x, y) print( - "local_x:{: >4}; _xrange({: >4}, {: >4})\n" - "local_y:{: >4}; _yrange({: >4}, {: >4})\n" + "local_*: ({: >4}, {: >4}) " + " width: ({: >4}, {: >4}) " + " height: ({: >4}, {: >4}) " "".format( local_x, + local_y, 0, self.width, - local_y, 0, self.height, ) @@ -552,12 +561,12 @@ def update_line(self, x: int, y: int) -> None: self.plot_line_point.append((local_x, local_y)) self._update_line = False else: - print( - "line_start ({: >3}, {: >3})".format( - self.plot_line_point[-1][0], - self.plot_line_point[-1][1], - ) - ) + # print( + # "line_start ({: >3}, {: >3})".format( + # self.plot_line_point[-1][0], + # self.plot_line_point[-1][1], + # ) + # ) bitmaptools.draw_line( self._screen_bitmap, self.plot_line_point[-1][0], @@ -568,30 +577,45 @@ def update_line(self, x: int, y: int) -> None: ) self.plot_line_point.append((local_x, local_y)) else: + # for better error messages we check in detail what failed... + if not self._check_local_x_in_range(local_x): + raise ValueError( + "local_x out of range: " + "local_x:{: >4}; _xrange({: >4}, {: >4})" + "".format( + local_x, + 0, + self.width, + ) + ) + if not self._check_local_y_in_range(local_y): + raise ValueError( + "local_y out of range: " + "local_y:{: >4}; _yrange({: >4}, {: >4})" + "".format( + local_y, + 0, + self.height, + ) + ) + else: + if not self._check_x_in_range(x): raise ValueError( - "local_x or local_y out of range:\n" - "local_x:{: >4}; _xrange({: >4}, {: >4})\n" - "local_y:{: >4}; _yrange({: >4}, {: >4})\n" + "x out of range: " + "x:{: >4}; xrange({: >4}, {: >4})" "".format( - local_x, - 0, - self.width, - local_y, - 0, - self.height, + x, + self._xrange[0], + self._xrange[1], ) ) - else: - raise ValueError( - "local_x or local_y out of range:\n" - "x:{: >4}; _xrange({: >4}, {: >4})\n" - "y:{: >4}; _yrange({: >4}, {: >4})\n" - "".format( - x, - self._xrange[0], - self._xrange[1], - y, - self._yrange[0], - self._yrange[1], + if not self._check_y_in_range(y): + raise ValueError( + "y out of range: " + "y:{: >4}; yrange({: >4}, {: >4})" + "".format( + y, + self._yrange[0], + self._yrange[1], + ) ) - ) diff --git a/examples/displayio_layout_cartesian_dev.py b/examples/displayio_layout_cartesian_dev.py index 49a7c0f..a26a5bd 100644 --- a/examples/displayio_layout_cartesian_dev.py +++ b/examples/displayio_layout_cartesian_dev.py @@ -38,20 +38,33 @@ display.show(my_group) # add high level Group to the display data = [ - (0, 0), + # (0, 0), (1, 1), + (1, 15), + (2, 1), + (2, 2), (3, 3), + (4, 3), + (4, 4), (5, 5), + (6, 5), + (6, 6), (7, 7), + (8, 7), + (8, 8), (9, 9), + (10, 9), + (10, 10), ] print("examples/displayio_layout_cartesian_lineplot.py") + +my_plane.update_line(0, 0) for x, y in data: my_plane.update_line(x, y) # my_plane.update_pointer(x, y) # print(my_plane.plot_line_point) - time.sleep(2.5) + time.sleep(0.5) while True: pass From 3bf14f2d1e98bae3a9af71c4936556b28b9e8d27 Mon Sep 17 00:00:00 2001 From: s-light Date: Thu, 16 Dec 2021 11:46:19 +0100 Subject: [PATCH 6/7] fix graph alignment error --- .../widgets/cartesian.py | 22 +++++++++++++++++-- examples/displayio_layout_cartesian_dev.py | 4 ++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/adafruit_displayio_layout/widgets/cartesian.py b/adafruit_displayio_layout/widgets/cartesian.py index 214cf3c..ddb5311 100644 --- a/adafruit_displayio_layout/widgets/cartesian.py +++ b/adafruit_displayio_layout/widgets/cartesian.py @@ -462,12 +462,26 @@ def _draw_pointers(self, x: int, y: int) -> None: def _calc_local_xy(self, x: int, y: int) -> (int, int): # x_size = self._xrange[1], self._xrange[0] - local_x = int((x - self._xrange[0]) * self._factorx) + self._nudge_x + # ok we have to calculate the local coordinates from the range: + # we have some things prepared allready: + # self._xrange = xrange + # self._normx = (self._xrange[1] - self._xrange[0]) / 100 + # self._valuex = self.width / 100 + # self._factorx = 100 / (self._xrange[1] - self._xrange[0]) + + local_x = ( + int((x - self._xrange[0]) * self._factorx * self._valuex) + self._nudge_x + ) + # details on `+ (self.height - 1)` + # we make sure we stay in range.. () + # as the bitmap is set to self.width & self.height + # but we are only allowed to draw 0..height-1 and 0..width-1 local_y = ( - int((self._yrange[0] - y) * self._factory) + int((self._yrange[0] - y) * self._factory * self._valuey) + (self.height - 1) + self._nudge_y ) + # print( # "({: >3}, {: >3}) --> ({: >3}, {: >3})".format( # x, @@ -578,6 +592,9 @@ def update_line(self, x: int, y: int) -> None: self.plot_line_point.append((local_x, local_y)) else: # for better error messages we check in detail what failed... + # this should never happen: + # we already checked the range of the input values. + # but in case our calculation is wrong we handle this case to.. if not self._check_local_x_in_range(local_x): raise ValueError( "local_x out of range: " @@ -599,6 +616,7 @@ def update_line(self, x: int, y: int) -> None: ) ) else: + # for better error messages we check in detail what failed... if not self._check_x_in_range(x): raise ValueError( "x out of range: " diff --git a/examples/displayio_layout_cartesian_dev.py b/examples/displayio_layout_cartesian_dev.py index a26a5bd..64ca565 100644 --- a/examples/displayio_layout_cartesian_dev.py +++ b/examples/displayio_layout_cartesian_dev.py @@ -23,7 +23,7 @@ x=11, # x position for the plane y=0, # y plane position width=140, # display width - height=120, # display height + height=110, # display height xrange=(0, 10), # x range yrange=(0, 10), # y range major_tick_stroke=1, # ticks width in pixels @@ -40,7 +40,7 @@ data = [ # (0, 0), (1, 1), - (1, 15), + # (1, 15), # create out of range error (2, 1), (2, 2), (3, 3), From b57349d45d144636340237da156e5c4d49da901c Mon Sep 17 00:00:00 2001 From: s-light Date: Thu, 16 Dec 2021 12:06:21 +0100 Subject: [PATCH 7/7] rearange for less duplicate code. --- .../widgets/cartesian.py | 113 +++++++----------- examples/displayio_layout_cartesian_dev.py | 3 +- 2 files changed, 47 insertions(+), 69 deletions(-) diff --git a/adafruit_displayio_layout/widgets/cartesian.py b/adafruit_displayio_layout/widgets/cartesian.py index ddb5311..1dc948e 100644 --- a/adafruit_displayio_layout/widgets/cartesian.py +++ b/adafruit_displayio_layout/widgets/cartesian.py @@ -310,8 +310,6 @@ def __init__( self.append(self._screen_tilegrid) self.append(self._corner_tilegrid) - self._update_line = True - self._pointer = None self._circle_palette = None self.plot_line_point = None @@ -460,36 +458,17 @@ def _draw_pointers(self, x: int, y: int) -> None: self.append(self._pointer) def _calc_local_xy(self, x: int, y: int) -> (int, int): - # x_size = self._xrange[1], self._xrange[0] - - # ok we have to calculate the local coordinates from the range: - # we have some things prepared allready: - # self._xrange = xrange - # self._normx = (self._xrange[1] - self._xrange[0]) / 100 - # self._valuex = self.width / 100 - # self._factorx = 100 / (self._xrange[1] - self._xrange[0]) - local_x = ( int((x - self._xrange[0]) * self._factorx * self._valuex) + self._nudge_x ) - # details on `+ (self.height - 1)` - # we make sure we stay in range.. () - # as the bitmap is set to self.width & self.height - # but we are only allowed to draw 0..height-1 and 0..width-1 + # details on `+ (self.height - 1)` : + # the bitmap is set to self.width & self.height + # but we are only allowed to draw to pixels 0..height-1 and 0..width-1 local_y = ( int((self._yrange[0] - y) * self._factory * self._valuey) + (self.height - 1) + self._nudge_y ) - - # print( - # "({: >3}, {: >3}) --> ({: >3}, {: >3})".format( - # x, - # y, - # local_x, - # local_y, - # ) - # ) return (local_x, local_y) def _check_local_x_in_range(self, local_x): @@ -512,29 +491,9 @@ def _check_y_in_range(self, y): def _check_xy_in_range(self, x, y): return self._check_x_in_range(x) and self._check_y_in_range(y) - def update_pointer(self, x: int, y: int) -> None: - """updater_pointer function - helper function to update pointer in the plane - :param int x: ``x`` coordinate in the local plane - :param int y: ``y`` coordinate in the local plane - :return: None - rtype: None - """ - local_x, local_y = self._calc_local_xy(x, y) - if local_x >= 0 or local_y <= 100: - if self._update_line: - self._draw_pointers(local_x, local_y) - self._update_line = False - else: - self._pointer.x = local_x - self._pointer.y = local_y - - def _set_plotter_line(self) -> None: - self.plot_line_point = [] - - def update_line(self, x: int, y: int) -> None: - """updater_line function - helper function to update pointer in the plane + def _add_point(self, x: int, y: int) -> None: + """_add_point function + helper function to add a point to the graph in the plane :param int x: ``x`` coordinate in the local plane :param int y: ``y`` coordinate in the local plane :return: None @@ -570,26 +529,9 @@ def update_line(self, x: int, y: int) -> None: ) if self._check_xy_in_range(x, y): if self._check_local_xy_in_range(local_x, local_y): - if self._update_line: - self._set_plotter_line() - self.plot_line_point.append((local_x, local_y)) - self._update_line = False - else: - # print( - # "line_start ({: >3}, {: >3})".format( - # self.plot_line_point[-1][0], - # self.plot_line_point[-1][1], - # ) - # ) - bitmaptools.draw_line( - self._screen_bitmap, - self.plot_line_point[-1][0], - self.plot_line_point[-1][1], - local_x, - local_y, - 1, - ) - self.plot_line_point.append((local_x, local_y)) + if self.plot_line_point is None: + self.plot_line_point = [] + self.plot_line_point.append((local_x, local_y)) else: # for better error messages we check in detail what failed... # this should never happen: @@ -637,3 +579,40 @@ def update_line(self, x: int, y: int) -> None: self._yrange[1], ) ) + + def update_pointer(self, x: int, y: int) -> None: + """updater_pointer function + helper function to update pointer in the plane + :param int x: ``x`` coordinate in the local plane + :param int y: ``y`` coordinate in the local plane + :return: None + rtype: None + """ + self._add_point(x, y) + if not self._pointer: + self._draw_pointers( + self.plot_line_point[-1][0], + self.plot_line_point[-1][1], + ) + else: + self._pointer.x = self.plot_line_point[-1][0] + self._pointer.y = self.plot_line_point[-1][1] + + def update_line(self, x: int, y: int) -> None: + """updater_line function + helper function to update line in the plane + :param int x: ``x`` coordinate in the local plane + :param int y: ``y`` coordinate in the local plane + :return: None + rtype: None + """ + self._add_point(x, y) + if len(self.plot_line_point) > 1: + bitmaptools.draw_line( + self._screen_bitmap, + self.plot_line_point[-2][0], + self.plot_line_point[-2][1], + self.plot_line_point[-1][0], + self.plot_line_point[-1][1], + 1, + ) diff --git a/examples/displayio_layout_cartesian_dev.py b/examples/displayio_layout_cartesian_dev.py index 64ca565..39a95c3 100644 --- a/examples/displayio_layout_cartesian_dev.py +++ b/examples/displayio_layout_cartesian_dev.py @@ -62,8 +62,7 @@ my_plane.update_line(0, 0) for x, y in data: my_plane.update_line(x, y) - # my_plane.update_pointer(x, y) - # print(my_plane.plot_line_point) + my_plane.update_pointer(x, y) time.sleep(0.5) while True: