ESPHome
2025.5.0
Loading...
Searching...
No Matches
esphome
components
qr_code
qr_code.cpp
Go to the documentation of this file.
1
#include "
qr_code.h
"
2
#include "
esphome/components/display/display.h
"
3
#include "
esphome/core/color.h
"
4
#include "
esphome/core/log.h
"
5
6
namespace
esphome
{
7
namespace
qr_code {
8
9
static
const
char
*
const
TAG =
"qr_code"
;
10
11
void
QrCode::dump_config
() {
12
ESP_LOGCONFIG(TAG,
"QR code:"
);
13
ESP_LOGCONFIG(TAG,
" Value: '%s'"
, this->
value_
.c_str());
14
}
15
16
void
QrCode::set_value
(
const
std::string &value) {
17
this->
value_
= value;
18
this->
needs_update_
=
true
;
19
}
20
21
void
QrCode::set_ecc
(qrcodegen_Ecc ecc) {
22
this->
ecc_
= ecc;
23
this->
needs_update_
=
true
;
24
}
25
26
void
QrCode::generate_qr_code
() {
27
ESP_LOGV(TAG,
"Generating QR code..."
);
28
uint8_t tempbuffer[qrcodegen_BUFFER_LEN_MAX];
29
30
if
(!qrcodegen_encodeText(this->
value_
.c_str(), tempbuffer, this->qr_, this->ecc_, qrcodegen_VERSION_MIN,
31
qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO,
true
)) {
32
ESP_LOGE(TAG,
"Failed to generate QR code"
);
33
}
34
}
35
36
void
QrCode::draw
(
display::Display
*buff, uint16_t x_offset, uint16_t y_offset,
Color
color,
int
scale) {
37
ESP_LOGV(TAG,
"Drawing QR code at (%d, %d)"
, x_offset, y_offset);
38
39
if
(this->
needs_update_
) {
40
this->
generate_qr_code
();
41
this->
needs_update_
=
false
;
42
}
43
44
uint8_t qrcode_width = qrcodegen_getSize(this->
qr_
);
45
46
for
(
int
y
= 0;
y
< qrcode_width * scale;
y
++) {
47
for
(
int
x
= 0;
x
< qrcode_width * scale;
x
++) {
48
if
(qrcodegen_getModule(this->
qr_
,
x
/ scale,
y
/ scale)) {
49
buff->
draw_pixel_at
(x_offset +
x
, y_offset +
y
, color);
50
}
51
}
52
}
53
}
54
55
uint8_t
QrCode::get_size
() {
56
if
(this->
needs_update_
) {
57
this->
generate_qr_code
();
58
this->
needs_update_
=
false
;
59
}
60
61
uint8_t size = qrcodegen_getSize(this->
qr_
);
62
63
return
size;
64
}
65
66
}
// namespace qr_code
67
}
// namespace esphome
esphome::display::Display
Definition
display.h:208
esphome::display::Display::draw_pixel_at
void draw_pixel_at(int x, int y)
Set a single pixel at the specified coordinates to default color.
Definition
display.h:226
esphome::qr_code::QrCode::value_
std::string value_
Definition
qr_code.h:30
esphome::qr_code::QrCode::qr_
uint8_t qr_[qrcodegen_BUFFER_LEN_MAX]
Definition
qr_code.h:33
esphome::qr_code::QrCode::draw
void draw(display::Display *buff, uint16_t x_offset, uint16_t y_offset, Color color, int scale)
Definition
qr_code.cpp:36
esphome::qr_code::QrCode::set_ecc
void set_ecc(qrcodegen_Ecc ecc)
Definition
qr_code.cpp:21
esphome::qr_code::QrCode::set_value
void set_value(const std::string &value)
Definition
qr_code.cpp:16
esphome::qr_code::QrCode::needs_update_
bool needs_update_
Definition
qr_code.h:32
esphome::qr_code::QrCode::dump_config
void dump_config() override
Definition
qr_code.cpp:11
esphome::qr_code::QrCode::get_size
uint8_t get_size()
Definition
qr_code.cpp:55
esphome::qr_code::QrCode::ecc_
qrcodegen_Ecc ecc_
Definition
qr_code.h:31
esphome::qr_code::QrCode::generate_qr_code
void generate_qr_code()
Definition
qr_code.cpp:26
color.h
display.h
log.h
esphome
Providing packet encoding functions for exchanging data with a remote host.
Definition
a01nyub.cpp:7
qr_code.h
esphome::Color
Definition
color.h:10
x
uint16_t x
Definition
tt21100.cpp:5
y
uint16_t y
Definition
tt21100.cpp:6
Generated by
1.12.0