settings.cxx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include <flipper_format/flipper_format.h>
  2. #include "settings.h"
  3. #define PINBALL_SETTINGS_FILENAME ".pinball0.conf"
  4. #define PINBALL_SETTINGS_PATH APP_DATA_PATH(PINBALL_SETTINGS_FILENAME)
  5. #define PINBALL_SETTINGS_FILE_TYPE "Pinball0 Settings File"
  6. #define PINBALL_SETTINGS_FILE_VERSION 1
  7. void pinball_load_settings(PinballApp* pb) {
  8. FlipperFormat* fff_settings = flipper_format_file_alloc(pb->storage);
  9. FuriString* tmp_str = furi_string_alloc();
  10. uint32_t tmp_data32 = 0;
  11. // init the settings to default values, then overwrite them if found in the settings file
  12. pb->settings.sound_enabled = true;
  13. pb->settings.led_enabled = true;
  14. pb->settings.vibrate_enabled = true;
  15. pb->settings.debug_mode = false;
  16. pb->selected_setting = 0;
  17. pb->max_settings = 4;
  18. do {
  19. if(!flipper_format_file_open_existing(fff_settings, PINBALL_SETTINGS_PATH)) {
  20. FURI_LOG_I(TAG, "SETTINGS: File not found, using defaults");
  21. break;
  22. }
  23. if(!flipper_format_read_header(fff_settings, tmp_str, &tmp_data32)) {
  24. FURI_LOG_E(TAG, "SETTINGS: Missing or incorrect header");
  25. break;
  26. }
  27. if(!strcmp(furi_string_get_cstr(tmp_str), PINBALL_SETTINGS_FILE_TYPE) &&
  28. (tmp_data32 == PINBALL_SETTINGS_FILE_VERSION)) {
  29. } else {
  30. FURI_LOG_E(TAG, "SETTINGS: Type or version mismatch");
  31. break;
  32. }
  33. if(flipper_format_read_uint32(fff_settings, "Sound", &tmp_data32, 1)) {
  34. pb->settings.sound_enabled = (tmp_data32 == 0) ? false : true;
  35. }
  36. if(flipper_format_read_uint32(fff_settings, "LED", &tmp_data32, 1)) {
  37. pb->settings.led_enabled = (tmp_data32 == 0) ? false : true;
  38. }
  39. if(flipper_format_read_uint32(fff_settings, "Vibrate", &tmp_data32, 1)) {
  40. pb->settings.vibrate_enabled = (tmp_data32 == 0) ? false : true;
  41. }
  42. if(flipper_format_read_uint32(fff_settings, "Debug", &tmp_data32, 1)) {
  43. pb->settings.debug_mode = (tmp_data32 == 0) ? false : true;
  44. }
  45. } while(false);
  46. furi_string_free(tmp_str);
  47. flipper_format_free(fff_settings);
  48. }
  49. void pinball_save_settings(PinballApp* pb) {
  50. FlipperFormat* fff_settings = flipper_format_file_alloc(pb->storage);
  51. uint32_t tmp_data32 = 0;
  52. FURI_LOG_I(TAG, "SETTINGS: Saving settings");
  53. do {
  54. if(!flipper_format_file_open_always(fff_settings, PINBALL_SETTINGS_PATH)) {
  55. FURI_LOG_E(TAG, "SETTINGS: Unable to open file for save!");
  56. break;
  57. }
  58. if(!flipper_format_write_header_cstr(
  59. fff_settings, PINBALL_SETTINGS_FILE_TYPE, PINBALL_SETTINGS_FILE_VERSION)) {
  60. FURI_LOG_E(TAG, "SETTINGS: Failed writing file type and version");
  61. break;
  62. }
  63. // now write out our settings data
  64. tmp_data32 = pb->settings.sound_enabled ? 1 : 0;
  65. if(!flipper_format_write_uint32(fff_settings, "Sound", &tmp_data32, 1)) {
  66. FURI_LOG_E(TAG, "SETTINGS: Failed to write 'Sound'");
  67. break;
  68. }
  69. tmp_data32 = pb->settings.led_enabled ? 1 : 0;
  70. if(!flipper_format_write_uint32(fff_settings, "LED", &tmp_data32, 1)) {
  71. FURI_LOG_E(TAG, "SETTINGS: Failed to write 'LED'");
  72. break;
  73. }
  74. tmp_data32 = pb->settings.vibrate_enabled ? 1 : 0;
  75. if(!flipper_format_write_uint32(fff_settings, "Vibrate", &tmp_data32, 1)) {
  76. FURI_LOG_E(TAG, "SETTINGS: Failed to write 'Vibrate'");
  77. break;
  78. }
  79. tmp_data32 = pb->settings.debug_mode ? 1 : 0;
  80. if(!flipper_format_write_uint32(fff_settings, "Debug", &tmp_data32, 1)) {
  81. FURI_LOG_E(TAG, "SETTINGS: Failed to write 'Debug'");
  82. break;
  83. }
  84. } while(false);
  85. flipper_format_file_close(fff_settings);
  86. flipper_format_free(fff_settings);
  87. }