Skip to content

Commit fab7c05

Browse files
authored
add qrcode_minVersion() to auto size generated qrcode (#1007)
add qrcode_minVersion() minor tweaks to qrManager::to_petscii()
1 parent 46e1b37 commit fab7c05

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

lib/qrcode/qrcode.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
#include "qrcode.h"
3737

38+
#include <stdio.h>
3839
#include <stdlib.h>
3940
#include <string.h>
4041

@@ -843,6 +844,7 @@ int8_t qrcode_initText(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8_
843844
}
844845

845846
bool qrcode_getModule(QRCode *qrcode, uint8_t x, uint8_t y) {
847+
//if (x < 0 || x >= qrcode->size || y < 0 || y >= qrcode->size) {
846848
if (x >= qrcode->size || y >= qrcode->size) {
847849
return false;
848850
}
@@ -860,3 +862,22 @@ void qrcode_getHex(QRCode *qrcode, char *result) {
860862
861863
}
862864
*/
865+
866+
int8_t qrcode_minVersion(uint8_t ecc, const char *data) {
867+
uint16_t len = strlen(data);
868+
869+
int8_t version = 1;
870+
uint16_t capacity;
871+
do {
872+
capacity = NUM_ALPHANUMERIC_CAPACITY[ecc][version-1];
873+
printf("Testing version[%d] ecc[%d] len[%d] capacity[%d]\n", version, ecc, len, capacity);
874+
if (len > capacity) {
875+
version++;
876+
} else {
877+
break;
878+
}
879+
} while (version <= 40);
880+
881+
printf("Set version[%d] ecc[%d] len[%d] capacity[%d]\n", version, ecc, len, capacity);
882+
return version;
883+
}

lib/qrcode/qrcode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ int8_t qrcode_initText(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8_
8383
int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8_t ecc, uint8_t *data, uint16_t length);
8484

8585
bool qrcode_getModule(QRCode *qrcode, uint8_t x, uint8_t y);
86-
86+
int8_t qrcode_minVersion(uint8_t ecc, const char *data);
8787

8888

8989
#ifdef __cplusplus

lib/qrcode/qrmanager.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,21 @@
1111
QRManager qrManager;
1212

1313
std::vector<uint8_t> QRManager::encode(const void* src, size_t len, size_t version, size_t ecc, size_t *out_len) {
14+
// If version is not specified, choose the smallest version that can hold the data
15+
if (version < 1)
16+
version = qrcode_minVersion(ecc, (const char*)src);
17+
1418
qrManager.version = version;
1519
qrManager.ecc_mode = ecc;
1620

1721
*out_len = 0;
1822
qrManager.out_buf.clear();
1923
qrManager.out_buf.shrink_to_fit();
2024

21-
if (version < 1 || version > 40 || ecc > 3) {
22-
return qrManager.out_buf;
25+
//if (version < 1 || version > 40 || ecc < 0 || ecc > 3) {
26+
//if (version < 1 || version > 40 || ecc > 3) {
27+
if (version > 40 || ecc > 3) {
28+
return qrManager.out_buf; // error
2329
}
2430

2531
QRCode qr_code;
@@ -164,9 +170,14 @@ void QRManager::to_petscii(void) {
164170
std::vector<uint8_t> out;
165171
bool reverse = false;
166172

167-
out.push_back(size);
173+
// out.push_back(0x00); // Load Address
174+
// out.push_back(0x04); // Screen RAM 0x0400
175+
176+
// out.push_back(size);
177+
out.push_back(13); // Carriage return
168178

169179
for (auto y = 0; y < size; y += 2) {
180+
out.push_back(32); // Space
170181
for (auto x = 0; x < size; x += 2) {
171182
uint8_t val = bytes[1+y*size+x];
172183
// QR Codes have odd number of rows/columns, so last PETSCII char is only half full
@@ -181,6 +192,8 @@ void QRManager::to_petscii(void) {
181192
}
182193
out.push_back(13); // Carriage return
183194
}
195+
// out.push_back(00);
196+
// out.push_back(00);
184197

185198
qrManager.out_buf = out;
186-
}
199+
}

lib/qrcode/qrmanager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class QRManager {
7373
* PETSCII character can represent 4 bits. Carriage returns (0x0D) are
7474
* added at the end of each row to facilitate printing direct to screen.
7575
*/
76-
void to_petscii(void);
76+
void to_petscii(void);
7777

7878
size_t size() { return version * 4 + 17; }
7979
void set_buffer(const std::string& buffer) { in_buf = buffer; }

0 commit comments

Comments
 (0)