Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PicoVector: Rewrite around new C pretty-poly.h. #853

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
5c2f9d7
PicoVector: Rewrite around new C pretty-poly.h.
Gadgetoid Sep 26, 2023
5d46e80
PicoVector: Rewrite MicroPython bindings.
Gadgetoid Sep 29, 2023
e98b7e0
PicoVector: Suppress errors.
Gadgetoid Apr 17, 2024
e4901fd
TEST: PicoVector: Vendor pretty-poly and tweak rotation.
Gadgetoid Apr 18, 2024
ddac27e
TEST: PicoVector: Swap rotate translation order.
Gadgetoid Apr 18, 2024
9f558c6
TEST: PicoVector: alright-fonts bringup.
Gadgetoid Apr 18, 2024
71aa70d
TEST: PicoVector: Break things until they work.
Gadgetoid Apr 19, 2024
086e3c0
PicoVector: fix pointer arithmatic in af_load_font_file.
Gadgetoid Jun 5, 2024
2a86aa3
PicoVector: Fix out of bounds drawing.
Gadgetoid Jun 5, 2024
e567b48
PicoVector: render text that doesn't end with a linebreak.
Gadgetoid Jun 5, 2024
aac61cb
PicoVector: C++ basic bringup.
Gadgetoid Jun 11, 2024
c084df6
PicoVector: Remove alright_fonts.cpp from cmake.
Gadgetoid Jul 9, 2024
c0f9e08
PicoGraphics: Add RGB565 alpha blending support.
Gadgetoid Jul 11, 2024
b954327
PicoVector: Fix x16 anti-aliasing.
Gadgetoid Jul 11, 2024
7869758
PicoGraphics: Add get_clip.
Gadgetoid Jul 11, 2024
f50d2e5
PicoVector: Remove malloc from MicroPython bindings.
Gadgetoid Jul 11, 2024
df562bf
PicoVector: Support float types in MicroPython bindings.
Gadgetoid Jul 11, 2024
7ca54fa
PicoVector: Use tile renderer for all pens.
Gadgetoid Jul 22, 2024
30e0703
PicoVector: Rewrite around new linked-lists poly.
Gadgetoid Jul 22, 2024
4ff30e8
PicoVector: Update C++ examples.
Gadgetoid Jul 23, 2024
53a0e0b
PicoVector: Big refactor, ppp primitives.
Gadgetoid Jul 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ add_subdirectory(galactic_unicorn)
add_subdirectory(gfx_pack)
add_subdirectory(cosmic_unicorn)
add_subdirectory(stellar_unicorn)
add_subdirectory(pico_w_explorer)
15 changes: 2 additions & 13 deletions examples/pico_display_2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
add_subdirectory(mandelbrot)

set(OUTPUT_NAME pico_display2_demo)

add_executable(
${OUTPUT_NAME}
pico_display_2_demo.cpp
)

# Pull in pico libraries that we need
target_link_libraries(${OUTPUT_NAME} pico_stdlib hardware_spi hardware_pwm hardware_dma rgbled button pico_display_2 st7789 pico_graphics)

# create map/bin/hex file etc.
pico_add_extra_outputs(${OUTPUT_NAME})
include(pico_display_2_demo.cmake)
include(pico_display_2_vector.cmake)
10 changes: 10 additions & 0 deletions examples/pico_display_2/pico_display_2_demo.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
add_executable(
pico_display_2_demo
pico_display_2_demo.cpp
)

# Pull in pico libraries that we need
target_link_libraries(pico_display_2_demo pico_stdlib hardware_spi hardware_pwm hardware_dma rgbled button pico_display_2 st7789 pico_graphics)

