QRCodeScreen.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. *
  3. * Copyright (c) 2020 Project CHIP Authors
  4. * Copyright (c) 2018 Nest Labs, Inc.
  5. * All rights reserved.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /**
  20. * @file QRCodeScreen.cpp
  21. *
  22. * Screen which displays a QR code.
  23. *
  24. */
  25. #include "QRCodeScreen.h"
  26. #if CONFIG_HAVE_DISPLAY
  27. // TODO organize includes below
  28. #include "esp_log.h"
  29. #include "esp_system.h"
  30. #include "esp_wifi.h"
  31. #include "freertos/FreeRTOS.h"
  32. #include "freertos/task.h"
  33. #include "qrcodegen.h"
  34. #include <lib/support/CodeUtils.h>
  35. #include <platform/CHIPDeviceLayer.h>
  36. #include <setup_payload/QRCodeSetupPayloadGenerator.h>
  37. // TODO need sensible library tag when put in library
  38. extern const char * TAG;
  39. namespace {
  40. constexpr int kVersion = 4;
  41. constexpr int kModuleSize = 4;
  42. constexpr int kBorderSize = 1;
  43. color_t qrCodeColor = TFT_LIGHTGREY;
  44. }; // namespace
  45. QRCodeScreen::QRCodeScreen(std::string text, std::string title) : title(title)
  46. {
  47. constexpr int qrCodeSize = qrcodegen_BUFFER_LEN_FOR_VERSION(kVersion);
  48. // TODO check text length against max size permitted, or maybe adjust version used accordingly
  49. std::vector<uint8_t> temp(qrCodeSize);
  50. qrCode.resize(qrCodeSize);
  51. if (!qrcodegen_encodeText(text.c_str(), temp.data(), qrCode.data(), qrcodegen_Ecc_LOW, kVersion, kVersion, qrcodegen_Mask_AUTO,
  52. true))
  53. {
  54. ESP_LOGE(TAG, "qrcodegen_encodeText() failed");
  55. qrCode.clear();
  56. }
  57. }
  58. void QRCodeScreen::Display()
  59. {
  60. if (qrCode.empty())
  61. {
  62. return;
  63. }
  64. const uint8_t * data = qrCode.data();
  65. const int size = qrcodegen_getSize(data);
  66. const int displaySize = (2 * kBorderSize + size) * kModuleSize;
  67. const int displayX = (DisplayWidth - displaySize) / 2;
  68. const int displayY = ScreenTitleSafeTop + ((DisplayHeight - ScreenTitleSafeTop - ScreenTitleSafeBottom) - displaySize) / 2;
  69. TFT_fillRect(displayX, displayY, displaySize, displaySize, qrCodeColor);
  70. for (int y = 0; y < size; ++y)
  71. {
  72. for (int x = 0; x < size; ++x)
  73. {
  74. if (qrcodegen_getModule(data, x, y))
  75. {
  76. TFT_fillRect(displayX + (kBorderSize + x) * kModuleSize, displayY + (kBorderSize + y) * kModuleSize, kModuleSize,
  77. kModuleSize, TFT_BLACK);
  78. }
  79. }
  80. }
  81. }
  82. #endif // CONFIG_HAVE_DISPLAY