Add triangle example

This commit is contained in:
shylie 2025-06-09 14:44:21 -04:00
parent c34f09dce1
commit 63bef7ecbc
4 changed files with 114 additions and 2 deletions

12
gfx2d/CMakeLists.txt Normal file
View File

@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.13)
add_executable(gfx2d
src/main.cpp
)
target_link_libraries(gfx2d common picoled hardware_pwm)
pico_enable_stdio_uart(gfx2d 0)
pico_enable_stdio_usb(gfx2d 1)
pico_add_extra_outputs(gfx2d)

100
gfx2d/src/main.cpp Normal file
View File

@ -0,0 +1,100 @@
#include <cmath>
#include <cstdlib>
#include <hardware/structs/io_bank0.h>
#include <pico/stdio_usb.h>
#include <pico/time.h>
#include <picoled/SSD1306.h>
#include <picoled/gfx/2D.h>
#include <pico/stdlib.h>
#include <hardware/pwm.h>
using namespace picoled;
namespace
{
float get_random()
{
return rand() / static_cast<float>(RAND_MAX);
}
int32_t get_encoder_delta(i2c_inst* i2c, uint8_t address)
{
// SEESAW_ENCODER_BASE and SEESAW_ENCODER_DELTA values
// https://github.com/adafruit/Adafruit_Seesaw/blob/master/Adafruit_seesaw.h
uint8_t buf[4] = { 0x11, 0x40, 0x00, 0x00 };
i2c_write_blocking(i2c, address, buf, 2, false);
sleep_us(250);
i2c_read_blocking(i2c, address, buf, 4, false);
return
(static_cast<int32_t>(buf[0]) << 24) |
(static_cast<int32_t>(buf[1]) << 16) |
(static_cast<int32_t>(buf[2]) << 8 ) |
(static_cast<int32_t>(buf[3]) << 0 );
}
constexpr uint8_t LEFT_ENCODER_ADDRESS = 0x36;
constexpr uint8_t RIGHT_ENCODER_ADDRESS = 0x37;
constexpr uint8_t OLED_ADDRESS = 0x3C;
}
int main()
{
stdio_usb_init();
srand(time_us_64());
i2c_init(i2c0, 1000 * 1000);
gpio_set_function(0, GPIO_FUNC_I2C);
gpio_set_function(1, GPIO_FUNC_I2C);
gpio_pull_up(0);
gpio_pull_up(1);
ssd1306 oled(i2c0, OLED_ADDRESS);
gfx::gfx2d gfx(oled);
float angle;
int64_t time = time_us_32();
buffer<pixel> tex(16, 16);
for (int x = 0; x < 16; x++)
{
for (int y = 0; y < 16; y++)
{
tex.write(x, y, x * y);
}
}
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;
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 at = { 0.0f, 0.0f };
const gfx::vec2 bt = { 0.0f, 0.0f };
const gfx::vec2 ct = { 1.0f, 1.0f };
gfx.draw_triangle({
{ ap, at },
{ bp, bt },
{ cp, ct }
}, tex);
oled.swap_buffers();
oled.update();
}
}

@ -1 +1 @@
Subproject commit 10e9176d13b249e2705745a441aa333d8a2c1823
Subproject commit dfeccd50b9d2e3583675b80cd4225cb98215356c

View File

@ -231,7 +231,7 @@ int main()
ai.draw(oled);
ball.draw(oled);
oled.update();
oled.swap_buffers();
oled.update();
}
}