barcode_utils.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "barcode_utils.h"
  2. BarcodeTypeObj* barcode_type_objs[NUMBER_OF_BARCODE_TYPES] = {NULL};
  3. void init_types() {
  4. BarcodeTypeObj* upc_a = malloc(sizeof(BarcodeTypeObj));
  5. upc_a->name = "UPC-A";
  6. upc_a->type = UPCA;
  7. upc_a->min_digits = 11;
  8. upc_a->max_digits = 12;
  9. upc_a->start_pos = 16;
  10. barcode_type_objs[UPCA] = upc_a;
  11. BarcodeTypeObj* ean_8 = malloc(sizeof(BarcodeTypeObj));
  12. ean_8->name = "EAN-8";
  13. ean_8->type = EAN8;
  14. ean_8->min_digits = 7;
  15. ean_8->max_digits = 8;
  16. ean_8->start_pos = 32;
  17. barcode_type_objs[EAN8] = ean_8;
  18. BarcodeTypeObj* ean_13 = malloc(sizeof(BarcodeTypeObj));
  19. ean_13->name = "EAN-13";
  20. ean_13->type = EAN13;
  21. ean_13->min_digits = 12;
  22. ean_13->max_digits = 13;
  23. ean_13->start_pos = 16;
  24. barcode_type_objs[EAN13] = ean_13;
  25. BarcodeTypeObj* code_39 = malloc(sizeof(BarcodeTypeObj));
  26. code_39->name = "CODE-39";
  27. code_39->type = CODE39;
  28. code_39->min_digits = 1;
  29. code_39->max_digits = -1;
  30. code_39->start_pos = 0;
  31. barcode_type_objs[CODE39] = code_39;
  32. BarcodeTypeObj* code_128 = malloc(sizeof(BarcodeTypeObj));
  33. code_128->name = "CODE-128";
  34. code_128->type = CODE128;
  35. code_128->min_digits = 1;
  36. code_128->max_digits = -1;
  37. code_128->start_pos = 0;
  38. barcode_type_objs[CODE128] = code_128;
  39. BarcodeTypeObj* codabar = malloc(sizeof(BarcodeTypeObj));
  40. codabar->name = "Codabar";
  41. codabar->type = CODABAR;
  42. codabar->min_digits = 1;
  43. codabar->max_digits = -1;
  44. codabar->start_pos = 0;
  45. barcode_type_objs[CODABAR] = codabar;
  46. BarcodeTypeObj* unknown = malloc(sizeof(BarcodeTypeObj));
  47. unknown->name = "Unknown";
  48. unknown->type = UNKNOWN;
  49. unknown->min_digits = 0;
  50. unknown->max_digits = 0;
  51. unknown->start_pos = 0;
  52. barcode_type_objs[UNKNOWN] = unknown;
  53. }
  54. void free_types() {
  55. for(int i = 0; i < NUMBER_OF_BARCODE_TYPES; i++) {
  56. free(barcode_type_objs[i]);
  57. }
  58. }
  59. BarcodeTypeObj* get_type(FuriString* type_string) {
  60. if(furi_string_cmp_str(type_string, "UPC-A") == 0) {
  61. return barcode_type_objs[UPCA];
  62. }
  63. if(furi_string_cmp_str(type_string, "EAN-8") == 0) {
  64. return barcode_type_objs[EAN8];
  65. }
  66. if(furi_string_cmp_str(type_string, "EAN-13") == 0) {
  67. return barcode_type_objs[EAN13];
  68. }
  69. if(furi_string_cmp_str(type_string, "CODE-39") == 0) {
  70. return barcode_type_objs[CODE39];
  71. }
  72. if(furi_string_cmp_str(type_string, "CODE-128") == 0) {
  73. return barcode_type_objs[CODE128];
  74. }
  75. if(furi_string_cmp_str(type_string, "Codabar") == 0) {
  76. return barcode_type_objs[CODABAR];
  77. }
  78. return barcode_type_objs[UNKNOWN];
  79. }
  80. const char* get_error_code_name(ErrorCode error_code) {
  81. switch(error_code) {
  82. case WrongNumberOfDigits:
  83. return "Wrong Number Of Digits";
  84. case InvalidCharacters:
  85. return "Invalid Characters";
  86. case UnsupportedType:
  87. return "Unsupported Type";
  88. case FileOpening:
  89. return "File Opening Error";
  90. case InvalidFileData:
  91. return "Invalid File Data";
  92. case MissingEncodingTable:
  93. return "Missing Encoding Table";
  94. case EncodingTableError:
  95. return "Encoding Table Error";
  96. case OKCode:
  97. return "OK";
  98. default:
  99. return "Unknown Code";
  100. };
  101. }
  102. const char* get_error_code_message(ErrorCode error_code) {
  103. switch(error_code) {
  104. case WrongNumberOfDigits:
  105. return "Wrong # of characters";
  106. case InvalidCharacters:
  107. return "Invalid characters";
  108. case UnsupportedType:
  109. return "Unsupported barcode type";
  110. case FileOpening:
  111. return "Could not open file";
  112. case InvalidFileData:
  113. return "Invalid file data";
  114. case MissingEncodingTable:
  115. return "Missing encoding table";
  116. case EncodingTableError:
  117. return "Encoding table error";
  118. case OKCode:
  119. return "OK";
  120. default:
  121. return "Could not read barcode data";
  122. };
  123. }