const std = @import("std");
const microzig = @import("microzig");
const rp2xxx = microzig.hal;
const time = rp2xxx.time;
const gpio = rp2xxx.gpio;
const SPI0 = rp2xxx.spi.instance.SPI0;
const uart = rp2xxx.uart.instance.num(0);
const uart_baud_rate = 115200;
const uart_tx_pin = gpio.num(0);
const uart_rx_pin = gpio.num(1);
const ssr = gpio.num(26);
const button = gpio.num(27);
const miso = gpio.num(16);
const csn = gpio.num(17);
const sck = gpio.num(18);
const mosi = gpio.num(19);
const rst = gpio.num(21);
// spi = SPI(0, 40_000_000, sck=Pin(18), mosi=Pin(19), miso=Pin(16))
// display = Display(spi, gamma=False, bgr=False, dc=Pin(20), cs=Pin(17), rst=Pin(21))
// display.clear(color565(64, 0, 255))
// const initDisplayCommands = [
// Command(code: 0x01, data: [], delay_ms: 150),
// Command(code: 0x01, data: [], delay_ms: 150),
// Command(code: 0x01, data: [], delay_ms: 150),
// Command(code: 0x01, data: [], delay_ms: 150),
// Command(code: 0x01, data: [], delay_ms: 150),
// Command(code: 0x01, data: [], delay_ms: 150),
// Command(code: 0x01, data: [], delay_ms: 150),
// Command(code: 0x01, data: [], delay_ms: 150),
const initDisplayCommands: []const Command = &.{
.{ .code = 0x01, .data = &.{}, .delay_ms = 150 }, // Software reset
// .{ .code = 0xEF, .data = &.{ 0x03, 0x80, 0x02 } },
.{ .code = 0xCF, .data = &.{ 0x00, 0xC1, 0x30 } }, // Pwr ctrl B
.{ .code = 0xED, .data = &.{ 0x64, 0x03, 0x12, 0x81 } }, // # Pwr on seq. ctrl
.{ .code = 0xE8, .data = &.{ 0x85, 0x00, 0x78 } }, // Driver timing ctrl A
.{ .code = 0xCB, .data = &.{ 0x39, 0x2C, 0x00, 0x34, 0x02 } }, // Pwr ctrl A
.{ .code = 0xF7, .data = &.{0x20} }, // Pump ratio control
.{ .code = 0xEA, .data = &.{ 0x00, 0x00 } }, // Driver timing ctrl B
.{ .code = 0xC0, .data = &.{0x23} }, // Pwr ctrl 1
.{ .code = 0xC1, .data = &.{0x10} }, // Pwr ctrl 2
.{ .code = 0xC5, .data = &.{ 0x3E, 0x28 } }, // VCM control1
.{ .code = 0xC7, .data = &.{0x86} }, // VCM control2
.{ .code = 0x36, .data = &.{0x80} }, // Memory Access Control
.{ .code = 0x37, .data = &.{0x00} }, // Vertical scroll zero
.{ .code = 0x3A, .data = &.{0x55} }, // COLMOD: Pixel Format Set
.{ .code = 0xB1, .data = &.{ 0x00, 0x18 } }, // Frame Rate Control (In Normal Mode/Full Colors)
.{ .code = 0xB6, .data = &.{ 0x08, 0x82, 0x27 } }, // Display Function Control
.{ .code = 0xF2, .data = &.{0x00} }, // 3Gamma Function Disable
.{ .code = 0x26, .data = &.{0x01} }, // Gamma curve selected
// .{ .code = 0xE0, .data = &.{ 0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, 0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00 } }, // Set Gamma Positive Gamma Correction
// .{ .code = 0xE1, .data = &.{ 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, 0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F } }, // Set Gamma Negative Gamma Correction
.{ .code = 0x11, .data = &.{}, .delay_ms = 150 }, // Exit Sleep/Sleep Out
.{ .code = 0x29, .data = &.{}, .delay_ms = 150 }, // Display on
fn send(self: Command) void {
SPI0.write_blocking(u8, &.{self.code});
fn sendData(data: []const u8) void {
SPI0.write_blocking(u8, data);
// Write a block of data to display.
// x0 (int): Starting X position.
// y0 (int): Starting Y position.
// x1 (int): Ending X position.
// y1 (int): Ending Y position.
// data (bytes): Data buffer to write.
fn block(x0: u16, y0: u16, x1: u16, y1: u16, data: []const u8) void {
send(.{ .code = 0x2A, .data = &.{ @intCast(x0 >> 8), @truncate(x0), @intCast(x1 >> 8), @truncate(x1) } }); // Column address set
send(.{ .code = 0x2B, .data = &.{ @intCast(y0 >> 8), @truncate(y0), @intCast(y1 >> 8), @truncate(y1) } }); // Page address set
send(.{ .code = 0x2C, .data = data });
fn draw_pixel(x: u16, y: u16, color: u16) void {
block(x, y, x, y, &.{color});
// def draw_circle(self, x0, y0, r, color):
// x0 (int): X coordinate of center point.
// y0 (int): Y coordinate of center point.
// color (int): RGB565 color value.
// self.draw_pixel(x0, y0 + r, color)
// self.draw_pixel(x0, y0 - r, color)
// self.draw_pixel(x0 + r, y0, color)
// self.draw_pixel(x0 - r, y0, color)
// self.draw_pixel(x0 + x, y0 + y, color)
// self.draw_pixel(x0 - x, y0 + y, color)
// self.draw_pixel(x0 + x, y0 - y, color)
// self.draw_pixel(x0 - x, y0 - y, color)
// self.draw_pixel(x0 + y, y0 + x, color)
// self.draw_pixel(x0 - y, y0 + x, color)
// self.draw_pixel(x0 + y, y0 - x, color)
// self.draw_pixel(x0 - y, y0 - x, color)
// def draw_image(self, path, x=0, y=0, w=320, h=240):
// """Draw image from flash.
// path (string): Image file path.
// x (int): X coordinate of image left. Default is 0.
// y (int): Y coordinate of image top. Default is 0.
// w (int): Width of image. Default is 320.
// h (int): Height of image. Default is 240.
// if self.is_off_grid(x, y, x2, y2):
// with open(path, "rb") as f:
// chunk_height = 1024 // w
// chunk_count, remainder = divmod(h, chunk_height)
// chunk_size = chunk_height * w * 2
// for c in range(0, chunk_count):
// buf = f.read(chunk_size)
// self.block(x, chunk_y,
// x2, chunk_y + chunk_height - 1,
// chunk_y += chunk_height
// buf = f.read(remainder * w * 2)
// self.block(x, chunk_y,
// x2, chunk_y + remainder - 1,
fn color565(r: u32, g: u32, b: u32) u16 {
return (r & 0xF8) << 8 | (g & 0xFC) << 3 | b >> 3;
fn clear(color: u8) void {
var buffer: [width]u8 = undefined;
while (i < buffer.len) : (i += 1) {
while (y < height) : (y += 1) {
block(0, y, width - 1, y + 8 - 1, &buffer);
fn hardware_reset() void {
inline for (&.{ miso, mosi, sck }) |pin| {
uart_tx_pin.set_function(.uart);
uart_rx_pin.set_function(.uart);
.baud_rate = uart_baud_rate,
.clock_config = rp2xxx.clock_config,
button.set_function(.sio);
button.set_direction(.in);
try SPI0.apply(.{ .clock_config = rp2xxx.clock_config, .baud_rate = 40_000_000 });
for (initDisplayCommands) |cmd| {
if (cmd.delay_ms) |delay_ms| {
var ascon = rp2xxx.rand.Ascon.init();
var rng = ascon.random();
const expect = std.testing.expect;
test "truncate u16 to u6" {
try expect(data2(@truncate(a)) == 64);
try expect(320 & 0xFF == 64);
try expect(320 >> 8 == 1);
try expect(data2(@intCast(a >> 8)) == 1);