# create map/bin/hex file etc.
pico_add_extra_outputs(pico_display_2_demo)
41 changes: 41 additions & 0 deletions examples/pico_display_2/pico_display_2_vector.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
function(static_asset NAME PATH)
get_filename_component(PATH ${PATH} ABSOLUTE)
get_filename_component(ASSET ${PATH} NAME)
get_filename_component(PATH ${PATH} DIRECTORY)
set(OBJNAME ${ASSET}.o)
add_custom_command(OUTPUT ${OBJNAME}
DEPENDS ${PATH}/${ASSET}
COMMENT "Building ${OBJNAME}"
WORKING_DIRECTORY "${PATH}"
COMMAND ${CMAKE_LINKER} -r -b binary -o ${CMAKE_CURRENT_BINARY_DIR}/${OBJNAME} ${ASSET}
COMMAND ${CMAKE_OBJDUMP} -t ${CMAKE_CURRENT_BINARY_DIR}/${OBJNAME}
)
# TODO figure out how to make static resources work
## COMMAND ${CMAKE_OBJCOPY} --rename-section .data=.rodata,alloc,load,readonly,data,contents ${CMAKE_CURRENT_BINARY_DIR}/${OBJNAME} ${CMAKE_CURRENT_BINARY_DIR}/${OBJNAME})
target_sources(${NAME} PRIVATE ${OBJNAME})
endfunction()

add_executable(
pico_display_2_vector
pico_display_2_vector.cpp
)

# Pull in pico libraries that we need
target_link_libraries(pico_display_2_vector
pico_stdlib
hardware_spi
hardware_pwm
hardware_dma
pico_display_2
st7789
pico_graphics
pico_vector
)

static_asset(pico_display_2_vector ${CMAKE_CURRENT_LIST_DIR}/vector/DynaPuff-Medium.af)

pico_enable_stdio_usb(pico_display_2_vector 0)
pico_enable_stdio_uart(pico_display_2_vector 1)

# create map/bin/hex file etc.
pico_add_extra_outputs(pico_display_2_vector)
67 changes: 67 additions & 0 deletions examples/pico_display_2/pico_display_2_vector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <string.h>
#include <math.h>
#include <vector>
#include <cstdlib>

#include "libraries/pico_display_2/pico_display_2.hpp"
#include "drivers/st7789/st7789.hpp"
#include "libraries/pico_graphics/pico_graphics.hpp"
#include "libraries/pico_vector/pico_vector.hpp"


using namespace pimoroni;

ST7789 st7789(320, 240, ROTATE_180, false, get_spi_pins(BG_SPI_FRONT));
PicoGraphics_PenRGB332 graphics(st7789.width, st7789.height, nullptr);

uint8_t vector_mem[PicoVector::pretty_poly_buffer_size()];

PicoVector vector(&graphics);

extern char _binary_DynaPuff_Medium_af_start[];
extern size_t _binary_DynaPuff_Medium_af_size;

int main() {
stdio_init_all();

Pen BG = graphics.create_pen(120, 40, 60);
Pen TEXT = graphics.create_pen(255, 255, 255);

st7789.set_backlight(255);

vector.set_font(_binary_DynaPuff_Medium_af_start, 30);

unsigned int a = 0;

while (true) {
Point text_location(0, 0);
graphics.set_pen(BG);
graphics.clear();
graphics.set_pen(TEXT);
graphics.text("Hello World", text_location, 320);

pp_point_t outline[] = {{-64, -64}, {64, -64}, {64, 64}, {-64, 64}};
pp_point_t hole[] = {{ -32, 32}, { 32, 32}, { 32, -32}, { -32, -32}};

pp_poly_t *poly = pp_poly_new();
pp_path_add_points(pp_poly_add_path(poly), outline, sizeof(outline) / sizeof(pp_point_t));
pp_path_add_points(pp_poly_add_path(poly), hole, sizeof(hole) / sizeof(pp_point_t));

pp_mat3_t pos = pp_mat3_identity();
pp_mat3_translate(&pos, 50, 50);
pp_mat3_rotate(&pos, a);
vector.draw(poly);
vector.text("Hello World", &pos);

// update screen
st7789.update(&graphics);
a += 1;
if (a > 359) {
a = 0;
}

pp_poly_free(poly);
}

return 0;
}
Binary file added examples/pico_display_2/vector/DynaPuff-Medium.af
Binary file not shown.
1 change: 1 addition & 0 deletions examples/pico_w_explorer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include(pico_w_explorer_vector.cmake)
10 changes: 10 additions & 0 deletions examples/pico_w_explorer/pico_w_explorer_vector.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
add_executable(
pico_w_explorer_vector
pico_w_explorer_vector.cpp
)

