display.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include <avr/pgmspace.h>
  2. #include "main.h"
  3. #include "display.h"
  4. #include "HD44780-I2C.h"
  5. #include "xprintf.h"
  6. __flash const unsigned char battery_states[][8] = {
  7. {
  8. 0b01110,
  9. 0b11111,
  10. 0b11111,
  11. 0b11111,
  12. 0b11111,
  13. 0b11111,
  14. 0b11111,
  15. 0b11111,
  16. },
  17. {
  18. 0b01110,
  19. 0b11111,
  20. 0b10001,
  21. 0b11111,
  22. 0b11111,
  23. 0b11111,
  24. 0b11111,
  25. 0b11111,
  26. },
  27. {
  28. 0b01110,
  29. 0b11111,
  30. 0b10001,
  31. 0b10001,
  32. 0b10001,
  33. 0b11111,
  34. 0b11111,
  35. 0b11111,
  36. },
  37. {
  38. 0b01110,
  39. 0b11111,
  40. 0b10001,
  41. 0b10001,
  42. 0b10001,
  43. 0b10001,
  44. 0b10001,
  45. 0b11111,
  46. },
  47. };
  48. struct disp_s {
  49. char line1[16];
  50. char line2[17];
  51. } disp;
  52. void battery_state_display(void) {
  53. unsigned char i;
  54. unsigned char index;
  55. if (System.bat_volt > 4.0)
  56. index = 0;
  57. else if (System.bat_volt > 3.7)
  58. index = 1;
  59. else if (System.bat_volt > 3.4)
  60. index = 2;
  61. else
  62. index = 3;
  63. LCD_WriteCommand(0x40 + 0); // 0x00
  64. for(i=0; i<8; i++){
  65. LCD_WriteData(battery_states[index][i]);
  66. };
  67. }
  68. void disp_func_startup(__attribute__ ((unused)) unsigned char changed) {
  69. strcpy_P(disp.line1, PSTR("Uruchamianie..."));
  70. }
  71. void disp_func_poweroff(__attribute__ ((unused)) unsigned char changed) {
  72. strcpy_P(disp.line1, PSTR("Wylaczanie..."));
  73. }
  74. void disp_func_poweroff_lowbat(unsigned char changed) {
  75. disp_func_poweroff(changed);
  76. strcpy_P(disp.line2, PSTR("Bateria slaba!"));
  77. }
  78. void disp_func_start_message(__attribute__ ((unused)) unsigned char changed) {
  79. strcpy_P(disp.line1, PSTR("Start"));
  80. switch(System.status){
  81. case STATUS_NO_POWER: case STATUS_OK: case STATUS_NO_GPS: disp.line2[0] = '\0'; break;
  82. case STATUS_NO_DISK: strcpy_P(disp.line2, PSTR("Brak karty!")); break;
  83. case STATUS_DISK_ERROR: strcpy_P(disp.line2, PSTR("Blad karty!")); break;
  84. case STATUS_FILE_WRITE_ERROR: strcpy_P(disp.line2, PSTR("Blad zapisu!")); break;
  85. case STATUS_FILE_SYNC_ERROR: strcpy_P(disp.line2, PSTR("Blad zapisu FAT!")); break;
  86. case STATUS_FILE_CLOSE_ERROR: strcpy_P(disp.line2, PSTR("Blad zamk.pliku!")); break;
  87. case STATUS_FILE_OPEN_ERROR: strcpy_P(disp.line2, PSTR("Blad otw. pliku!")); break;
  88. }
  89. }
  90. void disp_func_card_ok(__attribute__ ((unused)) unsigned char changed) {
  91. strcpy_P(disp.line1, PSTR("Karta OK!"));
  92. strcpy_P(disp.line2, PSTR("Czekam na GPS..."));
  93. }
  94. void disp_func_file_open(__attribute__ ((unused)) unsigned char changed) {
  95. strcpy_P(disp.line2, PSTR("Zapis aktywny"));
  96. }
  97. void disp_func_file_closed(__attribute__ ((unused)) unsigned char changed) {
  98. strcpy_P(disp.line2, PSTR("Pliki zamkniete"));
  99. }
  100. void disp_func_main_default(unsigned char changed) {
  101. disp.line1[0] = '\0';
  102. disp_func_file_open(changed);
  103. }
  104. void disp_func_coord(__attribute__ ((unused)) unsigned char changed) {
  105. if (System.location_valid == LOC_INVALID) {
  106. strcpy_P(disp.line1, PSTR("??? N/S"));
  107. strcpy_P(disp.line2, PSTR("??? E/W"));
  108. return;
  109. }
  110. xsprintf(disp.line1, PSTR("%2.6f%c"), (location.lat < 0)?(-location.lat):location.lat, (location.lat < 0)?'S':'N');
  111. xsprintf(disp.line2, PSTR("%3.6f%c"), (location.lon < 0)?(-location.lon):location.lon, (location.lon < 0)?'W':'E');
  112. }
  113. void disp_func_ele_sat(__attribute__ ((unused)) unsigned char changed) {
  114. if (System.location_valid == LOC_INVALID) {
  115. strcpy_P(disp.line1, PSTR("ele = ???"));
  116. } else {
  117. xsprintf(disp.line1, PSTR("ele = %.1fm"), location.alt);
  118. }
  119. xsprintf(disp.line2, PSTR("%d satelit"), System.satellites_used);
  120. }
  121. void (*__flash const disp_funcs[])(unsigned char) = {
  122. [DISPLAY_STATE_STARTUP] = disp_func_startup,
  123. [DISPLAY_STATE_POWEROFF] = disp_func_poweroff,
  124. [DISPLAY_STATE_POWEROFF_LOWBAT] = disp_func_poweroff_lowbat,
  125. [DISPLAY_STATE_START_MESSAGE] = disp_func_start_message,
  126. [DISPLAY_STATE_CARD_OK] = disp_func_card_ok,
  127. [DISPLAY_STATE_FILE_OPEN] = disp_func_file_open,
  128. [DISPLAY_STATE_FILE_CLOSED] = disp_func_file_closed,
  129. [DISPLAY_STATE_MAIN_DEFAULT] = disp_func_main_default,
  130. [DISPLAY_STATE_COORD] = disp_func_coord,
  131. [DISPLAY_STATE_ELE_SAT] = disp_func_ele_sat,
  132. };
  133. void display_refresh(unsigned char newstate) {
  134. unsigned char changed = 0;
  135. if (newstate)
  136. changed = 1;
  137. disp_funcs[System.display_state](changed);
  138. if (timer_expired(lcd)) {
  139. changed = 1;
  140. set_timer(lcd, 1000);
  141. }
  142. /* write to LCD */
  143. if (changed) {
  144. battery_state_display();
  145. unsigned char len;
  146. LCD_GoTo(0,0);
  147. len = strlen(disp.line1);
  148. LCD_WriteText(disp.line1);
  149. while (len<15) {
  150. len++;
  151. LCD_WriteData(' ');
  152. }
  153. LCD_WriteData(0); /* battery symbol */
  154. LCD_GoTo(0,1);
  155. len = strlen(disp.line2);
  156. LCD_WriteText(disp.line2);
  157. while (len<16) {
  158. len++;
  159. LCD_WriteData(' ');
  160. }
  161. }
  162. }
  163. void display_state(unsigned char newstate) {
  164. System.display_state = newstate;
  165. display_refresh(newstate);
  166. }