Deluge Firmware 1.3.0
Build date: 2025.10.15
Loading...
Searching...
No Matches
canvas.h
1/*
2 * Copyright © 2024 Synthstrom Audible Limited
3 *
4 * This file is part of The Synthstrom Audible Deluge Firmware.
5 *
6 * The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software Foundation,
8 * either version 3 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this program.
15 * If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include "RZA1/cpu_specific.h"
21#include "definitions.h"
22#include "deluge/util/d_string.h"
23#include <cstring>
24#include <functional>
25#include <optional>
26#include <string_view>
27
28namespace deluge::hid::display {
29class OLED;
30struct Icon;
31
32namespace oled_canvas {
33
34enum BorderRadius : uint8_t {
35 SMALL = 0, //< 1px
36 BIG = 1 //< 2px
37};
38
39struct Point {
40 int32_t x;
41 int32_t y;
42 bool operator==(const Point& other) const noexcept { return x == other.x && y == other.y; }
43};
44
46 bool thick{false};
47 std::optional<uint8_t> min_x{std::nullopt};
48 std::optional<uint8_t> max_x{std::nullopt};
49 std::optional<std::function<void(Point)>> point_callback{std::nullopt};
50};
51
52class Canvas {
53public:
54 Canvas() = default;
55 ~Canvas() = default;
56
57 Canvas(Canvas const& other) = delete;
58 Canvas(Canvas&& other) = delete;
59
60 Canvas& operator=(Canvas const& other) = delete;
61 Canvas& operator=(Canvas&& other) = delete;
62
65
67 void clear() {
68 // Takes about 1 fast-timer tick (whereas entire rendering takes around 8 to 15).
69 // So, not worth trying to use DMA here or anything.
70 memset(image_, 0, sizeof(image_));
71 };
72
79 void clearAreaExact(int32_t minX, int32_t minY, int32_t maxX, int32_t maxY);
80
82 void drawPixel(int32_t x, int32_t y);
83
85 void clearPixel(int32_t x, int32_t y);
86
88 void invertPixel(int32_t x, int32_t y);
89
95 void drawHorizontalLine(int32_t pixelY, int32_t startX, int32_t endX);
96
102 void drawVerticalLine(int32_t pixelX, int32_t startY, int32_t endY);
103
110 void drawLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, const DrawLineOptions& options = {});
111
118 void drawRectangle(int32_t minX, int32_t minY, int32_t maxX, int32_t maxY);
119
126 void drawRectangleRounded(int32_t minX, int32_t minY, int32_t maxX, int32_t maxY, BorderRadius radius = SMALL);
127
138 void drawString(std::string_view str, int32_t pixelX, int32_t pixelY, int32_t textWidth, int32_t textHeight,
139 int32_t scrollPos = 0, int32_t endX = OLED_MAIN_WIDTH_PIXELS, bool useTextWidth = false);
140
150 void drawStringCentred(char const* string, int32_t pixelY, int32_t textWidth, int32_t textHeight,
151 int32_t centrePos = OLED_MAIN_WIDTH_PIXELS / 2);
152
161 void drawStringCentered(char const* string, int32_t startX, int32_t startY, int32_t textSpacingX,
162 int32_t textSpacingY, int32_t totalWidth);
163
172 void drawStringCentered(StringBuf& stringBuf, int32_t startX, int32_t startY, int32_t textSpacingX,
173 int32_t textSpacingY, int32_t totalWidth);
174
181 void drawStringCentredShrinkIfNecessary(char const* string, int32_t pixelY, int32_t textWidth, int32_t textHeight);
182
190 void drawStringAlignRight(char const* string, int32_t pixelY, int32_t textWidth, int32_t textHeight,
191 int32_t rightPos = OLED_MAIN_WIDTH_PIXELS);
192
194 void drawChar(uint8_t theChar, int32_t pixelX, int32_t pixelY, int32_t textWidth, int32_t textHeight,
195 int32_t scrollPos = 0, int32_t endX = OLED_MAIN_WIDTH_PIXELS);
196
200 int32_t getCharIndex(uint8_t theChar);
201
206 int32_t getCharWidthInPixels(uint8_t theChar, int32_t textHeight);
207
213 int32_t getCharSpacingInPixels(uint8_t theChar, int32_t textHeight, bool isLastChar);
214
219 int32_t getStringWidthInPixels(char const* string, int32_t textHeight);
220
231 void drawGraphicMultiLine(uint8_t const* graphic, int32_t startX, int32_t startY, int32_t width, int32_t height = 8,
232 int32_t numBytesTall = 1, bool reversed = false);
239 void drawIcon(const Icon& icon, int32_t x, int32_t y, bool reversed = false);
240
248 void drawIconCentered(const Icon& icon, int32_t startX, int32_t totalWidth, int32_t y, bool reversed = false);
249
253 void drawScreenTitle(std::string_view text, bool drawSeparator = true);
254
261 void invertArea(int32_t xMin, int32_t width, int32_t startY, int32_t endY);
262
269 void invertAreaRounded(int32_t xMin, int32_t width, int32_t startY, int32_t endY, BorderRadius radius = SMALL);
270
277 void invertLeftEdgeForMenuHighlighting(int32_t xMin, int32_t width, int32_t startY, int32_t endY);
278
280
282 static constexpr uint32_t kImageHeight = OLED_MAIN_HEIGHT_PIXELS >> 3;
284 static constexpr uint32_t kImageWidth = OLED_MAIN_WIDTH_PIXELS;
285
286 using ImageStore = uint8_t[kImageHeight][kImageWidth];
287
289 ImageStore& hackGetImageStore() { return image_; }
290
291private:
292 [[gnu::aligned(CACHE_LINE_SIZE)]] uint8_t image_[kImageHeight][kImageWidth];
293};
294} // namespace oled_canvas
295} // namespace deluge::hid::display
Definition d_stringbuf.h:16
void drawString(std::string_view str, int32_t pixelX, int32_t pixelY, int32_t textWidth, int32_t textHeight, int32_t scrollPos=0, int32_t endX=OLED_MAIN_WIDTH_PIXELS, bool useTextWidth=false)
Definition canvas.cpp:198
void clearPixel(int32_t x, int32_t y)
Clear a single pixel.
Definition canvas.cpp:76
void drawIconCentered(const Icon &icon, int32_t startX, int32_t totalWidth, int32_t y, bool reversed=false)
Definition canvas.cpp:608
void drawScreenTitle(std::string_view text, bool drawSeparator=true)
Definition canvas.cpp:616
int32_t getStringWidthInPixels(char const *string, int32_t textHeight)
Definition canvas.cpp:515
void drawChar(uint8_t theChar, int32_t pixelX, int32_t pixelY, int32_t textWidth, int32_t textHeight, int32_t scrollPos=0, int32_t endX=OLED_MAIN_WIDTH_PIXELS)
Draw a single character.
Definition canvas.cpp:350
static constexpr uint32_t kImageHeight
Height of the image in bytes.
Definition canvas.h:282
void drawIcon(const Icon &icon, int32_t x, int32_t y, bool reversed=false)
Definition canvas.cpp:605
static constexpr uint32_t kImageWidth
Width of the image in pixels.
Definition canvas.h:284
void drawStringCentered(char const *string, int32_t startX, int32_t startY, int32_t textSpacingX, int32_t textSpacingY, int32_t totalWidth)
Definition canvas.cpp:272
void drawPixel(int32_t x, int32_t y)
Set a single pixel.
Definition canvas.cpp:71
void invertPixel(int32_t x, int32_t y)
Invert a single pixel.
Definition canvas.cpp:81
void clear()
Clear the entire image.
Definition canvas.h:67
void drawHorizontalLine(int32_t pixelY, int32_t startX, int32_t endX)
Definition canvas.cpp:86
void drawStringAlignRight(char const *string, int32_t pixelY, int32_t textWidth, int32_t textHeight, int32_t rightPos=OLED_MAIN_WIDTH_PIXELS)
Definition canvas.cpp:340
int32_t getCharSpacingInPixels(uint8_t theChar, int32_t textHeight, bool isLastChar)
Definition canvas.cpp:477
int32_t getCharWidthInPixels(uint8_t theChar, int32_t textHeight)
Definition canvas.cpp:445
void drawStringCentred(char const *string, int32_t pixelY, int32_t textWidth, int32_t textHeight, int32_t centrePos=OLED_MAIN_WIDTH_PIXELS/2)
Definition canvas.cpp:264
void clearAreaExact(int32_t minX, int32_t minY, int32_t maxX, int32_t maxY)
Definition canvas.cpp:30
void invertArea(int32_t xMin, int32_t width, int32_t startY, int32_t endY)
Definition canvas.cpp:626
void drawGraphicMultiLine(uint8_t const *graphic, int32_t startX, int32_t startY, int32_t width, int32_t height=8, int32_t numBytesTall=1, bool reversed=false)
Definition canvas.cpp:529
void invertLeftEdgeForMenuHighlighting(int32_t xMin, int32_t width, int32_t startY, int32_t endY)
inverts just the left edge
Definition canvas.cpp:692
void drawStringCentredShrinkIfNecessary(char const *string, int32_t pixelY, int32_t textWidth, int32_t textHeight)
Definition canvas.cpp:305
void drawLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, const DrawLineOptions &options={})
Definition canvas.cpp:130
int32_t getCharIndex(uint8_t theChar)
Definition canvas.cpp:423
void drawVerticalLine(int32_t pixelX, int32_t startY, int32_t endY)
Definition canvas.cpp:97
void drawRectangleRounded(int32_t minX, int32_t minY, int32_t maxX, int32_t maxY, BorderRadius radius=SMALL)
Definition canvas.cpp:182
void invertAreaRounded(int32_t xMin, int32_t width, int32_t startY, int32_t endY, BorderRadius radius=SMALL)
Definition canvas.cpp:652
void drawRectangle(int32_t minX, int32_t minY, int32_t maxX, int32_t maxY)
Definition canvas.cpp:174
ImageStore & hackGetImageStore()
XXX: DO NOT USE THIS OUTSIDE OF THE CORE OLED CODE.
Definition canvas.h:289