start work on 320x240 screen version using the pi pico

This commit is contained in:
Crispy 2025-06-16 19:39:07 +02:00
parent 8acd684e59
commit 90f648e0b0
13 changed files with 112280 additions and 20 deletions

1
pico_decoder/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
bin

View file

@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.13...3.27)
# initialize the SDK based on PICO_SDK_PATH
# note: this must happen before project()
include(pico_sdk_import.cmake)
project(Bad_Apple!!_Pico)
# initialize the Raspberry Pi Pico SDK
pico_sdk_init()
# rest of your project
add_executable(bad_apple_pico
src/main.c
)
# Add pico_stdlib library which aggregates commonly used features
target_link_libraries(bad_apple_pico
pico_stdlib
hardware_spi
)
# create map/bin/hex/uf2 file in addition to ELF.
pico_add_extra_outputs(bad_apple_pico)

8
pico_decoder/Makefile Executable file
View file

@ -0,0 +1,8 @@
build_thing:
cmake -B bin -DPICO_SDK_PATH=../../pico-sdk
make -j8 -C bin
# then manually copy bin/thing.uf2 to the pico
clean:
rm -rf bin

View file

@ -0,0 +1,121 @@
# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake
# This can be dropped into an external project to help locate this SDK
# It should be include()ed prior to project()
# Copyright 2020 (c) 2020 Raspberry Pi (Trading) Ltd.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
# following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
endif ()
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
endif ()
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
endif ()
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_TAG} AND (NOT PICO_SDK_FETCH_FROM_GIT_TAG))
set(PICO_SDK_FETCH_FROM_GIT_TAG $ENV{PICO_SDK_FETCH_FROM_GIT_TAG})
message("Using PICO_SDK_FETCH_FROM_GIT_TAG from environment ('${PICO_SDK_FETCH_FROM_GIT_TAG}')")
endif ()
if (PICO_SDK_FETCH_FROM_GIT AND NOT PICO_SDK_FETCH_FROM_GIT_TAG)
set(PICO_SDK_FETCH_FROM_GIT_TAG "master")
message("Using master as default value for PICO_SDK_FETCH_FROM_GIT_TAG")
endif()
set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK")
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable")
set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")
set(PICO_SDK_FETCH_FROM_GIT_TAG "${PICO_SDK_FETCH_FROM_GIT_TAG}" CACHE FILEPATH "release tag for SDK")
if (NOT PICO_SDK_PATH)
if (PICO_SDK_FETCH_FROM_GIT)
include(FetchContent)
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
if (PICO_SDK_FETCH_FROM_GIT_PATH)
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
endif ()
FetchContent_Declare(
pico_sdk
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
)
if (NOT pico_sdk)
message("Downloading Raspberry Pi Pico SDK")
# GIT_SUBMODULES_RECURSE was added in 3.17
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0")
FetchContent_Populate(
pico_sdk
QUIET
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
GIT_SUBMODULES_RECURSE FALSE
SOURCE_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-src
BINARY_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-build
SUBBUILD_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-subbuild
)
else ()
FetchContent_Populate(
pico_sdk
QUIET
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
SOURCE_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-src
BINARY_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-build
SUBBUILD_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-subbuild
)
endif ()
set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
endif ()
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
else ()
message(FATAL_ERROR
"SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
)
endif ()
endif ()
get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
if (NOT EXISTS ${PICO_SDK_PATH})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
endif ()
set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK")
endif ()
set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)
include(${PICO_SDK_INIT_CMAKE_FILE})

111341
pico_decoder/src/data.h Normal file

File diff suppressed because it is too large Load diff

231
pico_decoder/src/display.h Normal file
View file

