From 21a04135b634095a464b3394d2ac9c087236a0f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Wed, 28 Dec 2022 23:04:59 +0100 Subject: [PATCH] more demo stuff --- main.c | 159 +++++++++++++++++++++++++++++++++++++++++----- ufb/framebuffer.c | 2 +- ufb/framebuffer.h | 12 ++-- 3 files changed, 151 insertions(+), 22 deletions(-) diff --git a/main.c b/main.c index e3b4ae5..9d65a53 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,10 @@ #include +#include +#include #include "pico/stdlib.h" #include "hardware/gpio.h" #include "pico/binary_info.h" +#include "hardware/regs/rosc.h" #include "hardware/spi.h" @@ -65,9 +68,51 @@ static const fb_bitmap_t zir = { .data = zir_data }; +void seed_random_from_rosc() +{ + uint32_t random = 0x811c9dc5; + uint8_t next_byte = 0; + volatile uint32_t *rnd_reg = (uint32_t *)(ROSC_BASE + ROSC_RANDOMBIT_OFFSET); + + for (int i = 0; i < 16; i++) { + for (int k = 0; k < 8; k++) { + next_byte = (next_byte << 1) | (*rnd_reg & 1); + } + + random ^= next_byte; + random *= 0x01000193; + } + + srand(random); +} + + + +void freeline(float x1, float y1, float x2, float y2, fbcolor_t color) +{ + float dx = ((float) x2 - (float) x1); + float dy = (float) y2 - (float) y1; + float dxa = dx > 0 ? dx : -dx; + float dya = dy > 0 ? dy : -dy; + float maxdist = MAX(dxa, dya); + dx /= maxdist; + dy /= maxdist; + + fb_px((fbpos_t) roundf(x1), (fbpos_t) roundf(y1), color); + //fb_px((fbpos_t) x2, (fbpos_t) y2, color); + for (uint d = 0; d <= (uint) maxdist; d++) { + x1 += dx; + y1 += dy; + fb_px((fbpos_t) roundf(x1), (fbpos_t) roundf(y1), color); + } +} + + int main() { stdio_init_all(); + seed_random_from_rosc(); + gpio_init(LED_PIN); gpio_set_dir(LED_PIN, GPIO_OUT); @@ -75,27 +120,111 @@ int main() { fb_clear(); - fb_bitmap(5, 5, &zir); - fb_bitmap(20, 5, &zir); - - fb_text(40, 35, "MEOW", FONT_DOUBLE, 1); - fb_text(50, 20, "meow", FONT_DOUBLE, 1); - - fb_frame(0, 0, 128, 64, 2, 1); - fb_blit(); - while (1) { - oled_invert(true); +#define NUMBALLS 50 +#define BALLW 2 + struct ball { + float x; + float y; + float sx; + float sy; + }; - gpio_put(LED_PIN, 0); - sleep_ms(1000); + struct ball balls[NUMBALLS]; - oled_invert(false); + for (uint i = 0; i < NUMBALLS; i++) { + balls[i].x = (float) (random() % FBW); + balls[i].y = (float) (random() % FBH); + balls[i].sx = ((float) (random() % 100) - 50.0f) / 50.0f; + balls[i].sy = ((float) (random() % 100) - 50.0f) / 50.0f; + } - gpio_put(LED_PIN, 1); + float fi = 0; + + while (1) { + //gpio_put(LED_PIN, 0); + //oled_invert(true); + + fi += M_PI / 180.0f; + + for (uint i = 0; i < NUMBALLS; i++) { + struct ball *ball = &balls[i]; + ball->x += ball->sx; + ball->y += ball->sy; + + if (ball->sx < 0 && ball->x <= 0) { + ball->sx *= -1; + ball->x = 0; + } + if (ball->sx > 0 && ball->x >= FBW - BALLW) { + ball->sx *= -1; + ball->x = FBW - BALLW; + } + if (ball->sy < 0 && ball->y <= 0) { + ball->sy *= -1; + ball->y = 0; + } + if (ball->sy > 0 && ball->y >= FBH - BALLW) { + ball->sy *= -1; + ball->y = FBH - BALLW; + } + } + + // rotating hexagon + const float CW = 15; + const float AA = M_PI / 3.0f; + const float HEXX = 30; + const float HEXY = 32; + + float ar[] = { + sinf(0 + fi) * CW, + cosf(0 + fi) * CW, + + sinf(AA + fi) * CW, + cosf(AA + fi) * CW, + + sinf(AA*2 + fi) * CW, + cosf(AA*2 + fi) * CW, + + sinf(AA*3 + fi) * CW, + cosf(AA*3 + fi) * CW, + + sinf(AA*4 + fi) * CW, + cosf(AA*4 + fi) * CW, + + sinf(AA*5 + fi) * CW, + cosf(AA*5 + fi) * CW, + + sinf(AA*6 + fi) * CW, + cosf(AA*6 + fi) * CW, + }; + + fb_clear(); + fb_text(70, 30, "MEOW", FONT_DOUBLE, 1); + fb_text(80, 15, "meow", FONT_DOUBLE, 1); + + for (uint i = 3; i <= 13; i += 2) { + freeline(HEXX + ar[i-3], HEXY + ar[i-2], HEXX + ar[i-1], HEXY + ar[i], 1); + if (i == 3 || i == 7 || i == 11) { + fb_text(HEXX + ar[i-3] -5, HEXY + ar[i-2] - 3, "Miau!", FONT_5X7, 1); + } + } + + for (uint i = 0; i < NUMBALLS; i++) { + struct ball *ball = &balls[i]; + fb_rect((fbpos_t) roundf(ball->x), (fbpos_t) roundf(ball->y), BALLW, BALLW, 1); + } + + fb_blit(); + + sleep_ms(10); + + //oled_invert(false); + + //gpio_put(LED_PIN, 1); //puts("Hello World\n"); - sleep_ms(1000); + //sleep_ms(1000); } } diff --git a/ufb/framebuffer.c b/ufb/framebuffer.c index 33b37bd..bb4b813 100644 --- a/ufb/framebuffer.c +++ b/ufb/framebuffer.c @@ -48,7 +48,7 @@ void fb_invert() void fb_px(fbpos_t x, fbpos_t y, fbcolor_t color) { - if (x >= FBW || y >= FBH) { return; } + if (x >= FBW || y >= FBH || x < 0 || y < 0) { return; } const fbpos_t row = y / 8; const fbpos_t rowrem = y % 8; const fbsize_t cell = (fbsize_t) x + (fbsize_t) row * FBW; diff --git a/ufb/framebuffer.h b/ufb/framebuffer.h index 26752f4..9a430e6 100644 --- a/ufb/framebuffer.h +++ b/ufb/framebuffer.h @@ -16,9 +16,9 @@ /// Width/height type typedef uint16_t fbsize_t; /// Position type (X/Y) -typedef uint8_t fbpos_t; +typedef int16_t fbpos_t; /// Color type -typedef uint8_t fbcolor_t; +typedef uint8_t fbcolor_t; /// Bitmap struct typedef struct { @@ -88,12 +88,12 @@ void fb_frame(fbpos_t x, fbpos_t y, fbpos_t w, fbpos_t h, fbpos_t thickness, fbc /// Draw a bitmap using the bitmap struct #if IS_AVR - static inline void fb_bitmap_P(fbpos_t x, fbpos_t y, const fb_bitmap_t *bitmap) { - fb_bitmap_ex_P(x, y, bitmap->width, bitmap->height, bitmap->data, 1); + static inline void fb_bitmap_P(fbpos_t x, fbpos_t y, const fb_bitmap_t *bitmap, fbcolor_t color) { + fb_bitmap_ex_P(x, y, bitmap->width, bitmap->height, bitmap->data, color); } #else - static inline void fb_bitmap(fbpos_t x, fbpos_t y, const fb_bitmap_t *bitmap) { - fb_bitmap_ex(x, y, bitmap->width, bitmap->height, bitmap->data, 1); + static inline void fb_bitmap(fbpos_t x, fbpos_t y, const fb_bitmap_t *bitmap, fbcolor_t color) { + fb_bitmap_ex(x, y, bitmap->width, bitmap->height, bitmap->data, color); } #endif