settings.c 5.5 KB

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