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