// Wokwi Custom Chip - For information and examples see:
// https://link.wokwi.com/custom-chips-alpha
//
// SPDX-License-Identifier: MIT
// Copyright (C) 2022 Uri Shaked / wokwi.com
#include "wokwi-api.h"
#include <stdio.h>
#include <stdlib.h>
const int MS = 1000; // micros
typedef struct {
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
} rgba_t;
typedef struct {
buffer_t framebuffer;
uint32_t width;
uint32_t height;
uint32_t row;
} chip_state_t;
static void chip_timer_event(void *user_data);
void chip_init(void) {
chip_state_t *chip = malloc(sizeof(chip_state_t));
chip->row = 0;
chip->framebuffer = framebuffer_init(&chip->width, &chip->height);
printf("Framebuffer: width=%d, height=%d\n", chip->width, chip->height);
const timer_config_t timer_config = {
.callback = chip_timer_event,
.user_data = chip,
};
timer_t timer = timer_init(&timer_config);
timer_start(timer, 10 * MS, true);
}
void draw_line(chip_state_t *chip, uint32_t row, rgba_t color) {
uint32_t offset = chip->width * 4 * row;
for (int x = 0; x < chip->width * 4; x += 4) {
buffer_write(chip->framebuffer, offset + x, &color, sizeof(color));
}
}
void chip_timer_event(void *user_data) {
chip_state_t *chip = (chip_state_t*)user_data;
rgba_t dark_blue = { .r = 0x03, .g = 0x25, .b = 0x4c, .a = 0xff };
//rgba_t deep_green = { .g=0x30, .a = 0xff };
draw_line(chip, chip->row, dark_blue);
chip->row = (chip->row + 1) % chip->height;
rgba_t purple = { .r = 0xff, .g=0x00, .b=0xff, .a=0xff };
draw_line(chip, chip->row, purple);
}