Skip to content

Commit e16b33a

Browse files
nwahtschak909
authored andcommitted
[fuji] QR: add docs/comments
1 parent eeaaf91 commit e16b33a

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

lib/qrcode/qrmanager.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,27 @@ std::vector<uint8_t> QRManager::encode(const void* src, size_t len, size_t versi
1515
QRCode qr_code;
1616
uint8_t qr_bytes[qrcode_getBufferSize(version)];
1717

18-
bool err = qrcode_initText(&qr_code, qr_bytes, version, ecc, (const char*)src);
18+
uint8_t err = qrcode_initText(&qr_code, qr_bytes, version, ecc, (const char*)src);
1919

2020
size_t size = qr_code.size;
2121
*out_len = size*size;
2222

2323
qrManager.out_buf.clear();
2424
qrManager.out_buf.shrink_to_fit();
2525

26-
if (err) {
27-
return qrManager.out_buf;
28-
}
29-
30-
for (uint8_t x = 0; x < size; x++) {
31-
for (uint8_t y = 0; y < size; y++) {
32-
uint8_t on = qrcode_getModule(&qr_code, x, y);
33-
qrManager.out_buf.push_back(on);
26+
if (err == 0) {
27+
for (uint8_t x = 0; x < size; x++) {
28+
for (uint8_t y = 0; y < size; y++) {
29+
uint8_t on = qrcode_getModule(&qr_code, x, y);
30+
qrManager.out_buf.push_back(on);
31+
}
3432
}
3533
}
3634

3735
return qrManager.out_buf;
3836
}
3937

40-
std::vector<uint8_t> QRManager::to_bits(void) {
38+
void QRManager::to_bits(void) {
4139
auto bytes = qrManager.out_buf;
4240
size_t len = bytes.size();
4341
std::vector<uint8_t> out;
@@ -54,10 +52,14 @@ std::vector<uint8_t> QRManager::to_bits(void) {
5452
out.push_back(val);
5553

5654
qrManager.out_buf = out;
57-
return qrManager.out_buf;
5855
}
5956

6057
/*
58+
This collapses each 2x2 groups of modules (pixels) into a single ATASCII character.
59+
Values for ATASCII look up are as calculated as follows. Note that 6 and 9 have no
60+
suitable ATASCII character, so diagonal lines are used instead. These seem to work
61+
in most cases, but for best results you may want to use custom characters for those.
62+
6163
0 1 2 3 4 5 6* 7 8 9* 10 11 12 13 14 15
6264
- - x - - x x x - - x - - x x x - - x - - x x x - - x - - x x x
6365
- - - - - - - - x - x - x - x - - x - x - x - x x x x x x x x x
@@ -67,14 +69,15 @@ std::vector<uint8_t> QRManager::to_bits(void) {
6769
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
6870
uint8_t atascii[16] = {32, 12, 11, 149, 15, 25, 6, 137, 9, 7, 153, 143, 21, 139, 140, 160};
6971

70-
std::vector<uint8_t> QRManager::to_atascii(void) {
72+
void QRManager::to_atascii(void) {
7173
auto bytes = qrManager.out_buf;
7274
size_t size = sqrt(bytes.size()); // TODO: Pass through/store?
7375
std::vector<uint8_t> out;
7476

7577
for (auto y = 0; y < size; y += 2) {
7678
for (auto x = 0; x < size; x += 2) {
7779
uint8_t val = bytes[y*size+x];
80+
// QR Codes have odd number of rows/columns, so last ATASCII char is only half full
7881
if (x+1 < size) val |= bytes[y*size+x+1] << 1;
7982
if (y+1 < size) val |= bytes[(y+1)*size+x] << 2;
8083
if (y+1 < size && x+1 < size) val |= bytes[(y+1)*size+x+1] << 3;
@@ -84,5 +87,4 @@ std::vector<uint8_t> QRManager::to_atascii(void) {
8487
}
8588

8689
qrManager.out_buf = out;
87-
return qrManager.out_buf;
8890
}

lib/qrcode/qrmanager.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,24 @@ class QRManager {
3636
* whether it is on (black) or off (white).
3737
*/
3838
static std::vector<uint8_t> encode(const void* src, size_t len, size_t version, size_t ecc, size_t* out_len);
39-
static std::vector<uint8_t> to_bits(void);
40-
static std::vector<uint8_t> to_atascii(void);
39+
40+
/**
41+
* to_bits - Convert QR code in out_buf to compact binary format
42+
*
43+
* Replaces data in out_buf, where each byte is 0x00 or 0x01 with, with
44+
* compact data where each bit represents a single QR module (pixel).
45+
* So a 21x21 QR code will be 56 bytes (21*21/8). Data is returned LSB->MSB.
46+
*/
47+
void to_bits(void);
48+
49+
/**
50+
* to_atascii - Convert QR code in out_buf to ATASCII
51+
*
52+
* Replaces bytes in out_buf with vector of ATASCII characters. Each
53+
* ATASCII character can represent 4 bits. Atari newlines (0x9B) are
54+
* added at the end of each row to facilitate printing direct to screen.
55+
*/
56+
void to_atascii(void);
4157

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

0 commit comments

Comments
 (0)