Ausprobiert Teil 2
Rundes 1.28-Zoll-IPS-LCD-Touch Display -
Waveshare 24580
Anleitung für CircuitPython
Ausprobiert Teil 2
Rundes 1.28-Zoll-IPS-LCD-Touch Display -Waveshare 24580
Anleitung für CircuitPython
Im zweiten Teil geht es um das Einrichten verschiedener Touchbereiche zum:
- Stellen der Uhrzeit
- Aufrufen einer Stopuhrfunktion
- Starten eines Timers.
Die Einrichtung setzt die Treiber aus der vorhergehenden Anleitung voraus.

Zunächst zeige ich, wie man unter Verwendung des Displays bestimmte Bereiche für eine Touch-Berührung festlegt. Übernehmen Sie zuerst den Code. Im Anschluss werde ich ihn ausführlich erläutern. Erschrecken Sie auch nicht vor dem Umfang des Beispiels. Das Ganze gibt Ihnen praktisch ein Gerüst, in dem Sie dann Veränderungen vornehmen und eigene Funktionalität hinzufügen können. Der Bewegungssensor wird in diesem Beispiel noch nicht genutzt.
1 # SPDX-FileCopyrightText : 2024 Detlef Gebhardt, written for CircuitPython 9.0.5 2 # SPDX-FileCopyrightText : Copyright (c) 2024 Detlef Gebhardt 3 # SPDX-Filename : touch-example for Waveshare round 1.28 touch LCD 4 # SPDX-License-Identifier: dgebhardt.de 5 import time 6 import gc 7 import board 8 import busio 9 import displayio 10 import terminalio 11 import gc9a01 12 import cst816 13 from adafruit_display_text import label 14 from adafruit_ticks import ticks_ms 15 from adafruit_display_shapes.rect import Rect 16 from adafruit_display_shapes.roundrect import RoundRect 17 from adafruit_display_shapes.circle import Circle 18 from adafruit_display_shapes.line import Line 19 20 # Release any resources currently in use for the displays 21 displayio.release_displays() 22 # Make the displayio SPI bus and the GC9A01 display 23 spi = busio.SPI(clock=board.GP10, MOSI=board.GP11) 24 display_bus = displayio.FourWire(spi, command=board.GP8, chip_select=board.GP9, reset=board.GP13) 25 display = gc9a01.GC9A01(display_bus, width=240, height=240, backlight_pin=board.GP25) 26 display.rotation = 90 27 display.brightness = 1 28 29 ##Touch Pins 30 # I2C_SDA = 6 31 # I2C_SDL = 7 32 # I2C_INT = 17 33 # I2C_RST = 16 34 # Initialize I2C 35 i2c = busio.I2C(scl=board.GP7, sda=board.GP6) 36 touch = cst816.CST816(i2c) 37 touch.set_mode(3) 38 # Werte für 'gesture' 39 # bei 90 Grad gedrehtem display (display.rotation = 90) 40 # 41 # gesture = 2 up 42 # gesture = 1 down 43 # gesture = 4 right 44 # gesture = 3 left 45 # gesture = 12 long pressed 46 # 47 48 49 # Make the display context 50 main_screen = displayio.Group() 51 group1_screen = displayio.Group() 52 53 ###### 54 # 55 # Anfang main_screen 56 # 57 ###### 58 # make bitmap for the display background 59 background = displayio.Bitmap(240, 240, 1) 60 mypal = displayio.Palette(3) 61 mypal[0] = 0x800000 62 background.fill(0) 63 # Background oben 64 main_screen.append(displayio.TileGrid(background, pixel_shader=mypal)) 65 # Background unten 66 background2 = Rect(0, 140, 240, 100, fill=0xc0c0c0) 67 main_screen.append(background2) 68 line = Line(120,170,120,220,color=0x000000) 69 main_screen.append(line) 70 71 # show the display on area 72 roundrect_f= RoundRect(40,120,160,40,20,fill=0x009900, outline=0x00ff00) 73 main_screen.append(roundrect_f) 74 #create the label function 75 updating_label_f = label.Label(font=terminalio.FONT, text="display on", scale=2, color=0xffffff, line_spacing=1) 76 updating_label_f.anchor_point = (0, 0) 77 updating_label_f.anchored_position = (65, 125) 78 main_screen.append(updating_label_f) 79 80 #create a label 81 label_click = label.Label(font=terminalio.FONT, text=" click\n here\n\n\n\n\nclick click", scale=2, color=0xffff00, line_spacing=1) 82 label_click.anchor_point = (0, 0) 83 label_click.anchored_position = (50, 35) 84 main_screen.append(label_click) 85 86 # nur zur Kontrolle anzeigen 87 x_clock=120 88 y_clock=60 89 circle_clock = Circle(x_clock,y_clock,50,fill=None,outline=0xff0000) 90 main_screen.append(circle_clock) 91 x_stop=75 92 y_stop=195 93 circle_stopuhr = Circle(x_stop,y_stop,30,fill=None,outline=0xff0000) 94 main_screen.append(circle_stopuhr) 95 x_timer=165 96 y_timer=195 97 circle_timer = Circle(x_timer,y_timer,30,fill=None,outline=0xff0000) 98 main_screen.append(circle_timer) 99 100 ####### 101 # Ende main_screen 102 ####### 103 ####### 104 # 105 # Anfang group1_screen 106 # 107 ####### 108 109 # display background 110 background_func = displayio.Bitmap(240, 240, 1) 111 pal_color = displayio.Palette(3) 112 pal_color[0] = 0x800000 113 pal_color[1] = 0x008000 114 pal_color[2] = 0x008080 115 #background_func.fill(0) 116 group1_screen.append(displayio.TileGrid(background_func, pixel_shader=pal_color)) 117 # Background unten 118 background2 = Rect(0, 110, 240, 240, fill=0xc0c0c0) 119 group1_screen.append(background2) 120 121 # create the label for instruction 122 updating_label_instruction = label.Label(font=terminalio.FONT, text="", scale=2, color=0xffffcc, line_spacing=1) 123 updating_label_instruction.anchor_point = (0, 0) 124 updating_label_instruction.anchored_position = (30, 20) 125 group1_screen.append(updating_label_instruction) 126 127 # create a label (Uhr stellen) 128 updating_label2 = label.Label(font=terminalio.FONT, text="", scale=5, color=0x000000,line_spacing=1) 129 updating_label2.anchor_point = (0, 0) 130 updating_label2.anchored_position = (50, 120) 131 group1_screen.append(updating_label2) 132 # create a label (Stop) 133 updating_label3 = label.Label(font=terminalio.FONT, text="", scale=5, color=0x000000,line_spacing=1) 134 updating_label3.anchor_point = (0, 0) 135 updating_label3.anchored_position = (50, 120) 136 group1_screen.append(updating_label3) 137 138 ####### 139 # Ende group1_screen 140 ####### 141 142 start = ticks_ms() 143 disp_hell = ticks_ms() 144 display.root_group = main_screen 145 long = 10 146 uhrstellen = False 147 stopuhr = False 148 timer = False 149 150 while True: 151 point = touch.get_point() 152 gesture = touch.get_gesture() 153 press = touch.get_touch() 154 distance = touch.get_distance() 155 # 156 # Displayhelligkeit 157 # if (ticks_ms() - disp_hell)/1000 > long: display.brightness = 0.01 if point.x_point > 80 and point.x_point < 120 and point.y_point > 20 and point.y_point < 200 and press == True: display.brightness = 1 disp_hell = ticks_ms() # # oberen Kreis auswaehlen (Bereich siehe circle_time - 87,88) # r2=(point.x_point - (240-y_clock))*(point.x_point - (240-y_clock)) + (point.y_point - x_clock)*(point.y_point - x_clock) if r2 < 2500 and press == True and stopuhr == False and timer == False: display.brightness = 1 long = 90 uhrstellen = True h=0 m=0 updating_label_instruction.text = " example 1\n back\n long press" updating_label2.text = "{:02}:{:02}".format(h,m) display.root_group = group1_screen background_func.fill(0) # # unten links auswaehlen (Bereich siehe circle_stopuhr - 91,92) # r3=(point.x_point - (240-y_stop))*(point.x_point - (240-y_stop)) + (point.y_point - x_stop)*(point.y_point - x_stop) if r3 < 900 and press == True and uhrstellen == False and timer == False: display.brightness = 1 long = 90 stopuhr = True updating_label_instruction.text = " example 2\n back\n long press" updating_label2.text = "" updating_label3.text = "0.0 s" display.root_group = group1_screen background_func.fill(1) # # unten rechts auswaehlen ( Bereich siehe circle_timer - 95,96) # r4=(point.x_point - (240-y_timer))*(point.x_point - (240-y_timer)) + (point.y_point - x_timer)*(point.y_point - x_timer) if r4 < 900 and press == True and uhrstellen == False and stopuhr == False: display.brightness = 1 long = 90 timer = True updating_label_instruction.text = " example 3\n back\n long press" updating_label2.text = "" updating_label3.text = "00:00" display.root_group = group1_screen background_func.fill(1) # # z.B. Uhrzeit stellen # if uhrstellen == True and stopuhr == False and timer == False: if gesture == 12 and press == True: display.root_group = main_screen display.refresh() time.sleep(0.5) uhrstellen = False disp_hell = ticks_ms() long = 10 # # z.B. Stopuhr starten # if stopuhr == True and uhrstellen == False and timer == False: if gesture == 12 and press == True: updating_label3.text = "" display.root_group = main_screen display.refresh() time.sleep(0.5) stopuhr = False disp_hell = ticks_ms() long = 10 # # z.B. timer starten # if timer == True and uhrstellen == False and stopuhr == False: if gesture == 12 and press == True: updating_label3.text = "" display.root_group = main_screen display.refresh() time.sleep(0.5) timer = False disp_hell = ticks_ms() long = 10 gc.collect() #print(gc.mem_free())