From 7d64ebf3e3aba89c64eb084f8f7c1ed2b9744498 Mon Sep 17 00:00:00 2001 From: shylie Date: Sat, 14 Jun 2025 17:35:33 -0400 Subject: [PATCH] Add partial ST7789 support --- gfx2d/src/main.cpp | 57 +++++++++++++++++++++++++++++++--------------- picoled | 2 +- pong/src/main.cpp | 21 ++++++++++++++--- 3 files changed, 58 insertions(+), 22 deletions(-) diff --git a/gfx2d/src/main.cpp b/gfx2d/src/main.cpp index fc5d424..943f4bf 100644 --- a/gfx2d/src/main.cpp +++ b/gfx2d/src/main.cpp @@ -1,14 +1,15 @@ #include #include -#include #include +#include #include -#include +#include +#include + +#include #include -#include -#include using namespace picoled; @@ -43,6 +44,8 @@ constexpr uint8_t OLED_ADDRESS = 0x3C; int main() { + //set_sys_clock_khz(133000, false); + stdio_usb_init(); srand(time_us_64()); @@ -52,42 +55,60 @@ int main() gpio_pull_up(0); gpio_pull_up(1); - ssd1306 oled(i2c0, OLED_ADDRESS); + spi_init(spi0, 32000000); + gpio_set_function(2, GPIO_FUNC_SPI); + gpio_set_function(3, GPIO_FUNC_SPI); + gpio_set_function(4, GPIO_FUNC_SPI); + + gpio_set_function(5, GPIO_FUNC_SIO); + gpio_set_dir(5, GPIO_OUT); + gpio_put(5, false); + + gpio_set_function(6, GPIO_FUNC_SIO); + gpio_set_dir(6, GPIO_OUT); + gpio_put(6, false); + + st7789 oled(135, 240, spi0, 5, 6); gfx::gfx2d gfx(oled); - float angle; + float angle = 0; int64_t time = time_us_32(); - buffer tex(16, 16); - for (int x = 0; x < 16; x++) + buffer tex(64, 64); + for (int x = 0; x < 64; x++) { - for (int y = 0; y < 16; y++) + for (int y = 0; y < 64; y++) { - tex.write(x, y, x * y); + const uint8_t r = 4 * x; + const uint8_t g = 4 * y; + const uint8_t b = 254 - 2 * x - 2 * y; + tex.write(x, y, { r, g, b }); } } while (true) { - oled.clear(); - int64_t current = time_us_32(); const float dt = std::abs(current - time) / 1000000.0f; time = current; - angle -= get_encoder_delta(i2c0, RIGHT_ENCODER_ADDRESS) / 12.0f * 3.1415926f; + stdio_printf("%05f\n", dt); + + const int32_t delta = get_encoder_delta(i2c0, RIGHT_ENCODER_ADDRESS); + angle -= delta / 12.0f * 3.1415926f; const float angle2 = angle + (2 * 3.1415926f) / 3; const float angle3 = angle + 2 * (2 * 3.1415926f) / 3; - const gfx::vec2 ap = { 30.0f + 20 * cosf(angle), 30.0f + 20 * sinf(angle) }; - const gfx::vec2 bp = { 30.0f + 20 * cosf(angle2), 30.0f + 20 * sinf(angle2) }; - const gfx::vec2 cp = { 30.0f + 20 * cosf(angle3), 30.0f + 20 * sinf(angle3) }; + const gfx::vec2 ap = { 60.0f + 40 * cosf(angle), 60.0f + 40 * sinf(angle) }; + const gfx::vec2 bp = { 60.0f + 40 * cosf(angle2), 60.0f + 40 * sinf(angle2) }; + const gfx::vec2 cp = { 60.0f + 40 * cosf(angle3), 60.0f + 40 * sinf(angle3) }; const gfx::vec2 at = { 0.0f, 0.0f }; - const gfx::vec2 bt = { 0.0f, 0.0f }; - const gfx::vec2 ct = { 1.0f, 1.0f }; + const gfx::vec2 bt = { 1.0f, 0.0f }; + const gfx::vec2 ct = { 0.0f, 1.0f }; + oled.clear(); gfx.draw_triangle({ { ap, at }, { bp, bt }, diff --git a/picoled b/picoled index dfeccd5..846f909 160000 --- a/picoled +++ b/picoled @@ -1 +1 @@ -Subproject commit dfeccd50b9d2e3583675b80cd4225cb98215356c +Subproject commit 846f909cfe99ec1a9cd66299d380e35e44cfd89f diff --git a/pong/src/main.cpp b/pong/src/main.cpp index 6a5b059..8ca08c0 100644 --- a/pong/src/main.cpp +++ b/pong/src/main.cpp @@ -5,8 +5,10 @@ #include #include #include +#include +#include -#include +#include namespace { @@ -70,7 +72,7 @@ constexpr uint8_t OLED_ADDRESS = 0x3C; constexpr float PADDLE_SPEED = 20; constexpr float INITIAL_BALL_SPEED = 32; -constexpr uint8_t BUZZER_PIN = 2; +constexpr uint8_t BUZZER_PIN = 7; int64_t unbeep(alarm_id_t, void*) { @@ -98,7 +100,20 @@ int main() gpio_pull_up(0); gpio_pull_up(1); - picoled::ssd1306 oled(i2c0, OLED_ADDRESS); + spi_init(spi0, 32000000); + gpio_set_function(2, GPIO_FUNC_SPI); + gpio_set_function(3, GPIO_FUNC_SPI); + gpio_set_function(4, GPIO_FUNC_SPI); + + gpio_set_function(5, GPIO_FUNC_SIO); + gpio_set_dir(5, GPIO_OUT); + gpio_put(5, false); + + gpio_set_function(6, GPIO_FUNC_SIO); + gpio_set_dir(6, GPIO_OUT); + gpio_put(6, false); + + picoled::st7789 oled(135, 240, spi0, 5, 6); oled.update(); rect player = { 8.0f, oled.get_height() / 2.0f - 8.0f, 4.0f, 16.0f };