settings.c 5.2 KB

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