|
| 1 | +""" |
| 2 | +PyPortal Calculator Demo |
| 3 | +""" |
| 4 | +import time |
| 5 | +import board |
| 6 | +import displayio |
| 7 | +import os |
| 8 | +from collections import namedtuple |
| 9 | +from adafruit_display_text.label import Label |
| 10 | +from adafruit_bitmap_font import bitmap_font |
| 11 | +from adafruit_display_shapes.rect import Rect |
| 12 | +from adafruit_button import Button |
| 13 | +from calculator import Calculator |
| 14 | +import adafruit_touchscreen |
| 15 | +coords = namedtuple("Point", "x y") |
| 16 | + |
| 17 | +ts = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR, |
| 18 | + board.TOUCH_YD, board.TOUCH_YU, |
| 19 | + calibration=((5200, 59000), (5800, 57000)), |
| 20 | + size=(320, 240)) |
| 21 | + |
| 22 | +# Settings |
| 23 | +BUTTON_WIDTH = 60 |
| 24 | +BUTTON_HEIGHT = 30 |
| 25 | +BUTTON_MARGIN = 8 |
| 26 | +MAX_DIGITS = 29 |
| 27 | +BLACK = 0x0 |
| 28 | +ORANGE = 0xFF8800 |
| 29 | +WHITE = 0xFFFFFF |
| 30 | +GRAY = 0x888888 |
| 31 | +LABEL_OFFSET = 290 |
| 32 | + |
| 33 | +# Make the display context |
| 34 | +calc_group = displayio.Group(max_size=25) |
| 35 | +board.DISPLAY.show(calc_group) |
| 36 | + |
| 37 | +# Make a background color fill |
| 38 | +color_bitmap = displayio.Bitmap(320, 240, 1) |
| 39 | +color_palette = displayio.Palette(1) |
| 40 | +color_palette[0] = GRAY |
| 41 | +bg_sprite = displayio.TileGrid(color_bitmap, |
| 42 | + pixel_shader=color_palette, |
| 43 | + x=0, y=0) |
| 44 | +calc_group.append(bg_sprite) |
| 45 | + |
| 46 | +# Load the font |
| 47 | +font = bitmap_font.load_font("/fonts/Arial-12.bdf") |
| 48 | +buttons = [] |
| 49 | + |
| 50 | +# Some button placement functions |
| 51 | +def button_grid(row, col): |
| 52 | + return coords(BUTTON_MARGIN * (row + 1) + BUTTON_WIDTH * row + 20, |
| 53 | + BUTTON_MARGIN * (col + 1) + BUTTON_HEIGHT * col + 40) |
| 54 | + |
| 55 | +def make_button(row, col, label, width=1, color=WHITE, text_color=BLACK): |
| 56 | + pos = button_grid(row, col) |
| 57 | + button = Button(x=pos.x, y=pos.y, |
| 58 | + width=BUTTON_WIDTH * width + BUTTON_MARGIN * (width - 1), height=BUTTON_HEIGHT, |
| 59 | + label=label, label_font=font, label_color=text_color, fill_color=color, |
| 60 | + style=Button.ROUNDRECT) |
| 61 | + buttons.append(button) |
| 62 | + return button |
| 63 | + |
| 64 | +border = Rect(20, 8, 280, 35, fill=WHITE, outline=BLACK, stroke=2) |
| 65 | +calc_display = Label(font, text="0", color=BLACK, max_glyphs=MAX_DIGITS) |
| 66 | +calc_display.y = 25 |
| 67 | + |
| 68 | +clear_button = make_button(0, 0, "AC") |
| 69 | +make_button(1, 0, "+/-") |
| 70 | +make_button(2, 0, "%") |
| 71 | +make_button(3, 0, "/", 1, ORANGE, WHITE) |
| 72 | +make_button(0, 1, "7") |
| 73 | +make_button(1, 1, "8") |
| 74 | +make_button(2, 1, "9") |
| 75 | +make_button(3, 1, "x", 1, ORANGE, WHITE) |
| 76 | +make_button(0, 2, "4") |
| 77 | +make_button(1, 2, "5") |
| 78 | +make_button(2, 2, "6") |
| 79 | +make_button(3, 2, "-", 1, ORANGE, WHITE) |
| 80 | +make_button(0, 3, "1") |
| 81 | +make_button(1, 3, "2") |
| 82 | +make_button(2, 3, "3") |
| 83 | +make_button(3, 3, "+", 1, ORANGE, WHITE) |
| 84 | +make_button(0, 4, "0", 2) |
| 85 | +make_button(2, 4, ".") |
| 86 | +make_button(3, 4, "=", 1, ORANGE, WHITE) |
| 87 | + |
| 88 | +# Add the display and buttons to the main calc group |
| 89 | +calc_group.append(border) |
| 90 | +calc_group.append(calc_display) |
| 91 | +for b in buttons: |
| 92 | + calc_group.append(b.group) |
| 93 | + |
| 94 | +calculator = Calculator(calc_display, clear_button, LABEL_OFFSET) |
| 95 | + |
| 96 | +button = "" |
| 97 | +while True: |
| 98 | + point = ts.touch_point |
| 99 | + if point is not None: |
| 100 | + for i, b in enumerate(buttons): |
| 101 | + if b.contains(point) and button == "": |
| 102 | + b.selected = True |
| 103 | + button = b.label |
| 104 | + time.sleep(0.1) |
| 105 | + b.selected = False |
| 106 | + break |
| 107 | + else: |
| 108 | + if button != "": |
| 109 | + calculator.add_input(button) |
| 110 | + button = "" |
| 111 | + time.sleep(0.05) |
0 commit comments