generated from WerWolv/Tesla-Template
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathelm_qr.cpp
More file actions
54 lines (43 loc) · 1.63 KB
/
elm_qr.cpp
File metadata and controls
54 lines (43 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "elm_qr.hpp"
#include "qrcodegen.h"
namespace {
constexpr auto QR_BUFFER_SIZE = qrcodegen_BUFFER_LEN_MAX;
constexpr auto SCALE = 4;
} // namespace
void ElmQr::generateQrCode(const std::string& text) {
std::vector<u8> qr_buffer(QR_BUFFER_SIZE);
std::vector<u8> temp_buffer(QR_BUFFER_SIZE);
if (!qrcodegen_encodeText(text.c_str(), temp_buffer.data(), qr_buffer.data(), qrcodegen_Ecc_MEDIUM, qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true)) {
return;
}
const auto size = qrcodegen_getSize(qr_buffer.data());
if (size <= 0) {
return;
}
qr_size = size;
qr_data = qr_buffer;
Element::invalidate();
}
tsl::elm::Element *ElmQr::requestFocus(tsl::elm::Element *oldFocus, tsl::FocusDirection direction) {
return oldFocus;
}
void ElmQr::draw(tsl::gfx::Renderer *renderer) {
if (!qr_size) {
return;
}
const auto white = tsl::Color(0xFF, 0xFF, 0xFF, 0xFF);
const auto black = tsl::Color(0x00, 0x00, 0x00, 0xFF);
for (int y = 0; y < qr_size; y++) {
for (int x = 0; x < qr_size; x++) {
const auto c = qrcodegen_getModule(qr_data.data(), x, y) ? white : black;
for (int yy = 0; yy < SCALE; yy++) {
for (int xx = 0; xx < SCALE; xx++) {
renderer->setPixel(this->getX() + x * SCALE + xx, this->getY() + y * SCALE + yy, c);
}
}
}
}
}
void ElmQr::layout(u16 parentX, u16 parentY, u16 parentWidth, u16 parentHeight) {
this->setBoundaries(tsl::cfg::FramebufferWidth / 2 - qr_size * SCALE / 2, this->getY() + 10, qr_size * SCALE, qr_size * SCALE);
}