mag_state.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "mag_state.h"
  2. #define TAG "MagState"
  3. const GpioPin* mag_state_enum_to_pin(MagPin pin) {
  4. switch(pin) {
  5. case MagPinA7:
  6. return &gpio_ext_pa7;
  7. case MagPinA6:
  8. return &gpio_ext_pa6;
  9. case MagPinA4:
  10. return &gpio_ext_pa4;
  11. case MagPinB3:
  12. return &gpio_ext_pb3;
  13. case MagPinB2:
  14. return &gpio_ext_pb2;
  15. case MagPinC3:
  16. return &gpio_ext_pc3;
  17. case MagPinC1:
  18. return &gpio_ext_pc1;
  19. case MagPinC0:
  20. return &gpio_ext_pc0;
  21. default:
  22. return NULL;
  23. }
  24. }
  25. bool mag_state_gpio_is_valid(MagState* state) {
  26. return (state->pin_input != state->pin_output) && (state->pin_input != state->pin_enable) &&
  27. (state->pin_enable != state->pin_output);
  28. }
  29. void mag_state_gpio_reset(MagState* state) {
  30. state->pin_input = MAG_STATE_DEFAULT_PIN_INPUT;
  31. state->pin_output = MAG_STATE_DEFAULT_PIN_OUTPUT;
  32. state->pin_enable = MAG_STATE_DEFAULT_PIN_ENABLE;
  33. }
  34. bool mag_state_load(MagState* out_state) {
  35. MagState state;
  36. // Try to load from file
  37. bool loaded_from_file = false;
  38. Storage* storage = furi_record_open(RECORD_STORAGE);
  39. if(storage_file_exists(storage, MAG_STATE_PATH)) {
  40. FlipperFormat* file = flipper_format_file_alloc(storage);
  41. do {
  42. uint32_t tmp;
  43. FuriString* str = furi_string_alloc();
  44. if(!flipper_format_file_open_existing(file, MAG_STATE_PATH)) break;
  45. if(!flipper_format_read_header(file, str, &tmp)) break;
  46. if(furi_string_cmp_str(str, MAG_STATE_HEADER)) break;
  47. if(tmp != MAG_STATE_VER) break;
  48. if(!flipper_format_read_uint32(file, "pin_input", &tmp, 1)) break;
  49. state.pin_input = tmp;
  50. if(!flipper_format_read_uint32(file, "pin_output", &tmp, 1)) break;
  51. state.pin_output = tmp;
  52. if(!flipper_format_read_uint32(file, "pin_enable", &tmp, 1)) break;
  53. state.pin_enable = tmp;
  54. loaded_from_file = true;
  55. } while(0);
  56. flipper_format_free(file);
  57. }
  58. furi_record_close(RECORD_STORAGE);
  59. // If could not be read from file
  60. // Or file GPIO config is invalid (pins overlap)
  61. // Set defaults
  62. if(!loaded_from_file || !mag_state_gpio_is_valid(&state)) {
  63. mag_state_gpio_reset(&state);
  64. }
  65. // set defaults we don't save
  66. state.tx = MAG_STATE_DEFAULT_TX;
  67. state.track = MAG_STATE_DEFAULT_TRACK;
  68. state.reverse = MAG_STATE_DEFAULT_REVERSE;
  69. state.us_clock = MAG_STATE_DEFAULT_US_CLOCK;
  70. state.us_interpacket = MAG_STATE_DEFAULT_US_INTERPACKET;
  71. state.is_debug = furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug);
  72. // Copy to caller state before popping stack
  73. memcpy(out_state, &state, sizeof(state));
  74. return loaded_from_file;
  75. }
  76. void mag_state_save(MagState* state) {
  77. Storage* storage = furi_record_open(RECORD_STORAGE);
  78. storage_simply_mkdir(storage, MAG_STATE_DIR);
  79. FlipperFormat* file = flipper_format_file_alloc(storage);
  80. do {
  81. uint32_t tmp;
  82. if(!flipper_format_file_open_always(file, MAG_STATE_PATH)) break;
  83. if(!flipper_format_write_header_cstr(file, MAG_STATE_HEADER, MAG_STATE_VER)) break;
  84. tmp = state->pin_input;
  85. if(!flipper_format_write_uint32(file, "pin_input", &tmp, 1)) break;
  86. tmp = state->pin_output;
  87. if(!flipper_format_write_uint32(file, "pin_output", &tmp, 1)) break;
  88. tmp = state->pin_enable;
  89. if(!flipper_format_write_uint32(file, "pin_enable", &tmp, 1)) break;
  90. } while(0);
  91. flipper_format_free(file);
  92. furi_record_close(RECORD_STORAGE);
  93. }