Simple test

Displays annotations, examples relative to SwitchRound widget or as freeform.

examples/displayio_annotation_simpletest.py
 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
10
11import adafruit_touchscreen
12import board
13import displayio
14from adafruit_displayio_layout.widgets.annotation import Annotation
15from adafruit_displayio_layout.widgets.switch_round import SwitchRound as Switch
16
17display = board.DISPLAY
18
19ts = adafruit_touchscreen.Touchscreen(
20    board.TOUCH_XL,
21    board.TOUCH_XR,
22    board.TOUCH_YD,
23    board.TOUCH_YU,
24    calibration=((5200, 59000), (5800, 57000)),
25    size=(display.width, display.height),
26)
27
28# Create the switch widget
29my_switch = Switch(190, 50)
30
31# Create several annotations
32
33# This annotation is positioned relative to the switch widget, with default values.
34switch_annotation = Annotation(
35    widget=my_switch,  # positions are relative to the switch
36    text="Widget Annotation: Switch",
37)
38
39# This annotation is positioned relative to the switch widget, with the line
40# going in the downard direction and anchored at the middle bottom of the switch.
41# The position is "nudged" downward using ``position_offset`` to create a 1 pixel
42# gap between the end of the line and the switch.
43# The text is positioned under the line by setting ``text_under`` to True.
44switch_annotation_under = Annotation(
45    widget=my_switch,  # positions are relative to the switch
46    text="Annotation with: text_under = True",
47    delta_x=-10,
48    delta_y=15,  # line will go in downward direction (positive y)
49    anchor_point=(0.5, 1.0),  # middle, bottom of switch
50    position_offset=(0, 1),  # nudge downward by one pixel
51    text_under=True,
52)
53
54# This is a freeform annotation that is positioned using (x,y) values at the bottom, right
55# corner of the display (display.width, display.height).
56# The line direction is
57freeform_annotation = Annotation(
58    x=display.width,  # uses freeform (x,y) position
59    y=display.height,
60    text="Freeform annotation (display.width, height)",
61)
62
63my_group = displayio.Group()
64my_group.append(my_switch)
65my_group.append(switch_annotation)
66my_group.append(switch_annotation_under)
67my_group.append(freeform_annotation)
68
69# Add my_group to the display
70display.root_group = my_group
71
72# Start the main loop
73while True:
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