mirror of
https://github.com/esphome/esphome.git
synced 2024-12-02 11:44:13 +01:00
a718ac7ee0
Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl>
55 lines
1.5 KiB
C++
55 lines
1.5 KiB
C++
#include "qr_code.h"
|
|
#include "esphome/components/display/display_buffer.h"
|
|
#include "esphome/core/color.h"
|
|
#include "esphome/core/log.h"
|
|
|
|
namespace esphome {
|
|
namespace qr_code {
|
|
|
|
static const char *const TAG = "qr_code";
|
|
|
|
void QrCode::dump_config() {
|
|
ESP_LOGCONFIG(TAG, "QR code:");
|
|
ESP_LOGCONFIG(TAG, " Value: '%s'", this->value_.c_str());
|
|
}
|
|
|
|
void QrCode::set_value(const std::string &value) {
|
|
this->value_ = value;
|
|
this->needs_update_ = true;
|
|
}
|
|
|
|
void QrCode::set_ecc(qrcodegen_Ecc ecc) {
|
|
this->ecc_ = ecc;
|
|
this->needs_update_ = true;
|
|
}
|
|
|
|
void QrCode::generate_qr_code() {
|
|
ESP_LOGV(TAG, "Generating QR code...");
|
|
uint8_t tempbuffer[qrcodegen_BUFFER_LEN_MAX];
|
|
|
|
if (!qrcodegen_encodeText(this->value_.c_str(), tempbuffer, this->qr_, this->ecc_, qrcodegen_VERSION_MIN,
|
|
qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true)) {
|
|
ESP_LOGE(TAG, "Failed to generate QR code");
|
|
}
|
|
}
|
|
|
|
void QrCode::draw(display::DisplayBuffer *buff, uint16_t x_offset, uint16_t y_offset, Color color, int scale) {
|
|
ESP_LOGV(TAG, "Drawing QR code at (%d, %d)", x_offset, y_offset);
|
|
|
|
if (this->needs_update_) {
|
|
this->generate_qr_code();
|
|
this->needs_update_ = false;
|
|
}
|
|
|
|
uint8_t qrcode_width = qrcodegen_getSize(this->qr_);
|
|
|
|
for (int y = 0; y < qrcode_width * scale; y++) {
|
|
for (int x = 0; x < qrcode_width * scale; x++) {
|
|
if (qrcodegen_getModule(this->qr_, x / scale, y / scale)) {
|
|
buff->draw_pixel_at(x_offset + x, y_offset + y, color);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} // namespace qr_code
|
|
} // namespace esphome
|