From a51a6914f4a9c4f1abd829f4bcc5d66795a89917 Mon Sep 17 00:00:00 2001 From: MX <10697207+xMasterX@users.noreply.github.com> Date: Sat, 31 Dec 2022 02:51:50 +0300 Subject: [PATCH] Barcode Generator: Save last barcode --- CHANGELOG.md | 3 +- .../barcode_generator/barcode_generator.c | 101 ++++++++++-------- .../barcode_generator/barcode_generator.h | 39 ++++++- 3 files changed, 95 insertions(+), 48 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e12b275d..4a4fd9f85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,13 +2,14 @@ * CI/CD: Builds with extra apps pack included, see new option below * SubGHz: Allow manual creation of Nice Flor S, Nice Smilo * LF RFID: Allow removing password from T5577 to make it operational (PR #225 | by @TQMatvey & Tobirg (grvpvl)) +* Plugins -> Barcode Generator: Save last used barcode, and load on app start +* Plugins -> Barcode Generator: Add EAN-13 support (PR #234 | by @gid9798) * Plugins -> Arkanoid: Fix bug with unability to continue game after first level * Plugins -> Morse Code: Fixed crashes, fixed known issues (PR #249 | by @gid9798) * Plugins -> USB Keyboard & Mouse: Added ESCAPE key (PR #243 | by @huuck) * Plugins -> USB Keyboard & Mouse: Added Delete key * Plugins -> SubGHz Bruteforcer: Add holtek HT12X protocol support * Plugins -> POCSAG Pager: Fix UI bug -* Plugins -> Barcode Generator: Add EAN-13 support (PR #234 | by @gid9798) * Plugins -> Wifi Marauder: Enable +5volt on plugin start, to allow usage with custom boards * Plugins: Add 2048 Game (PR #233 | by @eugene-kirzhanov) * Plugins: Update TOTP, UniTemp, Wifi marauder to latest commits from their repo's diff --git a/applications/plugins/barcode_generator/barcode_generator.c b/applications/plugins/barcode_generator/barcode_generator.c index a07a12e6a..cbbb41671 100644 --- a/applications/plugins/barcode_generator/barcode_generator.c +++ b/applications/plugins/barcode_generator/barcode_generator.c @@ -1,8 +1,3 @@ -#include -#include -#include -#include - #include "barcode_generator.h" static BarcodeType* barcodeTypes[NUMBER_OF_BARCODE_TYPES]; @@ -103,9 +98,9 @@ int get_menu_text_location(int index) { } int get_barcode_max_index(PluginState* plugin_state) { - return plugin_state->doParityCalculation ? - barcodeTypes[plugin_state->barcodeTypeIndex]->numberOfDigits - 1 : - barcodeTypes[plugin_state->barcodeTypeIndex]->numberOfDigits; + return plugin_state->barcode_state.doParityCalculation ? + barcodeTypes[plugin_state->barcode_state.barcodeTypeIndex]->numberOfDigits - 1 : + barcodeTypes[plugin_state->barcode_state.barcodeTypeIndex]->numberOfDigits; } int calculate_check_digit(PluginState* plugin_state, BarcodeType* type) { @@ -114,12 +109,12 @@ int calculate_check_digit(PluginState* plugin_state, BarcodeType* type) { int checkDigitEven = 0; //add all odd positions. Confusing because 0index for(int i = 0; i < type->numberOfDigits - 1; i += 2) { - checkDigitOdd += plugin_state->barcodeNumeral[i]; + checkDigitOdd += plugin_state->barcode_state.barcodeNumeral[i]; } //add all even positions to above. Confusing because 0index for(int i = 1; i < type->numberOfDigits - 1; i += 2) { - checkDigitEven += plugin_state->barcodeNumeral[i]; + checkDigitEven += plugin_state->barcode_state.barcodeNumeral[i]; } if(type->bartype == BarTypeEAN13) { @@ -152,7 +147,7 @@ static void render_callback(Canvas* const canvas, void* ctx) { canvas, 64, get_menu_text_location(2), AlignCenter, AlignCenter, "Parity?"); canvas_draw_frame(canvas, 83, get_menu_text_location(2) - 3, 6, 6); - if(plugin_state->doParityCalculation == true) { + if(plugin_state->barcode_state.doParityCalculation == true) { canvas_draw_box(canvas, 85, get_menu_text_location(2) - 1, 2, 2); } canvas_draw_str_aligned( @@ -161,14 +156,14 @@ static void render_callback(Canvas* const canvas, void* ctx) { get_menu_text_location(3), AlignCenter, AlignCenter, - (barcodeTypes[plugin_state->barcodeTypeIndex])->name); + (barcodeTypes[plugin_state->barcode_state.barcodeTypeIndex])->name); canvas_draw_disc( canvas, 40, get_menu_text_location(plugin_state->menuIndex) - 1, 2); //draw menu cursor } else { - BarcodeType* type = barcodeTypes[plugin_state->barcodeTypeIndex]; + BarcodeType* type = barcodeTypes[plugin_state->barcode_state.barcodeTypeIndex]; //start saftey canvas_set_color(canvas, ColorBlack); @@ -181,13 +176,13 @@ static void render_callback(Canvas* const canvas, void* ctx) { startpos++; draw_digit( canvas, - plugin_state->barcodeNumeral[0], + plugin_state->barcode_state.barcodeNumeral[0], BarEncodingTypeRight, - get_digit_position(0, barcodeTypes[plugin_state->barcodeTypeIndex]), + get_digit_position(0, barcodeTypes[plugin_state->barcode_state.barcodeTypeIndex]), false); } - if(plugin_state->doParityCalculation) { //calculate the check digit - plugin_state->barcodeNumeral[type->numberOfDigits - 1] = + if(plugin_state->barcode_state.doParityCalculation) { //calculate the check digit + plugin_state->barcode_state.barcodeNumeral[type->numberOfDigits - 1] = calculate_check_digit(plugin_state, type); } for(int index = startpos; index < endpos; index++) { @@ -197,7 +192,9 @@ static void render_callback(Canvas* const canvas, void* ctx) { barEncodingType = BarEncodingTypeRight; } else { barEncodingType = - (FURI_BIT(EAN13ENCODE[plugin_state->barcodeNumeral[0]], index - 1)) ? + (FURI_BIT( + EAN13ENCODE[plugin_state->barcode_state.barcodeNumeral[0]], + index - 1)) ? BarEncodingTypeG : BarEncodingTypeLeft; } @@ -207,10 +204,14 @@ static void render_callback(Canvas* const canvas, void* ctx) { } } - int digitPosition = - get_digit_position(index, barcodeTypes[plugin_state->barcodeTypeIndex]); + int digitPosition = get_digit_position( + index, barcodeTypes[plugin_state->barcode_state.barcodeTypeIndex]); draw_digit( - canvas, plugin_state->barcodeNumeral[index], barEncodingType, digitPosition, true); + canvas, + plugin_state->barcode_state.barcodeNumeral[index], + barEncodingType, + digitPosition, + true); } //central separator @@ -223,7 +224,8 @@ static void render_callback(Canvas* const canvas, void* ctx) { canvas_draw_box( canvas, get_digit_position( - plugin_state->editingIndex, barcodeTypes[plugin_state->barcodeTypeIndex]) - + plugin_state->editingIndex, + barcodeTypes[plugin_state->barcode_state.barcodeTypeIndex]) - 1, 63, 7, @@ -247,15 +249,17 @@ static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queu furi_message_queue_put(event_queue, &event, FuriWaitForever); } -static void barcode_generator_state_init(PluginState* const plugin_state) { - for(int i = 0; i < BARCODE_MAX_LENS; ++i) { - plugin_state->barcodeNumeral[i] = i % 10; - } +static void barcode_generator_state_init(PluginState* plugin_state) { plugin_state->editingIndex = 0; plugin_state->mode = ViewMode; - plugin_state->doParityCalculation = true; plugin_state->menuIndex = MENU_INDEX_VIEW; - plugin_state->barcodeTypeIndex = 0; + if(!LOAD_BARCODE_SETTINGS(&plugin_state->barcode_state)) { + for(int i = 0; i < BARCODE_MAX_LENS; ++i) { + plugin_state->barcode_state.barcodeNumeral[i] = i % 10; + } + plugin_state->barcode_state.doParityCalculation = true; + plugin_state->barcode_state.barcodeTypeIndex = 0; + } } static bool handle_key_press_view(InputKey key, PluginState* plugin_state) { @@ -277,15 +281,15 @@ static bool handle_key_press_edit(InputKey key, PluginState* plugin_state) { switch(key) { case InputKeyUp: - plugin_state->barcodeNumeral[plugin_state->editingIndex] = - (plugin_state->barcodeNumeral[plugin_state->editingIndex] + 1) % 10; + plugin_state->barcode_state.barcodeNumeral[plugin_state->editingIndex] = + (plugin_state->barcode_state.barcodeNumeral[plugin_state->editingIndex] + 1) % 10; break; case InputKeyDown: - plugin_state->barcodeNumeral[plugin_state->editingIndex] = - (plugin_state->barcodeNumeral[plugin_state->editingIndex] == 0) ? + plugin_state->barcode_state.barcodeNumeral[plugin_state->editingIndex] = + (plugin_state->barcode_state.barcodeNumeral[plugin_state->editingIndex] == 0) ? 9 : - plugin_state->barcodeNumeral[plugin_state->editingIndex] - 1; + plugin_state->barcode_state.barcodeNumeral[plugin_state->editingIndex] - 1; break; case InputKeyRight: @@ -324,21 +328,24 @@ static bool handle_key_press_menu(InputKey key, PluginState* plugin_state) { case InputKeyRight: if(plugin_state->menuIndex == MENU_INDEX_TYPE) { - plugin_state->barcodeTypeIndex = - (plugin_state->barcodeTypeIndex == NUMBER_OF_BARCODE_TYPES - 1) ? + plugin_state->barcode_state.barcodeTypeIndex = + (plugin_state->barcode_state.barcodeTypeIndex == NUMBER_OF_BARCODE_TYPES - 1) ? 0 : - plugin_state->barcodeTypeIndex + 1; + plugin_state->barcode_state.barcodeTypeIndex + 1; } else if(plugin_state->menuIndex == MENU_INDEX_PARITY) { - plugin_state->doParityCalculation = !plugin_state->doParityCalculation; + plugin_state->barcode_state.doParityCalculation = + !plugin_state->barcode_state.doParityCalculation; } break; case InputKeyLeft: if(plugin_state->menuIndex == MENU_INDEX_TYPE) { - plugin_state->barcodeTypeIndex = (plugin_state->barcodeTypeIndex == 0) ? - NUMBER_OF_BARCODE_TYPES - 1 : - plugin_state->barcodeTypeIndex - 1; + plugin_state->barcode_state.barcodeTypeIndex = + (plugin_state->barcode_state.barcodeTypeIndex == 0) ? + NUMBER_OF_BARCODE_TYPES - 1 : + plugin_state->barcode_state.barcodeTypeIndex - 1; } else if(plugin_state->menuIndex == MENU_INDEX_PARITY) { - plugin_state->doParityCalculation = !plugin_state->doParityCalculation; + plugin_state->barcode_state.doParityCalculation = + !plugin_state->barcode_state.doParityCalculation; } break; @@ -348,12 +355,13 @@ static bool handle_key_press_menu(InputKey key, PluginState* plugin_state) { } else if(plugin_state->menuIndex == MENU_INDEX_EDIT) { plugin_state->mode = EditMode; } else if(plugin_state->menuIndex == MENU_INDEX_PARITY) { - plugin_state->doParityCalculation = !plugin_state->doParityCalculation; + plugin_state->barcode_state.doParityCalculation = + !plugin_state->barcode_state.doParityCalculation; } else if(plugin_state->menuIndex == MENU_INDEX_TYPE) { - plugin_state->barcodeTypeIndex = - (plugin_state->barcodeTypeIndex == NUMBER_OF_BARCODE_TYPES - 1) ? + plugin_state->barcode_state.barcodeTypeIndex = + (plugin_state->barcode_state.barcodeTypeIndex == NUMBER_OF_BARCODE_TYPES - 1) ? 0 : - plugin_state->barcodeTypeIndex + 1; + plugin_state->barcode_state.barcodeTypeIndex + 1; } break; @@ -430,6 +438,9 @@ int32_t barcode_generator_app(void* p) { furi_record_close(RECORD_GUI); view_port_free(view_port); furi_message_queue_free(event_queue); + // save settings + SAVE_BARCODE_SETTINGS(&plugin_state->barcode_state); + free(plugin_state); return 0; } diff --git a/applications/plugins/barcode_generator/barcode_generator.h b/applications/plugins/barcode_generator/barcode_generator.h index 1b5ff9e15..b39e43320 100644 --- a/applications/plugins/barcode_generator/barcode_generator.h +++ b/applications/plugins/barcode_generator/barcode_generator.h @@ -1,3 +1,34 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#define BARCODE_SETTINGS_FILE_NAME "apps/Misc/barcodegen.save" + +#define BARCODE_SETTINGS_VER (1) +#define BARCODE_SETTINGS_PATH EXT_PATH(BARCODE_SETTINGS_FILE_NAME) +#define BARCODE_SETTINGS_MAGIC (0xC2) + +#define SAVE_BARCODE_SETTINGS(x) \ + saved_struct_save( \ + BARCODE_SETTINGS_PATH, \ + (x), \ + sizeof(BarcodeState), \ + BARCODE_SETTINGS_MAGIC, \ + BARCODE_SETTINGS_VER) + +#define LOAD_BARCODE_SETTINGS(x) \ + saved_struct_load( \ + BARCODE_SETTINGS_PATH, \ + (x), \ + sizeof(BarcodeState), \ + BARCODE_SETTINGS_MAGIC, \ + BARCODE_SETTINGS_VER) + #define BARCODE_HEIGHT 50 #define BARCODE_Y_START 3 #define BARCODE_TEXT_OFFSET 9 @@ -45,11 +76,15 @@ typedef struct { typedef struct { int barcodeNumeral[BARCODE_MAX_LENS]; //The current barcode number + bool doParityCalculation; //Should do parity check? + int barcodeTypeIndex; +} BarcodeState; + +typedef struct { + BarcodeState barcode_state; int editingIndex; //The index of the editing symbol int menuIndex; //The index of the menu cursor Mode mode; //View, edit or menu - bool doParityCalculation; //Should do parity check? - int barcodeTypeIndex; } PluginState; static const int DIGITS[10][4] = {