settings.cxx 3.9 KB

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