Simple test

Create a single sliding switch.

examples/displayio_switchround_simpletest.py
 1# SPDX-FileCopyrightText: 2021 Kevin Matocha
 2#
 3# SPDX-License-Identifier: MIT
 4"""
 5Creates a single sliding switch widget.
 6"""
 7
 8import time
 9import board
10import displayio
11import adafruit_touchscreen
12from adafruit_displayio_layout.widgets.switch_round import SwitchRound as Switch
13
14display = board.DISPLAY
15
16ts = adafruit_touchscreen.Touchscreen(
17    board.TOUCH_XL,
18    board.TOUCH_XR,
19    board.TOUCH_YD,
20    board.TOUCH_YU,
21    calibration=((5200, 59000), (5800, 57000)),
22    size=(display.width, display.height),
23)
24
25# Create the switch
26my_switch = Switch(20, 30)
27
28
29my_group = displayio.Group()
30my_group.append(my_switch)
31
32# Add my_group to the display
33display.show(my_group)
34
35# Start the main loop
36while True:
37
38    p = ts.touch_point  # get any touches on the screen
39
40    if p:  # Check each switch if the touch point is within the switch touch area
41        # If touched, then flip the switch with .selected
42        if my_switch.contains(p):
43            my_switch.selected(p)
44
45    time.sleep(0.05)  # touch response on PyPortal is more accurate with a small delay

Switch test with multiple switches

Create multiple sliding switch with various sizes and orientations.

examples/displayio_switchround_multiple.py
  1# SPDX-FileCopyrightText: 2021 Kevin Matocha
  2#
  3# SPDX-License-Identifier: MIT
  4"""
  5Creates multiple sliding switch widgets of various size and orientations.
  6"""
  7
  8import time
  9import board
 10import displayio
 11import adafruit_touchscreen
 12from adafruit_displayio_layout.widgets.switch_round import SwitchRound as Switch
 13
 14display = board.DISPLAY
 15
 16# setup the touch screen
 17ts = adafruit_touchscreen.Touchscreen(
 18    board.TOUCH_XL,
 19    board.TOUCH_XR,
 20    board.TOUCH_YD,
 21    board.TOUCH_YU,
 22    calibration=((5200, 59000), (5800, 57000)),
 23    size=(display.width, display.height),
 24)
 25
 26
 27# Create the switches
 28
 29my_switch = Switch(20, 30)
 30
 31my_switch2 = Switch(
 32    x=120,
 33    y=35,
 34    height=30,  # Set height to 30 pixels.  If you do not specify width,
 35    # it is automatically set to a default aspect ratio
 36    touch_padding=10,  # add extra boundary for touch response
 37    value=True,
 38)  # initial value is set to True
 39
 40my_switch3 = Switch(
 41    x=20,
 42    y=85,
 43    height=40,
 44    fill_color_off=(255, 0, 0),  # Set off colorred, can use hex code (0xFF0000)
 45    outline_color_off=(80, 0, 0),
 46    background_color_off=(150, 0, 0),
 47    background_outline_color_off=(30, 0, 0),
 48)
 49
 50my_switch4 = Switch(
 51    x=120,
 52    y=85,
 53    height=40,
 54    width=110,  # you can set the width manually but it may look weird
 55)
 56
 57my_switch5 = Switch(
 58    x=20,
 59    y=140,
 60    height=40,
 61    display_button_text=False,  # do not show the 0/1 on the switch
 62)
 63
 64my_switch6 = Switch(
 65    x=120,
 66    y=140,
 67    height=40,
 68    horizontal=False,  # set orientation to vertical
 69)
 70
 71my_switch7 = Switch(
 72    x=180,
 73    y=140,
 74    height=40,
 75    horizontal=False,  # set orientation to vertical
 76    flip=True,  # swap the direction
 77)
 78
 79my_switch8 = Switch(
 80    x=0,
 81    y=0,  # this is a larger, vertical orientation switch
 82    height=60,
 83    horizontal=False,  # set orientation to vertical
 84    flip=True,  # swap the direction
 85)
 86# use anchor_point and anchored_position to set the my_switch8 position
 87# relative to the display size.
 88my_switch8.anchor_point = (1.0, 1.0)
 89# the switch anchor_point is the bottom right switch corner
 90my_switch8.anchored_position = (display.width - 10, display.height - 10)
 91# the switch anchored_position is 10 pixels from the display
 92# lower right corner
 93
 94my_group = displayio.Group()
 95my_group.append(my_switch)
 96my_group.append(my_switch2)
 97my_group.append(my_switch3)
 98my_group.append(my_switch4)
 99my_group.append(my_switch5)
100my_group.append(my_switch6)
101my_group.append(my_switch7)
102my_group.append(my_switch8)
103
104# Add my_group to the display
105display.show(my_group)
106
107
108# Start the main loop
109while True:
110
111    p = ts.touch_point  # get any touches on the screen
112
113    if p:  # Check each switch if the touch point is within the switch touch area
114        # If touched, then flip the switch with .selected
115        if my_switch.contains(p):
116            my_switch.selected(p)
117
118        elif my_switch2.contains(p):
119            my_switch2.selected(p)
120
121        elif my_switch3.contains(p):
122            my_switch3.selected(p)
123
124        elif my_switch4.contains(p):
125            my_switch4.selected(p)
126
127        elif my_switch5.contains(p):
128            my_switch5.selected(p)
129
130        elif my_switch6.contains(p):
131            my_switch6.selected(p)
132
133        elif my_switch7.contains(p):
134            my_switch7.selected(p)
135
136        elif my_switch8.contains(p):
137            my_switch8.selected(p)
138
139    time.sleep(0.05)  # touch response on PyPortal is more accurate with a small delay