settings.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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] = GNSS_MODE_BEIDOU,
  13. [CONF_U8_SKIP_POINTS] = 120,
  14. [CONF_U8_AUTO_PAUSE_TIME] = 120,
  15. [CONF_U8_AUTO_PAUSE_DIST] = 100,
  16. };
  17. const __flash unsigned char limits_min_u8[] = {
  18. [CONF_U8_GNSS_MODE] = GNSS_MODE_GPS_GLONASS_GALILEO,
  19. [CONF_U8_SKIP_POINTS] = 0,
  20. [CONF_U8_AUTO_PAUSE_TIME] = 10,
  21. [CONF_U8_AUTO_PAUSE_DIST] = 2,
  22. };
  23. const __flash unsigned char defaults_u8[] = {
  24. [CONF_U8_GNSS_MODE] = GNSS_MODE_GPS_GLONASS_GALILEO,
  25. [CONF_U8_SKIP_POINTS] = 15,
  26. [CONF_U8_AUTO_PAUSE_TIME] = 30,
  27. [CONF_U8_AUTO_PAUSE_DIST] = 10,
  28. };
  29. unsigned char settings_load(void) { /* 0 - ok, 1 - error */
  30. unsigned char crc=0, rcrc, i;
  31. unsigned char *cptr = (unsigned char *)&System.conf;
  32. unsigned char ret;
  33. eeprom_read_block(cptr, &config_eep, sizeof(struct config_s));
  34. for (i=0; i<sizeof(struct config_s); i++) {
  35. crc = _crc_ibutton_update(crc, cptr[i]);
  36. }
  37. rcrc = eeprom_read_byte(&config_crc);
  38. crc = _crc_ibutton_update(crc, rcrc);
  39. ret = check_config_data();
  40. if (crc) {
  41. xputs_P(PSTR("EEPROM read: bad CRC\r\n"));
  42. } else if (ret) {
  43. xputs_P(PSTR("EEPROM read: bad data\r\n"));
  44. } else {
  45. xputs_P(PSTR("EEPROM read OK\r\n"));
  46. }
  47. ret = ret || crc;
  48. return ret;
  49. }
  50. unsigned char check_config_data(void) { /* 0 - ok, 1 - error */
  51. unsigned char i, ret=0;
  52. for (i=0; i<=CONF_U8_LAST; i++) {
  53. if (System.conf.conf_u8[i] > limits_max_u8[i] || System.conf.conf_u8[i] < limits_min_u8[i]) {
  54. ret = 1;
  55. System.conf.conf_u8[i] = defaults_u8[i];
  56. }
  57. }
  58. return ret;
  59. }
  60. void settings_store(void) {
  61. unsigned char i, crc=0;
  62. unsigned char *cptr = (unsigned char *)&System.conf;
  63. eeprom_update_block(cptr, &config_eep, sizeof(struct config_s));
  64. for (i=0; i<sizeof(struct config_s); i++) {
  65. crc = _crc_ibutton_update(crc, cptr[i]);
  66. }
  67. eeprom_update_byte(&config_crc, crc);
  68. xputs_P(PSTR("EEPROM write done\r\n"));
  69. }
  70. unsigned char get_flag(unsigned char index) {
  71. volatile unsigned char *sptr = &System.conf.flags[index/8];
  72. index %= 8;
  73. unsigned char val = (*sptr) & _BV(index);
  74. return val;
  75. }
  76. void set_flag(unsigned char index, unsigned char val) {
  77. volatile unsigned char *sptr = &System.conf.flags[index/8];
  78. index %= 8;
  79. if (val)
  80. *sptr |= _BV(index);
  81. else
  82. *sptr &= ~_BV(index);
  83. }
  84. void display_gnss_mode(unsigned char val) {
  85. strcpy_P(disp.line2, gnss_names[val]);
  86. }
  87. void display_current_gnss_mode(void) {
  88. display_gnss_mode(System.conf.gnss_mode);
  89. }
  90. /* SETTINGS ITEMS */
  91. __flash const char _msg_disable_filters[] = "Nie filtruj";
  92. __flash const char _msg_enable_sbas[] = "Szukaj SBAS";
  93. __flash const char _msg_gnss_type[] = "Rodzaj GNSS";
  94. __flash const char _msg_skip_points[] = "Pomin punkty";
  95. __flash const char _msg_logging_after_boot[] = "Zapis po wlacz.";
  96. __flash const char _msg_back[] = "< Powrot";
  97. __flash const char _msg_auto_pause[] = "Autopauza";
  98. __flash const char _msg_auto_pause_time[] = "Autopauza czas";
  99. __flash const char _msg_auto_pause_dist[] = "Autopauza odleg";
  100. __flash const struct menu_pos settings_menu_list[] = {
  101. {
  102. .type = MENU_TYPE_DISPLAY,
  103. .display_type = MENU_DISPLAY_TYPE_STRING,
  104. .name = _msg_back,
  105. .allow_back = 1,
  106. },
  107. {
  108. .type = MENU_TYPE_SETTING_BOOL,
  109. .name = _msg_auto_pause,
  110. .index = CONFFLAG_AUTO_PAUSE,
  111. },
  112. {
  113. .type = MENU_TYPE_SETTING_U8,
  114. .name = _msg_auto_pause_time,
  115. .index = CONF_U8_AUTO_PAUSE_TIME,
  116. },
  117. {
  118. .type = MENU_TYPE_SETTING_U8,
  119. .display_type = MENU_DISPLAY_TYPE_U8_METERS,
  120. .name = _msg_auto_pause_dist,
  121. .index = CONF_U8_AUTO_PAUSE_DIST,
  122. },
  123. {
  124. .type = MENU_TYPE_SETTING_BOOL,
  125. .name = _msg_disable_filters,
  126. .index = CONFFLAG_DISABLE_FILTERS,
  127. },
  128. {
  129. .type = MENU_TYPE_SETTING_U8,
  130. .name = _msg_skip_points,
  131. .index = CONF_U8_SKIP_POINTS,
  132. },
  133. {
  134. .type = MENU_TYPE_SETTING_BOOL,
  135. .name = _msg_enable_sbas,
  136. .index = CONFFLAG_ENABLE_SBAS,
  137. .changed = gps_initialize,
  138. },
  139. {
  140. .type = MENU_TYPE_SETTING_U8,
  141. .display_type = MENU_DISPLAY_TYPE_NAME_FUNCTION,
  142. .name = _msg_gnss_type,
  143. .index = CONF_U8_GNSS_MODE,
  144. .display = display_current_gnss_mode,
  145. .changed = gps_initialize,
  146. },
  147. {
  148. .type = MENU_TYPE_SETTING_BOOL,
  149. .name = _msg_logging_after_boot,
  150. .index = CONFFLAG_LOGGING_AFTER_BOOT,
  151. },
  152. };
  153. __flash const struct menu_struct settings_menu = {
  154. .list = settings_menu_list,
  155. .num = sizeof(settings_menu_list) / sizeof(settings_menu_list[0]),
  156. };
  157. __flash const char gnss_gps_glonass_galileo[] = "GPS+GL.NS+GAL.EO";
  158. __flash const char gnss_gps[] = "GPS";
  159. __flash const char gnss_gps_galileo[] = "GPS+GALILEO";
  160. __flash const char gnss_galileo[] = "GALILEO";
  161. __flash const char gnss_gps_beidou[] = "GPS+BEIDOU";
  162. __flash const char gnss_beidou[] = "BEIDOU";
  163. __flash const char *gnss_names[] = {
  164. gnss_gps_glonass_galileo,
  165. gnss_gps,
  166. gnss_gps_galileo,
  167. gnss_galileo,
  168. gnss_gps_beidou,
  169. gnss_beidou,
  170. };