display.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. void disp_func_card_ok(__attribute__ ((unused)) unsigned char changed) {
  118. strcpy_P(disp.line1, _card_ok);
  119. strcpy_P(disp.line2, _gps_wait);
  120. }
  121. void disp_func_file_open(__attribute__ ((unused)) unsigned char changed) {
  122. strcpy_P(disp.line2, _logging_active);
  123. }
  124. void disp_func_file_closed(__attribute__ ((unused)) unsigned char changed) {
  125. strcpy_P(disp.line2, PSTR("Pliki zamkniete"));
  126. }
  127. void disp_func_main_default(__attribute__ ((unused)) unsigned char changed) {
  128. if (FLAGS & F_FILEOPEN)
  129. strcpy_P(disp.line1, _logging_active);
  130. else
  131. strcpy_P(disp.line1, _card_ok);
  132. if (FLAGS & F_GPSOK)
  133. strcpy_P(disp.line2, _gps_ok);
  134. else
  135. strcpy_P(disp.line2, _gps_wait);
  136. }
  137. void disp_func_coord(__attribute__ ((unused)) unsigned char changed) {
  138. if (System.location_valid == LOC_INVALID) {
  139. strcpy_P(disp.line1, PSTR("??? N/S"));
  140. strcpy_P(disp.line2, PSTR("??? E/W"));
  141. return;
  142. }
  143. xsprintf(disp.line1, PSTR("%2.6f%c"), (location.lat < 0)?(-location.lat):location.lat, (location.lat < 0)?'S':'N');
  144. xsprintf(disp.line2, PSTR("%3.6f%c"), (location.lon < 0)?(-location.lon):location.lon, (location.lon < 0)?'W':'E');
  145. }
  146. void disp_func_ele_sat(__attribute__ ((unused)) unsigned char changed) {
  147. if (System.location_valid == LOC_INVALID) {
  148. strcpy_P(disp.line1, PSTR("ele = ???"));
  149. } else {
  150. xsprintf(disp.line1, PSTR("ele = %.1fm"), location.alt);
  151. }
  152. xsprintf(disp.line2, PSTR("%2d satelit"), System.satellites_used);
  153. if (System.sbas)
  154. strcat_P(disp.line2, PSTR(", DGPS"));
  155. }
  156. void disp_func_main_menu(__attribute__ ((unused)) unsigned char changed) {
  157. strcpy_P(disp.line1, PSTR(" *** MENU *** "));
  158. strcpy_P(disp.line2, PSTR("> Ustawienia"));
  159. }
  160. void disp_func_settings_menu(__attribute__ ((unused)) unsigned char changed) {
  161. display_settings_menu_item();
  162. }
  163. void (*__flash const disp_funcs[])(unsigned char) = {
  164. [DISPLAY_STATE_STARTUP] = disp_func_startup,
  165. [DISPLAY_STATE_POWEROFF] = disp_func_poweroff,
  166. [DISPLAY_STATE_POWEROFF_LOWBAT] = disp_func_poweroff_lowbat,
  167. [DISPLAY_STATE_START_MESSAGE] = disp_func_start_message,
  168. [DISPLAY_STATE_CARD_OK] = disp_func_card_ok,
  169. [DISPLAY_STATE_FILE_OPEN] = disp_func_file_open,
  170. [DISPLAY_STATE_FILE_CLOSED] = disp_func_file_closed,
  171. [DISPLAY_STATE_MAIN_DEFAULT] = disp_func_main_default,
  172. [DISPLAY_STATE_COORD] = disp_func_coord,
  173. [DISPLAY_STATE_ELE_SAT] = disp_func_ele_sat,
  174. [DISPLAY_STATE_MAIN_MENU] = disp_func_main_menu,
  175. [DISPLAY_STATE_SETTINGS_MENU] = disp_func_settings_menu,
  176. };
  177. void display_refresh(unsigned char newstate) {
  178. unsigned char changed = 0;
  179. if (newstate)
  180. changed = 1;
  181. disp_funcs[System.display_state](changed);
  182. if (timer_expired(lcd)) {
  183. changed = 1;
  184. set_timer(lcd, 1000);
  185. }
  186. /* write to LCD */
  187. if (changed) {
  188. battery_state_display();
  189. unsigned char len;
  190. LCD_GoTo(0,0);
  191. len = strlen(disp.line1);
  192. LCD_WriteText(disp.line1);
  193. while (len<15) {
  194. len++;
  195. LCD_WriteData(' ');
  196. }
  197. LCD_WriteData(0); /* battery symbol */
  198. LCD_GoTo(0,1);
  199. len = strlen(disp.line2);
  200. LCD_WriteText(disp.line2);
  201. while (len<16) {
  202. len++;
  203. LCD_WriteData(' ');
  204. }
  205. }
  206. }
  207. void display_state(unsigned char newstate) {
  208. System.display_state = newstate;
  209. display_refresh(newstate);
  210. }