|
| 1 | +# SPDX-FileCopyrightText: 2024 Tim Cocks |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +""" |
| 5 | +SpiritBoard code.py |
| 6 | +Feather ESP32-S3 or S2 + TFT Featherwing 3.5" 480x320 pixels |
| 7 | +
|
| 8 | +Receive and display messages from the spirits. |
| 9 | +""" |
| 10 | +# pylint: disable=import-error, invalid-name |
| 11 | +import os |
| 12 | +import displayio |
| 13 | +import board |
| 14 | +from digitalio import DigitalInOut |
| 15 | +import adafruit_connection_manager |
| 16 | +import wifi |
| 17 | +import adafruit_requests |
| 18 | +from adafruit_io.adafruit_io import IO_HTTP |
| 19 | +from adafruit_hx8357 import HX8357 # TFT Featherwing 3.5" 480x320 display driver |
| 20 | +import adafruit_tsc2007 |
| 21 | + |
| 22 | +from spirit_board import SpiritBoard |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +# 3.5" TFT Featherwing is 480x320 |
| 27 | +displayio.release_displays() |
| 28 | +DISPLAY_WIDTH = 480 |
| 29 | +DISPLAY_HEIGHT = 320 |
| 30 | + |
| 31 | +# Initialize TFT Display |
| 32 | +spi = board.SPI() |
| 33 | +tft_cs = board.D9 |
| 34 | +tft_dc = board.D10 |
| 35 | +display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs) |
| 36 | +display = HX8357(display_bus, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT) |
| 37 | +display.rotation = 0 |
| 38 | +_touch_flip = (False, True) |
| 39 | + |
| 40 | +# Initialize 3.5" TFT Featherwing Touchscreen |
| 41 | +ts_cs_pin = DigitalInOut(board.D6) |
| 42 | +i2c = board.I2C() |
| 43 | +tsc = adafruit_tsc2007.TSC2007(i2c, irq=None) |
| 44 | + |
| 45 | +# Initialize a requests session |
| 46 | +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) |
| 47 | +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) |
| 48 | +requests = adafruit_requests.Session(pool, ssl_context) |
| 49 | + |
| 50 | +# Set your Adafruit IO Username and Key in secrets.py |
| 51 | +# (visit io.adafruit.com if you need to create an account, |
| 52 | +# or if you need your Adafruit IO key.) |
| 53 | +aio_username = os.getenv("AIO_USERNAME") |
| 54 | +aio_key = os.getenv("AIO_KEY") |
| 55 | + |
| 56 | +# Initialize an Adafruit IO HTTP API object |
| 57 | +io = IO_HTTP(aio_username, aio_key, requests) |
| 58 | + |
| 59 | +# initialize the SpiritBoard class |
| 60 | +spirit_board = SpiritBoard(display) |
| 61 | + |
| 62 | +# get messages from io or the local file |
| 63 | +messages = spirit_board.get_messages(io) |
| 64 | + |
| 65 | +# The planchette is already at the home position. |
| 66 | +# Slide it to home again to make it jump, in order |
| 67 | +# indicate the message is ready to be received. |
| 68 | +spirit_board.slide_planchette(SpiritBoard.LOCATIONS["home"], |
| 69 | + delay=0.02, step_size=6) |
| 70 | + |
| 71 | +# current message index |
| 72 | +message_index = 0 |
| 73 | +while True: |
| 74 | + # if the display was touched |
| 75 | + if tsc.touched: |
| 76 | + # write the message at the current index |
| 77 | + spirit_board.write_message(messages[message_index], step_size=8) |
| 78 | + |
| 79 | + # if there are more messages in the list inside of context |
| 80 | + if message_index < len(messages) - 1: |
| 81 | + # increment the message index |
| 82 | + message_index += 1 |
| 83 | + |
| 84 | + else: # there are no more messages in the list |
| 85 | + # reset the index to 0 |
| 86 | + message_index = 0 |
| 87 | + print("fetching next") |
| 88 | + |
| 89 | + # fetch new messages |
| 90 | + messages = spirit_board.get_messages(io) |
| 91 | + |
| 92 | + # make the planchette jump to indicate messages are ready to display |
| 93 | + spirit_board.slide_planchette(SpiritBoard.LOCATIONS["home"], |
| 94 | + delay=0.02, step_size=6) |
0 commit comments