Skip to content

Commit 45bd920

Browse files
committed
inclusion the ability to select widget x,y coordinates, update function is working
1 parent e5304e6 commit 45bd920

File tree

2 files changed

+84
-28
lines changed

2 files changed

+84
-28
lines changed

adafruit_displayio_layout/widgets/cartesian.py

Lines changed: 72 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
# pylint: disable=too-many-lines, too-many-instance-attributes, too-many-arguments
2525
# pylint: disable=too-many-locals, too-many-statements
2626

27-
2827
import math
2928
import displayio
3029
import board
3130
import terminalio
3231
from adafruit_displayio_layout.widgets.widget import Widget
3332
from adafruit_displayio_layout.widgets import _blit_rotate_scale
33+
import vectorio
3434

3535
try:
3636
import bitmaptools
@@ -41,53 +41,74 @@
4141
class Cartesian(Widget):
4242
"""A cartesian widget. The origin is set using ``x`` and ``y``.
4343
44-
:param int x: pixel position TODO
45-
:param int y: pixel position TODO
44+
:param int x: x position of the plane origin
45+
:param int y: y position of the plane origin
46+
47+
:param int display_color: background color to use defaults to black (0x000000)
48+
:param int width: requested width, in pixels defaults to screen width
49+
:param int height: requested height, in pixels defaults to screen height
50+
51+
:param int axes_color: axes lines color defaults to white (0xFFFFFF)
52+
:param int axes_stroke: axes lines thickness in pixels defaults to 2
53+
54+
:param int major_tick_stroke: tick lines thickness in pixels dafaults to 1
55+
:param int major_tick_lenght: tick lines lenght in pixels defaults to 5
4656
47-
:param int width: requested width, in pixels TODO
48-
:param int height: requested height, in pixels TODO
57+
:param int tick_label_font: tick label text font
58+
:param int tick_label_color: tick label text color
59+
60+
:param int pointer_radius: pointer radius in pixels defaults to 1
61+
:param int pointer_color: pointer color defaults to white (0xFFFFFF)
4962
5063
"""
5164

5265
def __init__(
5366
self,
67+
x: int = 10,
68+
y: int = board.DISPLAY.height - 10,
5469
display_color=0x000000,
5570
width: int = board.DISPLAY.width,
5671
height: int = board.DISPLAY.height,
57-
axes_color=0xFFFFFF,
58-
axes_stroke=2,
59-
tick_color=0xFFFFFF,
60-
major_tick_stroke=1,
61-
major_tick_length=10,
72+
axes_color: int = 0xFFFFFF,
73+
axes_stroke: int = 2,
74+
tick_color: int = 0xFFFFFF,
75+
major_tick_stroke: int = 1,
76+
major_tick_length: int = 5,
6277
tick_label_font=terminalio.FONT,
63-
tick_label_color=0xFFFFFF,
78+
tick_label_color: int = 0xFFFFFF,
79+
pointer_radius: int = 1,
80+
pointer_color: int = 0xFFFFFF,
6481
**kwargs,
65-
):
82+
) -> None:
6683

6784
super().__init__(**kwargs, max_size=3)
85+
self._origin_x = x
86+
self._origin_y = y
6887

6988
self._margin = 10
7089

7190
self._display_color = display_color
72-
self._display_width = width
73-
self._display_height = height
91+
self._widget_width = width
92+
self._widget_height = height
7493

7594
self._axes_line_color = axes_color
7695
self._axes_line_thickness = axes_stroke
7796

7897
self._tick_color = tick_color
7998
self._tick_line_thickness = major_tick_stroke
8099
self._tick_line_height = major_tick_length
100+
101+
self._pointer_radius = pointer_radius
102+
self._pointer_color = pointer_color
103+
81104
self._font = tick_label_font
82105
self._font_color = tick_label_color
83106

84107
self._font_width = self._get_font_height(self._font, 1)[0]
85108
self._font_height = self._get_font_height(self._font, 1)[1]
86109

87-
self._usable_width = self._display_width - self._font_width - 2 * self._margin
88-
self._usable_height = (
89-
self._display_height - self._font_height - 2 * self._margin
90-
)
110+
self._usable_width = self._widget_width - 2 * self._margin
111+
self._usable_height = self._widget_height - 2 * self._margin
91112
self._tickx_separation = 2 * self._font_width + 2
92113