@ -0,0 +1,231 @@
#include <stdint.h>
/*
Minimal helper library for the 320x240 TFT LCD, with ILI9341 controller chip
Required configuration, example:
#define DISPLAY_SPI spi0
#define PIN_TFT_BACKLIGHT 1
#define PIN_TFT_SCK 2
#define PIN_TFT_MOSI 3
#define PIN_TFT_CS 5 // optional
#define PIN_TFT_DC 6
#define PIN_TFT_RESET 7
*/
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
#define DC_C sio_hw->gpio_clr = (1ul << PIN_TFT_DC)
#define DC_D sio_hw->gpio_set = (1ul << PIN_TFT_DC)
#define WAIT_FOR_BUSY while (spi_get_hw(DISPLAY_SPI)->sr & SPI_SSPSR_BSY_BITS){};
#define tft_Write_8N(B) while (!spi_is_writable(DISPLAY_SPI)){}; spi_get_hw(DISPLAY_SPI)->dr = (uint8_t)(B)
#define COLOR_BLACK 0x0000 // 0, 0, 0
#define COLOR_NAVY 0x000F // 0, 0, 123
#define COLOR_DARKGREEN 0x03E0 // 0, 125, 0
#define COLOR_DARKCYAN 0x03EF // 0, 125, 123
#define COLOR_MAROON 0x7800 // 123, 0, 0
#define COLOR_PURPLE 0x780F // 123, 0, 123
#define COLOR_OLIVE 0x7BE0 // 123, 125, 0
#define COLOR_LIGHTGREY 0xC618 // 198, 195, 198
#define COLOR_DARKGREY 0x7BEF // 123, 125, 123
#define COLOR_BLUE 0x001F // 0, 0, 255
#define COLOR_GREEN 0x07E0 // 0, 255, 0
#define COLOR_CYAN 0x07FF // 0, 255, 255
#define COLOR_RED 0xF800 // 255, 0, 0
#define COLOR_MAGENTA 0xF81F // 255, 0, 255
#define COLOR_YELLOW 0xFFE0 // 255, 255, 0
#define COLOR_WHITE 0xFFFF // 255, 255, 255
#define COLOR_ORANGE 0xFD20 // 255, 165, 0
#define COLOR_GREENYELLOW 0xAFE5 // 173, 255, 41
#define COLOR_PINK 0xFC18 // 255, 130, 198
// #define ILI9341_TFTWIDTH 240 // ILI9341 max TFT width
// #define ILI9341_TFTHEIGHT 320 // ILI9341 max TFT height
// with swapped vertical/horizontal memory access, making it landscape mode
#define ILI9341_TFTWIDTH 320
#define ILI9341_TFTHEIGHT 240
#define ILI9341_NOP 0x00 // No-op register
#define ILI9341_SWRESET 0x01 // Software reset register
#define ILI9341_RDDID 0x04 // Read display identification information
#define ILI9341_RDDST 0x09 // Read Display Status
#define ILI9341_SLPIN 0x10 // Enter Sleep Mode
#define ILI9341_SLPOUT 0x11 // Sleep Out
#define ILI9341_PTLON 0x12 // Partial Mode ON
#define ILI9341_NORON 0x13 // Normal Display Mode ON
#define ILI9341_RDMODE 0x0A // Read Display Power Mode
#define ILI9341_RDMADCTL 0x0B // Read Display MADCTL
#define ILI9341_RDPIXFMT 0x0C // Read Display Pixel Format
#define ILI9341_RDIMGFMT 0x0D // Read Display Image Format
#define ILI9341_RDSELFDIAG 0x0F // Read Display Self-Diagnostic Result
#define ILI9341_INVOFF 0x20 // Display Inversion OFF
#define ILI9341_INVON 0x21 // Display Inversion ON
#define ILI9341_GAMMASET 0x26 // Gamma Set
#define ILI9341_DISPOFF 0x28 // Display OFF
#define ILI9341_DISPON 0x29 // Display ON
#define ILI9341_CASET 0x2A // Column Address Set
#define ILI9341_PASET 0x2B // Page Address Set
#define ILI9341_RAMWR 0x2C // Memory Write
#define ILI9341_RAMRD 0x2E // Memory Read
#define ILI9341_PTLAR 0x30 // Partial Area
#define ILI9341_VSCRDEF 0x33 // Vertical Scrolling Definition
#define ILI9341_MADCTL 0x36 // Memory Access Control
#define ILI9341_VSCRSADD 0x37 // Vertical Scrolling Start Address
#define ILI9341_PIXFMT 0x3A // COLMOD: Pixel Format Set
#define ILI9341_FRMCTR1 0xB1 // Frame Rate Control (In Normal Mode/Full Colors)
#define ILI9341_FRMCTR2 0xB2 // Frame Rate Control (In Idle Mode/8 colors)
#define ILI9341_FRMCTR3 0xB3 // Frame Rate control (In Partial Mode/Full Colors)
#define ILI9341_INVCTR 0xB4 // Display Inversion Control
#define ILI9341_DFUNCTR 0xB6 // Display Function Control
#define ILI9341_PWCTR1 0xC0 // Power Control 1
#define ILI9341_PWCTR2 0xC1 // Power Control 2
#define ILI9341_PWCTR3 0xC2 // Power Control 3
#define ILI9341_PWCTR4 0xC3 // Power Control 4
#define ILI9341_PWCTR5 0xC4 // Power Control 5
#define ILI9341_VMCTR1 0xC5 // VCOM Control 1
#define ILI9341_VMCTR2 0xC7 // VCOM Control 2
#define ILI9341_RDID1 0xDA // Read ID 1
#define ILI9341_RDID2 0xDB // Read ID 2
#define ILI9341_RDID3 0xDC // Read ID 3
#define ILI9341_RDID4 0xDD // Read ID 4
#define ILI9341_GMCTRP1 0xE0 // Positive Gamma Correction
#define ILI9341_GMCTRN1 0xE1 // Negative Gamma Correction
static const uint8_t initcmd[] = {
0xEF, 3, 0x03, 0x80, 0x02, // undocumented mystery command
0xCF, 3, 0x00, 0xC1, 0x30, // power control B
0xED, 4, 0x64, 0x03, 0x12, 0x81, // power on sequence control
0xE8, 3, 0x85, 0x00, 0x78, // driver timing control A
0xCB, 5, 0x39, 0x2C, 0x00, 0x34, 0x02, // power control A
0xF7, 1, 0x20, // pump ratio control
0xEA, 2, 0x00, 0x00, // driver timing control B
ILI9341_PWCTR1 , 1, 0x23, // Power control VRH[5:0]
ILI9341_PWCTR2 , 1, 0x10, // Power control SAP[2:0];BT[3:0]
ILI9341_VMCTR1 , 2, 0x3e, 0x28, // VCOM control 1
ILI9341_VMCTR2 , 1, 0x86, // VCOM control 2
ILI9341_MADCTL , 1, 0x48, // Memory Access Control, sets access order
// ILI9341_VSCRSADD, 1, 0x00, // Vertical scroll zero
ILI9341_PIXFMT , 1, 0x55, // pixel format, 16bpp (RGB565)
// 0xF6, 3, 0x01, 0x00, 0x02, // enable RGB mode
ILI9341_FRMCTR1 , 2, 0x00, 0x13, // frame rate control, division ratio 2^0, 79hz(why??)
ILI9341_DFUNCTR , 3, 0x08, 0x82, 0x27, // Display Function Control,
0xF2, 1, 0x00, // 3Gamma Function Disable
ILI9341_GAMMASET , 1, 0x01, // Gamma curve selected
ILI9341_GMCTRP1 , 15, 0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, // Set Gamma
0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00,
ILI9341_GMCTRN1 , 15, 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, // Set Gamma
0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F,
ILI9341_SLPOUT , 0x80, // Exit Sleep
ILI9341_DISPON , 0, // Display on
0x00 // End of list
};
void tft_send_command(u8 cmd, const u8 *args, size_t arg_count) {
#ifdef PIN_TFT_CS
gpio_put(PIN_TFT_CS, 0);
#endif
DC_C;
spi_write_blocking(DISPLAY_SPI, &cmd, 1);
if (arg_count) {
DC_D;
spi_write_blocking(DISPLAY_SPI, args, arg_count);
}
#ifdef PIN_TFT_CS
gpio_put(PIN_TFT_CS, 1);
#endif
}
void tft_init_pins() {
gpio_init(PIN_TFT_BACKLIGHT);
gpio_set_dir(PIN_TFT_BACKLIGHT, GPIO_OUT);
gpio_set_dir(PIN_TFT_SCK, GPIO_OUT);
gpio_set_function(PIN_TFT_SCK, GPIO_FUNC_SPI);
gpio_set_dir(PIN_TFT_MOSI, GPIO_OUT);
gpio_set_function(PIN_TFT_MOSI, GPIO_FUNC_SPI);
gpio_set_function(PIN_TFT_DC, GPIO_FUNC_SIO);
gpio_set_dir(PIN_TFT_DC, GPIO_OUT);
gpio_put(PIN_TFT_DC, 1);
#ifdef PIN_TFT_CS
gpio_init(PIN_TFT_CS);
gpio_set_dir(PIN_TFT_CS, GPIO_OUT);
#endif
gpio_set_function(PIN_TFT_RESET, GPIO_FUNC_SIO);
gpio_set_dir(PIN_TFT_RESET, GPIO_OUT);
gpio_put(PIN_TFT_RESET, 1);
}
void tft_init_display(uint baudrate) {
tft_init_pins();
spi_init(DISPLAY_SPI, baudrate);
// reset display
gpio_put(PIN_TFT_RESET, 1);
sleep_ms(5);
gpio_put(PIN_TFT_RESET, 0);
sleep_ms(20);
gpio_put(PIN_TFT_RESET, 1);
sleep_ms(150);
// init display
const u8 *addr = initcmd;
while (1) {
u8 cmd = *(addr++);
if (cmd == 0) break;
u8 x = *(addr++);
u8 arg_count = x & 0x7f;
tft_send_command(cmd, addr, arg_count);
addr += arg_count;
if (x & 0x80)
sleep_ms(120);
}
sleep_ms(150);
gpio_put(PIN_TFT_BACKLIGHT, 1);
}
void tft_set_pixel(int x, int y, u32 color) {
tft_send_command(ILI9341_CASET, (u8[]){x >> 8, x, x >> 8, x}, 4);
tft_send_command(ILI9341_PASET, (u8[]){y >> 8, y, y >> 8, y}, 4);
DC_C;
spi_get_hw(DISPLAY_SPI)->dr = ILI9341_RAMWR;
WAIT_FOR_BUSY;
DC_D;
tft_Write_8N((u8)(color >> 8));
tft_Write_8N((u8)(color & 0xff));
WAIT_FOR_BUSY;
}
void tft_fill(u16 color) {
tft_send_command(ILI9341_CASET, (u8[]){0, 0, ILI9341_TFTWIDTH >> 8, (u8)ILI9341_TFTWIDTH}, 4);
tft_send_command(ILI9341_PASET, (u8[]){0, 0, ILI9341_TFTHEIGHT >> 8, (u8)ILI9341_TFTHEIGHT}, 4);
DC_C;
spi_get_hw(DISPLAY_SPI)->dr = ILI9341_RAMWR;
WAIT_FOR_BUSY;
DC_D;
u8 upper = color >> 8;
u8 lower = color;
u32 pixel_count = ILI9341_TFTWIDTH * ILI9341_TFTHEIGHT;
while (pixel_count--) {
tft_Write_8N(upper);
tft_Write_8N(lower);
}
WAIT_FOR_BUSY;
}

