barcode_view.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. #include "../barcode_app.h"
  2. #include "barcode_view.h"
  3. #include "../encodings.h"
  4. /**
  5. * @brief Draws a single bit from a barcode at a specified location
  6. * @param canvas
  7. * @param bit a 1 or a 0 to signify a bit of data
  8. * @param x the top left x coordinate
  9. * @param y the top left y coordinate
  10. * @param width the width of the bit
  11. * @param height the height of the bit
  12. */
  13. static void draw_bit(Canvas* canvas, int bit, int x, int y, int width, int height) {
  14. if(bit == 1) {
  15. canvas_set_color(canvas, ColorBlack);
  16. } else {
  17. canvas_set_color(canvas, ColorWhite);
  18. }
  19. canvas_draw_box(canvas, x, y, width, height);
  20. }
  21. /**
  22. *
  23. */
  24. static void draw_error_str(Canvas* canvas, const char* error) {
  25. canvas_clear(canvas);
  26. canvas_draw_str_aligned(canvas, 62, 30, AlignCenter, AlignCenter, error);
  27. }
  28. /**
  29. * @param bits a string of 1's and 0's
  30. * @returns the x coordinate after the bits have been drawn, useful for drawing the next section of bits
  31. */
  32. static int draw_bits(Canvas* canvas, const char* bits, int x, int y, int width, int height) {
  33. int bits_length = strlen(bits);
  34. for(int i = 0; i < bits_length; i++) {
  35. char c = bits[i];
  36. int num = c - '0';
  37. draw_bit(canvas, num, x, y, width, height);
  38. x += width;
  39. }
  40. return x;
  41. }
  42. /**
  43. * Draws an EAN-8 type barcode, does not check if the barcode is valid
  44. * @param canvas the canvas
  45. * @param barcode_digits the digits in the barcode, must be 8 characters long
  46. */
  47. static void draw_ean_8(Canvas* canvas, BarcodeData* barcode_data) {
  48. FuriString* barcode_digits = barcode_data->correct_data;
  49. BarcodeTypeObj* type_obj = barcode_data->type_obj;
  50. int barcode_length = furi_string_size(barcode_digits);
  51. int x = type_obj->start_pos;
  52. int y = BARCODE_Y_START;
  53. int width = 1;
  54. int height = BARCODE_HEIGHT;
  55. //the guard patterns for the beginning, center, ending
  56. const char* end_bits = "101";
  57. const char* center_bits = "01010";
  58. //draw the starting guard pattern
  59. x = draw_bits(canvas, end_bits, x, y, width, height + 5);
  60. FuriString* code_part = furi_string_alloc();
  61. //loop through each digit, find the encoding, and draw it
  62. for(int i = 0; i < barcode_length; i++) {
  63. char current_digit = furi_string_get_char(barcode_digits, i);
  64. //the actual number and the index of the bits
  65. int index = current_digit - '0';
  66. //use the L-codes for the first 4 digits and the R-Codes for the last 4 digits
  67. if(i <= 3) {
  68. furi_string_set_str(code_part, UPC_EAN_L_CODES[index]);
  69. } else {
  70. furi_string_set_str(code_part, UPC_EAN_R_CODES[index]);
  71. }
  72. //convert the current_digit char into a string so it can be printed
  73. char current_digit_string[2];
  74. snprintf(current_digit_string, 2, "%c", current_digit);
  75. //set the canvas color to black to print the digit
  76. canvas_set_color(canvas, ColorBlack);
  77. canvas_draw_str(canvas, x + 1, y + height + 8, current_digit_string);
  78. //draw the bits of the barcode
  79. x = draw_bits(canvas, furi_string_get_cstr(code_part), x, y, width, height);
  80. //if the index has reached 3, that means 4 digits have been drawn and now draw the center guard pattern
  81. if(i == 3) {
  82. x = draw_bits(canvas, center_bits, x, y, width, height + 5);
  83. }
  84. }
  85. furi_string_free(code_part);
  86. //draw the ending guard pattern
  87. x = draw_bits(canvas, end_bits, x, y, width, height + 5);
  88. }
  89. static void draw_ean_13(Canvas* canvas, BarcodeData* barcode_data) {
  90. FuriString* barcode_digits = barcode_data->correct_data;
  91. BarcodeTypeObj* type_obj = barcode_data->type_obj;
  92. int barcode_length = furi_string_size(barcode_digits);
  93. int x = type_obj->start_pos;
  94. int y = BARCODE_Y_START;
  95. int width = 1;
  96. int height = BARCODE_HEIGHT;
  97. //the guard patterns for the beginning, center, ending
  98. const char* end_bits = "101";
  99. const char* center_bits = "01010";
  100. //draw the starting guard pattern
  101. x = draw_bits(canvas, end_bits, x, y, width, height + 5);
  102. FuriString* left_structure = furi_string_alloc();
  103. FuriString* code_part = furi_string_alloc();
  104. //loop through each digit, find the encoding, and draw it
  105. for(int i = 0; i < barcode_length; i++) {
  106. char current_digit = furi_string_get_char(barcode_digits, i);
  107. int index = current_digit - '0';
  108. if(i == 0) {
  109. furi_string_set_str(left_structure, EAN_13_STRUCTURE_CODES[index]);
  110. //convert the current_digit char into a string so it can be printed
  111. char current_digit_string[2];
  112. snprintf(current_digit_string, 2, "%c", current_digit);
  113. //set the canvas color to black to print the digit
  114. canvas_set_color(canvas, ColorBlack);
  115. canvas_draw_str(canvas, x - 10, y + height + 8, current_digit_string);
  116. continue;
  117. } else {
  118. //use the L-codes for the first 6 digits and the R-Codes for the last 6 digits
  119. if(i <= 6) {
  120. //get the encoding type at the current barcode bit position
  121. char encoding_type = furi_string_get_char(left_structure, i - 1);
  122. if(encoding_type == 'L') {
  123. furi_string_set_str(code_part, UPC_EAN_L_CODES[index]);
  124. } else {
  125. furi_string_set_str(code_part, EAN_G_CODES[index]);
  126. }
  127. } else {
  128. furi_string_set_str(code_part, UPC_EAN_R_CODES[index]);
  129. }
  130. //convert the current_digit char into a string so it can be printed
  131. char current_digit_string[2];
  132. snprintf(current_digit_string, 2, "%c", current_digit);
  133. //set the canvas color to black to print the digit
  134. canvas_set_color(canvas, ColorBlack);
  135. canvas_draw_str(canvas, x + 1, y + height + 8, current_digit_string);
  136. //draw the bits of the barcode
  137. x = draw_bits(canvas, furi_string_get_cstr(code_part), x, y, width, height);
  138. //if the index has reached 6, that means 6 digits have been drawn and we now draw the center guard pattern
  139. if(i == 6) {
  140. x = draw_bits(canvas, center_bits, x, y, width, height + 5);
  141. }
  142. }
  143. }
  144. furi_string_free(left_structure);
  145. furi_string_free(code_part);
  146. //draw the ending guard pattern
  147. x = draw_bits(canvas, end_bits, x, y, width, height + 5);
  148. }
  149. /**
  150. * Draw a UPC-A barcode
  151. */
  152. static void draw_upc_a(Canvas* canvas, BarcodeData* barcode_data) {
  153. FuriString* barcode_digits = barcode_data->correct_data;
  154. BarcodeTypeObj* type_obj = barcode_data->type_obj;
  155. int barcode_length = furi_string_size(barcode_digits);
  156. int x = type_obj->start_pos;
  157. int y = BARCODE_Y_START;
  158. int width = 1;
  159. int height = BARCODE_HEIGHT;
  160. //the guard patterns for the beginning, center, ending
  161. char* end_bits = "101";
  162. char* center_bits = "01010";
  163. //draw the starting guard pattern
  164. x = draw_bits(canvas, end_bits, x, y, width, height + 5);
  165. FuriString* code_part = furi_string_alloc();
  166. //loop through each digit, find the encoding, and draw it
  167. for(int i = 0; i < barcode_length; i++) {
  168. char current_digit = furi_string_get_char(barcode_digits, i);
  169. int index = current_digit - '0'; //convert the number into an int (also the index)
  170. //use the L-codes for the first 6 digits and the R-Codes for the last 6 digits
  171. if(i <= 5) {
  172. furi_string_set_str(code_part, UPC_EAN_L_CODES[index]);
  173. } else {
  174. furi_string_set_str(code_part, UPC_EAN_R_CODES[index]);
  175. }
  176. //convert the current_digit char into a string so it can be printed
  177. char current_digit_string[2];
  178. snprintf(current_digit_string, 2, "%c", current_digit);
  179. //set the canvas color to black to print the digit
  180. canvas_set_color(canvas, ColorBlack);
  181. canvas_draw_str(canvas, x + 1, y + height + 8, current_digit_string);
  182. //draw the bits of the barcode
  183. x = draw_bits(canvas, furi_string_get_cstr(code_part), x, y, width, height);
  184. //if the index has reached 6, that means 6 digits have been drawn and we now draw the center guard pattern
  185. if(i == 5) {
  186. x = draw_bits(canvas, center_bits, x, y, width, height + 5);
  187. }
  188. }
  189. furi_string_free(code_part);
  190. //draw the ending guard pattern
  191. x = draw_bits(canvas, end_bits, x, y, width, height + 5);
  192. }
  193. static void draw_code_39(Canvas* canvas, BarcodeData* barcode_data) {
  194. FuriString* raw_data = barcode_data->raw_data;
  195. FuriString* barcode_digits = barcode_data->correct_data;
  196. //BarcodeTypeObj* type_obj = barcode_data->type_obj;
  197. int barcode_length = furi_string_size(barcode_digits);
  198. int total_pixels = 0;
  199. for(int i = 0; i < barcode_length; i++) {
  200. //1 for wide, 0 for narrow
  201. char wide_or_narrow = furi_string_get_char(barcode_digits, i);
  202. int wn_digit = wide_or_narrow - '0'; //wide(1) or narrow(0) digit
  203. if(wn_digit == 1) {
  204. total_pixels += 3;
  205. } else {
  206. total_pixels += 1;
  207. }
  208. if((i + 1) % 9 == 0) {
  209. total_pixels += 1;
  210. }
  211. }
  212. int x = (128 - total_pixels) / 2;
  213. int y = BARCODE_Y_START;
  214. int width = 1;
  215. int height = BARCODE_HEIGHT;
  216. bool filled_in = true;
  217. //set the canvas color to black to print the digit
  218. canvas_set_color(canvas, ColorBlack);
  219. // canvas_draw_str_aligned(canvas, 62, 30, AlignCenter, AlignCenter, error);
  220. canvas_draw_str_aligned(
  221. canvas, 62, y + height + 8, AlignCenter, AlignBottom, furi_string_get_cstr(raw_data));
  222. for(int i = 0; i < barcode_length; i++) {
  223. //1 for wide, 0 for narrow
  224. char wide_or_narrow = furi_string_get_char(barcode_digits, i);
  225. int wn_digit = wide_or_narrow - '0'; //wide(1) or narrow(0) digit
  226. if(filled_in) {
  227. if(wn_digit == 1) {
  228. x = draw_bits(canvas, "111", x, y, width, height);
  229. } else {
  230. x = draw_bits(canvas, "1", x, y, width, height);
  231. }
  232. filled_in = false;
  233. } else {
  234. if(wn_digit == 1) {
  235. x = draw_bits(canvas, "000", x, y, width, height);
  236. } else {
  237. x = draw_bits(canvas, "0", x, y, width, height);
  238. }
  239. filled_in = true;
  240. }
  241. if((i + 1) % 9 == 0) {
  242. x = draw_bits(canvas, "0", x, y, width, height);
  243. filled_in = true;
  244. }
  245. }
  246. }
  247. static void draw_code_128(Canvas* canvas, BarcodeData* barcode_data) {
  248. FuriString* raw_data = barcode_data->raw_data;
  249. FuriString* barcode_digits = barcode_data->correct_data;
  250. int barcode_length = furi_string_size(barcode_digits);
  251. int x = (128 - barcode_length) / 2;
  252. int y = BARCODE_Y_START;
  253. int width = 1;
  254. int height = BARCODE_HEIGHT;
  255. x = draw_bits(canvas, furi_string_get_cstr(barcode_digits), x, y, width, height);
  256. //set the canvas color to black to print the digit
  257. canvas_set_color(canvas, ColorBlack);
  258. // canvas_draw_str_aligned(canvas, 62, 30, AlignCenter, AlignCenter, error);
  259. canvas_draw_str_aligned(
  260. canvas, 62, y + height + 8, AlignCenter, AlignBottom, furi_string_get_cstr(raw_data));
  261. }
  262. static void draw_codabar(Canvas* canvas, BarcodeData* barcode_data) {
  263. FuriString* raw_data = barcode_data->raw_data;
  264. FuriString* barcode_digits = barcode_data->correct_data;
  265. //BarcodeTypeObj* type_obj = barcode_data->type_obj;
  266. int barcode_length = furi_string_size(barcode_digits);
  267. int total_pixels = 0;
  268. for(int i = 0; i < barcode_length; i++) {
  269. //1 for wide, 0 for narrow
  270. char wide_or_narrow = furi_string_get_char(barcode_digits, i);
  271. int wn_digit = wide_or_narrow - '0'; //wide(1) or narrow(0) digit
  272. if(wn_digit == 1) {
  273. total_pixels += 3;
  274. } else {
  275. total_pixels += 1;
  276. }
  277. if((i + 1) % 7 == 0) {
  278. total_pixels += 1;
  279. }
  280. }
  281. int x = (128 - total_pixels) / 2;
  282. int y = BARCODE_Y_START;
  283. int width = 1;
  284. int height = BARCODE_HEIGHT;
  285. bool filled_in = true;
  286. //set the canvas color to black to print the digit
  287. canvas_set_color(canvas, ColorBlack);
  288. // canvas_draw_str_aligned(canvas, 62, 30, AlignCenter, AlignCenter, error);
  289. canvas_draw_str_aligned(
  290. canvas, 62, y + height + 8, AlignCenter, AlignBottom, furi_string_get_cstr(raw_data));
  291. for(int i = 0; i < barcode_length; i++) {
  292. //1 for wide, 0 for narrow
  293. char wide_or_narrow = furi_string_get_char(barcode_digits, i);
  294. int wn_digit = wide_or_narrow - '0'; //wide(1) or narrow(0) digit
  295. if(filled_in) {
  296. if(wn_digit == 1) {
  297. x = draw_bits(canvas, "111", x, y, width, height);
  298. } else {
  299. x = draw_bits(canvas, "1", x, y, width, height);
  300. }
  301. filled_in = false;
  302. } else {
  303. if(wn_digit == 1) {
  304. x = draw_bits(canvas, "000", x, y, width, height);
  305. } else {
  306. x = draw_bits(canvas, "0", x, y, width, height);
  307. }
  308. filled_in = true;
  309. }
  310. if((i + 1) % 7 == 0) {
  311. x = draw_bits(canvas, "0", x, y, width, height);
  312. filled_in = true;
  313. }
  314. }
  315. }
  316. static void barcode_draw_callback(Canvas* canvas, void* ctx) {
  317. furi_assert(ctx);
  318. BarcodeModel* barcode_model = ctx;
  319. BarcodeData* data = barcode_model->data;
  320. // const char* barcode_digits =;
  321. canvas_clear(canvas);
  322. if(data->valid) {
  323. switch(data->type_obj->type) {
  324. case UPCA:
  325. draw_upc_a(canvas, data);
  326. break;
  327. case EAN8:
  328. draw_ean_8(canvas, data);
  329. break;
  330. case EAN13:
  331. draw_ean_13(canvas, data);
  332. break;
  333. case CODE39:
  334. draw_code_39(canvas, data);
  335. break;
  336. case CODE128:
  337. case CODE128C:
  338. draw_code_128(canvas, data);
  339. break;
  340. case CODABAR:
  341. draw_codabar(canvas, data);
  342. break;
  343. case UNKNOWN:
  344. default:
  345. break;
  346. }
  347. } else {
  348. switch(data->reason) {
  349. case WrongNumberOfDigits:
  350. draw_error_str(canvas, "Wrong # of characters");
  351. break;
  352. case InvalidCharacters:
  353. draw_error_str(canvas, "Invalid characters");
  354. break;
  355. case UnsupportedType:
  356. draw_error_str(canvas, "Unsupported barcode type");
  357. break;
  358. case FileOpening:
  359. draw_error_str(canvas, "Could not open file");
  360. break;
  361. case InvalidFileData:
  362. draw_error_str(canvas, "Invalid file data");
  363. break;
  364. case MissingEncodingTable:
  365. draw_error_str(canvas, "Missing encoding table");
  366. break;
  367. case EncodingTableError:
  368. draw_error_str(canvas, "Encoding table error");
  369. break;
  370. default:
  371. draw_error_str(canvas, "Could not read barcode data");
  372. break;
  373. }
  374. }
  375. }
  376. bool barcode_input_callback(InputEvent* input_event, void* ctx) {
  377. UNUSED(ctx);
  378. //furi_assert(ctx);
  379. //Barcode* test_view_object = ctx;
  380. if(input_event->key == InputKeyBack) {
  381. return false;
  382. } else {
  383. return true;
  384. }
  385. }
  386. Barcode* barcode_view_allocate(BarcodeApp* barcode_app) {
  387. furi_assert(barcode_app);
  388. Barcode* barcode = malloc(sizeof(Barcode));
  389. barcode->view = view_alloc();
  390. barcode->barcode_app = barcode_app;
  391. view_set_context(barcode->view, barcode);
  392. view_allocate_model(barcode->view, ViewModelTypeLocking, sizeof(BarcodeModel));
  393. view_set_draw_callback(barcode->view, barcode_draw_callback);
  394. view_set_input_callback(barcode->view, barcode_input_callback);
  395. return barcode;
  396. }
  397. void barcode_free_model(Barcode* barcode) {
  398. with_view_model(
  399. barcode->view,
  400. BarcodeModel * model,
  401. {
  402. if(model->file_path != NULL) {
  403. furi_string_free(model->file_path);
  404. }
  405. if(model->data != NULL) {
  406. if(model->data->raw_data != NULL) {
  407. furi_string_free(model->data->raw_data);
  408. }
  409. if(model->data->correct_data != NULL) {
  410. furi_string_free(model->data->correct_data);
  411. }
  412. free(model->data);
  413. }
  414. },
  415. false);
  416. }
  417. void barcode_free(Barcode* barcode) {
  418. furi_assert(barcode);
  419. barcode_free_model(barcode);
  420. view_free(barcode->view);
  421. free(barcode);
  422. }
  423. View* barcode_get_view(Barcode* barcode) {
  424. furi_assert(barcode);
  425. return barcode->view;
  426. }