# Pull in pico libraries that we need
target_link_libraries(pico_w_explorer_vector pico_stdlib pico_graphics pico_vector st7789)

# create map/bin/hex file etc.
pico_add_extra_outputs(pico_w_explorer_vector)
56 changes: 56 additions & 0 deletions examples/pico_w_explorer/pico_w_explorer_vector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <string.h>
#include <math.h>
#include <vector>
#include <cstdlib>

#include "drivers/st7789/st7789.hpp"
#include "libraries/pico_graphics/pico_graphics.hpp"
#include "libraries/pico_vector/pico_vector.hpp"

using namespace pimoroni;


ST7789 st7789(320, 240, ROTATE_0, false, {PIMORONI_SPI_DEFAULT_INSTANCE, 17, SPI_DEFAULT_SCK, SPI_DEFAULT_MOSI, PIN_UNUSED, SPI_DEFAULT_MISO, 9});
PicoGraphics_PenRGB332 graphics(st7789.width, st7789.height, nullptr);
PicoVector vector(&graphics);

int main() {
st7789.set_backlight(255);

Pen WHITE = graphics.create_pen(255, 255, 255);
Pen BLACK = graphics.create_pen(0, 0, 0);

float angle = 0.0f;

while(true) {
graphics.set_pen(BLACK);
graphics.clear();

graphics.set_pen(WHITE);
graphics.text("Hello World", Point(0, 0), 320);

pp_point_t outline[] = {{-128, -128}, {128, -128}, {128, 128}, {-128, 128}};
pp_point_t hole[] = {{ -64, 64}, { 64, 64}, { 64, -64}, { -64, -64}};

pp_poly_t *poly = pp_poly_new();
pp_path_add_points(pp_poly_add_path(poly), outline, sizeof(outline) / sizeof(pp_point_t));
pp_path_add_points(pp_poly_add_path(poly), hole, sizeof(hole) / sizeof(pp_point_t));

vector.rotate(poly, {0, 0}, angle);
vector.translate(poly, {160, 120});

vector.draw(poly);

//pp_mat3_t t = pp_mat3_identity();
//vector.text("Hello World", {0, 0}, &t);

// update screen
st7789.update(&graphics);

angle += 1.0f;

pp_poly_free(poly);
}

return 0;
}
1 change: 1 addition & 0 deletions libraries/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ add_subdirectory(gfx_pack)
add_subdirectory(interstate75)
add_subdirectory(cosmic_unicorn)
add_subdirectory(stellar_unicorn)
add_subdirectory(pico_vector)
21 changes: 20 additions & 1 deletion libraries/pico_graphics/pico_graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ namespace pimoroni {

typedef int Pen;

struct Tile {
int32_t x, y, w, h;
uint32_t stride;
uint8_t *data;
};

struct Rect;

struct Point {
Expand Down Expand Up @@ -293,7 +299,7 @@ namespace pimoroni {
virtual void frame_convert(PenType type, conversion_callback_func callback);
virtual void sprite(void* data, const Point &sprite, const Point &dest, const int scale, const int transparent);

virtual bool render_pico_vector_tile(const Rect &bounds, uint8_t* alpha_data, uint32_t stride, uint8_t alpha_type) { return false; }
virtual bool render_tile(const Tile *tile) { return false; }

void set_font(const bitmap::font_t *font);
void set_font(const hershey::font_t *font);
Expand Down Expand Up @@ -440,6 +446,8 @@ namespace pimoroni {
static size_t buffer_size(uint w, uint h) {
return w * h / 2;
}

bool render_tile(const Tile *tile);
};

class PicoGraphics_PenP8 : public PicoGraphics {
Expand Down Expand Up @@ -473,6 +481,8 @@ namespace pimoroni {
static size_t buffer_size(uint w, uint h) {
return w * h;
}

bool render_tile(const Tile *tile);
};

class PicoGraphics_PenRGB332 : public PicoGraphics {
Expand All @@ -497,6 +507,8 @@ namespace pimoroni {
static size_t buffer_size(uint w, uint h) {
return w * h;
}

bool render_tile(const Tile *tile);
};

class PicoGraphics_PenRGB565 : public PicoGraphics {
Expand All @@ -513,6 +525,11 @@ namespace pimoroni {
static size_t buffer_size(uint w, uint h) {
return w * h * sizeof(RGB565);
}
void set_pixel_alpha(const Point &p, const uint8_t a) override;

bool supports_alpha_blend() override {return true;}

bool render_tile(const Tile *tile);
};

class PicoGraphics_PenRGB888 : public PicoGraphics {
Expand All @@ -529,6 +546,8 @@ namespace pimoroni {
static size_t buffer_size(uint w, uint h) {
return w * h * sizeof(uint32_t);
}

bool render_tile(const Tile *tile);
};


Expand Down
23 changes: 23 additions & 0 deletions libraries/pico_graphics/pico_graphics_pen_p4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,27 @@ namespace pimoroni {
});
}
}
bool PicoGraphics_PenP4::render_tile(const Tile *tile) {
for(int y = 0; y < tile->h; y++) {
uint8_t *palpha = &tile->data[(y * tile->stride)];
uint8_t *pdest = &((uint8_t *)frame_buffer)[(tile->x / 2) + ((tile->y + y) * (bounds.w / 2))];
for(int x = 0; x < tile->w; x++) {
uint8_t shift = (x & 1) ? 0 : 4;
uint8_t alpha = *palpha;

if(alpha == 0) {
} else {
*pdest &= shift ? 0x0f : 0xf0;
*pdest |= color << shift;
}

if(x & 1) {
pdest++;
}
palpha++;
}
}

return true;
}
}
20 changes: 20 additions & 0 deletions libraries/pico_graphics/pico_graphics_pen_p8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,24 @@ namespace pimoroni {
});
}
}

