Simple test¶
Displays annotations, examples relative to SwitchRound widget or as freeform.
1# SPDX-FileCopyrightText: 2021 Kevin Matocha
2#
3# SPDX-License-Identifier: MIT
4"""
5Example of the Annotation widget to annotate a Switch widget or
6for freeform annotation.
7"""
8
9import time
10import board
11import displayio
12import adafruit_touchscreen
13from adafruit_displayio_layout.widgets.switch_round import SwitchRound as Switch
14from adafruit_displayio_layout.widgets.annotation import Annotation
15
16display = board.DISPLAY
17
18ts = adafruit_touchscreen.Touchscreen(
19 board.TOUCH_XL,
20 board.TOUCH_XR,
21 board.TOUCH_YD,
22 board.TOUCH_YU,
23 calibration=((5200, 59000), (5800, 57000)),
24 size=(display.width, display.height),
25)
26
27# Create the switch widget
28my_switch = Switch(190, 50)
29
30# Create several annotations
31
32# This annotation is positioned relative to the switch widget, with default values.
33switch_annotation = Annotation(
34 widget=my_switch, # positions are relative to the switch
35 text="Widget Annotation: Switch",
36)
37
38# This annotation is positioned relative to the switch widget, with the line
39# going in the downard direction and anchored at the middle bottom of the switch.
40# The position is "nudged" downward using ``position_offset`` to create a 1 pixel
41# gap between the end of the line and the switch.
42# The text is positioned under the line by setting ``text_under`` to True.
43switch_annotation_under = Annotation(
44 widget=my_switch, # positions are relative to the switch
45 text="Annotation with: text_under = True",
46 delta_x=-10,
47 delta_y=15, # line will go in downward direction (positive y)
48 anchor_point=(0.5, 1.0), # middle, bottom of switch
49 position_offset=(0, 1), # nudge downward by one pixel
50 text_under=True,
51)
52
53# This is a freeform annotation that is positioned using (x,y) values at the bottom, right
54# corner of the display (display.width, display.height).
55# The line direction is
56freeform_annotation = Annotation(
57 x=display.width, # uses freeform (x,y) position
58 y=display.height,
59 text="Freeform annotation (display.width, height)",
60)
61
62my_group = displayio.Group()
63my_group.append(my_switch)
64my_group.append(switch_annotation)
65my_group.append(switch_annotation_under)
66my_group.append(freeform_annotation)
67
68# Add my_group to the display
69display.show(my_group)
70
71# Start the main loop
72while True:
73
74 p = ts.touch_point # get any touches on the screen
75
76 if p: # Check each switch if the touch point is within the switch touch area
77 # If touched, then flip the switch with .selected
78 if my_switch.contains(p):
79 my_switch.selected(p)
80
81 time.sleep(0.05) # touch response on PyPortal is more accurate with a small delay