Jelajahi Sumber

Added Code128-C support by Z0wl

Alan 2 tahun lalu
induk
melakukan
1fc14cf1fa
8 mengubah file dengan 264 tambahan dan 11 penghapusan
  1. 4 4
      README.md
  2. 4 1
      barcode_app.h
  3. 12 1
      barcode_utils.c
  4. 3 2
      barcode_utils.h
  5. 131 1
      barcode_validator.c
  6. 2 1
      barcode_validator.h
  7. 106 0
      encoding_tables/code128c_encodings.txt
  8. 2 1
      views/barcode_view.c

+ 4 - 4
README.md

@@ -80,9 +80,9 @@ For example, if you wanted to represent `1234` as a barcode you will need to ent
 
 
 ## Credits
 ## 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

+ 4 - 1
barcode_app.h

@@ -35,6 +35,9 @@
 //the folder where the code 128 encoding table is located
 //the folder where the code 128 encoding table is located
 #define CODE128_DICT_FILE_PATH BARCODE_DATA_FILE_DIR_PATH "/code128_encodings.txt"
 #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
 //the folder where the user stores their barcodes
 #define DEFAULT_USER_BARCODES EXT_PATH("apps_data/barcodes")
 #define DEFAULT_USER_BARCODES EXT_PATH("apps_data/barcodes")
 
 
@@ -85,4 +88,4 @@ uint32_t main_menu_callback(void* context);
 
 
 uint32_t exit_callback(void* context);
 uint32_t exit_callback(void* context);
 
 
-int32_t barcode_main(void* p);
+int32_t barcode_main(void* p);

+ 12 - 1
barcode_utils.c

@@ -43,6 +43,14 @@ void init_types() {
     code_128->start_pos = 0;
     code_128->start_pos = 0;
     barcode_type_objs[CODE128] = code_128;
     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));
     BarcodeTypeObj* codabar = malloc(sizeof(BarcodeTypeObj));
     codabar->name = "Codabar";
     codabar->name = "Codabar";
     codabar->type = CODABAR;
     codabar->type = CODABAR;
@@ -82,6 +90,9 @@ BarcodeTypeObj* get_type(FuriString* type_string) {
     if(furi_string_cmp_str(type_string, "CODE-128") == 0) {
     if(furi_string_cmp_str(type_string, "CODE-128") == 0) {
         return barcode_type_objs[CODE128];
         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) {
     if(furi_string_cmp_str(type_string, "Codabar") == 0) {
         return barcode_type_objs[CODABAR];
         return barcode_type_objs[CODABAR];
     }
     }
@@ -134,4 +145,4 @@ const char* get_error_code_message(ErrorCode error_code) {
     default:
     default:
         return "Could not read barcode data";
         return "Could not read barcode data";
     };
     };
-}
+}

+ 3 - 2
barcode_utils.h

@@ -3,7 +3,7 @@
 #include <furi.h>
 #include <furi.h>
 #include <furi_hal.h>
 #include <furi_hal.h>
 
 
-#define NUMBER_OF_BARCODE_TYPES 7
+#define NUMBER_OF_BARCODE_TYPES 8 
 
 
 typedef enum {
 typedef enum {
     WrongNumberOfDigits, //There is too many or too few digits in the barcode
     WrongNumberOfDigits, //There is too many or too few digits in the barcode
@@ -22,6 +22,7 @@ typedef enum {
     EAN13,
     EAN13,
     CODE39,
     CODE39,
     CODE128,
     CODE128,
+    CODE128C,
     CODABAR,
     CODABAR,
 
 
     UNKNOWN
     UNKNOWN
@@ -51,4 +52,4 @@ void init_types();
 void free_types();
 void free_types();
 BarcodeTypeObj* get_type(FuriString* type_string);
 BarcodeTypeObj* get_type(FuriString* type_string);
 const char* get_error_code_name(ErrorCode error_code);
 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);

+ 131 - 1
barcode_validator.c

@@ -13,6 +13,9 @@ void barcode_loader(BarcodeData* barcode_data) {
     case CODE128:
     case CODE128:
         code_128_loader(barcode_data);
         code_128_loader(barcode_data);
         break;
         break;
+    case CODE128C:
+        code_128c_loader(barcode_data);
+        break;
     case CODABAR:
     case CODABAR:
         codabar_loader(barcode_data);
         codabar_loader(barcode_data);
         break;
         break;
@@ -39,6 +42,7 @@ int calculate_check_digit(BarcodeData* barcode_data) {
         break;
         break;
     case CODE39:
     case CODE39:
     case CODE128:
     case CODE128:
+    case CODE128C:
     case CODABAR:
     case CODABAR:
     case UNKNOWN:
     case UNKNOWN:
     default:
     default:
@@ -345,6 +349,132 @@ void code_128_loader(BarcodeData* barcode_data) {
     furi_string_free(barcode_bits);
     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){
 void codabar_loader(BarcodeData* barcode_data){
     int barcode_length = furi_string_size(barcode_data->raw_data);
     int barcode_length = furi_string_size(barcode_data->raw_data);
 
 
@@ -400,4 +530,4 @@ void codabar_loader(BarcodeData* barcode_data){
 
 
     furi_string_cat(barcode_data->correct_data, barcode_bits);
     furi_string_cat(barcode_data->correct_data, barcode_bits);
     furi_string_free(barcode_bits);
     furi_string_free(barcode_bits);
-}
+}

+ 2 - 1
barcode_validator.h

@@ -10,5 +10,6 @@ void ean_8_loader(BarcodeData* barcode_data);
 void ean_13_loader(BarcodeData* barcode_data);
 void ean_13_loader(BarcodeData* barcode_data);
 void code_39_loader(BarcodeData* barcode_data);
 void code_39_loader(BarcodeData* barcode_data);
 void code_128_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 codabar_loader(BarcodeData* barcode_data);
-void barcode_loader(BarcodeData* barcode_data);
+void barcode_loader(BarcodeData* barcode_data);

+ 106 - 0
encoding_tables/code128c_encodings.txt

@@ -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

+ 2 - 1
views/barcode_view.c

@@ -406,6 +406,7 @@ static void barcode_draw_callback(Canvas* canvas, void* ctx) {
             draw_code_39(canvas, data);
             draw_code_39(canvas, data);
             break;
             break;
         case CODE128:
         case CODE128:
+        case CODE128C:
             draw_code_128(canvas, data);
             draw_code_128(canvas, data);
             break;
             break;
         case CODABAR:
         case CODABAR:
@@ -506,4 +507,4 @@ void barcode_free(Barcode* barcode) {
 View* barcode_get_view(Barcode* barcode) {
 View* barcode_get_view(Barcode* barcode) {
     furi_assert(barcode);
     furi_assert(barcode);
     return barcode->view;
     return barcode->view;
-}
+}