Skip to content

Tft spirit board #2904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions TFT_Spirit_Board/esp32s3_s2_tft_featherwing_480x320/code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# SPDX-FileCopyrightText: 2024 Tim Cocks
#
# SPDX-License-Identifier: MIT
"""
SpiritBoard code.py
Feather ESP32-S3 or S2 + TFT Featherwing 3.5" 480x320 pixels

Receive and display messages from the spirits.
"""
# pylint: disable=import-error, invalid-name
import os
import displayio
import board
from digitalio import DigitalInOut
import adafruit_connection_manager
import wifi
import adafruit_requests
from adafruit_io.adafruit_io import IO_HTTP
from adafruit_hx8357 import HX8357 # TFT Featherwing 3.5" 480x320 display driver
import adafruit_tsc2007

from spirit_board import SpiritBoard



# 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 = DigitalInOut(board.D6)
i2c = board.I2C()
tsc = adafruit_tsc2007.TSC2007(i2c, irq=None)

# Initialize a requests session
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
requests = adafruit_requests.Session(pool, ssl_context)

# Set your Adafruit IO Username and Key in secrets.py
# (visit io.adafruit.com if you need to create an account,
# or if you need your Adafruit IO key.)
aio_username = os.getenv("AIO_USERNAME")
aio_key = os.getenv("AIO_KEY")

# Initialize an Adafruit IO HTTP API object
io = IO_HTTP(aio_username, aio_key, requests)

# initialize the SpiritBoard class
spirit_board = SpiritBoard(display)

# get messages from io or the local file
messages = spirit_board.get_messages(io)

# The planchette is already at the home position.
# Slide it to home again to make it jump, in order
# indicate the message is ready to be received.
spirit_board.slide_planchette(SpiritBoard.LOCATIONS["home"],
delay=0.02, step_size=6)

# current message index
message_index = 0
while True:
# if the display was touched
if tsc.touched:
# write the message at the current index
spirit_board.write_message(messages[message_index], step_size=8)

# if there are more messages in the list inside of context
if message_index < len(messages) - 1:
# increment the message index
message_index += 1

else: # there are no more messages in the list
# reset the index to 0
message_index = 0
print("fetching next")

# fetch new messages
messages = spirit_board.get_messages(io)

# make the planchette jump to indicate messages are ready to display
spirit_board.slide_planchette(SpiritBoard.LOCATIONS["home"],
delay=0.02, step_size=6)
1 change: 1 addition & 0 deletions TFT_Spirit_Board/esp32s3_s2_tft_featherwing_480x320/lib
Binary file not shown.
Binary file not shown.
63 changes: 63 additions & 0 deletions TFT_Spirit_Board/shared/lib/anchored_tilegrid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# SPDX-FileCopyrightText: 2024 Tim Cocks
#
# SPDX-License-Identifier: MIT
"""
AnchoredTilegrid helper class
"""
try:
from typing import Tuple
except ImportError:
pass

from displayio import TileGrid


class AnchoredTileGrid(TileGrid):
"""
AnchoredTileGrid extends TileGrid and allows placing the TileGrid
relative to an arbitrary anchor point.
"""
def __init__(self, bitmap, **kwargs):
super().__init__(bitmap, **kwargs)
self._anchor_point = (0, 0)

self._anchored_position = (
0 if "x" not in kwargs else kwargs["x"],
0 if "y" not in kwargs else kwargs["y"]
)

@property
def anchor_point(self):
"""
The anchor point. tuple containing x and y values ranging
from 0 to 1.
"""
return self._anchor_point

@anchor_point.setter
def anchor_point(self, new_anchor_point: Tuple[float, float]) -> None:
self._anchor_point = new_anchor_point
# update the anchored_position using setter
self.anchored_position = self._anchored_position

@property
def anchored_position(self) -> Tuple[int, int]:
"""Position relative to the anchor_point. Tuple containing x,y
pixel coordinates."""
return self._anchored_position

@anchored_position.setter
def anchored_position(self, new_position: Tuple[int, int]) -> None:
self._anchored_position = new_position

if (self._anchor_point is not None) and (self._anchored_position is not None):
# Calculate (x,y) position
self.x = int(
new_position[0]
- round(self._anchor_point[0] * (self.tile_width * self.width))
)

self.y = int(
new_position[1]
- round(self._anchor_point[1] * (self.tile_height * self.height))
)
Loading