Ersa Wearable Platform
Embedded smartwatch firmware and application framework
Loading...
Searching...
No Matches
mock_display.h
Go to the documentation of this file.
1#pragma once
2
3#include "ersa/hal/display.h"
4#include <string>
5#include <vector>
6
7namespace ersa {
8namespace test {
9
10class MockDisplay : public hal::IDisplay {
11public:
12 static constexpr int16_t W = 200;
13 static constexpr int16_t H = 200;
14
15 MockDisplay() : buffer_(W * H, 0) {}
16
17 Result<void> init() override {
18 powered_ = true;
19 return Result<void>();
20 }
21
22 int16_t width() const override { return W; }
23 int16_t height() const override { return H; }
24
25 void clear(hal::Color color = hal::Color::Black) override {
26 std::fill(buffer_.begin(), buffer_.end(), (color == hal::Color::White) ? 1 : 0);
27 }
28
29 void drawPixel(int16_t x, int16_t y, hal::Color color) override {
30 if (x >= 0 && x < W && y >= 0 && y < H) {
31 buffer_[y * W + x] = (color == hal::Color::White) ? 1 : 0;
32 }
33 }
34
35 void drawLine(int16_t x1, int16_t y1, int16_t x2, int16_t y2, hal::Color color) override {
36 (void)x1; (void)y1; (void)x2; (void)y2; (void)color;
38 }
39
40 void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, hal::Color color) override {
41 for (int16_t j = y; j < y + h; ++j) {
42 for (int16_t i = x; i < x + w; ++i) {
43 drawPixel(i, j, color);
44 }
45 }
46 }
47
48 void drawFastHLine(int16_t x, int16_t y, int16_t w, hal::Color color) override { (void)x; (void)y; (void)w; (void)color; }
49 void drawFastVLine(int16_t x, int16_t y, int16_t h, hal::Color color) override { (void)x; (void)y; (void)h; (void)color; }
50 void drawCircle(int16_t x, int16_t y, int16_t r, hal::Color color) override { (void)x; (void)y; (void)r; (void)color; }
51 void fillCircle(int16_t x, int16_t y, int16_t r, hal::Color color) override { (void)x; (void)y; (void)r; (void)color; }
52 void drawRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, hal::Color color) override { (void)x; (void)y; (void)w; (void)h; (void)r; (void)color; }
53 void fillRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, hal::Color color) override { (void)x; (void)y; (void)w; (void)h; (void)r; (void)color; }
54 void setFont(hal::FontFace face) override { font_ = face; }
55 void setTextSize(uint8_t size) override { textSize_ = size; }
56 void setTextColor(hal::Color color) override { textColor_ = color; }
57 void setTextWrap(bool wrap) override { textWrap_ = wrap; }
58 void setCursor(int16_t x, int16_t y) override { cursorX_ = x; cursorY_ = y; }
59 size_t print(const char* text) override { lastText_ = text ? text : ""; return lastText_.size(); }
60 size_t print(uint8_t value) override { lastNumber_ = value; return 1; }
61 size_t print(int32_t value) override { lastNumber_ = value; return 1; }
62 size_t print(uint32_t value) override { lastNumber_ = static_cast<int32_t>(value); return 1; }
63 void getTextBounds(const char* text, int16_t x, int16_t y,
64 int16_t* x1, int16_t* y1, uint16_t* w, uint16_t* h) override {
65 (void)x; (void)y;
66 if (x1) *x1 = 0;
67 if (y1) *y1 = -8;
68 if (w) *w = static_cast<uint16_t>(text ? std::char_traits<char>::length(text) * 6 : 0);
69 if (h) *h = 8;
70 }
71
72 void refresh(bool full = false) override {
73 if (full) fullRefreshes_++;
74 else partialRefreshes_++;
75 powered_ = true;
76 }
77
78 void refreshRect(const Rect& rect) override {
79 (void)rect;
81 powered_ = true;
82 }
83
84 bool isBusy() const override { return false; }
85 void setBusyCallback(hal::DisplayBusyCallback cb, void* userData = nullptr) override {
86 (void)cb; (void)userData;
87 }
88
89 void powerOff() override { powered_ = false; }
90 void powerOn() override { powered_ = true; }
91 bool isPowered() const override { return powered_; }
92
93 const uint8_t* getBuffer() const override { return buffer_.data(); }
94 uint8_t* getBuffer() override { return buffer_.data(); }
95
96 hal::Color getPixel(int16_t x, int16_t y) const {
97 if (x >= 0 && x < W && y >= 0 && y < H) {
98 return buffer_[y * W + x] ? hal::Color::White : hal::Color::Black;
99 }
100 return hal::Color::Black;
101 }
102
103 uint32_t fullRefreshes_{0};
104 uint32_t partialRefreshes_{0};
105 bool powered_{true};
106 uint32_t drawLineCalls_{0};
109 uint8_t textSize_{1};
110 bool textWrap_{true};
111 int16_t cursorX_{0}, cursorY_{0};
112 std::string lastText_;
113 int32_t lastNumber_{0};
114
115private:
116 std::vector<uint8_t> buffer_;
117};
118
119} // namespace test
120} // namespace ersa
Definition types.h:52
Definition display.h:40
Definition mock_display.h:10
bool isBusy() const override
Return whether the panel is still processing a refresh.
Definition mock_display.h:84
void refreshRect(const Rect &rect) override
Definition mock_display.h:78
MockDisplay()
Definition mock_display.h:15
hal::Color textColor_
Definition mock_display.h:108
void setTextColor(hal::Color color) override
Definition mock_display.h:56
void drawPixel(int16_t x, int16_t y, hal::Color color) override
Definition mock_display.h:29
void drawFastVLine(int16_t x, int16_t y, int16_t h, hal::Color color) override
Definition mock_display.h:49
uint8_t * getBuffer() override
Definition mock_display.h:94
void drawFastHLine(int16_t x, int16_t y, int16_t w, hal::Color color) override
Definition mock_display.h:48
uint8_t textSize_
Definition mock_display.h:109
void getTextBounds(const char *text, int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) override
Definition mock_display.h:63
void drawCircle(int16_t x, int16_t y, int16_t r, hal::Color color) override
Definition mock_display.h:50
void setTextSize(uint8_t size) override
Definition mock_display.h:55
uint32_t fullRefreshes_
Definition mock_display.h:103
bool isPowered() const override
Definition mock_display.h:91
int16_t cursorY_
Definition mock_display.h:111
void drawRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, hal::Color color) override
Draw an unfilled rectangle with rounded corners.
Definition mock_display.h:52
hal::Color getPixel(int16_t x, int16_t y) const
Definition mock_display.h:96
size_t print(int32_t value) override
Draw a signed 32-bit value and return the number of characters consumed.
Definition mock_display.h:61
int16_t width() const override
Definition mock_display.h:22
void clear(hal::Color color=hal::Color::Black) override
Definition mock_display.h:25
void drawLine(int16_t x1, int16_t y1, int16_t x2, int16_t y2, hal::Color color) override
Definition mock_display.h:35
void powerOn() override
Definition mock_display.h:90
std::string lastText_
Definition mock_display.h:112
std::vector< uint8_t > buffer_
Definition mock_display.h:116
void fillCircle(int16_t x, int16_t y, int16_t r, hal::Color color) override
Definition mock_display.h:51
const uint8_t * getBuffer() const override
Definition mock_display.h:93
void setCursor(int16_t x, int16_t y) override
Definition mock_display.h:58
size_t print(const char *text) override
Definition mock_display.h:59
uint32_t drawLineCalls_
Definition mock_display.h:106
uint32_t partialRefreshes_
Definition mock_display.h:104
void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, hal::Color color) override
Definition mock_display.h:40
bool textWrap_
Definition mock_display.h:110
void fillRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, hal::Color color) override
Draw a filled rectangle with rounded corners.
Definition mock_display.h:53
static constexpr int16_t H
Definition mock_display.h:13
void setBusyCallback(hal::DisplayBusyCallback cb, void *userData=nullptr) override
Definition mock_display.h:85
bool powered_
Definition mock_display.h:105
static constexpr int16_t W
Definition mock_display.h:12
int16_t cursorX_
Definition mock_display.h:111
size_t print(uint32_t value) override
Draw an unsigned 32-bit value and return the number of characters consumed.
Definition mock_display.h:62
void powerOff() override
Definition mock_display.h:89
hal::FontFace font_
Definition mock_display.h:107
Result< void > init() override
Definition mock_display.h:17
void refresh(bool full=false) override
Definition mock_display.h:72
size_t print(uint8_t value) override
Draw an unsigned byte value and return the number of characters consumed.
Definition mock_display.h:60
void setFont(hal::FontFace face) override
Definition mock_display.h:54
int16_t height() const override
Definition mock_display.h:23
int32_t lastNumber_
Definition mock_display.h:113
void setTextWrap(bool wrap) override
Definition mock_display.h:57
void(*)(void *userData) DisplayBusyCallback
Definition display.h:16
Color
Definition display.h:10
FontFace
Definition display.h:20
Definition application.h:10
size_t size
Definition ota_service.cpp:47
Definition types.h:121