optimise ch32 code further

This commit is contained in:
Crispy 2025-06-19 01:03:55 +02:00
parent 382cd92629
commit a2b7e26ace
4 changed files with 223 additions and 228 deletions

View file

@ -58,10 +58,9 @@ void refresh_screen() {
while (page < 8) {
/* odd rows are unused so each nybble is expanded */
packet[packet_index] = expand[(frame[x] >> (page * 4)) & 0xf];
x++;
packet_index++;
if (x == WIDTH) {
x = 0;
x = (x + 1) % WIDTH;
if (x == 0) {
page++;
}
if (packet_index == SSD1306_PSZ) {
@ -84,7 +83,7 @@ void clear_screen() {
u8 next_byte() {
u8 byte = video[reader];
reader += 1;
reader++;
return byte;
}
@ -92,11 +91,11 @@ void fill_frame(u8 color) {
memset(frame, color ? 0xFFFFFFFF : 0x00, sizeof(frame));
}
void set_pixel(u8 x, u8 y, u8 color) {
void set_pixel(u32 x, u32 y, u8 color) {
if (color)
frame[x] |= 1<<y;
frame[x] |= 1 << y;
else
frame[x] &= ~(1<<y);
frame[x] &= ~(1 << y);
}
void rle_horizontal();
@ -145,17 +144,17 @@ void decode_next_frame() {
#ifdef USE_RLEHorizontal
void rle_horizontal() {
u8 x = 0;
u8 y = 0;
u32 x = 0;
u32 y = 0;
u8 color = 0;
while (y < HEIGHT) {
u32 run = next_byte();
while (run--) {
set_pixel(x, y, color);
x += 1;
x++;
if (x == WIDTH) {
x = 0;
y += 1;
y++;
}
}
color = 1 - color;
@ -165,17 +164,16 @@ void rle_horizontal() {
#ifdef USE_RLEVertical
void rle_vertical() {
u8 x = 0;
u8 y = 0;
u32 x = 0;
u32 y = 0;
u8 color = 0;
while (x < WIDTH) {
u32 run = next_byte();
while (run--) {
set_pixel(x, y, color);
y += 1;
if (y == HEIGHT) {
y = 0;
x += 1;
y = (y + 1) % HEIGHT;
if (y == 0) {
x++;
}
}
color = 1 - color;
@ -187,11 +185,11 @@ void rle_vertical() {
void bg_strips_h() {
u8 head = next_byte();
u8 bg = head >> 7;
u8 fg = 1 - bg;
u8 fg = !bg;
fill_frame(bg);
u8 strip_count = head & 127;
for (u8 i = 0; i < strip_count; i++) {
u16 packed = (next_byte() << 8) | next_byte();
u32 packed = (next_byte() << 8) | next_byte();
u8 y = packed >> 11;
u8 x_start = (packed >> 5) & 0x3f;
u8 width = packed & 0x1f;
@ -206,21 +204,17 @@ void bg_strips_h() {
void cell_diff_8v() {
u32 bitmap = (next_byte() << 16) | (next_byte() << 8) | next_byte();
u32 run = next_byte();
u8 color = 0;
u32 run = 0;
u8 color = 1;
for (u8 x = 0; x < WIDTH; x++) {
u8 cellx = x >> 3;
for (u8 y = 0; y < HEIGHT; y++) {
u8 celly = y >> 3;
if (bitmap >> (cellx + (WIDTH / 8) * celly) & 1) {
// advance rle once
if (run == 0) {
while (run == 0) {
run = next_byte();
color = !color;
if (run == 0) {
run = next_byte();
color = !color;
}
}
set_pixel(x, y, color);
run--;