Skip to content

Instantly share code, notes, and snippets.

@andyvanee
Last active June 6, 2024 04:41
Show Gist options
  • Save andyvanee/ed29f1fb5fc4fa677dab9690a55e185a to your computer and use it in GitHub Desktop.
Save andyvanee/ed29f1fb5fc4fa677dab9690a55e185a to your computer and use it in GitHub Desktop.
Actually minimal version of rp2040 scanvideo
/*
* Actually minimal scanvideo example
*/
#include <stdio.h>
#include "pico.h"
#include "pico/scanvideo.h"
#include "pico/scanvideo/composable_scanline.h"
#include "pico/stdlib.h"
// Highest possible VGA_MODE with RAW_RUN seems to be 320x240_60
#define VGA_MODE vga_mode_320x240_60
extern const struct scanvideo_pio_program video_24mhz_composable;
#define ALIGNED_SCANLINE_WORDS 0
static void render_scanline(struct scanvideo_scanline_buffer *dest) {
uint32_t *buf = dest->data;
uint16_t *output = (uint16_t*)buf;
int l = scanvideo_scanline_number(dest->scanline_id);
*output++ = COMPOSABLE_RAW_RUN;
// First color byte in RAW_RUN comes before run length
*output++ = l & 2 ? 0xffff : 0x0000;
*output++ = VGA_MODE.width;
*output++ = l & 2 ? 0x0000 : 0xffff;
for (int i = 2; i < VGA_MODE.width; i+=2) {
*output++ = l & 2 ? 0xffff : 0x0000;
*output++ = l & 2 ? 0x0000 : 0xffff;
}
// Must end with black pixel
*output++ = COMPOSABLE_RAW_1P;
*output++ = 0;
if (ALIGNED_SCANLINE_WORDS) {
*output++ = COMPOSABLE_EOL_ALIGN;
} else {
*output++ = COMPOSABLE_EOL_SKIP_ALIGN;
*output++ = 0xffff;
}
dest->data_used = (uint16_t)(((uint32_t*)output) - buf);
dest->status = SCANLINE_OK;
}
// static void frame_update_logic() {}
int vga_main(void) {
scanvideo_setup(&VGA_MODE);
scanvideo_timing_enable(true);
while (true) {
struct scanvideo_scanline_buffer *scanline_buffer = scanvideo_begin_scanline_generation(true);
// frame_update_logic();
render_scanline(scanline_buffer);
scanvideo_end_scanline_generation(scanline_buffer);
}
return 0;
}
int main(void) {
setup_default_uart();
return vga_main();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment