commit
a334899b61
|
@ -8,6 +8,7 @@ namespace pimoroni {
|
|||
int PicoGraphics::reset_pen(uint8_t i) {return -1;};
|
||||
int PicoGraphics::create_pen(uint8_t r, uint8_t g, uint8_t b) {return -1;};
|
||||
int PicoGraphics::create_pen_hsv(float h, float s, float v){return -1;};
|
||||
void PicoGraphics::set_pixel_alpha(const Point &p, const uint8_t a) {};
|
||||
void PicoGraphics::set_pixel_dither(const Point &p, const RGB &c) {};
|
||||
void PicoGraphics::set_pixel_dither(const Point &p, const RGB565 &c) {};
|
||||
void PicoGraphics::set_pixel_dither(const Point &p, const uint8_t &c) {};
|
||||
|
@ -16,6 +17,7 @@ namespace pimoroni {
|
|||
|
||||
int PicoGraphics::get_palette_size() {return 0;}
|
||||
RGB* PicoGraphics::get_palette() {return nullptr;}
|
||||
bool PicoGraphics::supports_alpha_blend() {return false;}
|
||||
|
||||
void PicoGraphics::set_dimensions(int width, int height) {
|
||||
bounds = clip = {0, 0, width, height};
|
||||
|
|
|
@ -47,7 +47,19 @@ namespace pimoroni {
|
|||
g((c >> 8) & 0xff),
|
||||
b(c & 0xff) {}
|
||||
constexpr RGB(int16_t r, int16_t g, int16_t b) : r(r), g(g), b(b) {}
|
||||
|
||||
|
||||
constexpr uint8_t blend(uint8_t s, uint8_t d, uint8_t a) {
|
||||
return d + ((a * (s - d) + 127) >> 8);
|
||||
}
|
||||
|
||||
constexpr RGB blend(RGB with, const uint8_t alpha) {
|
||||
return RGB(
|
||||
blend(with.r, r, alpha),
|
||||
blend(with.g, g, alpha),
|
||||
blend(with.b, b, alpha)
|
||||
);
|
||||
}
|
||||
|
||||
static RGB from_hsv(float h, float s, float v) {
|
||||
float i = floor(h * 6.0f);
|
||||
float f = h * 6.0f - i;
|
||||
|
@ -268,6 +280,7 @@ namespace pimoroni {
|
|||
|
||||
virtual int get_palette_size();
|
||||
virtual RGB* get_palette();
|
||||
virtual bool supports_alpha_blend();
|
||||
|
||||
virtual int create_pen(uint8_t r, uint8_t g, uint8_t b);
|
||||
virtual int create_pen_hsv(float h, float s, float v);
|
||||
|
@ -276,6 +289,7 @@ namespace pimoroni {
|
|||
virtual void set_pixel_dither(const Point &p, const RGB &c);
|
||||
virtual void set_pixel_dither(const Point &p, const RGB565 &c);
|
||||
virtual void set_pixel_dither(const Point &p, const uint8_t &c);
|
||||
virtual void set_pixel_alpha(const Point &p, const uint8_t a);
|
||||
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);
|
||||
|
||||
|
@ -471,6 +485,9 @@ namespace pimoroni {
|
|||
void set_pixel_span(const Point &p, uint l) override;
|
||||
void set_pixel_dither(const Point &p, const RGB &c) override;
|
||||
void set_pixel_dither(const Point &p, const RGB565 &c) override;
|
||||
void set_pixel_alpha(const Point &p, const uint8_t a) override;
|
||||
|
||||
bool supports_alpha_blend() override {return true;}
|
||||
|
||||
void sprite(void* data, const Point &sprite, const Point &dest, const int scale, const int transparent) override;
|
||||
|
||||
|
|
|
@ -34,6 +34,15 @@ namespace pimoroni {
|
|||
*buf++ = color;
|
||||
}
|
||||
}
|
||||
void PicoGraphics_PenRGB332::set_pixel_alpha(const Point &p, const uint8_t a) {
|
||||
if(!bounds.contains(p)) return;
|
||||
|
||||
uint8_t *buf = (uint8_t *)frame_buffer;
|
||||
|
||||
RGB332 blended = RGB(buf[p.y * bounds.w + p.x]).blend(RGB(color), a).to_rgb332();
|
||||
|
||||
buf[p.y * bounds.w + p.x] = blended;
|
||||
};
|
||||
void PicoGraphics_PenRGB332::set_pixel_dither(const Point &p, const RGB &c) {
|
||||
if(!bounds.contains(p)) return;
|
||||
uint8_t _dmv = dither16_pattern[(p.x & 0b11) | ((p.y & 0b11) << 2)];
|
||||
|
|
|
@ -0,0 +1,171 @@
|
|||
#include <cstdint>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
#include <map>
|
||||
|
||||
#include "alright_fonts.hpp"
|
||||
|
||||
using namespace pretty_poly;
|
||||
|
||||
namespace alright_fonts {
|
||||
/*
|
||||
utility functions
|
||||
*/
|
||||
pretty_poly::rect_t measure_character(text_metrics_t &tm, uint16_t codepoint) {
|
||||
if(tm.face.glyphs.count(codepoint) == 1) {
|
||||
glyph_t glyph = tm.face.glyphs[codepoint];
|
||||
|
||||
return {0, 0, ((glyph.advance * tm.size) / 128), tm.size};
|
||||
}
|
||||
|
||||
return {0, 0, 0, 0};
|
||||
}
|
||||
|
||||
/*
|
||||
render functions
|
||||
*/
|
||||
|
||||
void render_character(text_metrics_t &tm, uint16_t codepoint, pretty_poly::point_t<int> origin) {
|
||||
if(tm.face.glyphs.count(codepoint) == 1) {
|
||||
glyph_t glyph = tm.face.glyphs[codepoint];
|
||||
|
||||
// scale is a fixed point 16:16 value, our font data is already scaled to
|
||||
// -128..127 so to get the pixel size we want we can just shift the
|
||||
// users requested size up one bit
|
||||
unsigned scale = tm.size << 9;
|
||||
|
||||
pretty_poly::draw_polygon<int8_t>(glyph.contours, origin, scale);
|
||||
}
|
||||
}
|
||||
|
||||
void render_character(text_metrics_t &tm, uint16_t codepoint, pretty_poly::point_t<int> origin, pretty_poly::mat3_t transform) {
|
||||
if(tm.face.glyphs.count(codepoint) == 1) {
|
||||
glyph_t glyph = tm.face.glyphs[codepoint];
|
||||
|
||||
// scale is a fixed point 16:16 value, our font data is already scaled to
|
||||
// -128..127 so to get the pixel size we want we can just shift the
|
||||
// users requested size up one bit
|
||||
unsigned scale = tm.size << 9;
|
||||
|
||||
std::vector<pretty_poly::contour_t<int8_t>> contours;
|
||||
|
||||
for(auto i = 0u; i < glyph.contours.size(); i++) {
|
||||
unsigned int count = glyph.contours[i].count;
|
||||
point_t<int8_t> *points = (point_t<int8_t> *)malloc(sizeof(point_t<int8_t>) * count);
|
||||
for(auto j = 0u; j < count; j++) {
|
||||
point_t<float> point(glyph.contours[i].points[j].x, glyph.contours[i].points[j].y);
|
||||
point *= transform;
|
||||
points[j] = point_t<int8_t>(point.x, point.y);
|
||||
}
|
||||
contours.emplace_back(points, count);
|
||||
}
|
||||
|
||||
pretty_poly::draw_polygon<int8_t>(contours, origin, scale);
|
||||
|
||||
for(auto contour : contours) {
|
||||
free(contour.points);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
load functions
|
||||
*/
|
||||
|
||||
// big endian stream value helpers
|
||||
uint16_t ru16(file_io &ifs) {uint8_t w[2]; ifs.read((char *)w, 2); return w[0] << 8 | w[1];}
|
||||
int16_t rs16(file_io &ifs) {uint8_t w[2]; ifs.read((char *)w, 2); return w[0] << 8 | w[1];}
|
||||
uint32_t ru32(file_io &ifs) {uint8_t dw[4]; ifs.read((char *)dw, 4); return dw[0] << 24 | dw[1] << 16 | dw[2] << 8 | dw[3];}
|
||||
uint8_t ru8(file_io &ifs) {uint8_t w; ifs.read(&w, 1); return w;}
|
||||
int8_t rs8(file_io &ifs) {int8_t w; ifs.read(&w, 1); return w;}
|
||||
|
||||
bool face_t::load(file_io &ifs) {
|
||||
char marker[4];
|
||||
ifs.read(marker, sizeof(marker));
|
||||
|
||||
// check header magic bytes are present
|
||||
if(memcmp(marker, "af!?", 4) != 0) {
|
||||
// doesn't start with magic marker
|
||||
return false;
|
||||
}
|
||||
|
||||
// number of glyphs embedded in font file
|
||||
this->glyph_count = ru16(ifs);
|
||||
|
||||
// extract flags and ensure none set
|
||||
this->flags = ru16(ifs);
|
||||
if(this->flags != 0) {
|
||||
// unknown flags set
|
||||
return false;
|
||||
}
|
||||
|
||||
// extract glyph dictionary
|
||||
uint16_t glyph_entry_size = 9;
|
||||
uint32_t contour_data_offset = 8 + this->glyph_count * glyph_entry_size;
|
||||
for(auto i = 0; i < this->glyph_count; i++) {
|
||||
glyph_t g;
|
||||
g.codepoint = ru16(ifs);
|
||||
g.bounds.x = rs8(ifs);
|
||||
g.bounds.y = rs8(ifs);
|
||||
g.bounds.w = ru8(ifs);
|
||||
g.bounds.h = ru8(ifs);
|
||||
g.advance = ru8(ifs);
|
||||
|
||||
if(ifs.fail()) {
|
||||
// could not read glyph dictionary entry
|
||||
return false;
|
||||
}
|
||||
|
||||
// allocate space for the contour data and read it from the font file
|
||||
uint16_t contour_data_length = ru16(ifs);
|
||||
|
||||
// remember where we are in the dictionary
|
||||
int pos = ifs.tell();
|
||||
|
||||
// read contour data
|
||||
ifs.seek(contour_data_offset);
|
||||
while(true) {
|
||||
// get number of points in contour
|
||||
uint16_t count = ru16(ifs);
|
||||
|
||||
// if count is zero then this is the end of contour marker
|
||||
if(count == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
// allocate space to store point data for contour and read
|
||||
// from file
|
||||
pretty_poly::point_t<int8_t> *points = new pretty_poly::point_t<int8_t>[count];
|
||||
ifs.read((char *)points, count * 2);
|
||||
|
||||
g.contours.push_back({points, count});
|
||||
}
|
||||
|
||||
// return back to position in dictionary
|
||||
ifs.seek(pos);
|
||||
contour_data_offset += contour_data_length;
|
||||
|
||||
if(ifs.fail()) {
|
||||
// could not read glyph contour data
|
||||
return false;
|
||||
}
|
||||
|
||||
this->glyphs[g.codepoint] = g;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool face_t::load(std::string_view path) {
|
||||
file_io ifs(path);
|
||||
if(ifs.fail()) {
|
||||
// could not open file
|
||||
return false;
|
||||
}
|
||||
return load(ifs);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
#include <cstdint>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
#include <map>
|
||||
|
||||
#include "pretty_poly.hpp"
|
||||
|
||||
namespace alright_fonts {
|
||||
|
||||
struct glyph_t {
|
||||
uint16_t codepoint;
|
||||
pretty_poly::rect_t bounds;
|
||||
uint8_t advance;
|
||||
std::vector<pretty_poly::contour_t<int8_t>> contours;
|
||||
};
|
||||
|
||||
struct face_t {
|
||||
uint16_t glyph_count;
|
||||
uint16_t flags;
|
||||
std::map<uint16_t, glyph_t> glyphs;
|
||||
|
||||
face_t() {};
|
||||
face_t(pretty_poly::file_io &ifs) {load(ifs);}
|
||||
face_t(std::string_view path) {load(path);}
|
||||
|
||||
bool load(pretty_poly::file_io &ifs);
|
||||
bool load(std::string_view path);
|
||||
};
|
||||
|
||||
enum alignment_t {
|
||||
left = 0,
|
||||
center = 1,
|
||||
right = 2,
|
||||
justify = 4,
|
||||
top = 8,
|
||||
bottom = 16
|
||||
};
|
||||
|
||||
struct text_metrics_t {
|
||||
face_t face; // font to write in
|
||||
int size; // text size in pixels
|
||||
uint scroll; // vertical scroll offset
|
||||
int line_height; // spacing between lines (%)
|
||||
int letter_spacing; // spacing between characters
|
||||
int word_spacing; // spacing between words
|
||||
alignment_t align; // horizontal and vertical alignment
|
||||
//optional<mat3_t> transform; // arbitrary transformation
|
||||
pretty_poly::antialias_t antialiasing = pretty_poly::X4; // level of antialiasing to apply
|
||||
|
||||
void set_size(int s) {
|
||||
size = s;
|
||||
line_height = size;
|
||||
letter_spacing = 0;
|
||||
word_spacing = size / 2;
|
||||
}
|
||||
|
||||
text_metrics_t() {};
|
||||
};
|
||||
|
||||
/*
|
||||
utility functions
|
||||
*/
|
||||
pretty_poly::rect_t measure_character(text_metrics_t &tm, uint16_t codepoint);
|
||||
|
||||
/*
|
||||
render functions
|
||||
*/
|
||||
|
||||
void render_character(text_metrics_t &tm, uint16_t codepoint, pretty_poly::point_t<int> origin);
|
||||
void render_character(text_metrics_t &tm, uint16_t codepoint, pretty_poly::point_t<int> origin, pretty_poly::mat3_t transform);
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
add_library(pico_vector
|
||||
${CMAKE_CURRENT_LIST_DIR}/pico_vector.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/pretty_poly.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/alright_fonts.cpp
|
||||
)
|
||||
|
||||
target_include_directories(pico_vector INTERFACE ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
target_link_libraries(pico_vector pico_stdlib)
|
|
@ -0,0 +1,188 @@
|
|||
#include "pico_vector.hpp"
|
||||
#include <vector>
|
||||
|
||||
namespace pimoroni {
|
||||
void PicoVector::polygon(std::vector<pretty_poly::contour_t<picovector_point_type>> contours, Point origin, int scale) {
|
||||
pretty_poly::draw_polygon<picovector_point_type>(
|
||||
contours,
|
||||
pretty_poly::point_t<int>(origin.x, origin.y),
|
||||
scale);
|
||||
}
|
||||
|
||||
void PicoVector::rotate(std::vector<pretty_poly::contour_t<picovector_point_type>> &contours, Point origin, float angle) {
|
||||
pretty_poly::mat3_t t2 = pretty_poly::mat3_t::translation(origin.x, origin.y);
|
||||
pretty_poly::mat3_t t1 = pretty_poly::mat3_t::translation(-origin.x, -origin.y);
|
||||
angle = 2 * M_PI * (angle / 360.0f);
|
||||
pretty_poly::mat3_t r = pretty_poly::mat3_t::rotation(angle);
|
||||
for(auto &contour : contours) {
|
||||
for(auto i = 0u; i < contour.count; i++) {
|
||||
contour.points[i] *= t1;
|
||||
contour.points[i] *= r;
|
||||
contour.points[i] *= t2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PicoVector::translate(std::vector<pretty_poly::contour_t<picovector_point_type>> &contours, Point translation) {
|
||||
pretty_poly::mat3_t t = pretty_poly::mat3_t::translation(translation.x, translation.y);
|
||||
for(auto &contour : contours) {
|
||||
for(auto i = 0u; i < contour.count; i++) {
|
||||
contour.points[i] *= t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PicoVector::rotate(pretty_poly::contour_t<picovector_point_type> &contour, Point origin, float angle) {
|
||||
pretty_poly::mat3_t t2 = pretty_poly::mat3_t::translation(origin.x, origin.y);
|
||||
pretty_poly::mat3_t t1 = pretty_poly::mat3_t::translation(-origin.x, -origin.y);
|
||||
angle = 2 * M_PI * (angle / 360.0f);
|
||||
pretty_poly::mat3_t r = pretty_poly::mat3_t::rotation(angle);
|
||||
for(auto i = 0u; i < contour.count; i++) {
|
||||
contour.points[i] *= t1;
|
||||
contour.points[i] *= r;
|
||||
contour.points[i] *= t2;
|
||||
}
|
||||
}
|
||||
|
||||
void PicoVector::translate(pretty_poly::contour_t<picovector_point_type> &contour, Point translation) {
|
||||
pretty_poly::mat3_t t = pretty_poly::mat3_t::translation(translation.x, translation.y);
|
||||
for(auto i = 0u; i < contour.count; i++) {
|
||||
contour.points[i] *= t;
|
||||
}
|
||||
}
|
||||
|
||||
Point PicoVector::text(std::string_view text, Point origin) {
|
||||
// TODO: Normalize types somehow, so we're not converting?
|
||||
pretty_poly::point_t<int> caret = pretty_poly::point_t<int>(origin.x, origin.y);
|
||||
|
||||
// Align text from the bottom left
|
||||
caret.y += text_metrics.size;
|
||||
|
||||
int16_t space_width = alright_fonts::measure_character(text_metrics, ' ').w;
|
||||
if (space_width == 0) {
|
||||
space_width = text_metrics.word_spacing;
|
||||
}
|
||||
|
||||
size_t i = 0;
|
||||
|
||||
while(i < text.length()) {
|
||||
size_t next_space = text.find(' ', i + 1);
|
||||
|
||||
if(next_space == std::string::npos) {
|
||||
next_space = text.length();
|
||||
}
|
||||
|
||||
size_t next_linebreak = text.find('\n', i + 1);
|
||||
|
||||
if(next_linebreak == std::string::npos) {
|
||||
next_linebreak = text.length();
|
||||
}
|
||||
|
||||
size_t next_break = std::min(next_space, next_linebreak);
|
||||
|
||||
uint16_t word_width = 0;
|
||||
for(size_t j = i; j < next_break; j++) {
|
||||
word_width += alright_fonts::measure_character(text_metrics, text[j]).w;
|
||||
word_width += text_metrics.letter_spacing;
|
||||
}
|
||||
|
||||
if(caret.x != 0 && caret.x + word_width > graphics->clip.w) {
|
||||
caret.x = origin.x;
|
||||
caret.y += text_metrics.line_height;
|
||||
}
|
||||
|
||||
for(size_t j = i; j < std::min(next_break + 1, text.length()); j++) {
|
||||
if (text[j] == '\n') { // Linebreak
|
||||
caret.x = origin.x;
|
||||
caret.y += text_metrics.line_height;
|
||||
} else if (text[j] == ' ') { // Space
|
||||
caret.x += space_width;
|
||||
} else {
|
||||
alright_fonts::render_character(text_metrics, text[j], caret);
|
||||
}
|
||||
caret.x += alright_fonts::measure_character(text_metrics, text[j]).w;
|
||||
caret.x += text_metrics.letter_spacing;
|
||||
}
|
||||
|
||||
i = next_break + 1;
|
||||
}
|
||||
|
||||
return Point(caret.x, caret.y);
|
||||
}
|
||||
|
||||
Point PicoVector::text(std::string_view text, Point origin, float angle) {
|
||||
// TODO: Normalize types somehow, so we're not converting?
|
||||
pretty_poly::point_t<float> caret(0, 0);
|
||||
|
||||
// Prepare a transformation matrix for character and offset rotation
|
||||
angle = 2 * M_PI * (angle / 360.0f);
|
||||
pretty_poly::mat3_t transform = pretty_poly::mat3_t::rotation(angle);
|
||||
|
||||
// Align text from the bottom left
|
||||
caret.y += text_metrics.line_height;
|
||||
caret *= transform;
|
||||
|
||||
pretty_poly::point_t<float> space;
|
||||
pretty_poly::point_t<float> carriage_return(0, text_metrics.line_height);
|
||||
|
||||
space.x = alright_fonts::measure_character(text_metrics, ' ').w;
|
||||
if (space.x == 0) {
|
||||
space.x = text_metrics.word_spacing;
|
||||
}
|
||||
|
||||
space *= transform;
|
||||
carriage_return *= transform;
|
||||
|
||||
size_t i = 0;
|
||||
|
||||
while(i < text.length()) {
|
||||
size_t next_space = text.find(' ', i + 1);
|
||||
|
||||
if(next_space == std::string::npos) {
|
||||
next_space = text.length();
|
||||
}
|
||||
|
||||
size_t next_linebreak = text.find('\n', i + 1);
|
||||
|
||||
if(next_linebreak == std::string::npos) {
|
||||
next_linebreak = text.length();
|
||||
}
|
||||
|
||||
size_t next_break = std::min(next_space, next_linebreak);
|
||||
|
||||
uint16_t word_width = 0;
|
||||
for(size_t j = i; j < next_break; j++) {
|
||||
word_width += alright_fonts::measure_character(text_metrics, text[j]).w;
|
||||
word_width += text_metrics.letter_spacing;
|
||||
}
|
||||
|
||||
if(caret.x != 0 && caret.x + word_width > graphics->clip.w) {
|
||||
caret -= carriage_return;
|
||||
carriage_return.x = 0;
|
||||
}
|
||||
|
||||
for(size_t j = i; j < std::min(next_break + 1, text.length()); j++) {
|
||||
if (text[j] == '\n') { // Linebreak
|
||||
caret -= carriage_return;
|
||||
carriage_return.x = 0;
|
||||
} else if (text[j] == ' ') { // Space
|
||||
caret += space;
|
||||
carriage_return += space;
|
||||
} else {
|
||||
alright_fonts::render_character(text_metrics, text[j], pretty_poly::point_t<int>(origin.x + caret.x, origin.y + caret.y), transform);
|
||||
}
|
||||
pretty_poly::point_t<float> advance(
|
||||
alright_fonts::measure_character(text_metrics, text[j]).w + text_metrics.letter_spacing,
|
||||
0
|
||||
);
|
||||
advance *= transform;
|
||||
caret += advance;
|
||||
carriage_return += advance;
|
||||
}
|
||||
|
||||
i = next_break + 1;
|
||||
}
|
||||
|
||||
return Point(caret.x, caret.y);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
#include "pretty_poly.hpp"
|
||||
#include "alright_fonts.hpp"
|
||||
#include "pico_graphics.hpp"
|
||||
|
||||
namespace pimoroni {
|
||||
|
||||
// Integer point types cause compound error in transformations
|
||||
typedef float picovector_point_type;
|
||||
|
||||
class PicoVector {
|
||||
private:
|
||||
PicoGraphics *graphics;
|
||||
alright_fonts::text_metrics_t text_metrics;
|
||||
const uint8_t alpha_map[4] {0, 128, 192, 255};
|
||||
|
||||
public:
|
||||
PicoVector(PicoGraphics *graphics, void *mem = nullptr) : graphics(graphics) {
|
||||
pretty_poly::init(mem);
|
||||
|
||||
set_options([this](const pretty_poly::tile_t &tile) -> void {
|
||||
uint8_t *tile_data = tile.data;
|
||||
|
||||
if(this->graphics->supports_alpha_blend() && pretty_poly::settings::antialias != pretty_poly::NONE) {
|
||||
for(auto y = 0; y < tile.bounds.h; y++) {
|
||||
for(auto x = 0; x < tile.bounds.w; x++) {
|
||||
uint8_t alpha = *tile_data++;
|
||||
if (alpha >= 4) {
|
||||
this->graphics->set_pixel({x + tile.bounds.x, y + tile.bounds.y});
|
||||
} else if (alpha > 0) {
|
||||
alpha = alpha_map[alpha];
|
||||
this->graphics->set_pixel_alpha({x + tile.bounds.x, y + tile.bounds.y}, alpha);
|
||||
}
|
||||
}
|
||||
tile_data += tile.stride - tile.bounds.w;
|
||||
}
|
||||
} else {
|
||||
for(auto y = 0; y < tile.bounds.h; y++) {
|
||||
for(auto x = 0; x < tile.bounds.w; x++) {
|
||||
uint8_t alpha = *tile_data++;
|
||||
if (alpha) {
|
||||
this->graphics->set_pixel({x + tile.bounds.x, y + tile.bounds.y});
|
||||
}
|
||||
}
|
||||
tile_data += tile.stride - tile.bounds.w;
|
||||
}
|
||||
}
|
||||
}, graphics->supports_alpha_blend() ? pretty_poly::X4 : pretty_poly::NONE, {0, 0, graphics->bounds.w, graphics->bounds.h});
|
||||
}
|
||||
|
||||
void set_antialiasing(pretty_poly::antialias_t antialias) {
|
||||
set_options(pretty_poly::settings::callback, antialias, pretty_poly::settings::clip);
|
||||
}
|
||||
|
||||
void set_font_size(unsigned int font_size) {
|
||||
text_metrics.set_size(font_size);
|
||||
}
|
||||
|
||||
bool set_font(std::string_view font_path, unsigned int font_size) {
|
||||
bool result = text_metrics.face.load(font_path);
|
||||
|
||||
set_font_size(font_size);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void rotate(std::vector<pretty_poly::contour_t<picovector_point_type>> &contours, Point origin, float angle);
|
||||
void translate(std::vector<pretty_poly::contour_t<picovector_point_type>> &contours, Point translation);
|
||||
|
||||
void rotate(pretty_poly::contour_t<picovector_point_type> &contour, Point origin, float angle);
|
||||
void translate(pretty_poly::contour_t<picovector_point_type> &contour, Point translation);
|
||||
|
||||
Point text(std::string_view text, Point origin);
|
||||
Point text(std::string_view text, Point origin, float angle);
|
||||
|
||||
void polygon(std::vector<pretty_poly::contour_t<picovector_point_type>> contours, Point origin = Point(0, 0), int scale=65536);
|
||||
|
||||
static constexpr size_t pretty_poly_buffer_size() {
|
||||
return pretty_poly::buffer_size();
|
||||
};
|
||||
};
|
||||
}
|
|
@ -0,0 +1,239 @@
|
|||
#include <cstdint>
|
||||
#include <algorithm>
|
||||
#include <optional>
|
||||
#include <cstring>
|
||||
#include <new>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
#include "pretty_poly.hpp"
|
||||
|
||||
|
||||
#ifdef PP_DEBUG
|
||||
#define debug(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define debug(...)
|
||||
#endif
|
||||
|
||||
namespace pretty_poly {
|
||||
|
||||
uint8_t *tile_buffer;
|
||||
|
||||
int (*nodes)[32];
|
||||
unsigned *node_counts;
|
||||
|
||||
// default tile bounds to X1 antialiasing
|
||||
rect_t tile_bounds(0, 0, tile_buffer_size / node_buffer_size, node_buffer_size);
|
||||
|
||||
// user settings
|
||||
namespace settings {
|
||||
rect_t clip(0, 0, 320, 240);
|
||||
tile_callback_t callback;
|
||||
antialias_t antialias = antialias_t::NONE;
|
||||
}
|
||||
|
||||
void init(void *memory) {
|
||||
uintptr_t m = (uintptr_t)memory;
|
||||
tile_buffer = new(memory) uint8_t[tile_buffer_size];
|
||||
node_counts = new((void *)(m + tile_buffer_size)) unsigned[node_buffer_size];
|
||||
nodes = new((void *)(m + tile_buffer_size + (node_buffer_size * sizeof(unsigned)))) int[node_buffer_size][32];
|
||||
}
|
||||
|
||||
void set_options(tile_callback_t callback, antialias_t antialias, rect_t clip) {
|
||||
settings::callback = callback;
|
||||
settings::antialias = antialias;
|
||||
settings::clip = clip;
|
||||
|
||||
// recalculate the tile size for rendering based on antialiasing level
|
||||
int tile_height = node_buffer_size >> antialias;
|
||||
tile_bounds = rect_t(0, 0, tile_buffer_size / tile_height, tile_height);
|
||||
}
|
||||
|
||||
// dy step (returns 1, 0, or -1 if the supplied value is > 0, == 0, < 0)
|
||||
inline constexpr int sign(int v) {
|
||||
// assumes 32-bit int/unsigned
|
||||
return ((unsigned)-v >> 31) - ((unsigned)v >> 31);
|
||||
}
|
||||
|
||||
// write out the tile bits
|
||||
void debug_tile(const tile_t &tile) {
|
||||
debug(" - tile %d, %d (%d x %d)\n", tile.bounds.x, tile.bounds.y, tile.bounds.w, tile.bounds.h);
|
||||
for(auto y = 0; y < tile.bounds.h; y++) {
|
||||
debug("[%3d]: ", y);
|
||||
for(auto x = 0; x < tile.bounds.w; x++) {
|
||||
debug("%d", tile.get_value(x, y));
|
||||
}
|
||||
debug("\n");
|
||||
}
|
||||
debug("-----------------------\n");
|
||||
}
|
||||
|
||||
void add_line_segment_to_nodes(const point_t<int> &start, const point_t<int> &end) {
|
||||
// swap endpoints if line "pointing up", we do this because we
|
||||
// alway skip the last scanline (so that polygons can but cleanly
|
||||
// up against each other without overlap)
|
||||
int sx = start.x, sy = start.y, ex = end.x, ey = end.y;
|
||||
|
||||
if(ey < sy) {
|
||||
std::swap(sy, ey);
|
||||
std::swap(sx, ex);
|
||||
}
|
||||
|
||||
/*sx <<= settings::antialias;
|
||||
ex <<= settings::antialias;
|
||||
sy <<= settings::antialias;
|
||||
ey <<= settings::antialias;*/
|
||||
|
||||
int x = sx;
|
||||
int y = sy;
|
||||
int e = 0;
|
||||
|
||||
int xinc = sign(ex - sx);
|
||||
int einc = abs(ex - sx) + 1;
|
||||
|
||||
// todo: preclamp sy and ey (and therefore count) no need to perform
|
||||
// that test inside the loop
|
||||
int dy = ey - sy;
|
||||
int count = dy;
|
||||
debug(" + line segment from %d, %d to %d, %d\n", sx, sy, ex, ey);
|
||||
// loop over scanlines
|
||||
while(count--) {
|
||||
// consume accumulated error
|
||||
while(e > dy) {e -= dy; x += xinc;}
|
||||
|
||||
if(y >= 0 && y < (int)node_buffer_size) {
|
||||
// clamp node x value to tile bounds
|
||||
int nx = std::max(std::min(x, (int)(tile_bounds.w << settings::antialias)), 0);
|
||||
debug(" + adding node at %d, %d\n", x, y);
|
||||
// add node to node list
|
||||
nodes[y][node_counts[y]++] = nx;
|
||||
}
|
||||
|
||||
// step to next scanline and accumulate error
|
||||
y++;
|
||||
e += einc;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void build_nodes(const contour_t<T> &contour, const tile_t &tile, point_t<int> origin, int scale) {
|
||||
int ox = (origin.x - tile.bounds.x) << settings::antialias;
|
||||
int oy = (origin.y - tile.bounds.y) << settings::antialias;
|
||||
|
||||
// start with the last point to close the loop
|
||||
point_t<int> last(
|
||||
(((int(contour.points[contour.count - 1].x) * scale) << settings::antialias) / 65536) + ox,
|
||||
(((int(contour.points[contour.count - 1].y) * scale) << settings::antialias) / 65536) + oy
|
||||
);
|
||||
|
||||
for(auto i = 0u; i < contour.count; i++) {
|
||||
point_t<int> point(
|
||||
(((int(contour.points[i].x) * scale) << settings::antialias) / 65536) + ox,
|
||||
(((int(contour.points[i].y) * scale) << settings::antialias) / 65536) + oy
|
||||
);
|
||||
|
||||
add_line_segment_to_nodes(last, point);
|
||||
|
||||
last = point;
|
||||
}
|
||||
}
|
||||
|
||||
void render_nodes(const tile_t &tile) {
|
||||
for(auto y = 0; y < (int)node_buffer_size; y++) {
|
||||
if(node_counts[y] == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
std::sort(&nodes[y][0], &nodes[y][0] + node_counts[y]);
|
||||
|
||||
for(auto i = 0u; i < node_counts[y]; i += 2) {
|
||||
int sx = nodes[y][i + 0];
|
||||
int ex = nodes[y][i + 1];
|
||||
|
||||
if(sx == ex) {
|
||||
continue;
|
||||
}
|
||||
|
||||
debug(" - render span at %d from %d to %d\n", y, sx, ex);
|
||||
|
||||
for(int x = sx; x < ex; x++) {
|
||||
tile.data[(x >> settings::antialias) + (y >> settings::antialias) * tile.stride]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void draw_polygon(T *points, unsigned count) {
|
||||
std::vector<contour_t<T>> contours;
|
||||
contour_t<T> c(points, count);
|
||||
contours.push_back(c);
|
||||
draw_polygon<T>(contours);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void draw_polygon(std::vector<contour_t<T>> contours, point_t<int> origin, int scale) {
|
||||
|
||||
debug("> draw polygon with %lu contours\n", contours.size());
|
||||
|
||||
if(contours.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// determine extreme bounds
|
||||
rect_t polygon_bounds = contours[0].bounds();
|
||||
for(auto &contour : contours) {
|
||||
polygon_bounds = polygon_bounds.merge(contour.bounds());
|
||||
}
|
||||
|
||||
polygon_bounds.x = ((polygon_bounds.x * scale) / 65536) + origin.x;
|
||||
polygon_bounds.y = ((polygon_bounds.y * scale) / 65536) + origin.y;
|
||||
polygon_bounds.w = ((polygon_bounds.w * scale) / 65536);
|
||||
polygon_bounds.h = ((polygon_bounds.h * scale) / 65536);
|
||||
|
||||
debug(" - bounds %d, %d (%d x %d)\n", polygon_bounds.x, polygon_bounds.y, polygon_bounds.w, polygon_bounds.h);
|
||||
debug(" - clip %d, %d (%d x %d)\n", settings::clip.x, settings::clip.y, settings::clip.w, settings::clip.h);
|
||||
|
||||
|
||||
memset(nodes, 0, node_buffer_size * sizeof(unsigned) * 32);
|
||||
|
||||
// iterate over tiles
|
||||
debug(" - processing tiles\n");
|
||||
for(auto y = polygon_bounds.y; y < polygon_bounds.y + polygon_bounds.h; y += tile_bounds.h) {
|
||||
for(auto x = polygon_bounds.x; x < polygon_bounds.x + polygon_bounds.w; x += tile_bounds.w) {
|
||||
tile_t tile;
|
||||
tile.bounds = rect_t(x, y, tile_bounds.w, tile_bounds.h).intersection(settings::clip);
|
||||
tile.stride = tile_bounds.w;
|
||||
tile.data = tile_buffer;
|
||||
debug(" : %d, %d (%d x %d)\n", tile.bounds.x, tile.bounds.y, tile.bounds.w, tile.bounds.h);
|
||||
|
||||
// if no intersection then skip tile
|
||||
if(tile.bounds.empty()) {
|
||||
debug(" : empty when clipped, skipping\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
// clear existing tile data and nodes
|
||||
memset(node_counts, 0, node_buffer_size * sizeof(unsigned));
|
||||
memset(tile.data, 0, tile_buffer_size);
|
||||
|
||||
// build the nodes for each contour
|
||||
for(contour_t<T> &contour : contours) {
|
||||
debug(" : build nodes for contour\n");
|
||||
build_nodes(contour, tile, origin, scale);
|
||||
}
|
||||
|
||||
debug(" : render the tile\n");
|
||||
// render the tile
|
||||
render_nodes(tile);
|
||||
|
||||
settings::callback(tile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template void pretty_poly::draw_polygon<int>(std::vector<contour_t<int>> contours, point_t<int> origin, int scale);
|
||||
template void pretty_poly::draw_polygon<float>(std::vector<contour_t<float>> contours, point_t<int> origin, int scale);
|
||||
template void pretty_poly::draw_polygon<uint8_t>(std::vector<contour_t<uint8_t>> contours, point_t<int> origin, int scale);
|
||||
template void pretty_poly::draw_polygon<int8_t>(std::vector<contour_t<int8_t>> contours, point_t<int> origin, int scale);
|
|
@ -0,0 +1,72 @@
|
|||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <algorithm>
|
||||
#include <optional>
|
||||
#include <cstring>
|
||||
#include <new>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
#include "pretty_poly_types.hpp"
|
||||
|
||||
namespace pretty_poly {
|
||||
|
||||
class file_io {
|
||||
private:
|
||||
void *state;
|
||||
size_t filesize = 0;
|
||||
|
||||
public:
|
||||
file_io(std::string_view path);
|
||||
~file_io();
|
||||
size_t seek(size_t pos);
|
||||
size_t read(void *buf, size_t len);
|
||||
size_t tell();
|
||||
bool fail();
|
||||
};
|
||||
|
||||
// buffer that each tile is rendered into before callback
|
||||
constexpr unsigned tile_buffer_size = 1024;
|
||||
|
||||
// polygon node buffer handles at most 16 line intersections per scanline
|
||||
// is this enough for cjk/emoji? (requires a 2kB buffer)
|
||||
constexpr unsigned node_buffer_size = 32;
|
||||
|
||||
typedef std::function<void(const tile_t &tile)> tile_callback_t;
|
||||
|
||||
// user settings
|
||||
namespace settings {
|
||||
extern rect_t clip;
|
||||
extern tile_callback_t callback;
|
||||
extern antialias_t antialias;
|
||||
}
|
||||
|
||||
constexpr size_t buffer_size() {
|
||||
return tile_buffer_size + (node_buffer_size * sizeof(unsigned)) + (node_buffer_size * 32 * sizeof(int));
|
||||
}
|
||||
|
||||
constexpr size_t buffer_size();
|
||||
|
||||
void init(void *memory);
|
||||
|
||||
void set_options(tile_callback_t callback, antialias_t antialias, rect_t clip);
|
||||
|
||||
// dy step (returns 1, 0, or -1 if the supplied value is > 0, == 0, < 0)
|
||||
inline constexpr int sign(int v);
|
||||
|
||||
// write out the tile bits
|
||||
void debug_tile(const tile_t &tile);
|
||||
|
||||
void add_line_segment_to_nodes(const point_t<int> &start, const point_t<int> &end);
|
||||
|
||||
template<typename T>
|
||||
void build_nodes(const contour_t<T> &contour, const tile_t &tile, point_t<int> origin = point_t<int>(0, 0), int scale = 65536);
|
||||
|
||||
void render_nodes(const tile_t &tile);
|
||||
|
||||
template<typename T>
|
||||
void draw_polygon(T *points, unsigned count);
|
||||
|
||||
template<typename T>
|
||||
void draw_polygon(std::vector<contour_t<T>> contours, point_t<int> origin = point_t<int>(0, 0), int scale = 65536);
|
||||
}
|
|
@ -0,0 +1,132 @@
|
|||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <math.h>
|
||||
|
||||
#ifdef PP_DEBUG
|
||||
#define debug(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define debug(...)
|
||||
#endif
|
||||
|
||||
namespace pretty_poly {
|
||||
|
||||
enum antialias_t {NONE = 0, X4 = 1, X16 = 2};
|
||||
|
||||
// 3x3 matrix for coordinate transformations
|
||||
struct mat3_t {
|
||||
float v00, v10, v20, v01, v11, v21, v02, v12, v22 = 0.0f;
|
||||
mat3_t() = default;
|
||||
mat3_t(const mat3_t &m) = default;
|
||||
inline mat3_t& operator*= (const mat3_t &m) {
|
||||
float r00 = this->v00 * m.v00 + this->v01 * m.v10 + this->v02 * m.v20;
|
||||
float r01 = this->v00 * m.v01 + this->v01 * m.v11 + this->v02 * m.v21;
|
||||
float r02 = this->v00 * m.v02 + this->v01 * m.v12 + this->v02 * m.v22;
|
||||
float r10 = this->v10 * m.v00 + this->v11 * m.v10 + this->v12 * m.v20;
|
||||
float r11 = this->v10 * m.v01 + this->v11 * m.v11 + this->v12 * m.v21;
|
||||
float r12 = this->v10 * m.v02 + this->v11 * m.v12 + this->v12 * m.v22;
|
||||
float r20 = this->v20 * m.v00 + this->v21 * m.v10 + this->v22 * m.v20;
|
||||
float r21 = this->v20 * m.v01 + this->v21 * m.v11 + this->v22 * m.v21;
|
||||
float r22 = this->v20 * m.v02 + this->v21 * m.v12 + this->v22 * m.v22;
|
||||
this->v00 = r00; this->v01 = r01; this->v02 = r02;
|
||||
this->v10 = r10; this->v11 = r11; this->v12 = r12;
|
||||
this->v20 = r20; this->v21 = r21; this->v22 = r22;
|
||||
return *this;
|
||||
}
|
||||
|
||||
static mat3_t identity() {mat3_t m; m.v00 = m.v11 = m.v22 = 1.0f; return m;}
|
||||
static mat3_t rotation(float a) {
|
||||
float c = cosf(a), s = sinf(a); mat3_t r = mat3_t::identity();
|
||||
r.v00 = c; r.v01 = -s; r.v10 = s; r.v11 = c; return r;}
|
||||
static mat3_t translation(float x, float y) {
|
||||
mat3_t r = mat3_t::identity(); r.v02 = x; r.v12 = y; return r;}
|
||||
static mat3_t scale(float x, float y) {
|
||||
mat3_t r = mat3_t::identity(); r.v00 = x; r.v11 = y; return r;}
|
||||
};
|
||||
|
||||
// point type for contour points
|
||||
template<typename T = int>
|
||||
struct __attribute__ ((packed)) point_t {
|
||||
T x, y;
|
||||
point_t(T x, T y) : x(x), y(y) {}
|
||||
point_t() : x(0), y(0) {}
|
||||
inline point_t& operator-= (const point_t &a) {x -= a.x; y -= a.y; return *this;}
|
||||
inline point_t& operator+= (const point_t &a) {x += a.x; y += a.y; return *this;}
|
||||
inline point_t& operator*= (const float a) {x *= a; y *= a; return *this;}
|
||||
inline point_t& operator*= (const mat3_t &a) {this->transform(a); return *this;}
|
||||
inline point_t& operator/= (const float a) {x /= a; y /= a; return *this;}
|
||||
inline point_t& operator/= (const point_t &a) {x /= a.x; y /= a.y; return *this;}
|
||||
void transform(const mat3_t &m) {
|
||||
float tx = x, ty = y;
|
||||
this->x = (m.v00 * tx + m.v01 * ty + m.v02);
|
||||
this->y = (m.v10 * tx + m.v11 * ty + m.v12);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template<typename T> inline point_t<T> operator- (point_t<T> lhs, const point_t<T> &rhs) { lhs -= rhs; return lhs; }
|
||||
template<typename T> inline point_t<T> operator- (const point_t<T> &rhs) { return point_t<T>(-rhs.x, -rhs.y); }
|
||||
template<typename T> inline point_t<T> operator+ (point_t<T> lhs, const point_t<T> &rhs) { lhs += rhs; return lhs; }
|
||||
template<typename T> inline point_t<T> operator* (point_t<T> lhs, const float rhs) { lhs *= rhs; return lhs; }
|
||||
template<typename T> inline point_t<T> operator* (point_t<T> lhs, const point_t<T> &rhs) { lhs *= rhs; return lhs; }
|
||||
template<typename T> inline point_t<T> operator* (point_t<T> lhs, const mat3_t &rhs) { lhs *= rhs; return lhs; }
|
||||
template<typename T> inline point_t<T> operator/ (point_t<T> lhs, const float rhs) { lhs /= rhs; return lhs; }
|
||||
template<typename T> inline point_t<T> operator/ (point_t<T> lhs, const point_t<T> &rhs) { lhs.x /= rhs.x; lhs.y /= rhs.y; return lhs; }
|
||||
|
||||
|
||||
// rect type for bounds and clipping rectangles
|
||||
struct rect_t {
|
||||
int x, y, w, h;
|
||||
rect_t() : x(0), y(0), w(0), h(0) {}
|
||||
rect_t(int x, int y, int w, int h) : x(x), y(y), w(w), h(h) {}
|
||||
bool empty() const {return this->w == 0 && this->h == 0;}
|
||||
rect_t intersection(const rect_t &c) {
|
||||
return rect_t(std::max(this->x, c.x), std::max(this->y, c.y),
|
||||
std::max(0, std::min(this->x + this->w, c.x + c.w) - std::max(this->x, c.x)),
|
||||
std::max(0, std::min(this->y + this->h, c.y + c.h) - std::max(this->y, c.y)));
|
||||
}
|
||||
rect_t merge(const rect_t &c) {
|
||||
return rect_t(std::min(this->x, c.x), std::min(this->y, c.y),
|
||||
std::max(this->x + this->w, c.x + c.w) - std::min(this->x, c.x),
|
||||
std::max(this->y + this->h, c.y + c.h) - std::min(this->y, c.y));
|
||||
}
|
||||
};
|
||||
|
||||
struct tile_t {
|
||||
rect_t bounds;
|
||||
unsigned stride;
|
||||
uint8_t *data;
|
||||
|
||||
tile_t() {};
|
||||
|
||||
inline int get_value(int x, int y) const {
|
||||
return this->data[x + y * this->stride];
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct contour_t {
|
||||
point_t<T> *points;
|
||||
unsigned count;
|
||||
|
||||
contour_t() {}
|
||||
contour_t(std::vector<point_t<T>> v) : points(v.data()), count(v.size()) {};
|
||||
contour_t(point_t<T> *points, unsigned count) : points(points), count(count) {};
|
||||
|
||||
// TODO: Make this work, it's so much nicer to use auto point : contour
|
||||
//point_t<T> *begin() const { return points; };
|
||||
//point_t<T> *end() const { return points + count * sizeof(point_t<T>); };
|
||||
|
||||
rect_t bounds() {
|
||||
T minx = this->points[0].x, maxx = minx;
|
||||
T miny = this->points[0].y, maxy = miny;
|
||||
for(auto i = 1u; i < this->count; i++) {
|
||||
minx = std::min(minx, this->points[i].x);
|
||||
miny = std::min(miny, this->points[i].y);
|
||||
maxx = std::max(maxx, this->points[i].x);
|
||||
maxy = std::max(maxy, this->points[i].y);
|
||||
}
|
||||
return rect_t(minx, miny, maxx - minx, maxy - miny);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
|
@ -0,0 +1,119 @@
|
|||
import time
|
||||
import gc
|
||||
|
||||
from picographics import PicoGraphics, DISPLAY_PICO_W_EXPLORER, PEN_RGB332
|
||||
from picovector import PicoVector, Polygon, RegularPolygon, Rectangle, ANTIALIAS_X4
|
||||
|
||||
|
||||
display = PicoGraphics(DISPLAY_PICO_W_EXPLORER, pen_type=PEN_RGB332)
|
||||
|
||||
vector = PicoVector(display)
|
||||
vector.set_antialiasing(ANTIALIAS_X4)
|
||||
|
||||
RED = display.create_pen(200, 0, 0)
|
||||
BLACK = display.create_pen(0, 0, 0)
|
||||
GREY = display.create_pen(200, 200, 200)
|
||||
WHITE = display.create_pen(255, 255, 255)
|
||||
|
||||
"""
|
||||
# Redefine colours for a Blue clock
|
||||
RED = display.create_pen(200, 0, 0)
|
||||
BLACK = display.create_pen(135, 159, 169)
|
||||
GREY = display.create_pen(10, 40, 50)
|
||||
WHITE = display.create_pen(14, 60, 76)
|
||||
"""
|
||||
|
||||
WIDTH, HEIGHT = display.get_bounds()
|
||||
|
||||
hub = RegularPolygon(int(WIDTH / 2), int(HEIGHT / 2), 24, 5)
|
||||
|
||||
face = RegularPolygon(int(WIDTH / 2), int(HEIGHT / 2), 48, int(HEIGHT / 2))
|
||||
|
||||
print(time.localtime())
|
||||
|
||||
last_second = None
|
||||
|
||||
while True:
|
||||
t_start = time.ticks_ms()
|
||||
year, month, day, hour, minute, second, _, _ = time.localtime()
|
||||
|
||||
if last_second == second:
|
||||
continue
|
||||
|
||||
last_second = second
|
||||
|
||||
display.set_pen(0)
|
||||
display.clear()
|
||||
|
||||
display.set_pen(BLACK)
|
||||
display.circle(int(WIDTH / 2), int(HEIGHT / 2), int(HEIGHT / 2))
|
||||
display.set_pen(WHITE)
|
||||
display.circle(int(WIDTH / 2), int(HEIGHT / 2), int(HEIGHT / 2) - 4)
|
||||
|
||||
display.set_pen(GREY)
|
||||
|
||||
for a in range(60):
|
||||
tick_mark = Rectangle(int(WIDTH / 2) - 3, 10, 6, int(HEIGHT / 48))
|
||||
vector.rotate(tick_mark, 360 / 60.0 * a, int(WIDTH / 2), int(HEIGHT / 2))
|
||||
vector.translate(tick_mark, 0, 2)
|
||||
vector.draw(tick_mark)
|
||||
|
||||
for a in range(12):
|
||||
hour_mark = Rectangle(int(WIDTH / 2) - 5, 10, 10, int(HEIGHT / 10))
|
||||
vector.rotate(hour_mark, 360 / 12.0 * a, int(WIDTH / 2), int(HEIGHT / 2))
|
||||
vector.translate(hour_mark, 0, 2)
|
||||
vector.draw(hour_mark)
|
||||
|
||||
angle_second = second * 6
|
||||
second_hand_length = int(HEIGHT / 2) - int(HEIGHT / 8)
|
||||
second_hand = Polygon((-2, -second_hand_length), (-2, int(HEIGHT / 8)), (2, int(HEIGHT / 8)), (2, -second_hand_length))
|
||||
vector.rotate(second_hand, angle_second, 0, 0)
|
||||
vector.translate(second_hand, int(WIDTH / 2), int(HEIGHT / 2) + 5)
|
||||
|
||||
angle_minute = minute * 6
|
||||
angle_minute += second / 10.0
|
||||
minute_hand_length = int(HEIGHT / 2) - int(HEIGHT / 24)
|
||||
minute_hand = Polygon((-5, -minute_hand_length), (-10, int(HEIGHT / 16)), (10, int(HEIGHT / 16)), (5, -minute_hand_length))
|
||||
vector.rotate(minute_hand, angle_minute, 0, 0)
|
||||
vector.translate(minute_hand, int(WIDTH / 2), int(HEIGHT / 2) + 5)
|
||||
|
||||
angle_hour = (hour % 12) * 30
|
||||
angle_hour += minute / 2
|
||||
hour_hand_length = int(HEIGHT / 2) - int(HEIGHT / 8)
|
||||
hour_hand = Polygon((-5, -hour_hand_length), (-10, int(HEIGHT / 16)), (10, int(HEIGHT / 16)), (5, -hour_hand_length))
|
||||
vector.rotate(hour_hand, angle_hour, 0, 0)
|
||||
vector.translate(hour_hand, int(WIDTH / 2), int(HEIGHT / 2) + 5)
|
||||
|
||||
display.set_pen(GREY)
|
||||
|
||||
vector.draw(minute_hand)
|
||||
vector.draw(hour_hand)
|
||||
vector.draw(second_hand)
|
||||
|
||||
display.set_pen(BLACK)
|
||||
|
||||
for a in range(60):
|
||||
tick_mark = Rectangle(int(WIDTH / 2) - 3, 10, 6, int(HEIGHT / 48))
|
||||
vector.rotate(tick_mark, 360 / 60.0 * a, int(WIDTH / 2), int(HEIGHT / 2))
|
||||
vector.draw(tick_mark)
|
||||
|
||||
for a in range(12):
|
||||
hour_mark = Rectangle(int(WIDTH / 2) - 5, 10, 10, int(HEIGHT / 10))
|
||||
vector.rotate(hour_mark, 360 / 12.0 * a, int(WIDTH / 2), int(HEIGHT / 2))
|
||||
vector.draw(hour_mark)
|
||||
|
||||
vector.translate(minute_hand, 0, -5)
|
||||
vector.translate(hour_hand, 0, -5)
|
||||
vector.draw(minute_hand)
|
||||
vector.draw(hour_hand)
|
||||
|
||||
display.set_pen(RED)
|
||||
vector.translate(second_hand, 0, -5)
|
||||
vector.draw(second_hand)
|
||||
vector.draw(hub)
|
||||
|
||||
display.update()
|
||||
gc.collect()
|
||||
|
||||
t_end = time.ticks_ms()
|
||||
print(f"Took {t_end - t_start}ms")
|
|
@ -0,0 +1,119 @@
|
|||
import math
|
||||
import time
|
||||
from pimoroni_i2c import PimoroniI2C
|
||||
from breakout_as7262 import BreakoutAS7262
|
||||
from picographics import PicoGraphics, DISPLAY_PICO_W_EXPLORER, PEN_RGB332
|
||||
from picovector import PicoVector, Polygon, RegularPolygon, ANTIALIAS_X4
|
||||
|
||||
PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5}
|
||||
PINS_PICO_EXPLORER = {"sda": 20, "scl": 21}
|
||||
i2c = PimoroniI2C(**PINS_PICO_EXPLORER)
|
||||
|
||||
# Set up the AS7262 Spectrometer
|
||||
as7262 = BreakoutAS7262(i2c)
|
||||
as7262.set_gain(BreakoutAS7262.X16)
|
||||
as7262.set_measurement_mode(BreakoutAS7262.CONT_ROYGBR)
|
||||
as7262.set_illumination_current(BreakoutAS7262.MA12)
|
||||
as7262.set_indicator_current(BreakoutAS7262.MA4)
|
||||
as7262.set_leds(True, True)
|
||||
|
||||
# Set up the display
|
||||
display = PicoGraphics(DISPLAY_PICO_W_EXPLORER, pen_type=PEN_RGB332)
|
||||
display.set_backlight(0.8)
|
||||
|
||||
# Set up PicoVector
|
||||
vector = PicoVector(display)
|
||||
vector.set_antialiasing(ANTIALIAS_X4)
|
||||
|
||||
# Load an Alright Font
|
||||
result = vector.set_font("/AdvRe.af", 30)
|
||||
|
||||
WIDTH, HEIGHT = display.get_bounds()
|
||||
|
||||
CENTER_X = int(WIDTH / 2)
|
||||
CENTER_Y = int(HEIGHT / 2)
|
||||
|
||||
RADIUS = 90
|
||||
DEBUG = False
|
||||
|
||||
RED = display.create_pen(255, 0, 0)
|
||||
ORANGE = display.create_pen(255, 128, 0)
|
||||
YELLOW = display.create_pen(255, 255, 0)
|
||||
GREEN = display.create_pen(0, 255, 0)
|
||||
BLUE = display.create_pen(0, 0, 255)
|
||||
VIOLET = display.create_pen(255, 0, 255)
|
||||
|
||||
BLACK = display.create_pen(0, 0, 0)
|
||||
GREY = display.create_pen(128, 128, 128)
|
||||
WHITE = display.create_pen(255, 255, 255)
|
||||
|
||||
LABELS = ["R", "O", "Y", "G", "B", "V"]
|
||||
COLS = [RED, ORANGE, YELLOW, GREEN, BLUE, VIOLET]
|
||||
|
||||
|
||||
# Custom regular_polygon function to give each point its own "radius"
|
||||
def regular_polygon(o_x, o_y, radius, rotation):
|
||||
sides = 6
|
||||
angle = math.radians(360 / sides)
|
||||
rotation = math.radians(rotation)
|
||||
|
||||
points = []
|
||||
|
||||
for side in range(sides):
|
||||
current_angle = side * angle + rotation
|
||||
x = math.cos(current_angle) * radius[side]
|
||||
y = math.sin(current_angle) * radius[side]
|
||||
points.append((int(x) + o_x, int(y) + o_y))
|
||||
|
||||
return points
|
||||
|
||||
|
||||
lines = RegularPolygon(CENTER_X, CENTER_Y, 6, RADIUS)
|
||||
label_points = list(RegularPolygon(CENTER_X, CENTER_Y, 6, RADIUS * 0.7, -(360 / 12)))
|
||||
|
||||
|
||||
while True:
|
||||
# Clear to black
|
||||
display.set_pen(BLACK)
|
||||
display.clear()
|
||||
|
||||
# Add the title
|
||||
display.set_pen(WHITE)
|
||||
vector.text("Spectrograph", 5, -5)
|
||||
|
||||
# Get the spectrometer readings
|
||||
reading = list(as7262.read())
|
||||
|
||||
# Print out the readings
|
||||
if DEBUG:
|
||||
for i in range(6):
|
||||
print(f"{LABELS[i]}: {reading[i]:0.2f}", end=" ")
|
||||
print("")
|
||||
|
||||
# Draw the lines separating each section
|
||||
display.set_pen(GREY)
|
||||
for (x, y) in lines:
|
||||
display.line(CENTER_X, CENTER_Y, int(x), int(y))
|
||||
|
||||
# Scale readings for display
|
||||
for i in range(6):
|
||||
reading[i] = int(reading[i] / 3.0)
|
||||
reading[i] = min(reading[i], RADIUS)
|
||||
|
||||
# Create a 6 point polygon with each points distance from the center
|
||||
# scaled by the corresponding reading.
|
||||
points = regular_polygon(CENTER_X, CENTER_Y, reading, 0)
|
||||
|
||||
# Split the polygon into six triangles, one for each channel
|
||||
# draw each one, along with its corresponding label
|
||||
point_a = points[-1]
|
||||
for i in range(6):
|
||||
point_b = points[i]
|
||||
label_x, label_y = label_points[i]
|
||||
display.set_pen(COLS[i])
|
||||
vector.text(LABELS[i], int(label_x) - 5, int(label_y) - 20)
|
||||
vector.draw(Polygon(point_a, point_b, (CENTER_X, CENTER_Y)))
|
||||
point_a = point_b
|
||||
|
||||
display.update()
|
||||
time.sleep(1.0 / 60)
|
|
@ -0,0 +1,57 @@
|
|||
import time
|
||||
import math
|
||||
from picographics import PicoGraphics, DISPLAY_TUFTY_2040, PEN_RGB332
|
||||
|
||||
display = PicoGraphics(DISPLAY_TUFTY_2040, pen_type=PEN_RGB332)
|
||||
display.set_backlight(1.0)
|
||||
|
||||
WIDTH, HEIGHT = display.get_bounds()
|
||||
|
||||
BLACK = display.create_pen(0, 0, 0)
|
||||
|
||||
|
||||
def scaled_sine(start, finish, speed):
|
||||
s = math.sin(time.ticks_ms() / speed)
|
||||
s += 1 # -1 to +1 to 0 to 2
|
||||
s /= 2.0 # 0 to 2 to 0 to 1
|
||||
s *= finish - start
|
||||
s += start
|
||||
return s
|
||||
|
||||
|
||||
def regular_polygon(o_x, o_y, sides, radius, rotation):
|
||||
angle = math.radians(360 / sides)
|
||||
rotation = math.radians(rotation)
|
||||
|
||||
points = []
|
||||
|
||||
for side in range(sides):
|
||||
current_angle = side * angle + rotation
|
||||
x = math.cos(current_angle) * radius
|
||||
y = math.sin(current_angle) * radius
|
||||
points.append((int(x) + o_x, int(y) + o_y))
|
||||
|
||||
return points
|
||||
|
||||
|
||||
while True:
|
||||
sides = int(scaled_sine(3, 10, 500))
|
||||
rotation = time.ticks_ms() / 10
|
||||