93114
self._tick_bitmap = displayio.Bitmap(
@@ -106,11 +127,11 @@ def __init__(
106127
self._axesy_bitmap.fill(2)
107128

108129
self._screen_bitmap = displayio.Bitmap(
109-
self._usable_width, self._usable_height, 6
130+
self._usable_width, self._usable_height, 3
110131
)
111132

112133
self._screen_palette = displayio.Palette(6)
113-
self._screen_palette[0] = 0x000000
134+
self._screen_palette.make_transparent(0)
114135
self._screen_palette[1] = self._tick_color
115136
self._screen_palette[2] = self._axes_line_color
116137
self._screen_palette[3] = 0x00FFFF
@@ -120,12 +141,14 @@ def __init__(
120141
self._screen_tilegrid = displayio.TileGrid(
121142
self._screen_bitmap,
122143
pixel_shader=self._screen_palette,
123-
x=self._margin + self._font_width,
124-
y=self._margin,
144+
x=self._origin_x,
145+
y=self._origin_y,
125146
)
126147

127148
self._draw_axes()
128149
self._draw_ticks()
150+
self._draw_pointers()
151+
self.append(self._pointer_vector_shape)
129152
self.append(self._screen_tilegrid)
130153

131154
@staticmethod
@@ -141,7 +164,7 @@ def _get_font_height(font, scale):
141164
def _draw_axes(self):
142165
bitmaptools.rotozoom(
143166
self._screen_bitmap,
144-
ox=self._margin - 1,
167+
ox=self._margin,
145168
oy=self._usable_height,
146169
source_bitmap=self._axesx_bitmap,
147170
px=self._axesx_bitmap.width,
@@ -151,7 +174,7 @@ def _draw_axes(self):
151174

152175
bitmaptools.rotozoom(
153176
self._screen_bitmap,
154-
ox=int(self._usable_width - self._margin / 2),
177+
ox=int(self._usable_width + self._margin),
155178
oy=self._usable_height,
156179
source_bitmap=self._axesy_bitmap,
157180
px=self._axesy_bitmap.width,
@@ -203,3 +226,28 @@ def _draw_ticks(self):
203226
py=int(self._tick_bitmap.height / 2),
204227
angle=(90 * math.pi / 180), # in radians
205228
)
229+
230+
def _draw_pointers(self):
231+
self._pointer = vectorio.Circle(3)
232+
233+
self._circle_palette = displayio.Palette(2)
234+
self._circle_palette.make_transparent(0)
235+
self._circle_palette[1] = 0xFFFFFF
236+
237+
self._pointer_vector_shape = vectorio.VectorShape(
238+
shape=self._pointer,
239+
pixel_shader=self._circle_palette,
240+
x=0,
241+
y=0,
242+
)
243+
244+
def update_pointer(self, x: int, y: int):
245+
"""updater_pointer function
246+
helper function to update pointer in the plane
247+
:param x: x coordinate in the local plane
248+
:param y: y coordinate in the local plane
249+
:return: None
250+
rtype: None
251+
"""
252+
self._pointer_vector_shape.x = self._origin_x + x + self._margin
253+
self._pointer_vector_shape.y = self._origin_y + self._usable_height + y

examples/displayio_layout_cartesian_simpletest.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
This is a basic demonstration of a Cartesian widget.
77
"""
88

9+
import time
910
import board
1011
import displayio
1112
import terminalio
@@ -21,13 +22,15 @@
2122

2223
# Create a Dial widget
2324
my_plane = Cartesian(
24-
width=display.width, # display width
25-
height=display.height, # display height
25+
x=150, # x position for the plane
26+
y=100, # y plane position
27+
width=100, # display width
28+
height=100, # display height
2629
axes_color=0xFFFFFF, # axes line color
2730
axes_stroke=2, # axes lines width in pixels
2831
tick_color=0xFFFFFF, # ticks color
2932
major_tick_stroke=1, # ticks width in pixels
30-
major_tick_length=10, # ticks length in pixels
33+
major_tick_length=5, # ticks length in pixels
3134
tick_label_color=0xFFFFFF, # ticks line color
3235
tick_label_font=tick_font, # the font used for the tick labels
3336
)
@@ -36,5 +39,10 @@
3639
my_group.append(my_plane)
3740
display.show(my_group) # add high level Group to the display
3841

42+
posx = 0
43+
posy = 0
44+
3945
while True:
40-
pass
46+
my_plane.update_pointer(posx, posy)
47+
display.show(my_group)
48+
time.sleep(0.5)

0 commit comments

Comments
 (0)