|
| 1 | +# SPDX-FileCopyrightText: 2025 Liz Clark for Adafruit Industries |
| 2 | +# SPDX-FileCopyrightText: Adapted from Phil B.'s 16bit_hello Arduino Code |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: MIT |
| 5 | + |
| 6 | +'''Graphics example for the Vertical Newxie TFT''' |
| 7 | + |
| 8 | +import gc |
| 9 | +import math |
| 10 | +from random import randint |
| 11 | +import time |
| 12 | +import displayio |
| 13 | +import board |
| 14 | +import vectorio |
| 15 | +import terminalio |
| 16 | +import simpleio |
| 17 | +from adafruit_st7789 import ST7789 |
| 18 | +from adafruit_bitmap_font import bitmap_font |
| 19 | +from adafruit_display_text import label, wrap_text_to_lines |
| 20 | +from adafruit_display_shapes.rect import Rect |
| 21 | +from adafruit_display_shapes.circle import Circle |
| 22 | +from adafruit_display_shapes.roundrect import RoundRect |
| 23 | +from adafruit_display_shapes.triangle import Triangle |
| 24 | +from adafruit_display_shapes.line import Line |
| 25 | + |
| 26 | +displayio.release_displays() |
| 27 | + |
| 28 | +spi = board.SPI() |
| 29 | +tft_cs = board.D5 |
| 30 | +tft_dc = board.D6 |
| 31 | + |
| 32 | +display_bus = displayio.FourWire( |
| 33 | + spi, command=tft_dc, chip_select=tft_cs, reset=None |
| 34 | +) |
| 35 | + |
| 36 | +display = ST7789(display_bus, rotation=180, width=135, height=240, rowstart=40, colstart=53) |
| 37 | + |
| 38 | +bitmap = displayio.Bitmap(display.width, display.height, 3) |
| 39 | + |
| 40 | +red = 0xff0000 |
| 41 | +yellow = 0xcccc00 |
| 42 | +orange = 0xff5500 |
| 43 | +blue = 0x0000ff |
| 44 | +pink = 0xff00ff |
| 45 | +purple = 0x5500ff |
| 46 | +white = 0xffffff |
| 47 | +green = 0x00ff00 |
| 48 | +aqua = 0x125690 |
| 49 | + |
| 50 | +palette = displayio.Palette(3) |
| 51 | +palette[0] = 0x000000 # black |
| 52 | +palette[1] = white |
| 53 | +palette[2] = yellow |
| 54 | + |
| 55 | +palette.make_transparent(0) |
| 56 | + |
| 57 | +tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette) |
| 58 | + |
| 59 | +group = displayio.Group() |
| 60 | + |
| 61 | +def clean_up(group_name): |
| 62 | + for _ in range(len(group_name)): |
| 63 | + group_name.pop() |
| 64 | + gc.collect() |
| 65 | + |
| 66 | +def show_shapes(): |
| 67 | + gc.collect() |
| 68 | + cx = int(display.width / 2) |
| 69 | + cy = int(display.height / 2) |
| 70 | + minor = min(cx, cy) |
| 71 | + pad = 5 |
| 72 | + size = minor - pad |
| 73 | + half = int(size / 2) |
| 74 | + rect = Rect(cx - minor, cy - minor, size, size, stroke = 1, fill=red, outline = red) |
| 75 | + tri = Triangle(cx + pad, cy - pad, cx + pad + half, cy - minor, |
| 76 | + cx + minor - 1, cy - pad, fill=green, outline = green) |
| 77 | + circ = Circle(cx - pad - half, cy + pad + half, half, fill=blue, stroke = 1, outline = blue) |
| 78 | + rnd = RoundRect(cx + pad, cy + pad, size, size, int(size / 5), stroke = 1, |
| 79 | + fill=yellow, outline = yellow) |
| 80 | + |
| 81 | + group.append(rect) |
| 82 | + group.append(tri) |
| 83 | + group.append(circ) |
| 84 | + group.append(rnd) |
| 85 | + rect.fill = None |
| 86 | + tri.fill = None |
| 87 | + circ.fill = None |
| 88 | + rnd.fill = None |
| 89 | + |
| 90 | + time.sleep(2) |
| 91 | + |
| 92 | + rect.fill = red |
| 93 | + tri.fill = green |
| 94 | + circ.fill = blue |
| 95 | + rnd.fill = yellow |
| 96 | + time.sleep(2) |
| 97 | + clean_up(group) |
| 98 | + del rect |
| 99 | + del tri |
| 100 | + del circ |
| 101 | + del rnd |
| 102 | + gc.collect() |
| 103 | + |
| 104 | +def sine_chart(): |
| 105 | + gc.collect() |
| 106 | + cx = int(display.width / 2) |
| 107 | + cy = int(display.height / 2) |
| 108 | + minor = min(cx, cy) |
| 109 | + major = max(cx, cy) |
| 110 | + |
| 111 | + group.append(Line(cx, 0, cx, display.height, blue)) # v |
| 112 | + group.append(Line(0, cy, display.width, cy, blue)) # h |
| 113 | + |
| 114 | + for i in range(10): |
| 115 | + _n = simpleio.map_range(i, 0, 10, 0, major - 1) |
| 116 | + n = int(_n) |
| 117 | + group.append(Line(cx - n, cy - 5, cx - n, (cy - 5) + 11, blue)) # v |
| 118 | + group.append(Line(cx + n, cy - 5, cx + n, (cy - 5) + 11, blue)) # v |
| 119 | + group.append(Line(cx - 5, cy - n, (cx - 5) + 11, cy - n, blue)) # h |
| 120 | + group.append(Line(cx - 5, cy + n, (cx - 5) + 11, cy + n, blue)) # h |
| 121 | + |
| 122 | + for x in range(display.width): |
| 123 | + y = cy - int(math.sin((x - cx) * 0.05) * float(minor * 0.5)) |
| 124 | + bitmap[x, y] = 1 |
| 125 | + group.append(tile_grid) |
| 126 | + time.sleep(2) |
| 127 | + clean_up(group) |
| 128 | + |
| 129 | +def widget0(): |
| 130 | + gc.collect() |
| 131 | + data = [31, 42, 36, 58, 67, 88] |
| 132 | + num_points = len(data) |
| 133 | + |
| 134 | + text_area = label.Label(terminalio.FONT, text="Widget Sales", color=white) |
| 135 | + text_area.anchor_point = (0.5, 0.0) |
| 136 | + text_area.anchored_position = (display.width / 2, 3) |
| 137 | + group.append(text_area) |
| 138 | + for i in range(11): |
| 139 | + _x = simpleio.map_range(i, 0, 10, 0, display.width - 1) |
| 140 | + x = int(_x) |
| 141 | + group.append(Line(x, 20, x, display.height, blue)) |
| 142 | + _y = simpleio.map_range(i, 0, 10, 20, display.height - 1) |
| 143 | + y = int(_y) |
| 144 | + group.append(Line(0, y, display.width, y, blue)) |
| 145 | + prev_x = 0 |
| 146 | + _prev_y = simpleio.map_range(data[0], 0, 100, display.height - 1, 20) |
| 147 | + prev_y = int(_prev_y) |
| 148 | + for i in range(1, num_points): |
| 149 | + _new_x = simpleio.map_range(i, 0, num_points - 1, 0, display.width - 1) |
| 150 | + new_x = int(_new_x) |
| 151 | + _new_y = simpleio.map_range(data[i], 0, 100, display.height - 1, 20) |
| 152 | + new_y = int(_new_y) |
| 153 | + group.append(Line(prev_x, prev_y, new_x, new_y, aqua)) |
| 154 | + prev_x = new_x |
| 155 | + prev_y = new_y |
| 156 | + |
| 157 | + for i in range(num_points): |
| 158 | + _x = simpleio.map_range(i, 0, num_points - 1, 0, display.width - 1) |
| 159 | + x = int(_x) |
| 160 | + _y = simpleio.map_range(data[i], 0, 100, display.height - 1, 20) |
| 161 | + y = int(_y) |
| 162 | + group.append(Circle(x, y, 5, fill=None, stroke = 2, outline = white)) |
| 163 | + |
| 164 | + time.sleep(2) |
| 165 | + clean_up(group) |
| 166 | + |
| 167 | +def widget1(): |
| 168 | + gc.collect() |
| 169 | + data = [31, 42, 36, 58, 67, 88] |
| 170 | + num_points = len(data) |
| 171 | + bar_width = int(display.width / num_points) - 4 |
| 172 | + x_mapped_w = display.width + 2 |
| 173 | + h_mapped_h = display.height + 20 |
| 174 | + |
| 175 | + text_area = label.Label(terminalio.FONT, text="Widget Sales", color=white) |
| 176 | + text_area.anchor_point = (0.5, 0.0) |
| 177 | + text_area.anchored_position = (display.width / 2, 3) |
| 178 | + group.append(text_area) |
| 179 | + for i in range(11): |
| 180 | + _y = simpleio.map_range(i, 0, 10, 20, display.height - 1) |
| 181 | + y = int(_y) |
| 182 | + group.append(Line(0, y, display.width, y, blue)) |
| 183 | + for i in range(num_points): |
| 184 | + _x = simpleio.map_range(i, 0, num_points, 0, x_mapped_w) |
| 185 | + x = int(_x) |
| 186 | + _height = simpleio.map_range(data[i], 0, 100, h_mapped_h, 0) |
| 187 | + height = int(_height) |
| 188 | + group.append(vectorio.Rectangle(pixel_shader=palette, width=bar_width, |
| 189 | + height=display.height + 1, x=x, y=height, color_index = 2)) |
| 190 | + |
| 191 | + time.sleep(2) |
| 192 | + clean_up(group) |
| 193 | + |
| 194 | +def text_align(): |
| 195 | + gc.collect() |
| 196 | + TEXT = "hi!" |
| 197 | + |
| 198 | + text_area_top_left = label.Label(terminalio.FONT, text=TEXT, color=red) |
| 199 | + text_area_top_left.anchor_point = (0.0, 0.0) |
| 200 | + text_area_top_left.anchored_position = (0, 0) |
| 201 | + |
| 202 | + text_area_top_middle = label.Label(terminalio.FONT, text=TEXT, color=orange) |
| 203 | + text_area_top_middle.anchor_point = (0.5, 0.0) |
| 204 | + text_area_top_middle.anchored_position = (display.width / 2, 0) |
| 205 | + |
| 206 | + text_area_top_right = label.Label(terminalio.FONT, text=TEXT, color=yellow) |
| 207 | + text_area_top_right.anchor_point = (1.0, 0.0) |
| 208 | + text_area_top_right.anchored_position = (display.width, 0) |
| 209 | + |
| 210 | + text_area_middle_left = label.Label(terminalio.FONT, text=TEXT, color=green) |
| 211 | + text_area_middle_left.anchor_point = (0.0, 0.5) |
| 212 | + text_area_middle_left.anchored_position = (0, display.height / 2) |
| 213 | + |
| 214 | + text_area_middle_middle = label.Label(terminalio.FONT, text=TEXT, color=aqua) |
| 215 | + text_area_middle_middle.anchor_point = (0.5, 0.5) |
| 216 | + text_area_middle_middle.anchored_position = (display.width / 2, display.height / 2) |
| 217 | + |
| 218 | + text_area_middle_right = label.Label(terminalio.FONT, text=TEXT, color=blue) |
| 219 | + text_area_middle_right.anchor_point = (1.0, 0.5) |
| 220 | + text_area_middle_right.anchored_position = (display.width, display.height / 2) |
| 221 | + |
| 222 | + text_area_bottom_left = label.Label(terminalio.FONT, text=TEXT, color=purple) |
| 223 | + text_area_bottom_left.anchor_point = (0.0, 1.0) |
| 224 | + text_area_bottom_left.anchored_position = (0, display.height) |
| 225 | + |
| 226 | + text_area_bottom_middle = label.Label(terminalio.FONT, text=TEXT, color=pink) |
| 227 | + text_area_bottom_middle.anchor_point = (0.5, 1.0) |
| 228 | + text_area_bottom_middle.anchored_position = (display.width / 2, display.height) |
| 229 | + |
| 230 | + text_area_bottom_right = label.Label(terminalio.FONT, text=TEXT, color=white) |
| 231 | + text_area_bottom_right.anchor_point = (1.0, 1.0) |
| 232 | + text_area_bottom_right.anchored_position = (display.width, display.height) |
| 233 | + |
| 234 | + group.append(text_area_top_middle) |
| 235 | + group.append(text_area_top_left) |
| 236 | + group.append(text_area_top_right) |
| 237 | + group.append(text_area_middle_middle) |
| 238 | + group.append(text_area_middle_left) |
| 239 | + group.append(text_area_middle_right) |
| 240 | + group.append(text_area_bottom_middle) |
| 241 | + group.append(text_area_bottom_left) |
| 242 | + group.append(text_area_bottom_right) |
| 243 | + |
| 244 | + time.sleep(2) |
| 245 | + clean_up(group) |
| 246 | + |
| 247 | +def custom_font(): |
| 248 | + gc.collect() |
| 249 | + my_font = bitmap_font.load_font("/Helvetica-Bold-16.pcf") |
| 250 | + text_sample = "The quick brown fox jumps over the lazy dog." |
| 251 | + text_sample = "\n".join(wrap_text_to_lines(text_sample, 15)) |
| 252 | + text_area = label.Label(my_font, text="Custom Font", color=white) |
| 253 | + text_area.anchor_point = (0.0, 0.0) |
| 254 | + text_area.anchored_position = (0, 0) |
| 255 | + |
| 256 | + sample_text = label.Label(my_font, text=text_sample) |
| 257 | + sample_text.anchor_point = (0.5, 0.5) |
| 258 | + sample_text.anchored_position = (display.width / 2, display.height / 2) |
| 259 | + |
| 260 | + group.append(text_area) |
| 261 | + group.append(sample_text) |
| 262 | + |
| 263 | + time.sleep(2) |
| 264 | + clean_up(group) |
| 265 | + |
| 266 | + del my_font |
| 267 | + gc.collect() |
| 268 | + |
| 269 | +def bitmap_example(): |
| 270 | + gc.collect() |
| 271 | + blinka_bitmap = displayio.OnDiskBitmap("/adabot.bmp") |
| 272 | + blinka_grid = displayio.TileGrid(blinka_bitmap, pixel_shader=blinka_bitmap.pixel_shader) |
| 273 | + gc.collect() |
| 274 | + group.append(blinka_grid) |
| 275 | + |
| 276 | + time.sleep(2) |
| 277 | + clean_up(group) |
| 278 | + |
| 279 | + del blinka_grid |
| 280 | + del blinka_bitmap |
| 281 | + gc.collect() |
| 282 | + |
| 283 | +def sensor_values(): |
| 284 | + gc.collect() |
| 285 | + text_x = "X: %d" % randint(-25, 25) |
| 286 | + text_y = "Y: %d" % randint(-25, 25) |
| 287 | + text_z = "Z: %d" % randint(-25, 25) |
| 288 | + x_text = label.Label(terminalio.FONT, text=text_x, color=red) |
| 289 | + x_text.anchor_point = (0.0, 0.0) |
| 290 | + x_text.anchored_position = (2, 0) |
| 291 | + y_text = label.Label(terminalio.FONT, text=text_y, color=green) |
| 292 | + y_text.anchor_point = (0.0, 0.0) |
| 293 | + y_text.anchored_position = (2, 10) |
| 294 | + z_text = label.Label(terminalio.FONT, text=text_z, color=blue) |
| 295 | + z_text.anchor_point = (0.0, 0.0) |
| 296 | + z_text.anchored_position = (2, 20) |
| 297 | + group.append(x_text) |
| 298 | + group.append(y_text) |
| 299 | + group.append(z_text) |
| 300 | + |
| 301 | + for i in range(40): |
| 302 | + if i == 10: |
| 303 | + group.scale = 2 |
| 304 | + elif i == 20: |
| 305 | + group.scale = 3 |
| 306 | + elif i == 30: |
| 307 | + group.scale = 4 |
| 308 | + x_text.text = "X: %d" % randint(-50, 50) |
| 309 | + y_text.text = "Y: %d" % randint(-50, 50) |
| 310 | + z_text.text = "Z: %d" % randint(-50, 50) |
| 311 | + time.sleep(0.1) |
| 312 | + time.sleep(0.1) |
| 313 | + clean_up(group) |
| 314 | + group.scale = 1 |
| 315 | + |
| 316 | +display.root_group = group |
| 317 | + |
| 318 | +while True: |
| 319 | + show_shapes() |
| 320 | + sine_chart() |
| 321 | + widget0() |
| 322 | + widget1() |
| 323 | + text_align() |
| 324 | + custom_font() |
| 325 | + bitmap_example() |
| 326 | + sensor_values() |
0 commit comments