This commit is contained in:
Crispy 2025-06-19 00:03:28 +02:00
parent f05d146af7
commit f1f20cb5e1
3 changed files with 34 additions and 65 deletions

View file

@ -6,6 +6,6 @@ CH32V003FUN:=../../ch32v003fun/ch32v003fun
include ../../ch32v003fun/ch32v003fun/ch32v003fun.mk include ../../ch32v003fun/ch32v003fun/ch32v003fun.mk
flash : build flash : build
gdb-multiarch -x gdbinit -ex 'load' -ex 'detach' -ex 'quit' $(TARGET).elf gdb -x gdbinit -ex 'load' -ex 'detach' -ex 'quit' $(TARGET).elf
clean : cv_clean clean : cv_clean

View file

@ -1,7 +1,5 @@
#define SSD1306_128X32 #define SSD1306_128X32
#include "ch32v003fun.h" #include "ch32v003fun.h"
#include <stdio.h>
#include "ssd1306_i2c.h" #include "ssd1306_i2c.h"
#include "ssd1306.h" #include "ssd1306.h"
@ -21,8 +19,7 @@ u32 reader = 0;
u32 frame[WIDTH]; u32 frame[WIDTH];
int main() int main() {
{
SystemInit(); SystemInit();
// funGpioInitAll(); // funGpioInitAll();
// funPinMode(PIN_LED, GPIO_CFGLR_OUT_10Mhz_PP); // funPinMode(PIN_LED, GPIO_CFGLR_OUT_10Mhz_PP);
@ -36,8 +33,7 @@ int main()
ssd1306_cmd(*cmd_list++); ssd1306_cmd(*cmd_list++);
clear_screen(); clear_screen();
while (1) while (1) {
{
decode_next_frame(); decode_next_frame();
refresh_screen(); refresh_screen();
Delay_Ms(300); Delay_Ms(300);
@ -46,8 +42,7 @@ int main()
} }
} }
void refresh_screen() void refresh_screen() {
{
ssd1306_cmd(SSD1306_COLUMNADDR); ssd1306_cmd(SSD1306_COLUMNADDR);
ssd1306_cmd(X_OFFSET); // Column start address (0 = reset) ssd1306_cmd(X_OFFSET); // Column start address (0 = reset)
ssd1306_cmd(X_OFFSET + WIDTH - 1); // Column end address (127 = reset) ssd1306_cmd(X_OFFSET + WIDTH - 1); // Column end address (127 = reset)
@ -60,27 +55,23 @@ void refresh_screen()
u8 x; u8 x;
u8 packet_index; u8 packet_index;
u8 page = 0; u8 page = 0;
while (page < 8) while (page < 8) {
{
/* odd rows are unused so each nybble is expanded */ /* odd rows are unused so each nybble is expanded */
packet[packet_index] = expand[(frame[x] >> (page * 4)) & 0xf]; packet[packet_index] = expand[(frame[x] >> (page * 4)) & 0xf];
x++; x++;
packet_index++; packet_index++;
if (x == WIDTH) if (x == WIDTH) {
{
x = 0; x = 0;
page++; page++;
} }
if (packet_index == SSD1306_PSZ) if (packet_index == SSD1306_PSZ) {
{
ssd1306_data(packet, SSD1306_PSZ); ssd1306_data(packet, SSD1306_PSZ);
packet_index = 0; packet_index = 0;
} }
} }
} }
void clear_screen() void clear_screen() {
{
ssd1306_cmd(SSD1306_COLUMNADDR); ssd1306_cmd(SSD1306_COLUMNADDR);
ssd1306_cmd(0); // Column start address (0 = reset) ssd1306_cmd(0); // Column start address (0 = reset)
ssd1306_cmd(127); // Column end address (127 = reset) ssd1306_cmd(127); // Column end address (127 = reset)
@ -91,20 +82,17 @@ void clear_screen()
ssd1306_data(frame, SSD1306_PSZ); ssd1306_data(frame, SSD1306_PSZ);
} }
u8 next_byte() u8 next_byte() {
{
u8 byte = video[reader]; u8 byte = video[reader];
reader += 1; reader += 1;
return byte; return byte;
} }
void fill_frame(u8 color) void fill_frame(u8 color) {
{
memset(frame, color ? 0xFFFFFFFF : 0x00, sizeof(frame)); memset(frame, color ? 0xFFFFFFFF : 0x00, sizeof(frame));
} }
void set_pixel(u8 x, u8 y, u8 color) void set_pixel(u8 x, u8 y, u8 color) {
{
if (color) if (color)
frame[x] |= (1 << (y & 31)); frame[x] |= (1 << (y & 31));
else else
@ -116,11 +104,9 @@ void rle_vertical();
void bg_strips_h(); void bg_strips_h();
void cell_diff_8v(); void cell_diff_8v();
void decode_next_frame() void decode_next_frame() {
{
Encoding_t encoding = next_byte(); Encoding_t encoding = next_byte();
switch (encoding) switch (encoding) {
{
#ifdef USE_FillBlack #ifdef USE_FillBlack
case Encoding_FillBlack: case Encoding_FillBlack:
fill_frame(0); fill_frame(0);
@ -141,8 +127,8 @@ void decode_next_frame()
rle_vertical(); rle_vertical();
break; break;
#endif #endif
#ifdef USE_BGStripsH #ifdef USE_BGStripsH16
case Encoding_BGStripsH: case Encoding_BGStripsH16:
bg_strips_h(); bg_strips_h();
break; break;
#endif #endif
@ -158,20 +144,16 @@ void decode_next_frame()
} }
#ifdef USE_RLEHorizontal #ifdef USE_RLEHorizontal
void rle_horizontal() void rle_horizontal() {
{
u8 x = 0; u8 x = 0;
u8 y = 0; u8 y = 0;
u8 color = 0; u8 color = 0;
while (y < HEIGHT) while (y < HEIGHT) {
{
u8 run = next_byte(); u8 run = next_byte();
for (int i = 0; i < run; i++) for (int i = 0; i < run; i++) {
{
set_pixel(x, y, color); set_pixel(x, y, color);
x += 1; x += 1;
if (x == WIDTH) if (x == WIDTH) {
{
x = 0; x = 0;
y += 1; y += 1;
} }
@ -182,20 +164,16 @@ void rle_horizontal()
#endif #endif
#ifdef USE_RLEVertical #ifdef USE_RLEVertical
void rle_vertical() void rle_vertical() {
{
u8 x = 0; u8 x = 0;
u8 y = 0; u8 y = 0;
u8 color = 0; u8 color = 0;
while (x < WIDTH) while (x < WIDTH) {
{
u8 run = next_byte(); u8 run = next_byte();
for (int i = 0; i < run; i++) for (int i = 0; i < run; i++) {
{
set_pixel(x, y, color); set_pixel(x, y, color);
y += 1; y += 1;
if (y == HEIGHT) if (y == HEIGHT) {
{
y = 0; y = 0;
x += 1; x += 1;
} }
@ -205,22 +183,19 @@ void rle_vertical()
} }
#endif #endif
#ifdef USE_BGStripsH #ifdef USE_BGStripsH16
void bg_strips_h() void bg_strips_h() {
{
u8 head = next_byte(); u8 head = next_byte();
u8 bg = head >> 7; u8 bg = head >> 7;
u8 fg = 1 - bg; u8 fg = 1 - bg;
fill_frame(bg); fill_frame(bg);
u8 strip_count = head & 127; u8 strip_count = head & 127;
for (u8 i = 0; i < strip_count; i++) for (u8 i = 0; i < strip_count; i++) {
{
u16 packed = (next_byte() << 8) | next_byte(); u16 packed = (next_byte() << 8) | next_byte();
u8 y = packed >> 11; u8 y = packed >> 11;
u8 x_start = (packed >> 5) & 0x3f; u8 x_start = (packed >> 5) & 0x3f;
u8 width = packed & 0x1f; u8 width = packed & 0x1f;
for (int x = x_start; x < (x_start + width); x++) for (int x = x_start; x < (x_start + width); x++) {
{
set_pixel(x, y, fg); set_pixel(x, y, fg);
} }
} }
@ -228,27 +203,21 @@ void bg_strips_h()
#endif #endif
#ifdef USE_CellDiff8V #ifdef USE_CellDiff8V
void cell_diff_8v() void cell_diff_8v() {
{
u32 bitmap = (next_byte() << 16) | (next_byte() << 8) | next_byte(); u32 bitmap = (next_byte() << 16) | (next_byte() << 8) | next_byte();
u8 run_remaining = next_byte(); u8 run_remaining = next_byte();
u8 color = 0; u8 color = 0;
for (u8 x = 0; x < WIDTH; x++) for (u8 x = 0; x < WIDTH; x++) {
{
u8 cellx = x >> 3; u8 cellx = x >> 3;
for (u8 y = 0; y < HEIGHT; y++) for (u8 y = 0; y < HEIGHT; y++) {
{
u8 celly = y >> 3; u8 celly = y >> 3;
if (bitmap >> (cellx + (WIDTH / 8) * celly) & 1) if (bitmap >> (cellx + (WIDTH / 8) * celly) & 1) {
{
// advance rle once // advance rle once
if (run_remaining == 0) if (run_remaining == 0) {
{
run_remaining = next_byte(); run_remaining = next_byte();
color = 1 - color; color = 1 - color;
if (run_remaining == 0) if (run_remaining == 0) {
{
run_remaining = next_byte(); run_remaining = next_byte();
color = 1 - color; color = 1 - color;
} }

View file

@ -6,4 +6,4 @@ build_thing:
# then manually copy bin/thing.uf2 to the pico # then manually copy bin/thing.uf2 to the pico
clean: clean:
rm -rf bin rm -rf bin/*