277
pico_decoder/src/main.c Normal file
View file

@ -0,0 +1,277 @@
#include "pico/stdlib.h"
#include "hardware/spi.h"
#define DISPLAY_SPI spi0
#define PIN_TFT_BACKLIGHT 1
#define PIN_TFT_SCK 2
#define PIN_TFT_MOSI 3
// #define PIN_TFT_CS 5
#define PIN_TFT_DC 6
#define PIN_TFT_RESET 7
#include "display.h"
#include "data.h"
#define HEIGHT ILI9341_TFTHEIGHT
#define WIDTH ILI9341_TFTWIDTH
// todo pack as bits instead
#define AREA (HEIGHT * WIDTH)
u8 frame[AREA];
u32 reader;
bool error = false;
u8 last_frame_type;
u32 last_frame_start;
void decode_next_frame();
void refresh_screen();
void set_pixel(u16 x, u16 y, u8 color);
void draw_digit(u16 x, u16 y, u8 digit) {
const u16 digit_font[10] = {
0b111101101101111, // 0
0b010110010010111, // 1
0b111001111100111, // 2
0b111001111001111, // 3
0b101101111001001, // 4
0b111100111001111, // 5
0b111100111101111, // 6
0b111001001001001, // 7
0b111101111101111, // 8
0b111101111001111, // 9
};
for (int xx = 0; xx < 5; xx++)
for (int yy = 0; yy < 7; yy++){
set_pixel(x+xx-1, y+yy-1, 0);
}
u16 pixels = digit_font[digit];
set_pixel(x + 0, y + 0, (pixels >> 14) & 1);
set_pixel(x + 1, y + 0, (pixels >> 13) & 1);
set_pixel(x + 2, y + 0, (pixels >> 12) & 1);
set_pixel(x + 0, y + 1, (pixels >> 11) & 1);
set_pixel(x + 1, y + 1, (pixels >> 10) & 1);
set_pixel(x + 2, y + 1, (pixels >> 9) & 1);
set_pixel(x + 0, y + 2, (pixels >> 8) & 1);
set_pixel(x + 1, y + 2, (pixels >> 7) & 1);
set_pixel(x + 2, y + 2, (pixels >> 6) & 1);
set_pixel(x + 0, y + 3, (pixels >> 5) & 1);
set_pixel(x + 1, y + 3, (pixels >> 4) & 1);
set_pixel(x + 2, y + 3, (pixels >> 3) & 1);
set_pixel(x + 0, y + 4, (pixels >> 2) & 1);
set_pixel(x + 1, y + 4, (pixels >> 1) & 1);
set_pixel(x + 2, y + 4, (pixels >> 0) & 1);
}
void draw_num(u16 x, u16 y, u32 num) {
while (num) {
u8 digit = num%10;
num /= 10;
draw_digit(x, y, digit);
x -= 4;
}
}
int main() {
gpio_init(PICO_DEFAULT_LED_PIN);
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
tft_init_display(100 * 1000 * 1000); // max is 62.5MHz
// tft_fill(0);
while (!error) {
decode_next_frame();
draw_num(30,2, reader);
draw_num(15, 8, video[reader]);
refresh_screen();
sleep_ms(100);
if (reader >= sizeof(video))
reader = 0;
}
gpio_put(PICO_DEFAULT_LED_PIN, 1);
draw_num(30, 2, reader);
draw_num(15, 8, video[reader]);
draw_num(30, 16, last_frame_start);
draw_num(15, 22, last_frame_type);
refresh_screen();
while(1){};
}
void refresh_screen() {
tft_send_command(ILI9341_CASET, (u8[]){0, 0, HEIGHT >> 8, (u8)HEIGHT}, 4);
tft_send_command(ILI9341_PASET, (u8[]){0, 0, WIDTH >> 8, (u8)WIDTH}, 4);
DC_C;
spi_get_hw(DISPLAY_SPI)->dr = ILI9341_RAMWR;
WAIT_FOR_BUSY;
DC_D;
u32 pixel_count = AREA;
const u8 pixels[2] = {0, 176};
while (pixel_count--) {
u8 p = pixels[frame[pixel_count]];
tft_Write_8N(p);
tft_Write_8N(p);
}
WAIT_FOR_BUSY;
}
u8 next_byte() {
u8 byte = video[reader];
reader += 1;
return byte;
}
void bad_memset(u8* addr, u8 value, u32 len) {
while (len--) {
*(addr++) = value;
}
}
void fill_frame(u8 color) {
bad_memset(frame, color, sizeof(frame));
}
void set_pixel(u16 x, u16 y, u8 color)
{
if (x >= WIDTH || y >= HEIGHT){
while (1){
gpio_put(PICO_DEFAULT_LED_PIN, 1);
sleep_ms(50);
gpio_put(PICO_DEFAULT_LED_PIN, 0);
sleep_ms(50);
}
}
x = (WIDTH-1) - x;
const u32 index = x * HEIGHT + y;
frame[index] = color;
}
void rle_horizontal();
void rle_vertical();
void bg_strips_h();
void cell_diff_4vv();
void decode_next_frame()
{
Encoding_t encoding = next_byte();
u32 frame_start = reader;
switch (encoding)
{
#ifdef USE_FillBlack
case Encoding_FillBlack:
fill_frame(0);
break;
#endif
#ifdef USE_FillWhite
case Encoding_FillWhite:
fill_frame(1);
break;
#endif
#ifdef USE_RLEHorizontal
case Encoding_RLEHorizontal:
rle_horizontal();
break;
#endif
#ifdef USE_RLEVertical
case Encoding_RLEVertical:
rle_vertical();
break;
#endif
#ifdef USE_BGStripsH24
case Encoding_BGStripsH24:
bg_strips_h();
break;
#endif
#ifdef USE_CellDiff4VV_small
case Encoding_CellDiff4VV_small:
cell_diff_4vv();
break;
#endif
default:
error = true;
return;
}
last_frame_type = encoding;
last_frame_start = frame_start;
}
#ifdef USE_RLEHorizontal
void rle_horizontal()
{
u16 x = 0;
u16 y = 0;
u8 color = 0;
while (y < HEIGHT) {
u8 run = next_byte();
for (int i = 0; i < run; i++) {
set_pixel(x, y, color);
x += 1;
if (x == WIDTH) {
x = 0;
y += 1;
}
}
color = !color;
}
}
#endif
#ifdef USE_RLEVertical
void rle_vertical()
{
u16 x = 0;
u16 y = 0;
u8 color = 0;
while (x < WIDTH)
{
u16 run = next_byte();
for (u16 i = 0; i < run; i++)
{
set_pixel(x, y, color);
y += 1;
if (y == HEIGHT) {
y = 0;
x += 1;
}
}
color = !color;
}
}
#endif
#ifdef USE_BGStripsH24
void bg_strips_h()
{
u8 head = next_byte();
u8 bg = head >> 7;
u8 fg = 1 - bg;
fill_frame(bg);
u16 strip_count = ((u16)(head & 127) << 8) | next_byte();
for (u16 i = 0; i < strip_count; i++) {
u16 y = next_byte();
u16 x_lower = next_byte();
u16 x_width = next_byte();
u16 x_start = x_lower | ((x_width & 0x80) << 1);
u8 width = x_width & 0x7f;
for (int x = x_start; x < (x_start + width); x++) {
set_pixel(x, y, fg);
}
}
}
#endif
#ifdef USE_CellDiff4VV_small
void cell_diff_4vv() {
const u32 cells_x = WIDTH/4;
const u32 cells_y = HEIGHT/4;
bool modified_cells[cells_x*cells_y];
for (u16 x = 0; x < WIDTH; x++) {
for (u16 y = 0; y < HEIGHT; y++) {
}
}
}
#endif