Simple test

Ensure your device works with this simple test.

examples/checkbox_simpletest.py
 1# SPDX-FileCopyrightText: Copyright (c) 2025 Tim Cocks for Adafruit Industries
 2#
 3# SPDX-License-Identifier: MIT
 4"""
 5Basic checkbox example that uses a USB Host mouse to allow clicking on the CheckBox
 6"""
 7
 8import time
 9
10import supervisor
11import terminalio
12from adafruit_display_text.bitmap_label import Label
13from adafruit_usb_host_mouse import find_and_init_boot_mouse
14from displayio import Group
15
16from adafruit_checkbox import CheckBox
17
18display = supervisor.runtime.display
19
20# group to hold visual elements
21main_group = Group()
22
23# make the group visible on the display
24display.root_group = main_group
25
26# set up USB host mouse
27mouse = find_and_init_boot_mouse()
28if mouse is None:
29    raise RuntimeError("No mouse found connected to USB Host")
30
31# timestamp of last increment
32last_count_time = 0
33# counter variable
34i = 0
35
36# initialize and show a CheckBox
37keep_counting_checkbox = CheckBox(terminalio.FONT, text="Keep Counting", checked=True)
38keep_counting_checkbox.anchor_point = (0, 0)
39keep_counting_checkbox.anchored_position = (2, 2)
40main_group.append(keep_counting_checkbox)
41
42# label to hold the current count
43count_label = Label(terminalio.FONT, text=str(i), color=0xFFFFFF)
44count_label.anchor_point = (0, 0)
45count_label.anchored_position = (2, 20)
46main_group.append(count_label)
47
48# add the mouse cursor to the main group
49main_group.append(mouse.tilegrid)
50while True:
51    # update mouse
52    pressed_btns = mouse.update()
53
54    # if the checkbox was left clicked
55    if pressed_btns is not None and "left" in pressed_btns:
56        if keep_counting_checkbox.contains((mouse.x, mouse.y)):
57            # toggle the checked state
58            keep_counting_checkbox.checked = not keep_counting_checkbox.checked
59
60    if keep_counting_checkbox.checked:
61        # increment the counter ever 0.5 seconds and update the label
62        if time.monotonic() > last_count_time + 0.5:
63            last_count_time = time.monotonic()
64            i += 1
65            count_label.text = str(i)