Skip to content

Commit

Permalink
Refactor track autoselect
Browse files Browse the repository at this point in the history
Prior hacky approach didn't work for launches w/ args (straight to emulate scene), this fixes it
  • Loading branch information
zacharyweiss committed Apr 28, 2024
1 parent 51179f0 commit ab15432
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 31 deletions.
41 changes: 31 additions & 10 deletions helpers/mag_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@
#define MAG_DEVELOPER "Zachary Weiss"
#define MAG_GITHUB "github.com/zacharyweiss/magspoof_flipper"

typedef enum {
MagViewSubmenu,
MagViewDialogEx,
MagViewPopup,
MagViewLoading,
MagViewWidget,
MagViewVariableItemList,
MagViewTextInput,
} MagView;

typedef enum {
MagReverseStateOff,
MagReverseStateOn,
Expand All @@ -36,6 +26,37 @@ typedef enum {
MagTxCC1101_868,
} MagTxState;

typedef enum {
MagPinA7,
MagPinA6,
MagPinA4,
MagPinB3,
MagPinB2,
MagPinC3,
MagPinC1,
MagPinC0,
} MagPin;

#define MAG_STATE_DEFAULT_REVERSE MagReverseStateOff
#define MAG_STATE_DEFAULT_TRACK MagTrackStateOneAndTwo
#define MAG_STATE_DEFAULT_TX MagTxStateGPIO
#define MAG_STATE_DEFAULT_US_CLOCK 240
#define MAG_STATE_DEFAULT_US_INTERPACKET 10
#define MAG_STATE_DEFAULT_PIN_INPUT MagPinA7
#define MAG_STATE_DEFAULT_PIN_OUTPUT MagPinA6
#define MAG_STATE_DEFAULT_PIN_ENABLE MagPinA4
#define MAG_STATE_DEFAULT_ALLOW_UART false

typedef enum {
MagViewSubmenu,
MagViewDialogEx,
MagViewPopup,
MagViewLoading,
MagViewWidget,
MagViewVariableItemList,
MagViewTextInput,
} MagView;

typedef enum {
UART_TerminalEventRefreshConsoleOutput = 0,
UART_TerminalEventStartConsole,
Expand Down
4 changes: 4 additions & 0 deletions mag.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ int32_t mag_app(void* p) {
scene_manager_next_scene(mag->scene_manager, MagSceneStart);
} else {
mag_device_load_data(mag->mag_dev, mag->args, true);
MagTrackState auto_track = mag_device_autoselect_track_state(mag->mag_dev);
if(auto_track) {
mag->state.track = auto_track;
}
scene_manager_next_scene(mag->scene_manager, MagSceneEmulate);
}

Expand Down
21 changes: 21 additions & 0 deletions mag_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,27 @@ bool mag_device_parse_card_string(MagDevice* mag_dev, FuriString* f_card_str) {
return true;
}

MagTrackState mag_device_autoselect_track_state(MagDevice* mag_dev) {
// messy code to quickly check which tracks are available for emulation/display
bool is_empty_t1 = furi_string_empty(mag_dev->dev_data.track[0].str);
bool is_empty_t2 = furi_string_empty(mag_dev->dev_data.track[1].str);
bool is_empty_t3 = furi_string_empty(mag_dev->dev_data.track[2].str);

if(!is_empty_t1 && !is_empty_t2) {
return MagTrackStateOneAndTwo;
} else if(!is_empty_t1) {
return MagTrackStateOne;
} else if(!is_empty_t2) {
return MagTrackStateTwo;
} else if(!is_empty_t3) {
return MagTrackStateThree;
}

// if all empty (or something wrong with the above code)
// return default value
return MAG_STATE_DEFAULT_TRACK;
}

void mag_device_set_loading_callback(
MagDevice* mag_dev,
MagLoadingCallback callback,
Expand Down
3 changes: 3 additions & 0 deletions mag_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <dialogs/dialogs.h>

#include "mag_icons.h"
#include "helpers/mag_types.h"

#include <assets_icons.h>

Expand Down Expand Up @@ -56,6 +57,8 @@ bool mag_device_delete(MagDevice* mag_dev, bool use_load_path);

bool mag_device_parse_card_string(MagDevice* mag_dev, FuriString* card_str);

MagTrackState mag_device_autoselect_track_state(MagDevice* mag_dev);

void mag_device_set_loading_callback(
MagDevice* mag_dev,
MagLoadingCallback callback,
Expand Down
21 changes: 0 additions & 21 deletions mag_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,6 @@
#define MAG_STATE_DIR STORAGE_APP_DATA_PATH_PREFIX
#define MAG_STATE_PATH MAG_STATE_DIR "/mag_state.txt"

typedef enum {
MagPinA7,
MagPinA6,
MagPinA4,
MagPinB3,
MagPinB2,
MagPinC3,
MagPinC1,
MagPinC0,
} MagPin;

#define MAG_STATE_DEFAULT_REVERSE MagReverseStateOff
#define MAG_STATE_DEFAULT_TRACK MagTrackStateOneAndTwo
#define MAG_STATE_DEFAULT_TX MagTxStateGPIO
#define MAG_STATE_DEFAULT_US_CLOCK 240
#define MAG_STATE_DEFAULT_US_INTERPACKET 10
#define MAG_STATE_DEFAULT_PIN_INPUT MagPinA7
#define MAG_STATE_DEFAULT_PIN_OUTPUT MagPinA6
#define MAG_STATE_DEFAULT_PIN_ENABLE MagPinA4
#define MAG_STATE_DEFAULT_ALLOW_UART false

typedef struct {
MagTxState tx;
MagTrackState track;
Expand Down
4 changes: 4 additions & 0 deletions scenes/mag_scene_file_select.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ void mag_scene_file_select_on_enter(void* context) {
Mag* mag = context;
mag_device_set_loading_callback(mag->mag_dev, mag_show_loading_popup, mag);
if(mag_file_select(mag->mag_dev)) {
MagTrackState auto_track = mag_device_autoselect_track_state(mag->mag_dev);
if(auto_track) {
mag->state.track = auto_track;
}
scene_manager_next_scene(mag->scene_manager, MagSceneSavedMenu);
} else {
scene_manager_search_and_switch_to_previous_scene(mag->scene_manager, MagSceneStart);
Expand Down

0 comments on commit ab15432

Please sign in to comment.