|
| 1 | +# SPDX-FileCopyrightText: 2022 Cedar Grove Maker Studios |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +""" |
| 5 | +touch_calibrator_built_in.py 2022-05-27 v2.2 |
| 6 | +
|
| 7 | +Author(s): JG for Cedar Grove Maker Studios |
| 8 | +
|
| 9 | +On-screen touchscreen calibrator for built-in displays. |
| 10 | +
|
| 11 | +When the test screen appears, use a stylus to swipe to the four edges |
| 12 | +of the visible display area. As the screen is calibrated, the small red |
| 13 | +square tracks the stylus tip (REPL_ONLY=False). Minimum and maximum |
| 14 | +calibration values will display on the screen and in the REPL. The calibration |
| 15 | +tuple can be copied and pasted into the calling code's touchscreen |
| 16 | +instantiation statement. |
| 17 | +
|
| 18 | +DISPLAY_ROTATION: Display rotation value in degrees. Only values of |
| 19 | +None, 0, 90, 180, and 270 degrees are accepted. Defaults to None, the |
| 20 | +previous orientation of the display. |
| 21 | +
|
| 22 | +REPL_ONLY: If False, calibration values are shown graphically on the screen |
| 23 | +and printed to the REPL. If True, the values are only printed to the REPL. |
| 24 | +Default value is False. |
| 25 | +""" |
| 26 | + |
| 27 | +import time |
| 28 | +import board |
| 29 | +import displayio |
| 30 | +import vectorio |
| 31 | +import terminalio |
| 32 | +from adafruit_display_text.label import Label |
| 33 | +from simpleio import map_range |
| 34 | +import adafruit_touchscreen |
| 35 | + |
| 36 | + |
| 37 | +# Operational parameters: |
| 38 | +DISPLAY_ROTATION = 0 # Specify 0, 90, 180, or 270 degrees |
| 39 | +REPL_ONLY = False # True to disable graphics |
| 40 | + |
| 41 | +# pylint: disable=too-few-public-methods |
| 42 | +class Colors: |
| 43 | + """A collection of colors used for graphic objects.""" |
| 44 | + |
| 45 | + BLUE_DK = 0x000060 # Screen fill |
| 46 | + RED = 0xFF0000 # Boundary |
| 47 | + WHITE = 0xFFFFFF # Text |
| 48 | + |
| 49 | + |
| 50 | +# Instantiate the built-in display |
| 51 | +display = board.DISPLAY |
| 52 | + |
| 53 | +# Check rotation value and update display. |
| 54 | +# Always set rotation before instantiating the touchscreen. |
| 55 | +if DISPLAY_ROTATION is not None and DISPLAY_ROTATION in (0, 90, 180, 270): |
| 56 | + display.rotation = DISPLAY_ROTATION |
| 57 | +else: |
| 58 | + print("Warning: invalid rotation value -- defaulting to zero") |
| 59 | + display.rotation = 0 |
| 60 | + time.sleep(1) |
| 61 | + |
| 62 | +# Activate the display graphics unless REPL_ONLY=True |
| 63 | +if not REPL_ONLY: |
| 64 | + display_group = displayio.Group() |
| 65 | + display.show(display_group) |
| 66 | + |
| 67 | +# Instantiate touch screen without calibration or display size parameters |
| 68 | +if display.rotation == 0: |
| 69 | + ts = adafruit_touchscreen.Touchscreen( |
| 70 | + board.TOUCH_XL, |
| 71 | + board.TOUCH_XR, |
| 72 | + board.TOUCH_YD, |
| 73 | + board.TOUCH_YU, |
| 74 | + # calibration=((5200, 59000), (5250, 59500)), |
| 75 | + # size=(board.DISPLAY.width, board.DISPLAY.height), |
| 76 | + ) |
| 77 | + |
| 78 | +elif display.rotation == 90: |
| 79 | + ts = adafruit_touchscreen.Touchscreen( |
| 80 | + board.TOUCH_YU, |
| 81 | + board.TOUCH_YD, |
| 82 | + board.TOUCH_XL, |
| 83 | + board.TOUCH_XR, |
| 84 | + # calibration=((5250, 59500), (5200, 59000)), |
| 85 | + # size=(board.DISPLAY.width, board.DISPLAY.height), |
| 86 | + ) |
| 87 | + |
| 88 | +elif display.rotation == 180: |
| 89 | + ts = adafruit_touchscreen.Touchscreen( |
| 90 | + board.TOUCH_XR, |
| 91 | + board.TOUCH_XL, |
| 92 | + board.TOUCH_YU, |
| 93 | + board.TOUCH_YD, |
| 94 | + # calibration=((5200, 59000), (5250, 59500)), |
| 95 | + # size=(board.DISPLAY.width, board.DISPLAY.height), |
| 96 | + ) |
| 97 | + |
| 98 | +elif display.rotation == 270: |
| 99 | + ts = adafruit_touchscreen.Touchscreen( |
| 100 | + board.TOUCH_YD, |
| 101 | + board.TOUCH_YU, |
| 102 | + board.TOUCH_XR, |
| 103 | + board.TOUCH_XL, |
| 104 | + # calibration=((5250, 59500), (5200, 59000)), |
| 105 | + # size=(board.DISPLAY.width, board.DISPLAY.height), |
| 106 | + ) |
| 107 | +else: |
| 108 | + raise ValueError("Rotation value must be 0, 90, 180, or 270") |
| 109 | + |
| 110 | +# Define the graphic objects if REPL_ONLY = False |
| 111 | +if not REPL_ONLY: |
| 112 | + # Define the text graphic objects |
| 113 | + font_0 = terminalio.FONT |
| 114 | + |
| 115 | + coordinates = Label( |
| 116 | + font=font_0, |
| 117 | + text="calib: ((x_min, x_max), (y_min, y_max))", |
| 118 | + color=Colors.WHITE, |
| 119 | + ) |
| 120 | + coordinates.anchor_point = (0.5, 0.5) |
| 121 | + coordinates.anchored_position = ( |
| 122 | + board.DISPLAY.width // 2, |
| 123 | + board.DISPLAY.height // 4, |
| 124 | + ) |
| 125 | + |
| 126 | + display_rotation = Label( |
| 127 | + font=font_0, |
| 128 | + text="rotation: " + str(display.rotation), |
| 129 | + color=Colors.WHITE, |
| 130 | + ) |
| 131 | + display_rotation.anchor_point = (0.5, 0.5) |
| 132 | + display_rotation.anchored_position = ( |
| 133 | + board.DISPLAY.width // 2, |
| 134 | + board.DISPLAY.height // 4 - 30, |
| 135 | + ) |
| 136 | + |
| 137 | + # Define graphic objects for the screen fill, boundary, and touch pen |
| 138 | + target_palette = displayio.Palette(1) |
| 139 | + target_palette[0] = Colors.BLUE_DK |
| 140 | + screen_fill = vectorio.Rectangle( |
| 141 | + pixel_shader=target_palette, |
| 142 | + x=2, |
| 143 | + y=2, |
| 144 | + width=board.DISPLAY.width - 4, |
| 145 | + height=board.DISPLAY.height - 4, |
| 146 | + ) |
| 147 | + |
| 148 | + target_palette = displayio.Palette(1) |
| 149 | + target_palette[0] = Colors.RED |
| 150 | + boundary = vectorio.Rectangle( |
| 151 | + pixel_shader=target_palette, |
| 152 | + x=0, |
| 153 | + y=0, |
| 154 | + width=board.DISPLAY.width, |
| 155 | + height=board.DISPLAY.height, |
| 156 | + ) |
| 157 | + |
| 158 | + pen = vectorio.Rectangle( |
| 159 | + pixel_shader=target_palette, |
| 160 | + x=board.DISPLAY.width // 2, |
| 161 | + y=board.DISPLAY.height // 2, |
| 162 | + width=10, |
| 163 | + height=10, |
| 164 | + ) |
| 165 | + |
| 166 | + display_group.append(boundary) |
| 167 | + display_group.append(screen_fill) |
| 168 | + display_group.append(pen) |
| 169 | + display_group.append(coordinates) |
| 170 | + display_group.append(display_rotation) |
| 171 | + |
| 172 | +# pylint: disable=invalid-name |
| 173 | +# Reset x and y values to raw touchscreen mid-point before measurement |
| 174 | +x_min = x_max = y_min = y_max = 65535 // 2 |
| 175 | + |
| 176 | +print("Touchscreen Calibrator") |
| 177 | +print(" Use a stylus to swipe slightly beyond the") |
| 178 | +print(" four edges of the visible display area.") |
| 179 | +print(" ") |
| 180 | +print(f" display rotation: {display.rotation} degrees") |
| 181 | +print(" Calibration values follow:") |
| 182 | +print(" ") |
| 183 | + |
| 184 | +while True: |
| 185 | + time.sleep(0.100) |
| 186 | + touch = ts.touch_point # Check for touch |
| 187 | + if touch: |
| 188 | + # Remember minimum and maximum values for the calibration tuple |
| 189 | + x_min = min(x_min, touch[0]) |
| 190 | + x_max = max(x_max, touch[0]) |
| 191 | + y_min = min(y_min, touch[1]) |
| 192 | + y_max = max(y_max, touch[1]) |
| 193 | + |
| 194 | + # Show the calibration tuple. |
| 195 | + print(f"(({x_min}, {x_max}), ({y_min}, {y_max}))") |
| 196 | + if not REPL_ONLY: |
| 197 | + pen.x = int(map_range(touch[0], x_min, x_max, 0, board.DISPLAY.width)) - 5 |
| 198 | + pen.y = int(map_range(touch[1], y_min, y_max, 0, board.DISPLAY.height)) - 5 |
| 199 | + coordinates.text = f"calib: (({x_min}, {x_max}), ({y_min}, {y_max}))" |
0 commit comments