display.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include <avr/pgmspace.h>
  2. #include "main.h"
  3. #include "display.h"
  4. #include "HD44780-I2C.h"
  5. #include "xprintf.h"
  6. #include "working_modes.h"
  7. __flash const unsigned char battery_states[][8] = {
  8. {
  9. 0b01110,
  10. 0b11111,
  11. 0b11111,
  12. 0b11111,
  13. 0b11111,
  14. 0b11111,
  15. 0b11111,
  16. 0b11111,
  17. },
  18. {
  19. 0b01110,
  20. 0b11111,
  21. 0b10001,
  22. 0b11111,
  23. 0b11111,
  24. 0b11111,
  25. 0b11111,
  26. 0b11111,
  27. },
  28. {
  29. 0b01110,
  30. 0b11111,
  31. 0b10001,
  32. 0b10001,
  33. 0b10001,
  34. 0b11111,
  35. 0b11111,
  36. 0b11111,
  37. },
  38. {
  39. 0b01110,
  40. 0b11111,
  41. 0b10001,
  42. 0b10001,
  43. 0b10001,
  44. 0b10001,
  45. 0b10001,
  46. 0b11111,
  47. },
  48. };
  49. __flash const unsigned char custom_chars[] = {
  50. 0b00000, /* 0x01 down arrow */
  51. 0b00100,
  52. 0b00100,
  53. 0b00100,
  54. 0b10101,
  55. 0b01110,
  56. 0b00100,
  57. 0b00000,
  58. 0b00000, /* 0x02 up arrow */
  59. 0b00100,
  60. 0b01110,
  61. 0b10101,
  62. 0b00100,
  63. 0b00100,
  64. 0b00100,
  65. 0b00000,
  66. };
  67. struct disp_s disp;
  68. void disp_init(void) { /* send custom characters starting with 0x01 */
  69. unsigned char i;
  70. LCD_WriteCommand(0x40 + 8); // 0x01
  71. for(i=0; i<sizeof(custom_chars); i++){
  72. LCD_WriteData(custom_chars[i]);
  73. };
  74. }
  75. void battery_state_display(void) {
  76. unsigned char i;
  77. unsigned char index;
  78. if (System.bat_volt > 4.0)
  79. index = 0;
  80. else if (System.bat_volt > 3.7)
  81. index = 1;
  82. else if (System.bat_volt > 3.4)
  83. index = 2;
  84. else
  85. index = 3;
  86. LCD_WriteCommand(0x40 + 0); // 0x00
  87. for(i=0; i<8; i++){
  88. LCD_WriteData(battery_states[index][i]);
  89. };
  90. }
  91. void disp_func_startup(__attribute__ ((unused)) unsigned char changed) {
  92. strcpy_P(disp.line1, PSTR("Uruchamianie..."));
  93. }
  94. void disp_func_poweroff(__attribute__ ((unused)) unsigned char changed) {
  95. strcpy_P(disp.line1, PSTR("Wylaczanie..."));
  96. }
  97. void disp_func_poweroff_lowbat(unsigned char changed) {
  98. disp_func_poweroff(changed);
  99. strcpy_P(disp.line2, PSTR("Bateria slaba!"));
  100. }
  101. void disp_func_start_message(__attribute__ ((unused)) unsigned char changed) {
  102. strcpy_P(disp.line1, PSTR("Start"));
  103. switch(System.status){
  104. case STATUS_NO_POWER: case STATUS_OK: case STATUS_NO_GPS: disp.line2[0] = '\0'; break;
  105. case STATUS_NO_DISK: strcpy_P(disp.line2, PSTR("Brak karty!")); break;
  106. case STATUS_DISK_ERROR: strcpy_P(disp.line2, PSTR("Blad karty!")); break;
  107. case STATUS_FILE_WRITE_ERROR: strcpy_P(disp.line2, PSTR("Blad zapisu!")); break;
  108. case STATUS_FILE_SYNC_ERROR: strcpy_P(disp.line2, PSTR("Blad zapisu FAT!")); break;
  109. case STATUS_FILE_CLOSE_ERROR: strcpy_P(disp.line2, PSTR("Blad zamk.pliku!")); break;
  110. case STATUS_FILE_OPEN_ERROR: strcpy_P(disp.line2, PSTR("Blad otw. pliku!")); break;
  111. }
  112. }
  113. __flash const char _gps_wait[] = "Czekam na GPS...";
  114. __flash const char _gps_ok[] = "GPS OK!";
  115. __flash const char _card_ok[] = "Karta OK!";
  116. __flash const char _logging_active[] = "Zapis aktywny";
  117. __flash const char _logging_paused[] = "Zapis wstrzymany";
  118. void disp_func_card_ok(__attribute__ ((unused)) unsigned char changed) {
  119. strcpy_P(disp.line1, _card_ok);
  120. strcpy_P(disp.line2, _gps_wait);
  121. }
  122. void disp_func_file_open(__attribute__ ((unused)) unsigned char changed) {
  123. strcpy_P(disp.line2, _logging_active);
  124. }
  125. void disp_func_file_closed(__attribute__ ((unused)) unsigned char changed) {
  126. strcpy_P(disp.line2, PSTR("Pliki zamkniete"));
  127. }
  128. void disp_func_main_default(__attribute__ ((unused)) unsigned char changed) {
  129. if (FLAGS & F_FILEOPEN) {
  130. if (System.tracking_paused)
  131. strcpy_P(disp.line1, _logging_paused);
  132. else
  133. strcpy_P(disp.line1, _logging_active);
  134. } else
  135. strcpy_P(disp.line1, _card_ok);
  136. if (FLAGS & F_GPSOK)
  137. strcpy_P(disp.line2, _gps_ok);
  138. else
  139. strcpy_P(disp.line2, _gps_wait);
  140. }
  141. void disp_func_coord(__attribute__ ((unused)) unsigned char changed) {
  142. if (System.location_valid == LOC_INVALID) {
  143. strcpy_P(disp.line1, PSTR("??? N/S"));
  144. strcpy_P(disp.line2, PSTR("??? E/W"));
  145. return;
  146. }
  147. xsprintf(disp.line1, PSTR("%2.6f%c"), (location.lat < 0)?(-location.lat):location.lat, (location.lat < 0)?'S':'N');
  148. xsprintf(disp.line2, PSTR("%3.6f%c"), (location.lon < 0)?(-location.lon):location.lon, (location.lon < 0)?'W':'E');
  149. }
  150. void disp_func_ele_sat(__attribute__ ((unused)) unsigned char changed) {
  151. if (System.location_valid == LOC_INVALID) {
  152. strcpy_P(disp.line1, PSTR("ele = ???"));
  153. } else {
  154. xsprintf(disp.line1, PSTR("ele = %.1fm"), location.alt);
  155. }
  156. xsprintf(disp.line2, PSTR("%2d satelit"), System.satellites_used);
  157. if (System.sbas)
  158. strcat_P(disp.line2, PSTR(", DGPS"));
  159. }
  160. void disp_func_main_menu(__attribute__ ((unused)) unsigned char changed) {
  161. display_main_menu_item();
  162. }
  163. void disp_func_settings_menu(__attribute__ ((unused)) unsigned char changed) {
  164. display_settings_menu_item();
  165. }
  166. void (*__flash const disp_funcs[])(unsigned char) = {
  167. [DISPLAY_STATE_STARTUP] = disp_func_startup,
  168. [DISPLAY_STATE_POWEROFF] = disp_func_poweroff,
  169. [DISPLAY_STATE_POWEROFF_LOWBAT] = disp_func_poweroff_lowbat,
  170. [DISPLAY_STATE_START_MESSAGE] = disp_func_start_message,
  171. [DISPLAY_STATE_CARD_OK] = disp_func_card_ok,
  172. [DISPLAY_STATE_FILE_OPEN] = disp_func_file_open,
  173. [DISPLAY_STATE_FILE_CLOSED] = disp_func_file_closed,
  174. [DISPLAY_STATE_MAIN_DEFAULT] = disp_func_main_default,
  175. [DISPLAY_STATE_COORD] = disp_func_coord,
  176. [DISPLAY_STATE_ELE_SAT] = disp_func_ele_sat,
  177. [DISPLAY_STATE_MAIN_MENU] = disp_func_main_menu,
  178. [DISPLAY_STATE_SETTINGS_MENU] = disp_func_settings_menu,
  179. };
  180. void display_refresh(unsigned char newstate) {
  181. unsigned char changed = 0;
  182. if (newstate)
  183. changed = 1;
  184. disp_funcs[System.display_state](changed);
  185. if (timer_expired(lcd)) {
  186. changed = 1;
  187. set_timer(lcd, 1000);
  188. }
  189. /* write to LCD */
  190. if (changed) {
  191. battery_state_display();
  192. unsigned char len;
  193. LCD_GoTo(0,0);
  194. len = strlen(disp.line1);
  195. LCD_WriteText(disp.line1);
  196. while (len<15) {
  197. len++;
  198. LCD_WriteData(' ');
  199. }
  200. LCD_WriteData(0); /* battery symbol */
  201. LCD_GoTo(0,1);
  202. len = strlen(disp.line2);
  203. LCD_WriteText(disp.line2);
  204. while (len<16) {
  205. len++;
  206. LCD_WriteData(' ');
  207. }
  208. }
  209. }
  210. void display_state(unsigned char newstate) {
  211. System.display_state = newstate;
  212. display_refresh(newstate);
  213. }