bool PicoGraphics_PenP8::render_tile(const Tile *tile) {
for(int y = 0; y < tile->h; y++) {
uint8_t *palpha = &tile->data[(y * tile->stride)];
uint8_t *pdest = &((uint8_t *)frame_buffer)[tile->x + ((tile->y + y) * bounds.w)];
for(int x = 0; x < tile->w; x++) {
uint8_t alpha = *palpha;

if(alpha == 0) {
} else {
*pdest = color;
}

pdest++;
palpha++;
}
}

return true;
}
}
37 changes: 37 additions & 0 deletions libraries/pico_graphics/pico_graphics_pen_rgb332.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,41 @@ namespace pimoroni {
}
}
}
bool PicoGraphics_PenRGB332::render_tile(const Tile *tile) {
for(int y = 0; y < tile->h; y++) {
uint8_t *palpha = &tile->data[(y * tile->stride)];
uint8_t *pdest = &((uint8_t *)frame_buffer)[tile->x + ((tile->y + y) * bounds.w)];
for(int x = 0; x < tile->w; x++) {
uint8_t alpha = *palpha;
uint8_t dest = *pdest;

// TODO: Try to alpha blend RGB332... somewhat?
if(alpha == 255) {
*pdest = color;
}else if(alpha == 0) {
}else{
// blend tha pixel
uint16_t sr = (color & 0b11100000) >> 5;
uint16_t sg = (color & 0b00011100) >> 2;
uint16_t sb = (color & 0b00000011);

uint16_t dr = (dest & 0b11100000) >> 5;
uint16_t dg = (dest & 0b00011100) >> 2;
uint16_t db = (dest & 0b00000011);

uint8_t r = ((sr * alpha) + (dr * (255 - alpha))) >> 8;
uint8_t g = ((sg * alpha) + (dg * (255 - alpha))) >> 8;
uint8_t b = ((sb * alpha) + (db * (255 - alpha))) >> 8;

// recombine the channels
*pdest = (r << 5) | (g << 2) | (b);
}

pdest++;
palpha++;
}
}

return true;
}
}
Loading