settings.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 struct menu_pos settings_menu_list[] = {
  111. {
  112. .type = MENU_TYPE_DISPLAY,
  113. .display_type = MENU_DISPLAY_TYPE_STRING,
  114. .name = _msg_settings,
  115. .value = _msg_back,
  116. .allow_back = 1,
  117. },
  118. {
  119. .type = MENU_TYPE_SETTING_BOOL,
  120. .name = _msg_auto_pause,
  121. .index = CONFFLAG_AUTO_PAUSE,
  122. },
  123. {
  124. .type = MENU_TYPE_SETTING_U8,
  125. .display_type = MENU_DISPLAY_TYPE_U8_SECONDS,
  126. .name = _msg_auto_pause_time,
  127. .index = CONF_U8_AUTO_PAUSE_TIME,
  128. },
  129. {
  130. .type = MENU_TYPE_SETTING_U8,
  131. .display_type = MENU_DISPLAY_TYPE_U8_METERS,
  132. .name = _msg_auto_pause_dist,
  133. .index = CONF_U8_AUTO_PAUSE_DIST,
  134. },
  135. {
  136. .type = MENU_TYPE_SETTING_U8,
  137. .display_type = MENU_DISPLAY_TYPE_U8_KMH,
  138. .name = _msg_auto_pause_speed,
  139. .index = CONF_U8_AUTO_PAUSE_SPEED,
  140. },
  141. {
  142. .type = MENU_TYPE_SETTING_BOOL,
  143. .name = _msg_disable_filters,
  144. .index = CONFFLAG_DISABLE_FILTERS,
  145. },
  146. {
  147. .type = MENU_TYPE_SETTING_U8,
  148. .name = _msg_skip_points,
  149. .index = CONF_U8_SKIP_POINTS,
  150. },
  151. {
  152. .type = MENU_TYPE_SETTING_BOOL,
  153. .name = _msg_enable_sbas,
  154. .index = CONFFLAG_ENABLE_SBAS,
  155. .changed = gps_initialize,
  156. },
  157. {
  158. .type = MENU_TYPE_SETTING_U8,
  159. .name = _msg_min_sats,
  160. .index = CONF_U8_MIN_SATS,
  161. .changed = check_min_sat_limit,
  162. },
  163. {
  164. .type = MENU_TYPE_SETTING_U8,
  165. .display_type = MENU_DISPLAY_TYPE_NAME_FUNCTION,
  166. .name = _msg_gnss_type,
  167. .index = CONF_U8_GNSS_MODE,
  168. .display = display_current_gnss_mode,
  169. .changed = gps_initialize,
  170. },
  171. {
  172. .type = MENU_TYPE_SETTING_BOOL,
  173. .name = _msg_logging_after_boot,
  174. .index = CONFFLAG_LOGGING_AFTER_BOOT,
  175. },
  176. };
  177. __flash const struct menu_struct settings_menu = {
  178. .list = settings_menu_list,
  179. .num = sizeof(settings_menu_list) / sizeof(settings_menu_list[0]),
  180. };
  181. __flash const char gnss_gps_glonass_galileo[] = "GPS+GL.NS+GAL.EO";
  182. __flash const char gnss_gps[] = "GPS";
  183. __flash const char gnss_gps_galileo[] = "GPS+GALILEO";
  184. __flash const char gnss_galileo[] = "GALILEO";
  185. __flash const char gnss_gps_beidou[] = "GPS+BEIDOU";
  186. __flash const char gnss_beidou[] = "BEIDOU";
  187. __flash const char *gnss_names[] = {
  188. gnss_gps_glonass_galileo,
  189. gnss_gps,
  190. gnss_gps_galileo,
  191. gnss_galileo,
  192. gnss_gps_beidou,
  193. gnss_beidou,
  194. };