settings.c 4.6 KB

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