I've successfully wired up the two and am getting the "Hello World!" screen just fine.
I am going to be reading analog voltages at three pins and have those updating every 0.5 seconds (just reading one pin for now until I figure out the display part).
Here's the code I'm using and I know it's wrong because after a few dozen updates, it fails for "Memory error, memory allocation failed, allocating 512 bytes"..
Code: Select all
# Code Updated 2022-05-25 to attempt dynamic updating of voltage levels
import board
from digitalio import DigitalInOut, Direction, Pull
from analogio import AnalogOut, AnalogIn
from adafruit_display_text import label
from adafruit_st7789 import ST7789
import terminalio
import displayio
import time
# Analog input on A2
analog1in = AnalogIn(board.A2)
# Helper to convert analog input to voltage
def getVoltage(pin):
return (pin.value * 3.3) / 65536
# First set some parameters used for shapes and text
BORDER = 5
FONTSCALE = 2
BACKGROUND_COLOR = 0x000077 # Blue
FOREGROUND_COLOR = 0x770000 # Red
TEXT_COLOR = 0xFFFF77
# Release any resources currently in use for the displays
displayio.release_displays()
spi = board.SPI()
tft_cs = board.D7
tft_dc = board.D10
display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs)
display = ST7789(display_bus, rotation=270, width=240, height=135, rowstart=40, colstart=53)
# Make the display context
splash = displayio.Group()
display.show(splash)
color_bitmap = displayio.Bitmap(display.width, display.height, 1)
color_palette = displayio.Palette(1)
color_palette[0] = BACKGROUND_COLOR
bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
splash.append(bg_sprite)
while True:
# Read analog voltage on A2
text = "Id: %0.3f" % getVoltage(analog1in)
text_area = label.Label(terminalio.FONT, text=text, color=TEXT_COLOR)
text_width = text_area.bounding_box[2] * FONTSCALE
text_group = displayio.Group(
scale=FONTSCALE,
x=display.width // 2 - text_width // 2,
y=display.height // 2 - 16,
)
text_group.append(text_area)
splash.append(text_group)
time.sleep(.5)
Ultimately, the display should read like this (numbers are examples only):
Id: 5.678
Vd: 2.926
Vb: 12.204
And this with the numbers updating every half second (as set by my time.sleep(.5) )
I would really appreciate some direction - Coming into the TFT display world on microcontrollers is a huge change from what I've been doing on the Raspberry Pi and I feel like a complete noob! :) Thanks!
EDIT: This seems to be working much better - it was a random shot in the dark - using display.show, rather than sprite.append:
Code: Select all
while True:
# Read analog voltage on D0
#print("Id: %0.3f" % getVoltage(analog1in))
text = "Id: %0.3f" % getVoltage(analog1in)
text_area = label.Label(terminalio.FONT, text=text, color=TEXT_COLOR, scale=FONTSCALE)
text_width = text_area.bounding_box[2] * FONTSCALE
# Set the location
text_area.x = display.width // 2 - text_width // 2
text_area.y = display.height // 2
display.show(text_area)
time.sleep(.5)