u8g2_qrcode.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "u8g2/u8g2.h"
  2. #include "qrcode/qrcode.h"
  3. #include "flipper.h"
  4. void u8g2_DrawPixelSize(u8g2_t* u8g2, uint8_t x, uint8_t y, uint8_t size) {
  5. for(uint8_t px = 0; px < size; px++) {
  6. for(uint8_t py = 0; py < size; py++) {
  7. u8g2_DrawPixel(u8g2, x + px, y + py);
  8. }
  9. }
  10. }
  11. void u8g2_qrcode(void* p) {
  12. FuriRecordSubscriber* log = get_default_log();
  13. // open record
  14. FuriRecordSubscriber* fb_record =
  15. furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL);
  16. // Allocate a chunk of memory to store the QR code
  17. // https://github.com/ricmoo/QRCode
  18. // we init version 1, 21x21 px, 16 alphanumeric chars with
  19. // QUARTILE error correction
  20. const uint8_t qr_version = 1;
  21. const uint8_t qr_error_correction = ECC_QUARTILE;
  22. const uint8_t qr_x = 32;
  23. const uint8_t qr_y = 0;
  24. const uint8_t qr_size = 3;
  25. // The structure to manage the QR code
  26. QRCode qrcode;
  27. // QR Code init
  28. uint8_t qrcodeBytes[qrcode_getBufferSize(qr_version)];
  29. qrcode_initText(&qrcode, qrcodeBytes, qr_version, qr_error_correction, "HELLO FLIPPER");
  30. if(fb_record == NULL) {
  31. fuprintf(log, "[widget] cannot create fb record\n");
  32. furiac_exit(NULL);
  33. }
  34. u8g2_t* fb = furi_take(fb_record);
  35. // clear display
  36. if(fb != NULL) {
  37. u8g2_ClearBuffer(fb);
  38. }
  39. while(1) {
  40. if(fb != NULL) {
  41. // draw qr code
  42. for(uint8_t y = 0; y < qrcode.size; y++) {
  43. for(uint8_t x = 0; x < qrcode.size; x++) {
  44. if(qrcode_getModule(&qrcode, x, y)) {
  45. u8g2_SetDrawColor(fb, 1);
  46. u8g2_DrawPixelSize(fb, qr_x + x * qr_size, qr_y + y * qr_size, qr_size);
  47. } else {
  48. u8g2_SetDrawColor(fb, 0);
  49. u8g2_DrawPixelSize(fb, qr_x + x * qr_size, qr_y + y * qr_size, qr_size);
  50. }
  51. }
  52. }
  53. } else {
  54. furiac_exit(NULL);
  55. }
  56. furi_commit(fb_record);
  57. delay(1);
  58. }
  59. }