settings.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include <util/crc16.h>
  2. #include "main.h"
  3. #include "settings.h"
  4. #include "display.h"
  5. #include "xprintf.h"
  6. #include "working_modes.h"
  7. #include "nmea.h"
  8. #include "menu.h"
  9. EEMEM struct config_s config_eep;
  10. EEMEM unsigned char config_crc;
  11. const __flash unsigned char limits_max_u8[] = {
  12. [CONF_U8_GNSS_MODE] = 5,
  13. [CONF_U8_SKIP_POINTS] = 120,
  14. };
  15. unsigned char settings_load(void) { /* 0 - ok, 1 - error */
  16. unsigned char crc=0, rcrc, i;
  17. unsigned char *cptr = (unsigned char *)&System.conf;
  18. unsigned char ret;
  19. eeprom_read_block(cptr, &config_eep, sizeof(struct config_s));
  20. for (i=0; i<sizeof(struct config_s); i++) {
  21. crc = _crc_ibutton_update(crc, cptr[i]);
  22. }
  23. rcrc = eeprom_read_byte(&config_crc);
  24. crc = _crc_ibutton_update(crc, rcrc);
  25. ret = check_config_data();
  26. if (crc) {
  27. xputs_P(PSTR("EEPROM read: bad CRC\r\n"));
  28. } else if (ret) {
  29. xputs_P(PSTR("EEPROM read: bad data\r\n"));
  30. } else {
  31. xputs_P(PSTR("EEPROM read OK\r\n"));
  32. }
  33. ret = ret || crc;
  34. return ret;
  35. }
  36. unsigned char check_config_data(void) { /* 0 - ok, 1 - error */
  37. unsigned char i, ret=0;
  38. for (i=0; i<=CONF_U8_LAST; i++) {
  39. if (System.conf.conf_u8[i] > limits_max_u8[i]) {
  40. ret = 1;
  41. System.conf.conf_u8[i] = 0;
  42. }
  43. }
  44. return ret;
  45. }
  46. void settings_store(void) {
  47. unsigned char i, crc=0;
  48. unsigned char *cptr = (unsigned char *)&System.conf;
  49. eeprom_update_block(cptr, &config_eep, sizeof(struct config_s));
  50. for (i=0; i<sizeof(struct config_s); i++) {
  51. crc = _crc_ibutton_update(crc, cptr[i]);
  52. }
  53. eeprom_update_byte(&config_crc, crc);
  54. xputs_P(PSTR("EEPROM write done\r\n"));
  55. }
  56. unsigned char get_flag(unsigned char index) {
  57. volatile unsigned char *sptr = &System.conf.flags[index/8];
  58. index %= 8;
  59. unsigned char val = (*sptr) & _BV(index);
  60. return val;
  61. }
  62. void set_flag(unsigned char index, unsigned char val) {
  63. volatile unsigned char *sptr = &System.conf.flags[index/8];
  64. index %= 8;
  65. if (val)
  66. *sptr |= _BV(index);
  67. else
  68. *sptr &= ~_BV(index);
  69. }
  70. void display_gnss_mode(unsigned char val) {
  71. strcpy_P(disp.line2, gnss_names[val]);
  72. }
  73. void display_current_gnss_mode(void) {
  74. display_gnss_mode(System.conf.gnss_mode);
  75. }
  76. /* SETTINGS ITEMS */
  77. __flash const char _msg_disable_filters[] = "Nie filtruj";
  78. __flash const char _msg_enable_sbas[] = "Szukaj SBAS";
  79. __flash const char _msg_gnss_type[] = "Rodzaj GNSS";
  80. __flash const char _msg_skip_points[] = "Pomin punkty";
  81. __flash const char _msg_logging_after_boot[] = "Zapis po wlacz.";
  82. __flash const char _msg_back[] = "< Powrot";
  83. __flash const struct menu_pos settings_menu_list[] = {
  84. {
  85. .type = MENU_TYPE_DISPLAY,
  86. .display_type = MENU_DISPLAY_TYPE_STRING,
  87. .name = _msg_back,
  88. .allow_back = 1,
  89. },
  90. {
  91. .type = MENU_TYPE_SETTING_BOOL,
  92. .name = _msg_disable_filters,
  93. .index = CONFFLAG_DISABLE_FILTERS,
  94. },
  95. {
  96. .type = MENU_TYPE_SETTING_U8,
  97. .name = _msg_skip_points,
  98. .index = CONF_U8_SKIP_POINTS,
  99. },
  100. {
  101. .type = MENU_TYPE_SETTING_BOOL,
  102. .name = _msg_enable_sbas,
  103. .index = CONFFLAG_ENABLE_SBAS,
  104. .changed = gps_initialize,
  105. },
  106. {
  107. .type = MENU_TYPE_SETTING_U8,
  108. .display_type = MENU_DISPLAY_TYPE_NAME_FUNCTION,
  109. .name = _msg_gnss_type,
  110. .index = CONF_U8_GNSS_MODE,
  111. .display = display_current_gnss_mode,
  112. .changed = gps_initialize,
  113. },
  114. {
  115. .type = MENU_TYPE_SETTING_BOOL,
  116. .name = _msg_logging_after_boot,
  117. .index = CONFFLAG_LOGGING_AFTER_BOOT,
  118. },
  119. };
  120. __flash const struct menu_struct settings_menu = {
  121. .list = settings_menu_list,
  122. .num = sizeof(settings_menu_list) / sizeof(settings_menu_list[0]),
  123. };
  124. __flash const char gnss_gps_glonass_galileo[] = "GPS+GL.NS+GAL.EO";
  125. __flash const char gnss_gps[] = "GPS";
  126. __flash const char gnss_gps_galileo[] = "GPS+GALILEO";
  127. __flash const char gnss_galileo[] = "GALILEO";
  128. __flash const char gnss_gps_beidou[] = "GPS+BEIDOU";
  129. __flash const char gnss_beidou[] = "BEIDOU";
  130. __flash const char *gnss_names[] = {
  131. gnss_gps_glonass_galileo,
  132. gnss_gps,
  133. gnss_gps_galileo,
  134. gnss_galileo,
  135. gnss_gps_beidou,
  136. gnss_beidou,
  137. };