settings.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. EEMEM struct config_s config_eep;
  9. EEMEM unsigned char config_crc;
  10. const __flash unsigned char limits_max_u8[] = {
  11. [CONF_U8_GNSS_MODE] = 5,
  12. [CONF_U8_SKIP_POINTS] = 120,
  13. };
  14. unsigned char settings_load(void) { /* 0 - ok, 1 - error */
  15. unsigned char crc=0, rcrc, i;
  16. unsigned char *cptr = (unsigned char *)&System.conf;
  17. unsigned char ret;
  18. eeprom_read_block(cptr, &config_eep, sizeof(struct config_s));
  19. for (i=0; i<sizeof(struct config_s); i++) {
  20. crc = _crc_ibutton_update(crc, cptr[i]);
  21. }
  22. rcrc = eeprom_read_byte(&config_crc);
  23. crc = _crc_ibutton_update(crc, rcrc);
  24. ret = check_config_data();
  25. if (crc) {
  26. xputs_P(PSTR("EEPROM read: bad CRC\r\n"));
  27. } else if (ret) {
  28. xputs_P(PSTR("EEPROM read: bad data\r\n"));
  29. } else {
  30. xputs_P(PSTR("EEPROM read OK\r\n"));
  31. }
  32. ret = ret || crc;
  33. return ret;
  34. }
  35. unsigned char check_config_data(void) { /* 0 - ok, 1 - error */
  36. unsigned char i, ret=0;
  37. for (i=0; i<=CONF_U8_LAST; i++) {
  38. if (System.conf.conf_u8[i] > limits_max_u8[i]) {
  39. ret = 1;
  40. System.conf.conf_u8[i] = 0;
  41. }
  42. }
  43. return ret;
  44. }
  45. void settings_store(void) {
  46. unsigned char i, crc=0;
  47. unsigned char *cptr = (unsigned char *)&System.conf;
  48. eeprom_update_block(cptr, &config_eep, sizeof(struct config_s));
  49. for (i=0; i<sizeof(struct config_s); i++) {
  50. crc = _crc_ibutton_update(crc, cptr[i]);
  51. }
  52. eeprom_update_byte(&config_crc, crc);
  53. xputs_P(PSTR("EEPROM write done\r\n"));
  54. }
  55. void settings_display_and_modify_bool(unsigned char mindex, unsigned char k) {
  56. unsigned char index = settings_menu[mindex].index;
  57. unsigned char val = get_flag(index);
  58. const __flash char *name = settings_menu[mindex].name;
  59. if (k == K_LEFT || k == K_RIGHT) { /* change value */
  60. val = !val;
  61. set_flag(index, val);
  62. if (settings_menu[mindex].changed != NULL)
  63. settings_menu[mindex].changed();
  64. }
  65. strcpy_P(disp.line1, name);
  66. settings_menu[mindex].display(val);
  67. }
  68. void settings_display_and_modify_u8(unsigned char mindex, unsigned char k) {
  69. unsigned char index = settings_menu[mindex].index;
  70. unsigned char val = System.conf.conf_u8[index];
  71. const __flash char *name = settings_menu[mindex].name;
  72. if (k == K_LEFT) {
  73. if (val)
  74. val--;
  75. }
  76. if (k == K_RIGHT) {
  77. if (val < limits_max_u8[index])
  78. val++;
  79. }
  80. if (k == K_LEFT || k == K_RIGHT) {
  81. System.conf.conf_u8[index] = val;
  82. if (settings_menu[mindex].changed != NULL)
  83. settings_menu[mindex].changed();
  84. }
  85. strcpy_P(disp.line1, name);
  86. settings_menu[mindex].display(val);
  87. }
  88. unsigned char get_flag(unsigned char index) {
  89. volatile unsigned char *sptr = &System.conf.flags[index/8];
  90. index %= 8;
  91. unsigned char val = (*sptr) & _BV(index);
  92. return val;
  93. }
  94. void set_flag(unsigned char index, unsigned char val) {
  95. volatile unsigned char *sptr = &System.conf.flags[index/8];
  96. index %= 8;
  97. if (val)
  98. *sptr |= _BV(index);
  99. else
  100. *sptr &= ~_BV(index);
  101. }
  102. void settings_bool_disp_default(unsigned char val) {
  103. if (val)
  104. strcpy_P(disp.line2, PSTR("< Tak > "));
  105. else
  106. strcpy_P(disp.line2, PSTR("< Nie > "));
  107. strcat_P(disp.line2, HAVE_NEXT_SETTING_POSITION?PSTR(" \x01"):PSTR(" ")); /* down arrow */
  108. strcat_P(disp.line2, HAVE_PREV_SETTING_POSITION?PSTR(" \x02"):PSTR(" ")); /* up arrow */
  109. }
  110. void settings_u8_disp_default(unsigned char val) {
  111. xsprintf(disp.line2, PSTR("%d"), (int)val);
  112. strcat_P(disp.line2, HAVE_NEXT_SETTING_POSITION?PSTR(" \x01"):PSTR(" ")); /* down arrow */
  113. strcat_P(disp.line2, HAVE_PREV_SETTING_POSITION?PSTR(" \x02"):PSTR(" ")); /* up arrow */
  114. }
  115. void display_gnss_mode(unsigned char val) {
  116. strcpy_P(disp.line2, gnss_names[val]);
  117. }
  118. /* SETTINGS ITEMS */
  119. __flash const char _msg_disable_filters[] = "Nie filtruj";
  120. __flash const char _msg_enable_sbas[] = "Szukaj SBAS";
  121. __flash const char _msg_gnss_type[] = "Rodzaj GNSS";
  122. __flash const char _msg_skip_points[] = "Pomin punkty";
  123. __flash const char _msg_back[] = "< Powrot";
  124. __flash const struct settings_menu_pos_s settings_menu[SETTINGS_MENU_MAXPOS+1] = {
  125. {
  126. .type = SETTINGS_TYPE_BACK,
  127. .name = _msg_back,
  128. },
  129. {
  130. .type = SETTINGS_TYPE_BOOL,
  131. .name = _msg_disable_filters,
  132. .index = CONFFLAG_DISABLE_FILTERS,
  133. .display = settings_bool_disp_default,
  134. },
  135. {
  136. .type = SETTINGS_TYPE_U8,
  137. .name = _msg_skip_points,
  138. .index = CONF_U8_SKIP_POINTS,
  139. .display = settings_u8_disp_default,
  140. },
  141. {
  142. .type = SETTINGS_TYPE_BOOL,
  143. .name = _msg_enable_sbas,
  144. .index = CONFFLAG_ENABLE_SBAS,
  145. .display = settings_bool_disp_default,
  146. .changed = gps_initialize,
  147. },
  148. {
  149. .type = SETTINGS_TYPE_U8,
  150. .name = _msg_gnss_type,
  151. .index = CONF_U8_GNSS_MODE,
  152. .display = display_gnss_mode,
  153. .changed = gps_initialize,
  154. },
  155. };
  156. __flash const char gnss_gps_glonass_galileo[] = "GPS+GL.NS+GAL.EO";
  157. __flash const char gnss_gps[] = "GPS";
  158. __flash const char gnss_gps_galileo[] = "GPS+GALILEO";
  159. __flash const char gnss_galileo[] = "GALILEO";
  160. __flash const char gnss_gps_beidou[] = "GPS+BEIDOU";
  161. __flash const char gnss_beidou[] = "BEIDOU";
  162. __flash const char *gnss_names[] = {
  163. gnss_gps_glonass_galileo,
  164. gnss_gps,
  165. gnss_gps_galileo,
  166. gnss_galileo,
  167. gnss_gps_beidou,
  168. gnss_beidou,
  169. };