display.c 7.0 KB

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