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
 6import busio
 7import digitalio
 8import board
 9
10from adafruit_ra8875 import ra8875
11from adafruit_ra8875.ra8875 import color565
12
13BLACK = color565(0, 0, 0)
14RED = color565(255, 0, 0)
15BLUE = color565(0, 255, 0)
16GREEN = color565(0, 0, 255)
17YELLOW = color565(255, 255, 0)
18CYAN = color565(0, 255, 255)
19MAGENTA = color565(255, 0, 255)
20WHITE = color565(255, 255, 255)
21
22# Configuration for CS and RST pins:
23cs_pin = digitalio.DigitalInOut(board.D9)
24rst_pin = digitalio.DigitalInOut(board.D10)
25int_pin = digitalio.DigitalInOut(board.D11)
26
27# Config for display baudrate (default max is 6mhz):
28BAUDRATE = 6000000
29
30# Setup SPI bus using hardware SPI:
31spi = busio.SPI(clock=board.SCK, MOSI=board.MOSI, MISO=board.MISO)
32
33# Create and setup the RA8875 display:
34display = ra8875.RA8875(spi, cs=cs_pin, rst=rst_pin, baudrate=BAUDRATE)
35display.init()
36
37display.fill(RED)
38time.sleep(0.500)
39display.fill(YELLOW)
40time.sleep(0.500)
41display.fill(BLUE)
42time.sleep(0.500)
43display.fill(CYAN)
44time.sleep(0.500)
45display.fill(MAGENTA)
46time.sleep(0.500)
47display.fill(BLACK)
48display.circle(100, 100, 50, BLACK)
49display.fill_circle(100, 100, 49, BLUE)
50
51display.fill_rect(10, 10, 400, 200, GREEN)
52display.rect(10, 10, 400, 200, BLUE)
53display.fill_round_rect(200, 10, 200, 100, 10, RED)
54display.round_rect(200, 10, 200, 100, 10, BLUE)
55display.pixel(10, 10, BLACK)
56display.pixel(11, 11, BLACK)
57display.line(10, 10, 200, 100, RED)
58display.fill_triangle(200, 15, 250, 100, 150, 125, YELLOW)
59display.triangle(200, 15, 250, 100, 150, 125, BLACK)
60display.fill_ellipse(300, 100, 100, 40, BLUE)
61display.ellipse(300, 100, 100, 40, RED)
62display.curve(50, 100, 80, 40, 2, BLACK)
63display.fill_curve(50, 100, 78, 38, 2, WHITE)
64
65display.txt_set_cursor(display.width // 2 - 200, display.height // 2 - 20)
66display.txt_trans(WHITE)
67display.txt_size(2)
68testvar = 99
69display.txt_write("Player Score: " + str(testvar))
70
71display.touch_init(int_pin)
72display.touch_enable(True)
73
74x_scale = 1024 / display.width
75y_scale = 1024 / display.height
76
77# Main loop:
78while True:
79    if display.touched():
80        coords = display.touch_read()
81        display.fill_circle(
82            int(coords[0] / x_scale), int(coords[1] / y_scale), 4, MAGENTA
83        )
84        display.txt_color(WHITE, BLACK)
85        display.txt_set_cursor(display.width // 2 - 220, display.height // 2 - 20)
86        display.txt_size(2)
87        display.txt_write(
88            "Position ("
89            + str(int(coords[0] / x_scale))
90            + ", "
91            + str(int(coords[1] / y_scale))
92            + ")"
93        )

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 busio
 8import digitalio
 9import board
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("{:d}x{:d} image".format(self.width, self.height))
65        print("{:d}-bit encoding detected".format(self.bpp))
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(
84                            line_data[i + 2], line_data[i + 1], line_data[i]
85                        )
86                    current_line_data = current_line_data + struct.pack(">H", color)
87                disp.setxy(x, self.height - line + y)
88                disp.push_pixels(current_line_data)
89            disp.set_window(0, 0, disp.width, disp.height)
90
91
92bitmap = BMP("/ra8875_blinka.bmp")
93x_position = (display.width // 2) - (bitmap.width // 2)
94y_position = (display.height // 2) - (bitmap.height // 2)
95bitmap.draw(display, x_position, y_position)