preferences.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * preferences.c
  3. *
  4. * Created on: Feb 22, 2022
  5. * Author: radioman
  6. */
  7. #include "preferences.h"
  8. #include "stm32l1xx_hal.h"
  9. static Pref_td Preferences[PREF_COUNT];
  10. static const Pref_td SavedPreferences[PREF_COUNT] = PREF_DEFAULT_VALUES;
  11. void PrefIncValue(PrefNames_t PrefName)
  12. {
  13. Preferences[PrefName].value = (Preferences[PrefName].value +
  14. Preferences[PrefName].step) % (Preferences[PrefName].max_val + 1);
  15. Preferences[PrefName].unsaved = true;
  16. }
  17. void PrefSetValue(PrefNames_t PrefName, int val)
  18. {
  19. Preferences[PrefName].value = val;
  20. Preferences[PrefName].unsaved = true;
  21. }
  22. int PrefGetValue(PrefNames_t PrefName)
  23. {
  24. return Preferences[PrefName].value;
  25. }
  26. void PrefLoadFromFlash()
  27. {
  28. for(int i = 0; i < PREF_COUNT; i++)
  29. {
  30. Preferences[i] = SavedPreferences[i];
  31. }
  32. }
  33. void PrefSaveToFlash()
  34. {
  35. HAL_FLASH_Unlock();
  36. for(int i = 0; i < PREF_COUNT; i++)
  37. {
  38. if(Preferences[i].unsaved)
  39. {
  40. if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)(&(SavedPreferences[i].value)), Preferences[i].value) == HAL_OK)
  41. Preferences[i].unsaved = false;
  42. }
  43. }
  44. HAL_FLASH_Lock();
  45. }