settings.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <util/crc16.h>
  2. #include "main.h"
  3. #include "settings.h"
  4. #include "display.h"
  5. #include "xprintf.h"
  6. EEMEM struct config_s config_eep;
  7. EEMEM unsigned char config_crc;
  8. unsigned char settings_load(void) { /* 0 - ok, 1 - error */
  9. unsigned char crc=0, rcrc, i;
  10. unsigned char *cptr = (unsigned char *)&System.conf;
  11. unsigned char ret;
  12. eeprom_read_block(cptr, &config_eep, sizeof(struct config_s));
  13. for (i=0; i<sizeof(struct config_s); i++) {
  14. crc = _crc_ibutton_update(crc, cptr[i]);
  15. }
  16. rcrc = eeprom_read_byte(&config_crc);
  17. crc = _crc_ibutton_update(crc, rcrc);
  18. ret = check_config_data();
  19. if (crc) {
  20. xputs_P(PSTR("EEPROM read: bad CRC\r\n"));
  21. } else if (ret) {
  22. xputs_P(PSTR("EEPROM read: bad data\r\n"));
  23. } else {
  24. xputs_P(PSTR("EEPROM read OK\r\n"));
  25. }
  26. ret = ret || crc;
  27. return ret;
  28. }
  29. unsigned char check_config_data(void) { /* 0 - ok, 1 - error */
  30. return 0;
  31. }
  32. void settings_store(void) {
  33. unsigned char i, crc=0;
  34. unsigned char *cptr = (unsigned char *)&System.conf;
  35. eeprom_update_block(cptr, &config_eep, sizeof(struct config_s));
  36. for (i=0; i<sizeof(struct config_s); i++) {
  37. crc = _crc_ibutton_update(crc, cptr[i]);
  38. }
  39. eeprom_update_byte(&config_crc, crc);
  40. xputs_P(PSTR("EEPROM write done\r\n"));
  41. }
  42. void settings_display_and_modify_bool(unsigned char index, __flash const char *name, unsigned char k, unsigned char have_prev, unsigned char have_next) {
  43. unsigned char val = get_flag(index);
  44. if (k == K_LEFT || k == K_RIGHT) { /* change value */
  45. set_flag(index, !val);
  46. }
  47. strcpy_P(disp.line1, name);
  48. if (val)
  49. strcpy_P(disp.line2, PSTR("< Tak > "));
  50. else
  51. strcpy_P(disp.line2, PSTR("< Nie > "));
  52. strcat_P(disp.line2, have_next?PSTR(" \x01"):PSTR(" ")); /* down arrow */
  53. strcat_P(disp.line2, have_prev?PSTR(" \x02"):PSTR(" ")); /* up arrow */
  54. }
  55. unsigned char get_flag(unsigned char index) {
  56. volatile unsigned char *sptr = &System.conf.flags[index/8];
  57. index %= 8;
  58. unsigned char val = (*sptr) & _BV(index);
  59. return val;
  60. }
  61. void set_flag(unsigned char index, unsigned char val) {
  62. volatile unsigned char *sptr = &System.conf.flags[index/8];
  63. index %= 8;
  64. if (val)
  65. *sptr |= _BV(index);
  66. else
  67. *sptr &= ~_BV(index);
  68. }