Skip to content

Commit

Permalink
Prelim state save/load, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharyweiss committed Apr 28, 2024
1 parent 0acc57d commit ae5ca16
Show file tree
Hide file tree
Showing 14 changed files with 308 additions and 161 deletions.
79 changes: 28 additions & 51 deletions helpers/mag_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,8 @@ const int sublen[] = {32, 48, 48};

uint8_t last_value = 2;

const GpioPin* mag_gpio_enum_to_pin(MagSettingPin pin) {
switch(pin) {
case MagSettingPinA7:
return &gpio_ext_pa7;
case MagSettingPinA6:
return &gpio_ext_pa6;
case MagSettingPinA4:
return &gpio_ext_pa4;
case MagSettingPinB3:
return &gpio_ext_pb3;
case MagSettingPinB2:
return &gpio_ext_pb2;
case MagSettingPinC3:
return &gpio_ext_pc3;
case MagSettingPinC1:
return &gpio_ext_pc1;
case MagSettingPinC0:
return &gpio_ext_pc0;
default:
return NULL;
}
}

void play_halfbit(bool value, MagSetting* setting) {
switch(setting->tx) {
void play_halfbit(bool value, MagState* state) {
switch(state->tx) {
case MagTxStateRFID:
furi_hal_gpio_write(RFID_PIN_OUT, value);
/*furi_hal_gpio_write(RFID_PIN_OUT, !value);
Expand Down Expand Up @@ -111,7 +88,7 @@ void play_halfbit(bool value, MagSetting* setting) {
last_value = value;
}

void play_track(uint8_t* bits_manchester, uint16_t n_bits, MagSetting* setting, bool reverse) {
void play_track(uint8_t* bits_manchester, uint16_t n_bits, MagState* state, bool reverse) {
for(uint16_t i = 0; i < n_bits; i++) {
uint16_t j = (reverse) ? (n_bits - i - 1) : i;
uint8_t byte = j / 8;
Expand Down Expand Up @@ -140,9 +117,9 @@ void play_track(uint8_t* bits_manchester, uint16_t n_bits, MagSetting* setting,
// for DWT->CYCCNT. Note timer is aliased to 64us as per
// #define FURI_HAL_CORTEX_INSTRUCTIONS_PER_MICROSECOND (SystemCoreClock / 1000000) | furi_hal_cortex.c

play_halfbit(bit, setting);
furi_delay_us(setting->us_clock);
// if (i % 2 == 1) furi_delay_us(setting->us_interpacket);
play_halfbit(bit, state);
furi_delay_us(state->us_clock);
// if (i % 2 == 1) furi_delay_us(state->us_interpacket);
}
}

Expand All @@ -154,7 +131,7 @@ void tx_init_rfid() {
// furi_hal_ibutton_start_drive();
furi_hal_ibutton_pin_write(false);

// Initializing at GpioSpeedLow seems sufficient for our needs; no improvements seen by increasing speed setting
// Initializing at GpioSpeedLow seems sufficient for our needs; no improvements seen by increasing speed state

// this doesn't seem to make a difference, leaving it in
furi_hal_gpio_init(&gpio_rfid_data_in, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
Expand Down Expand Up @@ -202,9 +179,9 @@ void tx_deinit_piezo() {
furi_hal_gpio_init(&gpio_speaker, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
}

bool tx_init(MagSetting* setting) {
bool tx_init(MagState* state) {
// Initialize configured TX method
switch(setting->tx) {
switch(state->tx) {
case MagTxStateRFID:
tx_init_rfid();
break;
Expand Down Expand Up @@ -242,9 +219,9 @@ bool tx_init(MagSetting* setting) {
return true;
}

bool tx_deinit(MagSetting* setting) {
bool tx_deinit(MagState* state) {
// Reset configured TX method
switch(setting->tx) {
switch(state->tx) {
case MagTxStateRFID:
tx_deinit_rfid();
break;
Expand Down Expand Up @@ -285,7 +262,7 @@ bool tx_deinit(MagSetting* setting) {
}

void mag_spoof(Mag* mag) {
MagSetting* setting = mag->setting;
MagState* state = &mag->state;

// TODO: cleanup this section. Possibly move precompute + tx_init to emulate_on_enter?
FuriString* ft1 = mag->mag_dev->dev_data.track[0].str;
Expand Down Expand Up @@ -343,47 +320,47 @@ void mag_spoof(Mag* mag) {
last_value = 2;
bool bit = false;

if(!tx_init(setting)) return;
if(!tx_init(state)) return;

FURI_CRITICAL_ENTER();
for(uint16_t i = 0; i < (ZERO_PREFIX * 2); i++) {
// is this right?
if(!!(i % 2)) bit ^= 1;
play_halfbit(bit, setting);
furi_delay_us(setting->us_clock);
play_halfbit(bit, state);
furi_delay_us(state->us_clock);
}

if((setting->track == MagTrackStateOneAndTwo) || (setting->track == MagTrackStateOne))
play_track((uint8_t*)bits_t1_manchester, bits_t1_count, setting, false);
if((state->track == MagTrackStateOneAndTwo) || (state->track == MagTrackStateOne))
play_track((uint8_t*)bits_t1_manchester, bits_t1_count, state, false);

if((setting->track == MagTrackStateOneAndTwo))
if((state->track == MagTrackStateOneAndTwo))
for(uint16_t i = 0; i < (ZERO_BETWEEN * 2); i++) {
if(!!(i % 2)) bit ^= 1;
play_halfbit(bit, setting);
furi_delay_us(setting->us_clock);
play_halfbit(bit, state);
furi_delay_us(state->us_clock);
}

if((setting->track == MagTrackStateOneAndTwo) || (setting->track == MagTrackStateTwo))
if((state->track == MagTrackStateOneAndTwo) || (state->track == MagTrackStateTwo))
play_track(
(uint8_t*)bits_t2_manchester,
bits_t2_count,
setting,
(setting->reverse == MagReverseStateOn));
state,
(state->reverse == MagReverseStateOn));

if((setting->track == MagTrackStateThree))
play_track((uint8_t*)bits_t3_manchester, bits_t3_count, setting, false);
if((state->track == MagTrackStateThree))
play_track((uint8_t*)bits_t3_manchester, bits_t3_count, state, false);

for(uint16_t i = 0; i < (ZERO_SUFFIX * 2); i++) {
if(!!(i % 2)) bit ^= 1;
play_halfbit(bit, setting);
furi_delay_us(setting->us_clock);
play_halfbit(bit, state);
furi_delay_us(state->us_clock);
}
FURI_CRITICAL_EXIT();

free(data1);
free(data2);
free(data3);
tx_deinit(setting);
tx_deinit(state);
}

uint16_t add_bit(bool value, uint8_t* out, uint16_t count) {
Expand Down
10 changes: 4 additions & 6 deletions helpers/mag_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
#include <stdio.h>
#include <string.h>

const GpioPin* mag_gpio_enum_to_pin(MagSettingPin pin);

void play_halfbit(bool value, MagSetting* setting);
void play_track(uint8_t* bits_manchester, uint16_t n_bits, MagSetting* setting, bool reverse);
void play_halfbit(bool value, MagState* state);
void play_track(uint8_t* bits_manchester, uint16_t n_bits, MagState* state, bool reverse);

void tx_init_rf(int hz);
void tx_init_rfid();
void tx_init_piezo();
bool tx_init(MagSetting* setting);
bool tx_init(MagState* state);
void tx_deinit_piezo();
void tx_deinit_rfid();
bool tx_deinit(MagSetting* setting);
bool tx_deinit(MagState* state);

uint16_t add_bit(bool value, uint8_t* out, uint16_t count);
uint16_t add_bit_manchester(bool value, uint8_t* out, uint16_t count);
Expand Down
11 changes: 0 additions & 11 deletions helpers/mag_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ typedef enum {
MagViewTextInput,
} MagView;

typedef enum {
MagSettingPinA7,
MagSettingPinA6,
MagSettingPinA4,
MagSettingPinB3,
MagSettingPinB2,
MagSettingPinC3,
MagSettingPinC1,
MagSettingPinC0,
} MagSettingPin;

typedef enum {
MagReverseStateOff,
MagReverseStateOn,
Expand Down
32 changes: 3 additions & 29 deletions mag.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,6 @@ static bool mag_debug_back_event_callback(void* context) {
return scene_manager_handle_back_event(mag->scene_manager);
}

static MagSetting* mag_setting_alloc() {
// temp hardcoded defaults
MagSetting* setting = malloc(sizeof(MagSetting));
setting->reverse = SETTING_DEFAULT_REVERSE;
setting->track = SETTING_DEFAULT_TRACK;
setting->tx = SETTING_DEFAULT_TX;
setting->us_clock = SETTING_DEFAULT_US_CLOCK;
setting->us_interpacket = SETTING_DEFAULT_US_INTERPACKET;

setting->pin_input = SETTING_DEFAULT_PIN_INPUT;
setting->pin_output = SETTING_DEFAULT_PIN_OUTPUT;
setting->pin_enable = SETTING_DEFAULT_PIN_ENABLE;

setting->is_debug = furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug);

return setting;
}

static Mag* mag_alloc() {
Mag* mag = malloc(sizeof(Mag));

Expand All @@ -52,7 +34,7 @@ static Mag* mag_alloc() {
mag->view_dispatcher, mag_debug_back_event_callback);

mag->mag_dev = mag_device_alloc();
mag->setting = mag_setting_alloc();
mag_state_load(&mag->state);

// Open GUI record
mag->gui = furi_record_open(RECORD_GUI);
Expand Down Expand Up @@ -97,13 +79,9 @@ static Mag* mag_alloc() {
mag->expansion = furi_record_open(RECORD_EXPANSION);
expansion_disable(mag->expansion);

return mag;
}
// Move UART here? conditional upon setting?

static void mag_setting_free(MagSetting* setting) {
furi_assert(setting);

free(setting);
return mag;
}

static void mag_free(Mag* mag) {
Expand All @@ -117,10 +95,6 @@ static void mag_free(Mag* mag) {
mag_device_free(mag->mag_dev);
mag->mag_dev = NULL;

// Mag setting
mag_setting_free(mag->setting);
mag->setting = NULL;

// Submenu
view_dispatcher_remove_view(mag->view_dispatcher, MagViewSubmenu);
submenu_free(mag->submenu);
Expand Down
24 changes: 2 additions & 22 deletions mag_i.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "mag_device.h"
#include "mag_state.h"
//#include "helpers/mag_helpers.h"
#include "helpers/mag_types.h"

Expand Down Expand Up @@ -47,33 +48,12 @@
}
#endif

#define SETTING_DEFAULT_REVERSE MagReverseStateOff
#define SETTING_DEFAULT_TRACK MagTrackStateOneAndTwo
#define SETTING_DEFAULT_TX MagTxStateGPIO
#define SETTING_DEFAULT_US_CLOCK 240
#define SETTING_DEFAULT_US_INTERPACKET 10
#define SETTING_DEFAULT_PIN_INPUT MagSettingPinA7
#define SETTING_DEFAULT_PIN_OUTPUT MagSettingPinA6
#define SETTING_DEFAULT_PIN_ENABLE MagSettingPinA4

enum MagCustomEvent {
MagEventNext = 100,
MagEventExit,
MagEventPopupClosed,
};

typedef struct {
MagTxState tx;
MagTrackState track;
MagReverseState reverse;
uint32_t us_clock;
uint32_t us_interpacket;
MagSettingPin pin_input;
MagSettingPin pin_output;
MagSettingPin pin_enable;
bool is_debug;
} MagSetting;

typedef struct {
ViewDispatcher* view_dispatcher;
Gui* gui;
Expand All @@ -88,7 +68,7 @@ typedef struct {
FuriString* file_name;
FuriString* args;

MagSetting* setting;
MagState state;

// Common views
Submenu* submenu;
Expand Down
Loading

0 comments on commit ae5ca16

Please sign in to comment.