Skip to content

Commit

Permalink
Added Code128-C support by Z0wl
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingal1337 committed Jun 11, 2023
1 parent e58df59 commit 1fc14cf
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 11 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ For example, if you wanted to represent `1234` as a barcode you will need to ent

## Credits

[Kingal1337](https://github.com/Kingal1337) - Developer
- [Kingal1337](https://github.com/Kingal1337) - Developer
- [Z0wl](https://github.com/Z0wl) - Added Code128-C Support
- [@teeebor](https://github.com/teeebor) - Menu Code Snippet

[@teeebor](https://github.com/teeebor) - Menu Code Snippet


[1] - Only supports Set B and only the characters from 0-94
[1] - supports Set B (only the characters from 0-94). Also supports Set C
5 changes: 4 additions & 1 deletion barcode_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
//the folder where the code 128 encoding table is located
#define CODE128_DICT_FILE_PATH BARCODE_DATA_FILE_DIR_PATH "/code128_encodings.txt"

//the folder where the code 128 C encoding table is located
#define CODE128C_DICT_FILE_PATH BARCODE_DATA_FILE_DIR_PATH "/code128c_encodings.txt"

//the folder where the user stores their barcodes
#define DEFAULT_USER_BARCODES EXT_PATH("apps_data/barcodes")

Expand Down Expand Up @@ -85,4 +88,4 @@ uint32_t main_menu_callback(void* context);

uint32_t exit_callback(void* context);

int32_t barcode_main(void* p);
int32_t barcode_main(void* p);
13 changes: 12 additions & 1 deletion barcode_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ void init_types() {
code_128->start_pos = 0;
barcode_type_objs[CODE128] = code_128;

BarcodeTypeObj* code_128c = malloc(sizeof(BarcodeTypeObj));
code_128c->name = "CODE-128C";
code_128c->type = CODE128C;
code_128c->min_digits = 2;
code_128c->max_digits = -1;
code_128c->start_pos = 0;
barcode_type_objs[CODE128C] = code_128c;

BarcodeTypeObj* codabar = malloc(sizeof(BarcodeTypeObj));
codabar->name = "Codabar";
codabar->type = CODABAR;
Expand Down Expand Up @@ -82,6 +90,9 @@ BarcodeTypeObj* get_type(FuriString* type_string) {
if(furi_string_cmp_str(type_string, "CODE-128") == 0) {
return barcode_type_objs[CODE128];
}
if(furi_string_cmp_str(type_string, "CODE-128C") == 0) {
return barcode_type_objs[CODE128C];
}
if(furi_string_cmp_str(type_string, "Codabar") == 0) {
return barcode_type_objs[CODABAR];
}
Expand Down Expand Up @@ -134,4 +145,4 @@ const char* get_error_code_message(ErrorCode error_code) {
default:
return "Could not read barcode data";
};
}
}
5 changes: 3 additions & 2 deletions barcode_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <furi.h>
#include <furi_hal.h>

#define NUMBER_OF_BARCODE_TYPES 7
#define NUMBER_OF_BARCODE_TYPES 8

typedef enum {
WrongNumberOfDigits, //There is too many or too few digits in the barcode
Expand All @@ -22,6 +22,7 @@ typedef enum {
EAN13,
CODE39,
CODE128,
CODE128C,
CODABAR,

UNKNOWN
Expand Down Expand Up @@ -51,4 +52,4 @@ void init_types();
void free_types();
BarcodeTypeObj* get_type(FuriString* type_string);
const char* get_error_code_name(ErrorCode error_code);
const char* get_error_code_message(ErrorCode error_code);
const char* get_error_code_message(ErrorCode error_code);
132 changes: 131 additions & 1 deletion barcode_validator.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ void barcode_loader(BarcodeData* barcode_data) {
case CODE128:
code_128_loader(barcode_data);
break;
case CODE128C:
code_128c_loader(barcode_data);
break;
case CODABAR:
codabar_loader(barcode_data);
break;
Expand All @@ -39,6 +42,7 @@ int calculate_check_digit(BarcodeData* barcode_data) {
break;
case CODE39:
case CODE128:
case CODE128C:
case CODABAR:
case UNKNOWN:
default:
Expand Down Expand Up @@ -345,6 +349,132 @@ void code_128_loader(BarcodeData* barcode_data) {
furi_string_free(barcode_bits);
}

/**
* Loads a code 128 C barcode
*/
void code_128c_loader(BarcodeData* barcode_data) {
int barcode_length = furi_string_size(barcode_data->raw_data);

//the start code for character set C
int start_code_value = 105;

//The bits for the start code
const char* start_code_bits = "11010011100";

//The bits for the stop code
const char* stop_code_bits = "1100011101011";

int min_digits = barcode_data->type_obj->min_digits;

int checksum_adder = start_code_value;
int checksum_digits = 0;

//the calculated check digit
int final_check_digit = 0;

// check the length of the barcode, must contain atleast 2 character,
// this can have as many characters as it wants, it might not fit on the screen
// code 128 C: the length must be even
if((barcode_length < min_digits) || (barcode_length & 1)) {
barcode_data->reason = WrongNumberOfDigits;
barcode_data->valid = false;
return;
}
//Open Storage
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* ff = flipper_format_file_alloc(storage);

FuriString* barcode_bits = furi_string_alloc();

//add the start code
furi_string_cat(barcode_bits, start_code_bits);

if(!flipper_format_file_open_existing(ff, CODE128C_DICT_FILE_PATH)) {
FURI_LOG_E(TAG, "c128c Could not open file %s", CODE128C_DICT_FILE_PATH);
barcode_data->reason = MissingEncodingTable;
barcode_data->valid = false;
} else {
FuriString* value = furi_string_alloc();
FuriString* char_bits = furi_string_alloc();
for(int i = 0; i < barcode_length; i+=2) {
char barcode_char1 = furi_string_get_char(barcode_data->raw_data, i);
char barcode_char2 = furi_string_get_char(barcode_data->raw_data, i+1);
FURI_LOG_I(TAG, "c128c bc1='%c' bc2='%c'", barcode_char1, barcode_char2);

char current_chars[4];
snprintf(current_chars, 3, "%c%c", barcode_char1, barcode_char2);
FURI_LOG_I(TAG, "c128c current_chars='%s'", current_chars);

//using the value of the characters, get the characters bits
if(!flipper_format_read_string(ff, current_chars, char_bits)) {
FURI_LOG_E(TAG, "c128c Could not read \"%s\" string", current_chars);
barcode_data->reason = EncodingTableError;
barcode_data->valid = false;
break;
} else {
//add the bits to the full barcode
furi_string_cat(barcode_bits, char_bits);

// calculate the checksum
checksum_digits += 1;
checksum_adder += (atoi(current_chars) * checksum_digits);

FURI_LOG_I(
TAG,
"c128c \"%s\" string: %s : %s : %d : %d : %d",
current_chars,
furi_string_get_cstr(char_bits),
furi_string_get_cstr(value),
checksum_digits,
(atoi(furi_string_get_cstr(value)) * checksum_digits),
checksum_adder);
}
//bring the file pointer back to the begining
flipper_format_rewind(ff);
}
//calculate the check digit and convert it into a c string for lookup in the encoding table
final_check_digit = checksum_adder % 103;
FURI_LOG_I(TAG, "c128c finale_check_digit=%d", final_check_digit);

int length = snprintf(NULL, 0, "%d", final_check_digit);
if (final_check_digit < 100)
length=2;
char* final_check_digit_string = malloc(length+1);
snprintf(final_check_digit_string, length + 1, "%02d", final_check_digit);

//after the checksum has been calculated, add the bits to the full barcode
if(!flipper_format_read_string(ff, final_check_digit_string, char_bits)) {
FURI_LOG_E(TAG, "c128c cksum Could not read \"%s\" string", final_check_digit_string);
barcode_data->reason = EncodingTableError;
barcode_data->valid = false;
} else {
//add the check digit bits to the full barcode
furi_string_cat(barcode_bits, char_bits);

FURI_LOG_I(
TAG,
"check digit \"%s\" string: %s",
final_check_digit_string,
furi_string_get_cstr(char_bits));
}

free(final_check_digit_string);
furi_string_free(value);
furi_string_free(char_bits);
}

//add the stop code
furi_string_cat(barcode_bits, stop_code_bits);

//Close Storage
flipper_format_free(ff);
furi_record_close(RECORD_STORAGE);

FURI_LOG_I(TAG, "c128c %s", furi_string_get_cstr(barcode_bits));
furi_string_cat(barcode_data->correct_data, barcode_bits);
furi_string_free(barcode_bits);
}

void codabar_loader(BarcodeData* barcode_data){
int barcode_length = furi_string_size(barcode_data->raw_data);

Expand Down Expand Up @@ -400,4 +530,4 @@ void codabar_loader(BarcodeData* barcode_data){

furi_string_cat(barcode_data->correct_data, barcode_bits);
furi_string_free(barcode_bits);
}
}
3 changes: 2 additions & 1 deletion barcode_validator.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ void ean_8_loader(BarcodeData* barcode_data);
void ean_13_loader(BarcodeData* barcode_data);
void code_39_loader(BarcodeData* barcode_data);
void code_128_loader(BarcodeData* barcode_data);
void code_128c_loader(BarcodeData* barcode_data);
void codabar_loader(BarcodeData* barcode_data);
void barcode_loader(BarcodeData* barcode_data);
void barcode_loader(BarcodeData* barcode_data);
106 changes: 106 additions & 0 deletions encoding_tables/code128c_encodings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
00: 11011001100
01: 11001101100
02: 11001100110
03: 10010011000
04: 10010001100
05: 10001001100
06: 10011001000
07: 10011000100
08: 10001100100
09: 11001001000
10: 11001000100
11: 11000100100
12: 10110011100
13: 10011011100
14: 10011001110
15: 10111001100
16: 10011101100
17: 10011100110
18: 11001110010
19: 11001011100
20: 11001001110
21: 11011100100
22: 11001110100
23: 11101101110
24: 11101001100
25: 11100101100
26: 11100100110
27: 11101100100
28: 11100110100
29: 11100110010
30: 11011011000
31: 11011000110
32: 11000110110
33: 10100011000
34: 10001011000
35: 10001000110
36: 10110001000
37: 10001101000
38: 10001100010
39: 11010001000
40: 11000101000
41: 11000100010
42: 10110111000
43: 10110001110
44: 10001101110
45: 10111011000
46: 10111000110
47: 10001110110
48: 11101110110
49: 11010001110
50: 11000101110
51: 11011101000
52: 11011100010
53: 11011101110
54: 11101011000
55: 11101000110
56: 11100010110
57: 11101101000
58: 11101100010
59: 11100011010
60: 11101111010
61: 11001000010
62: 11110001010
63: 10100110000
64: 10100001100
65: 10010110000
66: 10010000110
67: 10000101100
68: 10000100110
69: 10110010000
70: 10110000100
71: 10011010000
72: 10011000010
73: 10000110100
74: 10000110010
75: 11000010010
76: 11001010000
77: 11110111010
78: 11000010100
79: 10001111010
80: 10100111100
81: 10010111100
82: 10010011110
83: 10111100100
84: 10011110100
85: 10011110010
86: 11110100100
87: 11110010100
88: 11110010010
89: 11011011110
90: 11011110110
91: 11110110110
92: 10101111000
93: 10100011110
94: 10001011110
95: 10111101000
96: 10111100010
97: 11110101000
98: 11110100010
99: 10111011110
100: 10111101110
101: 11101011110
102: 11110101110
103: 11010000100
104: 11010010000
105: 11010011100
3 changes: 2 additions & 1 deletion views/barcode_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ static void barcode_draw_callback(Canvas* canvas, void* ctx) {
draw_code_39(canvas, data);
break;
case CODE128:
case CODE128C:
draw_code_128(canvas, data);
break;
case CODABAR:
Expand Down Expand Up @@ -506,4 +507,4 @@ void barcode_free(Barcode* barcode) {
View* barcode_get_view(Barcode* barcode) {
furi_assert(barcode);
return barcode->view;
}
}

0 comments on commit 1fc14cf

Please sign in to comment.