From 07c62a849aa9c898d6a1d16481911883fdbc0687 Mon Sep 17 00:00:00 2001 From: Jonathan Williamson Date: Mon, 21 Feb 2022 17:42:29 +0000 Subject: [PATCH] added rotation by angle in degrees to text() and glyph() methods --- examples/badger2040/badger2040_fonts.cpp | 1 + libraries/badger2040/badger2040.cpp | 30 ++++++++++++++++++++---- libraries/badger2040/badger2040.hpp | 4 ++-- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/examples/badger2040/badger2040_fonts.cpp b/examples/badger2040/badger2040_fonts.cpp index 1dab94a0..c6bb729a 100644 --- a/examples/badger2040/badger2040_fonts.cpp +++ b/examples/badger2040/badger2040_fonts.cpp @@ -65,6 +65,7 @@ int main() { printf("\n\n=======\nbadger2040 starting up\n\n"); badger.init(); + badger.update_speed(1); uint32_t i = 0; diff --git a/libraries/badger2040/badger2040.cpp b/libraries/badger2040/badger2040.cpp index 7b2ae398..dbe4795f 100644 --- a/libraries/badger2040/badger2040.cpp +++ b/libraries/badger2040/badger2040.cpp @@ -194,7 +194,11 @@ namespace pimoroni { return &_font->chars[c - 32]; } - int32_t Badger2040::glyph(unsigned char c, int32_t x, int32_t y, float s) { + inline float deg2rad(float degrees) { + return (degrees * M_PI) / 180.0f; + } + + int32_t Badger2040::glyph(unsigned char c, int32_t x, int32_t y, float s, float a) { // if space character then return a width to move the caret by const hershey_font_glyph_t *gd = glyph_data(c); @@ -203,6 +207,10 @@ namespace pimoroni { return 0; } + a = deg2rad(a); + float as = sin(a); + float ac = cos(a); + const int8_t *pv = gd->vertices; int8_t cx = (*pv++) * s; int8_t cy = (*pv++) * s; @@ -216,8 +224,14 @@ namespace pimoroni { int8_t nx = (*pv++) * s; int8_t ny = (*pv++) * s; + int rcx = cx * ac - cy * as; + int rcy = cx * as + cy * ac; + + int rnx = nx * ac - ny * as; + int rny = nx * as + ny * ac; + if(pen_down) { - line(cx + x, cy + y, nx + x, ny + y); + line(rcx + x, rcy + y, rnx + x, rny + y); } cx = nx; @@ -229,12 +243,20 @@ namespace pimoroni { return gd->width * s; } - void Badger2040::text(std::string message, int32_t x, int32_t y, float s) { + void Badger2040::text(std::string message, int32_t x, int32_t y, float s, float a) { int32_t cx = x; int32_t cy = y; + int32_t ox = 0; + + float as = sin(deg2rad(a)); + float ac = cos(deg2rad(a)); + for(auto &c : message) { - cx += glyph(c, cx, cy, s); + int rcx = ox * ac; + int rcy = ox * as; + + ox += glyph(c, cx + rcx, cy + rcy, s, a); } } diff --git a/libraries/badger2040/badger2040.hpp b/libraries/badger2040/badger2040.hpp index 29b4f14b..1eb608d1 100644 --- a/libraries/badger2040/badger2040.hpp +++ b/libraries/badger2040/badger2040.hpp @@ -55,9 +55,9 @@ namespace pimoroni { void image(const uint8_t *data); // text (fonts: sans, sans_bold, gothic, cursive_bold, cursive, serif_italic, serif, serif_bold) - void text(std::string message, int32_t x, int32_t y, float s = 1.0f); + void text(std::string message, int32_t x, int32_t y, float s = 1.0f, float a = 0.0f); const hershey_font_glyph_t* glyph_data(unsigned char c); - int32_t glyph(unsigned char c, int32_t x, int32_t y, float s); + int32_t glyph(unsigned char c, int32_t x, int32_t y, float s, float a = 0.0f); void debug_command(uint8_t command, size_t len, const uint8_t *data); void dump_otp(uint8_t *otp_data);