2021-01-16 12:26:03 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-01-20 15:59:48 +00:00
|
|
|
#include <string>
|
2021-01-16 12:26:03 +00:00
|
|
|
#include <cstdint>
|
2021-01-18 07:58:19 +00:00
|
|
|
#include <algorithm>
|
2021-01-23 23:41:09 +00:00
|
|
|
#include <vector>
|
2022-03-29 15:13:14 +01:00
|
|
|
#include "libraries/bitmap_fonts/font6_data.hpp"
|
2021-01-16 12:26:03 +00:00
|
|
|
|
|
|
|
// a tiny little graphics library for our Pico products
|
|
|
|
// supports only 16-bit (565) RGB framebuffers
|
|
|
|
namespace pimoroni {
|
|
|
|
|
2022-05-25 17:22:20 +01:00
|
|
|
typedef uint8_t Pen;
|
2022-05-27 16:52:43 +01:00
|
|
|
typedef uint16_t RGB565;
|
2021-01-18 07:58:19 +00:00
|
|
|
|
2021-01-23 23:41:09 +00:00
|
|
|
struct Rect;
|
|
|
|
|
|
|
|
struct Point {
|
2021-01-18 07:58:19 +00:00
|
|
|
int32_t x = 0, y = 0;
|
|
|
|
|
2021-01-23 23:41:09 +00:00
|
|
|
Point() = default;
|
|
|
|
Point(int32_t x, int32_t y) : x(x), y(y) {}
|
2021-01-18 07:58:19 +00:00
|
|
|
|
2021-01-23 23:41:09 +00:00
|
|
|
inline Point& operator-= (const Point &a) { x -= a.x; y -= a.y; return *this; }
|
|
|
|
inline Point& operator+= (const Point &a) { x += a.x; y += a.y; return *this; }
|
2021-01-18 07:58:19 +00:00
|
|
|
|
2021-01-23 23:41:09 +00:00
|
|
|
Point clamp(const Rect &r) const;
|
2021-01-18 07:58:19 +00:00
|
|
|
};
|
|
|
|
|
2021-01-23 23:41:09 +00:00
|
|
|
struct Rect {
|
2021-01-18 07:58:19 +00:00
|
|
|
int32_t x = 0, y = 0, w = 0, h = 0;
|
|
|
|
|
2021-01-23 23:41:09 +00:00
|
|
|
Rect() = default;
|
|
|
|
Rect(int32_t x, int32_t y, int32_t w, int32_t h) : x(x), y(y), w(w), h(h) {}
|
|
|
|
Rect(const Point &tl, const Point &br) : x(tl.x), y(tl.y), w(br.x - tl.x), h(br.y - tl.y) {}
|
2021-01-18 07:58:19 +00:00
|
|
|
|
|
|
|
bool empty() const;
|
2021-01-23 23:41:09 +00:00
|
|
|
bool contains(const Point &p) const;
|
|
|
|
bool contains(const Rect &p) const;
|
|
|
|
bool intersects(const Rect &r) const;
|
|
|
|
Rect intersection(const Rect &r) const;
|
2021-01-18 07:58:19 +00:00
|
|
|
|
|
|
|
void inflate(int32_t v);
|
|
|
|
void deflate(int32_t v);
|
|
|
|
};
|
|
|
|
|
2021-01-16 12:26:03 +00:00
|
|
|
class PicoGraphics {
|
|
|
|
public:
|
2022-05-25 17:22:20 +01:00
|
|
|
Pen *frame_buffer;
|
2021-01-20 15:59:48 +00:00
|
|
|
|
2021-01-23 23:41:09 +00:00
|
|
|
Rect bounds;
|
|
|
|
Rect clip;
|
2021-01-18 07:58:19 +00:00
|
|
|
|
2021-01-23 23:41:09 +00:00
|
|
|
Pen pen;
|
2021-01-16 12:26:03 +00:00
|
|
|
|
2022-03-29 15:13:14 +01:00
|
|
|
const bitmap::font_t *font;
|
2021-01-16 12:26:03 +00:00
|
|
|
|
2022-05-28 18:08:47 +01:00
|
|
|
RGB565 palette[256];
|
|
|
|
uint8_t palette_status[256];
|
2022-05-27 16:52:43 +01:00
|
|
|
|
|
|
|
enum PaletteMode {
|
|
|
|
PaletteModeRGB332 = 0,
|
|
|
|
PaletteModeUSER = 1
|
|
|
|
};
|
|
|
|
|
2022-05-28 18:08:47 +01:00
|
|
|
enum PaletteStatus : uint8_t {
|
|
|
|
PaletteStatusReserved = 1,
|
|
|
|
PaletteStatusUsed = 2
|
|
|
|
};
|
|
|
|
|
2022-05-27 16:52:43 +01:00
|
|
|
PaletteMode palette_mode = PaletteModeRGB332;
|
2022-05-25 17:22:20 +01:00
|
|
|
|
2021-02-23 17:07:03 +00:00
|
|
|
public:
|
2022-05-25 17:22:20 +01:00
|
|
|
PicoGraphics(uint16_t width, uint16_t height, void *frame_buffer);
|
2022-03-29 15:13:14 +01:00
|
|
|
void set_font(const bitmap::font_t *font);
|
2021-01-23 23:41:09 +00:00
|
|
|
void set_pen(Pen p);
|
2022-05-27 16:52:43 +01:00
|
|
|
void set_palette_mode(PaletteMode mode);
|
2021-01-23 23:41:09 +00:00
|
|
|
|
2022-05-28 21:26:17 +01:00
|
|
|
[[deprecated("Use uint8_t create_pen(uint8_t, uint8_t, uint8_t).")]]
|
|
|
|
void set_pen(uint8_t r, uint8_t g, uint8_t b);
|
|
|
|
|
2022-05-29 13:16:31 +01:00
|
|
|
static constexpr Pen rgb_to_rgb332_index(uint8_t r, uint8_t g, uint8_t b) {
|
|
|
|
return (r & 0b11100000) | ((g & 0b11100000) >> 3) | ((b & 0b11000000) >> 6);
|
|
|
|
}
|
|
|
|
|
2022-05-27 16:52:43 +01:00
|
|
|
static constexpr RGB565 create_pen_rgb565(uint8_t r, uint8_t g, uint8_t b) {
|
2021-01-23 23:41:09 +00:00
|
|
|
uint16_t p = ((r & 0b11111000) << 8) |
|
2022-05-26 11:51:15 +01:00
|
|
|
((g & 0b11111100) << 3) |
|
|
|
|
((b & 0b11111000) >> 3);
|
|
|
|
|
|
|
|
return __builtin_bswap16(p);
|
|
|
|
}
|
|
|
|
|
2022-05-27 16:52:43 +01:00
|
|
|
static constexpr RGB565 create_pen_rgb332(uint8_t r, uint8_t g, uint8_t b) {
|
2022-05-26 11:51:15 +01:00
|
|
|
uint16_t p = ((r & 0b11100000) << 8) |
|
|
|
|
((g & 0b11100000) << 3) |
|
|
|
|
((b & 0b11000000) >> 3);
|
|
|
|
|
|
|
|
return __builtin_bswap16(p);
|
|
|
|
}
|
|
|
|
|
2022-05-28 01:00:56 +01:00
|
|
|
void set_dimensions(int width, int height) {
|
|
|
|
bounds.w = width;
|
|
|
|
bounds.h = height;
|
|
|
|
clip.w = width;
|
|
|
|
clip.h = height;
|
|
|
|
}
|
|
|
|
|
2022-05-27 16:52:43 +01:00
|
|
|
int create_pen(uint8_t r, uint8_t g, uint8_t b);
|
2022-05-26 11:51:15 +01:00
|
|
|
|
2022-05-28 18:08:47 +01:00
|
|
|
int reserve_palette();
|
2022-05-27 16:52:43 +01:00
|
|
|
void empty_palette();
|
|
|
|
void rgb332_palette();
|
2022-05-25 17:22:20 +01:00
|
|
|
|
2022-05-27 16:52:43 +01:00
|
|
|
int search_palette(RGB565 c);
|
|
|
|
int get_palette(uint8_t i);
|
|
|
|
void set_palette(uint8_t i, RGB565 c);
|
|
|
|
int put_palette(RGB565 c);
|
2021-01-16 12:26:03 +00:00
|
|
|
|
2021-01-23 23:41:09 +00:00
|
|
|
void set_clip(const Rect &r);
|
2021-01-18 07:58:19 +00:00
|
|
|
void remove_clip();
|
|
|
|
|
2021-01-23 23:41:09 +00:00
|
|
|
Pen* ptr(const Point &p);
|
|
|
|
Pen* ptr(const Rect &r);
|
|
|
|
Pen* ptr(int32_t x, int32_t y);
|
2021-01-16 12:26:03 +00:00
|
|
|
|
2021-01-18 07:58:19 +00:00
|
|
|
void clear();
|
2021-01-23 23:41:09 +00:00
|
|
|
void pixel(const Point &p);
|
|
|
|
void pixel_span(const Point &p, int32_t l);
|
|
|
|
void rectangle(const Rect &r);
|
|
|
|
void circle(const Point &p, int32_t r);
|
|
|
|
void character(const char c, const Point &p, uint8_t scale = 2);
|
|
|
|
void text(const std::string &t, const Point &p, int32_t wrap, uint8_t scale = 2);
|
2022-05-26 12:47:06 +01:00
|
|
|
int32_t measure_text(const std::string &t, uint8_t scale = 2);
|
2021-01-23 23:41:09 +00:00
|
|
|
void polygon(const std::vector<Point> &points);
|
|
|
|
void triangle(Point p1, Point p2, Point p3);
|
|
|
|
void line(Point p1, Point p2);
|
2021-01-16 12:26:03 +00:00
|
|
|
};
|
|
|
|
|
2022-04-18 19:40:05 +01:00
|
|
|
}
|