ISF15ACP4 Driver 1.0.0
API reference
Loading...
Searching...
No Matches
isf15acp4_canvas.ipp
Go to the documentation of this file.
1#ifndef ISF15ACP4_CANVAS_HEADER_INCLUDED
2#error "isf15acp4_canvas.ipp must be included from isf15acp4_canvas.hpp"
3#endif
4
5#include "isf15acp4_font.hpp"
6
7namespace isf15acp4 {
8
9inline Canvas::Canvas(ProductVariant variant) noexcept : geometry_(GetGeometry(variant)) {
10 Clear(colors::Black);
11}
12
13inline void Canvas::Clear(Color565 color) noexcept {
14 for (std::size_t i = 0; i < geometry_.framebuffer_bytes / 2; ++i) {
15 buffer_[i] = color;
16 }
17}
18
19inline void Canvas::SetPixel(uint8_t x, uint8_t y, Color565 color) noexcept {
20 if (x >= geometry_.width || y >= geometry_.height) {
21 return;
22 }
23 buffer_[static_cast<std::size_t>(y) * geometry_.width + x] = color;
24}
25
26inline Color565 Canvas::GetPixel(uint8_t x, uint8_t y) const noexcept {
27 if (x >= geometry_.width || y >= geometry_.height) {
28 return colors::Black;
29 }
30 return buffer_[static_cast<std::size_t>(y) * geometry_.width + x];
31}
32
33inline void Canvas::DrawHLine(uint8_t x0, uint8_t x1, uint8_t y, Color565 color) noexcept {
34 if (x0 > x1) {
35 const uint8_t tmp = x0;
36 x0 = x1;
37 x1 = tmp;
38 }
39 for (uint8_t x = x0; x <= x1; ++x) {
40 SetPixel(x, y, color);
41 }
42}
43
44inline void Canvas::DrawVLine(uint8_t x, uint8_t y0, uint8_t y1, Color565 color) noexcept {
45 if (y0 > y1) {
46 const uint8_t tmp = y0;
47 y0 = y1;
48 y1 = tmp;
49 }
50 for (uint8_t y = y0; y <= y1; ++y) {
51 SetPixel(x, y, color);
52 }
53}
54
55inline void Canvas::DrawRect(const Rect& rect, Color565 color) noexcept {
56 DrawHLine(rect.x0, rect.x1, rect.y0, color);
57 DrawHLine(rect.x0, rect.x1, rect.y1, color);
58 DrawVLine(rect.x0, rect.y0, rect.y1, color);
59 DrawVLine(rect.x1, rect.y0, rect.y1, color);
60}
61
62inline void Canvas::FillRect(const Rect& rect, Color565 color) noexcept {
63 for (uint8_t y = rect.y0; y <= rect.y1; ++y) {
64 DrawHLine(rect.x0, rect.x1, y, color);
65 }
66}
67
68inline void Canvas::DrawChar(uint8_t x, uint8_t y, char ch, Color565 color, Color565 bg) noexcept {
69 uint8_t cols[5] = {};
70 if (!font::GetGlyph(ch, cols)) {
71 return;
72 }
73
74 for (uint8_t col = 0; col < font::kWidth; ++col) {
75 for (uint8_t row = 0; row < font::kHeight; ++row) {
76 const bool on = (cols[col] >> row) & 0x01;
77 SetPixel(static_cast<uint8_t>(x + col), static_cast<uint8_t>(y + row), on ? color : bg);
78 }
79 }
80}
81
82inline void Canvas::DrawString(uint8_t x, uint8_t y, const char* text, Color565 color,
83 Color565 bg) noexcept {
84 if (text == nullptr) {
85 return;
86 }
87
88 uint8_t cursor = x;
89 while (*text != '\0') {
90 DrawChar(cursor, y, *text, color, bg);
91 cursor = static_cast<uint8_t>(cursor + font::kWidth + font::kSpacing);
92 ++text;
93 }
94}
95
96inline void Canvas::BlitRgb565(uint8_t x, uint8_t y, uint8_t src_width, uint8_t src_height,
97 const Color565* src) noexcept {
98 if (src == nullptr) {
99 return;
100 }
101
102 for (uint8_t row = 0; row < src_height; ++row) {
103 for (uint8_t col = 0; col < src_width; ++col) {
104 SetPixel(static_cast<uint8_t>(x + col), static_cast<uint8_t>(y + row),
105 src[static_cast<std::size_t>(row) * src_width + col]);
106 }
107 }
108}
109
110} // namespace isf15acp4
void SetPixel(uint8_t x, uint8_t y, Color565 color) noexcept
Set one pixel if in bounds.
Definition isf15acp4_canvas.ipp:19
Color565 GetPixel(uint8_t x, uint8_t y) const noexcept
Get one pixel; returns black when out of bounds.
Definition isf15acp4_canvas.ipp:26
void FillRect(const Rect &rect, Color565 color) noexcept
Fill a solid rectangle.
Definition isf15acp4_canvas.ipp:62
void Clear(Color565 color) noexcept
Clear the framebuffer to a solid color.
Definition isf15acp4_canvas.ipp:13
void BlitRgb565(uint8_t x, uint8_t y, uint8_t src_width, uint8_t src_height, const Color565 *src) noexcept
Blit a contiguous RGB565 buffer into the canvas.
Definition isf15acp4_canvas.ipp:96
void DrawVLine(uint8_t x, uint8_t y0, uint8_t y1, Color565 color) noexcept
Draw a vertical line inclusive of both endpoints.
Definition isf15acp4_canvas.ipp:44
void DrawString(uint8_t x, uint8_t y, const char *text, Color565 color, Color565 bg=colors::Black) noexcept
Draw a null-terminated string.
Definition isf15acp4_canvas.ipp:82
void DrawRect(const Rect &rect, Color565 color) noexcept
Draw rectangle outline.
Definition isf15acp4_canvas.ipp:55
Canvas(ProductVariant variant=ProductVariant::Isf15acp4) noexcept
Construct a canvas for the given product geometry.
Definition isf15acp4_canvas.ipp:9
void DrawChar(uint8_t x, uint8_t y, char ch, Color565 color, Color565 bg=colors::Black) noexcept
Draw a single character using the built-in 5x7 font.
Definition isf15acp4_canvas.ipp:68
void DrawHLine(uint8_t x0, uint8_t x1, uint8_t y, Color565 color) noexcept
Draw a horizontal line inclusive of both endpoints.
Definition isf15acp4_canvas.ipp:33
Compact 5x7 bitmap font for SmartDisplay text rendering.
constexpr Color565 Black
Definition isf15acp4_types.hpp:116
constexpr uint8_t kWidth
Definition isf15acp4_font.hpp:13
constexpr uint8_t kHeight
Definition isf15acp4_font.hpp:14
bool GetGlyph(char ch, uint8_t out_cols[5]) noexcept
Get glyph columns for an ASCII character.
Definition isf15acp4_font_data.cpp:110
constexpr uint8_t kSpacing
Definition isf15acp4_font.hpp:15
Definition isf15acp4.hpp:19
ProductVariant
Supported NKK SmartDisplay OLED product variants.
Definition isf15acp4_types.hpp:19
uint16_t Color565
16-bit RGB565 color value (R5G6B5).
Definition isf15acp4_types.hpp:50
constexpr const DisplayGeometry & GetGeometry(ProductVariant variant) noexcept
Get geometry metadata for a product variant.
Definition isf15acp4_types.hpp:77
Axis-aligned rectangle in display coordinates (inclusive bounds).
Definition isf15acp4_types.hpp:96