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 2 text word wrap and transform #1027

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
3de60d1
PicoGraphics: Layers.
Gadgetoid Oct 7, 2024
d528c6f
PicoGraphics: Support multiple layers in more types.
Gadgetoid Oct 7, 2024
6c136e0
PicoVector: Rewrite around new C pretty-poly.h.
Gadgetoid Sep 26, 2023
6f2d4bf
PicoVector: Rewrite MicroPython bindings.
Gadgetoid Sep 29, 2023
da262ac
PicoVector: Suppress errors.
Gadgetoid Apr 17, 2024
0354b8b
PicoVector: Vendor pretty-poly and tweak rotation.
Gadgetoid Apr 18, 2024
27fd61b
PicoVector: Swap rotate translation order.
Gadgetoid Apr 18, 2024
1fe1d3c
PicoVector: alright-fonts bringup.
Gadgetoid Apr 18, 2024
742c2cb
PicoVector: Break things until they work.
Gadgetoid Apr 19, 2024
739c77c
PicoVector: fix pointer arithmatic in af_load_font_file.
Gadgetoid Jun 5, 2024
58dcc62
PicoVector: Fix out of bounds drawing.
Gadgetoid Jun 5, 2024
4698336
PicoVector: render text that doesn't end with a linebreak.
Gadgetoid Jun 5, 2024
8a719c2
PicoVector: C++ basic bringup.
Gadgetoid Jun 11, 2024
94d14ed
PicoVector: Remove alright_fonts.cpp from cmake.
Gadgetoid Jul 9, 2024
031956a
PicoGraphics: Add RGB565 alpha blending support.
Gadgetoid Jul 11, 2024
dcc24fd
PicoVector: Fix x16 anti-aliasing.
Gadgetoid Jul 11, 2024
263fe40
PicoGraphics: Add get_clip.
Gadgetoid Jul 11, 2024
7d04ea7
PicoVector: Remove malloc from MicroPython bindings.
Gadgetoid Jul 11, 2024
740e351
PicoVector: Support float types in MicroPython bindings.
Gadgetoid Jul 11, 2024
be0d617
PicoVector: Use tile renderer for all pens.
Gadgetoid Jul 22, 2024
6f092a2
PicoVector: Rewrite around new linked-lists poly.
Gadgetoid Jul 22, 2024
841225e
PicoVector: Update C++ examples.
Gadgetoid Jul 23, 2024
a123cfa
PicoVector: Big refactor, ppp primitives.
Gadgetoid Jul 25, 2024
adb9a1e
PicoGraphics: Add layer support to PicoVector tile renderer.
Gadgetoid Oct 8, 2024
352bd47
PicoVector: Improve text rendering and control.
Gadgetoid Oct 8, 2024
5bc852e
PicoGraphics: RGB565 skip layers if not enabled.
Gadgetoid Oct 9, 2024
a292905
PicoVector: Refactor text multiline support.
Gadgetoid Oct 9, 2024
49b496d
PicoVector: Remove (ifdef guard) debug functions.
Gadgetoid Oct 10, 2024
03a8d51
PicoGraphics: Add Presto.
Gadgetoid Aug 14, 2024
f2d84fa
Plasma: Add support for GPIOs >=32.
Gadgetoid Oct 24, 2024
96beb08
PicoGraphics: Non-blocking Inky update for #936.
Gadgetoid Oct 31, 2024
225ecfc
PicoVector: Prefix some pretty-poly variables.
Gadgetoid Nov 14, 2024
0425d77
PicoVector: Add optional text max width and max height.
MichaelBell Nov 17, 2024
a4adf0d
PicoVector: Apply overall transform to text rendering.
MichaelBell Nov 17, 2024
7a0fb98
PicoVector: Fix C++ example compilation.
MichaelBell Nov 17, 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
20 changes: 18 additions & 2 deletions drivers/plasma/apa102.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,26 @@
namespace plasma {

APA102::APA102(uint num_leds, PIO pio, uint sm, uint pin_dat, uint pin_clk, uint freq, RGB* buffer) : buffer(buffer), num_leds(num_leds), pio(pio), sm(sm) {
// NOTE: This sets the gpio_base for *the entire PIO* not just this state machine
uint range_max = std::max(pin_dat, pin_clk);
uint range_min = std::min(pin_dat, pin_clk);

// Both pins in 16-48 range
if(range_max >= 32 && range_min >= 16) {
pio_set_gpio_base(pio, 16);
// Both pins in 0-31 range
} else if(range_max <= 31) {
pio_set_gpio_base(pio, 0);
// Pins in different ranges: invalid combo!
} else {
// TODO: Need some means to notify the caller
pio_set_gpio_base(pio, 0);
}

pio_program_offset = pio_add_program(pio, &apa102_program);

pio_sm_set_pins_with_mask(pio, sm, 0, (1u << pin_clk) | (1u << pin_dat));
pio_sm_set_pindirs_with_mask(pio, sm, ~0u, (1u << pin_clk) | (1u << pin_dat));
pio_sm_set_pins_with_mask(pio, sm, 0, (1u << (pin_clk - pio_get_gpio_base(pio))) | (1u << (pin_dat - pio_get_gpio_base(pio))));
pio_sm_set_pindirs_with_mask(pio, sm, ~0u, (1u << (pin_clk - pio_get_gpio_base(pio))) | (1u << (pin_dat - pio_get_gpio_base(pio))));
pio_gpio_init(pio, pin_clk);
pio_gpio_init(pio, pin_dat);

Expand Down
3 changes: 3 additions & 0 deletions drivers/plasma/ws2812.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
namespace plasma {

WS2812::WS2812(uint num_leds, PIO pio, uint sm, uint pin, uint freq, bool rgbw, COLOR_ORDER color_order, RGB* buffer) : buffer(buffer), num_leds(num_leds), color_order(color_order), pio(pio), sm(sm) {
// NOTE: This sets the gpio_base for *the entire PIO* not just this state machine
pio_set_gpio_base(pio, pin >= 32 ? 16 : 0);

pio_program_offset = pio_add_program(pio, &ws2812_program);

pio_gpio_init(pio, pin);
Expand Down
2 changes: 1 addition & 1 deletion drivers/st7735/st7735.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ namespace pimoroni {

// Native 16-bit framebuffer update
void ST7735::update(PicoGraphics *graphics) {
if(graphics->pen_type == PicoGraphics::PEN_RGB565) {
if(graphics->pen_type == PicoGraphics::PEN_RGB565 && graphics->layers == 1) {
command(reg::RAMWR, width * height * sizeof(uint16_t), (const char*)graphics->frame_buffer);
} else {
command(reg::RAMWR);
Expand Down
2 changes: 1 addition & 1 deletion drivers/st7789/st7789.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ namespace pimoroni {
void ST7789::update(PicoGraphics *graphics) {
uint8_t cmd = reg::RAMWR;

if(graphics->pen_type == PicoGraphics::PEN_RGB565) { // Display buffer is screen native
if(graphics->pen_type == PicoGraphics::PEN_RGB565 && graphics->layers == 1) { // Display buffer is screen native
command(cmd, width * height * sizeof(uint16_t), (const char*)graphics->frame_buffer);
} else {
gpio_put(dc, 0); // command mode
Expand Down
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", 0, 0, &pos);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively could make these optional arguments if you prefer - I had kind of forgotten that this was also exposed to C++.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to make these optional, since it also bubbles up to Python. C isn't super helpful about that, though. I wonder if it makes sense to have them part of the text metrics, or if that's just too conceptually weird.

Sensible API design for this has been quite tricky!


// 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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly uncommenting this was unwise as I don't have one of these to test. I got confused between this and the new RP2350 based explorer 😆


// 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)
8 changes: 8 additions & 0 deletions libraries/pico_graphics/pico_graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ namespace pimoroni {
RGB* PicoGraphics::get_palette() {return nullptr;}
bool PicoGraphics::supports_alpha_blend() {return false;}

void PicoGraphics::set_layer(uint l) {
this->layer = l;
this->layer_offset = this->bounds.w * this->bounds.h * l;
};
uint PicoGraphics::get_layer() {
return this->layer;
};

void PicoGraphics::set_dimensions(int width, int height) {
bounds = clip = {0, 0, width, height};
}
Expand Down
Loading