display_button_simpletest.py
1 import os 2 import board 3 import displayio 4 from adafruit_bitmap_font import bitmap_font 5 from adafruit_button import Button 6 import adafruit_touchscreen 7 8 # These pins are used as both analog and digital! XL, XR and YU must be analog 9 # and digital capable. YD just need to be digital 10 ts = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR, 11 board.TOUCH_YD, board.TOUCH_YU, 12 calibration=((5200, 59000), (5800, 57000)), 13 size=(320, 240)) 14 15 # the current working directory (where this file is) 16 cwd = ("/"+__file__).rsplit('/', 1)[0] 17 fonts = [file for file in os.listdir(cwd+"/fonts/") 18 if (file.endswith(".bdf") and not file.startswith("._"))] 19 for i, filename in enumerate(fonts): 20 fonts[i] = cwd+"/fonts/"+filename 21 print(fonts) 22 THE_FONT = "/fonts/Chicago-12.bdf" 23 DISPLAY_STRING = "Button Text" 24 25 # Make the display context 26 splash = displayio.Group(max_size=20) 27 board.DISPLAY.show(splash) 28 BUTTON_WIDTH = 80 29 BUTTON_HEIGHT = 40 30 BUTTON_MARGIN = 20 31 32 ########################################################################## 33 # Make a background color fill 34 35 color_bitmap = displayio.Bitmap(320, 240, 1) 36 color_palette = displayio.Palette(1) 37 color_palette[0] = 0x404040 38 bg_sprite = displayio.TileGrid(color_bitmap, 39 pixel_shader=color_palette, 40 position=(0, 0)) 41 print(bg_sprite.position) 42 splash.append(bg_sprite) 43 44 ########################################################################## 45 46 # Load the font 47 font = bitmap_font.load_font(THE_FONT) 48 49 buttons = [] 50 # Default button styling: 51 button_0 = Button(x=BUTTON_MARGIN, y=BUTTON_MARGIN, 52 width=BUTTON_WIDTH, height=BUTTON_HEIGHT, 53 label="button0", label_font=font) 54 buttons.append(button_0) 55 56 # a button with no indicators at all 57 button_1 = Button(x=BUTTON_MARGIN*2+BUTTON_WIDTH, y=BUTTON_MARGIN, 58 width=BUTTON_WIDTH, height=BUTTON_HEIGHT, 59 fill_color=None, outline_color=None) 60 buttons.append(button_1) 61 62 # various colorings 63 button_2 = Button(x=BUTTON_MARGIN*3+2*BUTTON_WIDTH, y=BUTTON_MARGIN, 64 width=BUTTON_WIDTH, height=BUTTON_HEIGHT, 65 label="button2", label_font=font, label_color=0x0000FF, 66 fill_color=0x00FF00, outline_color=0xFF0000) 67 buttons.append(button_2) 68 69 # Transparent button with text 70 button_3 = Button(x=BUTTON_MARGIN, y=BUTTON_MARGIN*2+BUTTON_HEIGHT, 71 width=BUTTON_WIDTH, height=BUTTON_HEIGHT, 72 label="button3", label_font=font, label_color=0x0, 73 fill_color=None, outline_color=None) 74 buttons.append(button_3) 75 76 # a roundrect 77 button_4 = Button(x=BUTTON_MARGIN, y=BUTTON_MARGIN*2+BUTTON_HEIGHT, 78 width=BUTTON_WIDTH, height=BUTTON_HEIGHT, 79 label="button4", label_font=font, style=Button.ROUNDRECT) 80 buttons.append(button_4) 81 82 # a shadowrect 83 button_5 = Button(x=BUTTON_MARGIN*2+BUTTON_WIDTH, y=BUTTON_MARGIN*2+BUTTON_HEIGHT, 84 width=BUTTON_WIDTH, height=BUTTON_HEIGHT, 85 label="button5", label_font=font, style=Button.SHADOWRECT) 86 buttons.append(button_5) 87 88 # a shadowroundrect 89 button_6 = Button(x=BUTTON_MARGIN*3+2*BUTTON_WIDTH, y=BUTTON_MARGIN*2+BUTTON_HEIGHT, 90 width=BUTTON_WIDTH, height=BUTTON_HEIGHT, 91 label="button5", label_font=font, style=Button.SHADOWROUNDRECT) 92 buttons.append(button_6) 93 94 95 96 for b in buttons: 97 splash.append(b.group) 98 99 100 while True: 101 p = ts.touch_point 102 if p: 103 print(p) 104 for i, b in enumerate(buttons): 105 if b.contains(p): 106 print("Button %d pressed" % i) 107 b.selected = True 108 else: 109 b.selected = False