Simple test¶
Ensure your device works with this simple test.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
# Quick test of RA8875 with Feather M4
import time
import busio
import digitalio
import board
import adafruit_ra8875.ra8875 as ra8875
from adafruit_ra8875.ra8875 import color565
BLACK = color565(0, 0, 0)
RED = color565(255, 0, 0)
BLUE = color565(0, 255, 0)
GREEN = color565(0, 0, 255)
YELLOW = color565(255, 255, 0)
CYAN = color565(0, 255, 255)
MAGENTA = color565(255, 0, 255)
WHITE = color565(255, 255, 255)
# Configuration for CS and RST pins:
cs_pin = digitalio.DigitalInOut(board.D9)
rst_pin = digitalio.DigitalInOut(board.D10)
int_pin = digitalio.DigitalInOut(board.D11)
# Config for display baudrate (default max is 6mhz):
BAUDRATE = 6000000
# Setup SPI bus using hardware SPI:
spi = busio.SPI(clock=board.SCK, MOSI=board.MOSI, MISO=board.MISO)
# Create and setup the RA8875 display:
display = ra8875.RA8875(spi, cs=cs_pin, rst=rst_pin, baudrate=BAUDRATE)
display.init()
display.fill(RED)
time.sleep(0.500)
display.fill(YELLOW)
time.sleep(0.500)
display.fill(BLUE)
time.sleep(0.500)
display.fill(CYAN)
time.sleep(0.500)
display.fill(MAGENTA)
time.sleep(0.500)
display.fill(BLACK)
display.circle(100, 100, 50, BLACK)
display.fill_circle(100, 100, 49, BLUE)
display.fill_rect(10, 10, 400, 200, GREEN)
display.rect(10, 10, 400, 200, BLUE)
display.fill_round_rect(200, 10, 200, 100, 10, RED)
display.round_rect(200, 10, 200, 100, 10, BLUE)
display.pixel(10, 10, BLACK)
display.pixel(11, 11, BLACK)
display.line(10, 10, 200, 100, RED)
display.fill_triangle(200, 15, 250, 100, 150, 125, YELLOW)
display.triangle(200, 15, 250, 100, 150, 125, BLACK)
display.fill_ellipse(300, 100, 100, 40, BLUE)
display.ellipse(300, 100, 100, 40, RED)
display.curve(50, 100, 80, 40, 2, BLACK)
display.fill_curve(50, 100, 78, 38, 2, WHITE)
display.txt_set_cursor(display.width // 2 - 200, display.height // 2 - 20)
display.txt_trans(WHITE)
display.txt_size(2)
testvar = 99
display.txt_write("Player Score: " + str(testvar))
display.touch_init(int_pin)
display.touch_enable(True)
x_scale = 1024 / display.width
y_scale = 1024 / display.height
# Main loop:
while True:
if display.touched():
coords = display.touch_read()
display.fill_circle(
int(coords[0] / x_scale), int(coords[1] / y_scale), 4, MAGENTA
)
display.txt_color(WHITE, BLACK)
display.txt_set_cursor(display.width // 2 - 220, display.height // 2 - 20)
display.txt_size(2)
display.txt_write(
"Position ("
+ str(int(coords[0] / x_scale))
+ ", "
+ str(int(coords[1] / y_scale))
+ ")"
)
|
Bitmap test¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
# Quick bitmap test of RA8875 with Feather M4
import busio
import digitalio
import board
import adafruit_ra8875.ra8875 as ra8875
from adafruit_ra8875.ra8875 import color565
try:
import struct
except ImportError:
import ustruct as struct
WHITE = color565(255, 255, 255)
# Configuration for CS and RST pins:
cs_pin = digitalio.DigitalInOut(board.D9)
rst_pin = digitalio.DigitalInOut(board.D10)
# Config for display baudrate (default max is 6mhz):
BAUDRATE = 8000000
# Setup SPI bus using hardware SPI:
spi = busio.SPI(clock=board.SCK, MOSI=board.MOSI, MISO=board.MISO)
# Create and setup the RA8875 display:
display = ra8875.RA8875(spi, cs=cs_pin, rst=rst_pin, baudrate=BAUDRATE)
display.init()
display.fill(WHITE)
def convert_555_to_565(rgb):
return (rgb & 0x7FE0) << 1 | 0x20 | rgb & 0x001F
class BMP:
def __init__(self, filename):
self.filename = filename
self.colors = None
self.data = 0
self.data_size = 0
self.bpp = 0
self.width = 0
self.height = 0
self.read_header()
def read_header(self):
if self.colors:
return
with open(self.filename, "rb") as f:
f.seek(10)
self.data = int.from_bytes(f.read(4), "little")
f.seek(18)
self.width = int.from_bytes(f.read(4), "little")
self.height = int.from_bytes(f.read(4), "little")
f.seek(28)
self.bpp = int.from_bytes(f.read(2), "little")
f.seek(34)
self.data_size = int.from_bytes(f.read(4), "little")
f.seek(46)
self.colors = int.from_bytes(f.read(4), "little")
def draw(self, disp, x=0, y=0):
print("{:d}x{:d} image".format(self.width, self.height))
print("{:d}-bit encoding detected".format(self.bpp))
line = 0
line_size = self.width * (self.bpp // 8)
if line_size % 4 != 0:
line_size += 4 - line_size % 4
current_line_data = b""
with open(self.filename, "rb") as f:
f.seek(self.data)
disp.set_window(x, y, self.width, self.height)
for line in range(self.height):
current_line_data = b""
line_data = f.read(line_size)
for i in range(0, line_size, self.bpp // 8):
if (line_size - i) < self.bpp // 8:
break
if self.bpp == 16:
color = convert_555_to_565(line_data[i] | line_data[i + 1] << 8)
if self.bpp == 24 or self.bpp == 32:
color = color565(
line_data[i + 2], line_data[i + 1], line_data[i]
)
current_line_data = current_line_data + struct.pack(">H", color)
disp.setxy(x, self.height - line + y)
disp.push_pixels(current_line_data)
disp.set_window(0, 0, disp.width, disp.height)
bitmap = BMP("/ra8875_blinka.bmp")
x_position = (display.width // 2) - (bitmap.width // 2)
y_position = (display.height // 2) - (bitmap.height // 2)
bitmap.draw(display, x_position, y_position)
|