display.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. __flash const char _gps_wait[] = "Czekam na GPS...";
  91. __flash const char _gps_ok[] = "GPS OK!";
  92. __flash const char _card_ok[] = "Karta OK!";
  93. __flash const char _logging_active[] = "Zapis aktywny";
  94. void disp_func_card_ok(__attribute__ ((unused)) unsigned char changed) {
  95. strcpy_P(disp.line1, _card_ok);
  96. strcpy_P(disp.line2, _gps_wait);
  97. }
  98. void disp_func_file_open(__attribute__ ((unused)) unsigned char changed) {
  99. strcpy_P(disp.line2, _logging_active);
  100. }
  101. void disp_func_file_closed(__attribute__ ((unused)) unsigned char changed) {
  102. strcpy_P(disp.line2, PSTR("Pliki zamkniete"));
  103. }
  104. void disp_func_main_default(__attribute__ ((unused)) unsigned char changed) {
  105. if (FLAGS & F_FILEOPEN)
  106. strcpy_P(disp.line1, _logging_active);
  107. else
  108. strcpy_P(disp.line1, _card_ok);
  109. if (FLAGS & F_GPSOK)
  110. strcpy_P(disp.line2, _gps_ok);
  111. else
  112. strcpy_P(disp.line2, _gps_wait);
  113. }
  114. void disp_func_coord(__attribute__ ((unused)) unsigned char changed) {
  115. if (System.location_valid == LOC_INVALID) {
  116. strcpy_P(disp.line1, PSTR("??? N/S"));
  117. strcpy_P(disp.line2, PSTR("??? E/W"));
  118. return;
  119. }
  120. xsprintf(disp.line1, PSTR("%2.6f%c"), (location.lat < 0)?(-location.lat):location.lat, (location.lat < 0)?'S':'N');
  121. xsprintf(disp.line2, PSTR("%3.6f%c"), (location.lon < 0)?(-location.lon):location.lon, (location.lon < 0)?'W':'E');
  122. }
  123. void disp_func_voltage(__attribute__ ((unused)) unsigned char changed) {
  124. strcpy_P(disp.line1, PSTR("Batt.voltage"));
  125. xsprintf(disp.line2, PSTR("%.2f V"), System.bat_volt);
  126. }
  127. void disp_func_temperature(__attribute__ ((unused)) unsigned char changed) {
  128. strcpy_P(disp.line1, PSTR("Temperature"));
  129. xsprintf(disp.line2, PSTR("%.1f C"), System.temperature);
  130. }
  131. void disp_func_ele_sat(__attribute__ ((unused)) unsigned char changed) {
  132. if (System.location_valid == LOC_INVALID) {
  133. strcpy_P(disp.line1, PSTR("ele = ???"));
  134. } else {
  135. xsprintf(disp.line1, PSTR("ele = %.0f m"), location.alt);
  136. }
  137. xsprintf(disp.line2, PSTR("%d satelit"), System.satellites_used);
  138. }
  139. void disp_func_main_menu(__attribute__ ((unused)) unsigned char changed) {
  140. strcpy_P(disp.line1, PSTR(" *** MENU *** "));
  141. disp.line2[0] = '>';
  142. disp.line2[1] = '\0';
  143. }
  144. void (*__flash const disp_funcs[])(unsigned char) = {
  145. [DISPLAY_STATE_STARTUP] = disp_func_startup,
  146. [DISPLAY_STATE_POWEROFF] = disp_func_poweroff,
  147. [DISPLAY_STATE_POWEROFF_LOWBAT] = disp_func_poweroff_lowbat,
  148. [DISPLAY_STATE_START_MESSAGE] = disp_func_start_message,
  149. [DISPLAY_STATE_CARD_OK] = disp_func_card_ok,
  150. [DISPLAY_STATE_FILE_OPEN] = disp_func_file_open,
  151. [DISPLAY_STATE_FILE_CLOSED] = disp_func_file_closed,
  152. [DISPLAY_STATE_MAIN_DEFAULT] = disp_func_main_default,
  153. [DISPLAY_STATE_COORD] = disp_func_coord,
  154. [DISPLAY_STATE_ELE_SAT] = disp_func_ele_sat,
  155. [DISPLAY_STATE_VOLTAGE] = disp_func_voltage,
  156. [DISPLAY_STATE_MAIN_MENU] = disp_func_main_menu,
  157. [DISPLAY_STATE_TEMPERATURE] = disp_func_temperature,
  158. };
  159. void display_refresh(unsigned char newstate) {
  160. unsigned char changed = 0;
  161. if (newstate)
  162. changed = 1;
  163. disp_funcs[System.display_state](changed);
  164. if (timer_expired(lcd)) {
  165. changed = 1;
  166. set_timer(lcd, 1000);
  167. }
  168. /* write to LCD */
  169. if (changed) {
  170. battery_state_display();
  171. unsigned char len;
  172. LCD_GoTo(0,0);
  173. len = strlen(disp.line1);
  174. LCD_WriteText(disp.line1);
  175. while (len<15) {
  176. len++;
  177. LCD_WriteData(' ');
  178. }
  179. LCD_WriteData(0); /* battery symbol */
  180. LCD_GoTo(0,1);
  181. len = strlen(disp.line2);
  182. LCD_WriteText(disp.line2);
  183. while (len<16) {
  184. len++;
  185. LCD_WriteData(' ');
  186. }
  187. }
  188. }
  189. void display_state(unsigned char newstate) {
  190. System.display_state = newstate;
  191. display_refresh(newstate);
  192. }