Skip to content

Commit 202ca0b

Browse files
committed
resolve #2, resolve #3 manual mode selection, qrcodes w/multi lines
1 parent 36dc9f4 commit 202ca0b

File tree

4 files changed

+364
-97
lines changed

4 files changed

+364
-97
lines changed

README.md

+69-17
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,49 @@ Version: 0
2323
Message: your content here
2424
```
2525

26+
qrcode v2 supports a newer format as well (the old format still works for
27+
backward compatibility, or, if you don't need the newer features, the app will
28+
read version "0" files faster):
29+
30+
```
31+
Filetype: QRCode
32+
Version: 1
33+
QRMode: B
34+
QRVersion: 6
35+
QRECC: L
36+
Message: your content here
37+
Message: multi-line content is possible
38+
```
39+
40+
In a version "1" file, the `QRMode`, `QRVersion`, and `QRECC` are optional
41+
(though, must be in that order if more than one are specified). The app will
42+
attempt to use the specified mode, version, and/or ECC, if the content will
43+
fit. Otherwise, it may select a different mode, version, and/or ECC. Keep
44+
reading to learn about the meaning of `QRMode`, `QRVersion`, and `QRECC`.
45+
46+
Version "1" files also support multi-line content. Each line starting with
47+
`Message:` will be concatenated together with newline characters.
48+
49+
My recommendation is to allow the app to select a mode, version, and ECC level
50+
for you and, then, if you find that your qrcode reader prefers specific
51+
settings, update the file appropriately.
52+
2653
### Message Format
2754
qrcodes support 4 formats called "modes": numeric, alpha-numeric, binary, and
2855
kanji. Because of the limited screen real-estate on the [Flipper Zero], you'll
2956
want to pick the best mode for the data you are trying to display.
3057

31-
The app will automatically detect the best mode to use, so the only thing you
32-
need to do is make sure the message in your file is formatted to use the best
33-
mode. For example, if your message is entirely numeric, make sure you don't
34-
include any extraneous punctuation in your file. If you're only encoding a
35-
domain name, make sure it's uppercase to take advantage of alpha-numeric mode,
36-
etc.
58+
If unspecified in the `.qrcode` file, the app will automatically detect the
59+
best mode to use based on the message content.
3760

38-
#### Numeric Mode
39-
Consists of only numbers, nothing else. This mode can encode the most data.
61+
#### Numeric Mode (QRMode: N)
62+
Consists of only numbers, nothing else. This mode can encode the most data and
63+
is useful for things like phone numbers. To use this mode, your message must
64+
_not_ contain non-numeric characters. For example, a message content of "(xxx)
65+
xxx-xxxx" can _not_ use numeric mode (it would require "binary" mode, in fact).
66+
Instead, your message should just be "xxxxxxxxxx".
4067

41-
#### Alpha-Numeric Mode
68+
#### Alpha-Numeric Mode (QRMode: A)
4269
This mode can encode numbers, uppercase letters *only*, spaces, and the
4370
following symbols: `$%*+-./:`. This format _may_ be appropriate for urls, as
4471
long as you're only encoding the domain name and you remember to use uppercase
@@ -48,30 +75,56 @@ case-sensitive.
4875

4976
A qrcode in alpha-numeric mode can encode ~40% less data than numeric mode.
5077

51-
#### Binary Mode
78+
#### Binary Mode (QRMode: B)
5279
This mode is a little bit of a misnomer: binary mode simply means that the
5380
message will be encoded as 8-bit bytes. The qrcode standard stipulates that
5481
text will use ISO-8859-1 (also known as Latin-1) encoding, _not_ utf8 as would
5582
be the standard these days. However, _some_ readers _may_ automatically detect
5683
utf8. To be standard-compliant, that basically means you can only use Latin
5784
letters, numbers, and symbols.
5885

86+
Multi-line messages will always be in binary mode, since the other modes cannot
87+
encode a newline character.
88+
5989
A qrcode in binary mode can encode ~60% less data than numeric mode, and ~30%
6090
less than alpha-numeric.
6191

62-
#### Kanji Mode
92+
#### Kanji Mode (QRMode: K)
6393
This mode is unsupported, so I won't go into detail. A limitation of the
6494
underlying qrcode library that I'm using, unfortunately. If there's interest,
6595
perhaps I'll hack in support sometime.
6696

97+
### QRVersion
98+
A qrcode's version specifies how "big" it is. Higher versions contain more
99+
"modules" (ie, the "pixels" that make up qrcodes) and, thus, can encode more
100+
data. A version 1 qrcode contains 21x21 modules, whereas a version 11 code (the
101+
largest the Flipper Zero can display) contains 61x61 modules. The modules of a
102+
version 1 code will be 3x3 pixels on the Flipper Zero screen; version 2 and 3
103+
qrcodes will each have 2x2 pixel modules; and version 4 through 11 qrcodes will
104+
have single pixel modules.
105+
106+
If unspecified in the `.qrcode` file, the app will automatically select the
107+
lowest version that can contain all of the message content, given the mode
108+
selected in the previous step.
109+
110+
### QRECC
111+
A qrcode's ECC level determines the qrcode's resilience to "damage". In the
112+
case of the Flipper Zero, "damage" might be a dirty screen, dead pixels, or
113+
even screen glare. Higher ECC modes are more resilient, but can contain less
114+
data. The ECC modes are Low, Medium, Quartile, and High and can be specified in
115+
the `.qrcode` file using the first letter (L, M, Q, and H).
116+
117+
qrcode readers may have an easier time reading qrcodes with higher ECC levels,
118+
so, if unspecified in the `.qrcode` file, the app will select the highest ECC
119+
level that can contain all of the message content, given the qrcode mode and
120+
version selected in the previous steps.
121+
67122
## Using the App
68123
The app is fairly straightforward. When it first starts, the file browser will
69124
automatically open to the `qrcodes` directory and display any `.qrcode` files.
70125
Select one using the arrow keys and the center button. The qrcode will display.
71-
If you push the right arrow, some stats will display: the qrcode "Version" -
72-
which corresponds to how big it is; the ECC level - which determines the
73-
qrcode's resilience to damage, such as a dirty screen (Low, Medium, Quartile,
74-
and High); and the qrcode Mode (Numeric, Alpha-Numeric, Binary, or Kanji).
126+
If you push the right arrow, some stats will display: the qrcode "Version"; the
127+
ECC level; and the qrcode Mode (Numeric, Alpha-Numeric, Binary, or Kanji).
75128

76129
While viewing the stats, you can select Version or ECC using the up and down
77130
arrows and the center button. You can then increase or decrease the Version or
@@ -133,7 +186,6 @@ git clone git@github.com:bmatcuk/flipperzero-qrcode.git
133186
Next, in the base of the [flipperzero-firmware] directory, run fbt:
134187

135188
```bash
136-
cd ..
137189
./fbt fap_qrcode
138190
```
139191

@@ -146,7 +198,7 @@ find the .fap, should it change in the future).
146198
This application uses the [QRCode] library by ricmoo. This is the same library
147199
that is in the lib directory of the flipper-firmware repo (which was originally
148200
included for a [now-removed demo app]), but modified slightly to fix some
149-
compiler errors.
201+
compiler errors and allow the explicit selection of the qrcode mode.
150202

151203
[now-removed demo app]: https://github.com/flipperdevices/flipperzero-firmware/pull/160/files
152204
[flipperzero-firmware]: https://github.com/flipperdevices/flipperzero-firmware

qrcode.c

+21-25
Original file line numberDiff line numberDiff line change
@@ -119,21 +119,21 @@ static int8_t getAlphanumeric(char c) {
119119
return -1;
120120
}
121121

122-
static bool isAlphanumeric(const char *text, uint16_t length) {
123-
while (length != 0) {
124-
if (getAlphanumeric(text[--length]) == -1) { return false; }
125-
}
126-
return true;
127-
}
122+
/* static bool isAlphanumeric(const char *text, uint16_t length) { */
123+
/* while (length != 0) { */
124+
/* if (getAlphanumeric(text[--length]) == -1) { return false; } */
125+
/* } */
126+
/* return true; */
127+
/* } */
128128

129129

130-
static bool isNumeric(const char *text, uint16_t length) {
131-
while (length != 0) {
132-
char c = text[--length];
133-
if (c < '0' || c > '9') { return false; }
134-
}
135-
return true;
136-
}
130+
/* static bool isNumeric(const char *text, uint16_t length) { */
131+
/* while (length != 0) { */
132+
/* char c = text[--length]; */
133+
/* if (c < '0' || c > '9') { return false; } */
134+
/* } */
135+
/* return true; */
136+
/* } */
137137

138138

139139
// We store the following tightly packed (less 8) in modeInfo
@@ -614,11 +614,8 @@ static void rs_getRemainder(uint8_t degree, uint8_t *coeff, uint8_t *data, uint8
614614

615615

616616

617-
static int8_t encodeDataCodewords(BitBucket *dataCodewords, const uint8_t *text, uint16_t length, uint8_t version) {
618-
int8_t mode = MODE_BYTE;
619-
620-
if (isNumeric((char*)text, length)) {
621-
mode = MODE_NUMERIC;
617+
static int8_t encodeDataCodewords(BitBucket *dataCodewords, const uint8_t *text, uint16_t length, int8_t mode, uint8_t version) {
618+
if (mode == MODE_NUMERIC) {
622619
bb_appendBits(dataCodewords, 1 << MODE_NUMERIC, 4);
623620
bb_appendBits(dataCodewords, length, getModeBits(version, MODE_NUMERIC));
624621

@@ -639,8 +636,7 @@ static int8_t encodeDataCodewords(BitBucket *dataCodewords, const uint8_t *text,
639636
bb_appendBits(dataCodewords, accumData, accumCount * 3 + 1);
640637
}
641638

642-
} else if (isAlphanumeric((char*)text, length)) {
643-
mode = MODE_ALPHANUMERIC;
639+
} else if (mode == MODE_ALPHANUMERIC) {
644640
bb_appendBits(dataCodewords, 1 << MODE_ALPHANUMERIC, 4);
645641
bb_appendBits(dataCodewords, length, getModeBits(version, MODE_ALPHANUMERIC));
646642

@@ -758,7 +754,7 @@ uint16_t qrcode_getBufferSize(uint8_t version) {
758754
}
759755

760756
// @TODO: Return error if data is too big.
761-
int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8_t ecc, uint8_t *data, uint16_t length) {
757+
int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, int8_t mode, uint8_t version, uint8_t ecc, uint8_t *data, uint16_t length) {
762758
uint8_t size = version * 4 + 17;
763759
qrcode->version = version;
764760
qrcode->size = size;
@@ -781,7 +777,7 @@ int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8
781777
bb_initBuffer(&codewords, codewordBytes, (int32_t)sizeof(codewordBytes));
782778

783779
// Place the data code words into the buffer
784-
int8_t mode = encodeDataCodewords(&codewords, data, length, version);
780+
mode = encodeDataCodewords(&codewords, data, length, mode, version);
785781

786782
if (mode < 0) { return -1; }
787783
qrcode->mode = mode;
@@ -834,9 +830,9 @@ int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8
834830
return 0;
835831
}
836832

837-
int8_t qrcode_initText(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8_t ecc, const char *data) {
838-
return qrcode_initBytes(qrcode, modules, version, ecc, (uint8_t*)data, strlen(data));
839-
}
833+
/* int8_t qrcode_initText(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8_t ecc, const char *data) { */
834+
/* return qrcode_initBytes(qrcode, modules, version, ecc, (uint8_t*)data, strlen(data)); */
835+
/* } */
840836

841837
bool qrcode_getModule(QRCode *qrcode, uint8_t x, uint8_t y) {
842838
if (x >= qrcode->size || y >= qrcode->size) {

qrcode.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ extern "C"{
8585

8686
uint16_t qrcode_getBufferSize(uint8_t version);
8787

88-
int8_t qrcode_initText(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8_t ecc, const char *data);
89-
int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8_t ecc, uint8_t *data, uint16_t length);
88+
/* int8_t qrcode_initText(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8_t ecc, const char *data); */
89+
int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, int8_t mode, uint8_t version, uint8_t ecc, uint8_t *data, uint16_t length);
9090

9191
bool qrcode_getModule(QRCode *qrcode, uint8_t x, uint8_t y);
9292

0 commit comments

Comments
 (0)