settings.c 5.8 KB

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