From 9194f7eb27f503e0f712d3e6407e48a16fc4ef99 Mon Sep 17 00:00:00 2001 From: Daniel Eisterhold Date: Wed, 27 Mar 2024 18:56:26 -0500 Subject: [PATCH] Add get_size method to QR Code header (#6430) --- esphome/components/qr_code/qr_code.cpp | 12 ++++++++++++ esphome/components/qr_code/qr_code.h | 2 ++ tests/components/qr_code/test.esp32-c3-idf.yaml | 7 +++++++ tests/components/qr_code/test.esp32-c3.yaml | 7 +++++++ tests/components/qr_code/test.esp32-idf.yaml | 7 +++++++ tests/components/qr_code/test.esp32.yaml | 7 +++++++ tests/components/qr_code/test.esp8266.yaml | 7 +++++++ tests/components/qr_code/test.rp2040.yaml | 7 +++++++ 8 files changed, 56 insertions(+) diff --git a/esphome/components/qr_code/qr_code.cpp b/esphome/components/qr_code/qr_code.cpp index aecf7628dc..b60e60a4b0 100644 --- a/esphome/components/qr_code/qr_code.cpp +++ b/esphome/components/qr_code/qr_code.cpp @@ -51,5 +51,17 @@ void QrCode::draw(display::Display *buff, uint16_t x_offset, uint16_t y_offset, } } } + +uint8_t QrCode::get_size() { + if (this->needs_update_) { + this->generate_qr_code(); + this->needs_update_ = false; + } + + uint8_t size = qrcodegen_getSize(this->qr_); + + return size; +} + } // namespace qr_code } // namespace esphome diff --git a/esphome/components/qr_code/qr_code.h b/esphome/components/qr_code/qr_code.h index d88e0aa09a..ab4c587b6d 100644 --- a/esphome/components/qr_code/qr_code.h +++ b/esphome/components/qr_code/qr_code.h @@ -24,6 +24,8 @@ class QrCode : public Component { void generate_qr_code(); + uint8_t get_size(); + protected: std::string value_; qrcodegen_Ecc ecc_; diff --git a/tests/components/qr_code/test.esp32-c3-idf.yaml b/tests/components/qr_code/test.esp32-c3-idf.yaml index 3e875c026c..63973b1aa2 100644 --- a/tests/components/qr_code/test.esp32-c3-idf.yaml +++ b/tests/components/qr_code/test.esp32-c3-idf.yaml @@ -11,6 +11,13 @@ display: cs_pin: 8 dc_pin: 9 reset_pin: 10 + lambda: |- + // Draw a QR code in the center of the screen + auto scale = 2; + auto size = id(homepage_qr).get_size() * scale; + auto x = (it.get_width() / 2) - (size / 2); + auto y = (it.get_height() / 2) - (size / 2); + it.qr_code(x, y, id(homepage_qr), Color(255,255,255), scale); qr_code: - id: homepage_qr diff --git a/tests/components/qr_code/test.esp32-c3.yaml b/tests/components/qr_code/test.esp32-c3.yaml index 3e875c026c..63973b1aa2 100644 --- a/tests/components/qr_code/test.esp32-c3.yaml +++ b/tests/components/qr_code/test.esp32-c3.yaml @@ -11,6 +11,13 @@ display: cs_pin: 8 dc_pin: 9 reset_pin: 10 + lambda: |- + // Draw a QR code in the center of the screen + auto scale = 2; + auto size = id(homepage_qr).get_size() * scale; + auto x = (it.get_width() / 2) - (size / 2); + auto y = (it.get_height() / 2) - (size / 2); + it.qr_code(x, y, id(homepage_qr), Color(255,255,255), scale); qr_code: - id: homepage_qr diff --git a/tests/components/qr_code/test.esp32-idf.yaml b/tests/components/qr_code/test.esp32-idf.yaml index b354a3f512..3e70d3258f 100644 --- a/tests/components/qr_code/test.esp32-idf.yaml +++ b/tests/components/qr_code/test.esp32-idf.yaml @@ -11,6 +11,13 @@ display: cs_pin: 12 dc_pin: 13 reset_pin: 21 + lambda: |- + // Draw a QR code in the center of the screen + auto scale = 2; + auto size = id(homepage_qr).get_size() * scale; + auto x = (it.get_width() / 2) - (size / 2); + auto y = (it.get_height() / 2) - (size / 2); + it.qr_code(x, y, id(homepage_qr), Color(255,255,255), scale); qr_code: - id: homepage_qr diff --git a/tests/components/qr_code/test.esp32.yaml b/tests/components/qr_code/test.esp32.yaml index b354a3f512..3e70d3258f 100644 --- a/tests/components/qr_code/test.esp32.yaml +++ b/tests/components/qr_code/test.esp32.yaml @@ -11,6 +11,13 @@ display: cs_pin: 12 dc_pin: 13 reset_pin: 21 + lambda: |- + // Draw a QR code in the center of the screen + auto scale = 2; + auto size = id(homepage_qr).get_size() * scale; + auto x = (it.get_width() / 2) - (size / 2); + auto y = (it.get_height() / 2) - (size / 2); + it.qr_code(x, y, id(homepage_qr), Color(255,255,255), scale); qr_code: - id: homepage_qr diff --git a/tests/components/qr_code/test.esp8266.yaml b/tests/components/qr_code/test.esp8266.yaml index dd0d75c472..3c304d7575 100644 --- a/tests/components/qr_code/test.esp8266.yaml +++ b/tests/components/qr_code/test.esp8266.yaml @@ -11,6 +11,13 @@ display: cs_pin: 5 dc_pin: 15 reset_pin: 16 + lambda: |- + // Draw a QR code in the center of the screen + auto scale = 2; + auto size = id(homepage_qr).get_size() * scale; + auto x = (it.get_width() / 2) - (size / 2); + auto y = (it.get_height() / 2) - (size / 2); + it.qr_code(x, y, id(homepage_qr), Color(255,255,255), scale); qr_code: - id: homepage_qr diff --git a/tests/components/qr_code/test.rp2040.yaml b/tests/components/qr_code/test.rp2040.yaml index b0e046d275..94cb772ba3 100644 --- a/tests/components/qr_code/test.rp2040.yaml +++ b/tests/components/qr_code/test.rp2040.yaml @@ -11,6 +11,13 @@ display: cs_pin: 20 dc_pin: 21 reset_pin: 22 + lambda: |- + // Draw a QR code in the center of the screen + auto scale = 2; + auto size = id(homepage_qr).get_size() * scale; + auto x = (it.get_width() / 2) - (size / 2); + auto y = (it.get_height() / 2) - (size / 2); + it.qr_code(x, y, id(homepage_qr), Color(255,255,255), scale); qr_code: - id: homepage_qr