Instantly share code, notes, and snippets.
Created
December 7, 2023 21:23
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save DJDevon3/d9f7f2f8a7dff571477c39934d646e5d to your computer and use it in GitHub Desktop.
circuitpython_hamburger_nav_v1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SPDX-FileCopyrightText: 2021 FoamyGuy | |
# SPDX-License-Identifier: MIT | |
# Hamburger Touch Menu by FoamyGuy 2020 | |
# https://gist.github.com/FoamyGuy/b11390a020d213b1ee15fb5f02eacfff | |
# Code updated to Circuit Python 9.0: 2023 DJDevon3 | |
import time | |
import board | |
import displayio | |
import digitalio | |
from adafruit_bitmap_font import bitmap_font | |
from adafruit_display_text.label import Label | |
from adafruit_button import Button | |
import adafruit_imageload | |
import terminalio | |
from adafruit_hx8357 import HX8357 | |
import adafruit_stmpe610 | |
# 3.5" TFT Featherwing is 480x320 | |
displayio.release_displays() | |
DISPLAY_WIDTH = 480 | |
DISPLAY_HEIGHT = 320 | |
# Initialize TFT Display | |
spi = board.SPI() | |
tft_cs = board.D9 | |
tft_dc = board.D10 | |
display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs) | |
display = HX8357(display_bus, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT) | |
display.rotation = 0 | |
_touch_flip = (False, True) | |
# Initialize 3.5" TFT Featherwing Touchscreen | |
ts_cs_pin = digitalio.DigitalInOut(board.D6) | |
ts = adafruit_stmpe610.Adafruit_STMPE610_SPI( | |
board.SPI(), | |
ts_cs_pin, | |
calibration=((231, 3703), (287, 3787)), | |
size=(display.width, display.height), | |
disp_rotation=display.rotation, | |
touch_flip=_touch_flip, | |
) | |
class TileGridButton(displayio.TileGrid): | |
def contains(self, touch_point): | |
if self.x < touch_point[0] and self.x + width > touch_point[0]: | |
if self.y < touch_point[1] and self.y + height > touch_point[1]: | |
return True | |
return False | |
NAV_VISIBLE = False | |
CLICK_COOLDOWN = 0.5 | |
currently_showing_layer = None | |
# ------------- Display Groups ------------- # | |
splash = displayio.Group() # The Main Display Group | |
view1 = displayio.Group() # Group for View 1 objects | |
view2 = displayio.Group() # Group for View 2 objects | |
view3 = displayio.Group() # Group for View 3 objects | |
MENU_ITEMS = [ | |
"Page 1", | |
"Page 2", | |
"Page 3" | |
] | |
MENU_VIEWS = [ | |
view1, | |
view2, | |
view3 | |
] | |
MENU_BTNS = [] | |
def hideLayer(hide_target): | |
try: | |
splash.remove(hide_target) | |
except ValueError: | |
pass | |
def showLayer(show_target): | |
try: | |
time.sleep(0.1) | |
splash.append(show_target) | |
except ValueError: | |
pass | |
# Set the font and preload letters | |
font = bitmap_font.load_font("/fonts/Arial-16.bdf") | |
# Quick Colors for Labels | |
TEXT_BLACK = 0x000000 | |
TEXT_BLUE = 0x0000FF | |
TEXT_CYAN = 0x00FFFF | |
TEXT_GRAY = 0x8B8B8B | |
TEXT_GREEN = 0x00FF00 | |
TEXT_LIGHTBLUE = 0x90C7FF | |
TEXT_MAGENTA = 0xFF00FF | |
TEXT_ORANGE = 0xFFA500 | |
TEXT_PURPLE = 0x800080 | |
TEXT_RED = 0xFF0000 | |
TEXT_WHITE = 0xFFFFFF | |
TEXT_YELLOW = 0xFFFF00 | |
# Text Label Objects | |
view1_label = Label(font, text="Text Window 1", color=TEXT_YELLOW) | |
view1_label.x = 40 | |
view1_label.y = 40 | |
view1.append(view1_label) | |
# Text Label Objects | |
view2_label = Label(font, text="Text Window 2", color=TEXT_PURPLE) | |
view2_label.x = 80 | |
view2_label.y = 80 | |
view2.append(view2_label) | |
# Text Label Objects | |
view3_label = Label(font, text="Text Window 3", color=TEXT_CYAN) | |
view3_label.x = 80 | |
view3_label.y = 120 | |
view3.append(view3_label) | |
currently_showing_layer = view3 | |
showLayer(view3) | |
hamburger_icon, palette = adafruit_imageload.load("icons/menu.bmp", | |
bitmap=displayio.Bitmap, | |
palette=displayio.Palette) | |
palette.make_transparent(1) | |
width = hamburger_icon.width | |
height = hamburger_icon.height | |
# Create a TileGrid to hold the bitmap | |
icon_btn = TileGridButton(hamburger_icon, pixel_shader=palette) | |
splash.append(icon_btn) | |
display.root_group = splash | |
def create_btn(text): | |
# Make the button | |
button = Button( | |
x=0, | |
y=0, | |
width=100, | |
height=30, | |
fill_color=0xCCCCCC, | |
outline_color=0x999999, | |
label=text, | |
label_font=terminalio.FONT, | |
label_color=0x000000, | |
) | |
return button | |
nav_menu_group = displayio.Group() | |
nav_menu_group.x = 0 | |
nav_menu_group.y = 0 | |
for i, item in enumerate(MENU_ITEMS): | |
print(item) | |
btn = create_btn(item) | |
btn.y = btn.height * i | |
print(btn.y) | |
MENU_BTNS.append(btn) | |
nav_menu_group.append(btn) | |
prev_click_time = 0 | |
while True: | |
p = ts.touch_point | |
if p and time.monotonic() > prev_click_time + CLICK_COOLDOWN: | |
print(p) | |
if not NAV_VISIBLE: | |
if icon_btn.contains(p): | |
prev_click_time = time.monotonic() | |
showLayer(nav_menu_group) | |
NAV_VISIBLE = True | |
print("CLICKED!") | |
else: | |
for i, btn in enumerate(MENU_BTNS): | |
if btn.contains(p): | |
prev_click_time = time.monotonic() | |
print("Clicked {}".format(MENU_ITEMS[i])) | |
NAV_VISIBLE = False | |
hideLayer(currently_showing_layer) | |
currently_showing_layer = MENU_VIEWS[i] | |
showLayer(MENU_VIEWS[i]) | |
hideLayer(nav_menu_group) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment