display.c 6.9 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. __flash const char _gps_wait[] = "Czekam na GPS...";
  93. __flash const char _gps_ok[] = "GPS OK!";
  94. __flash const char _card_ok[] = "Karta OK!";
  95. __flash const char _logging_active[] = "Zapis aktywny";
  96. __flash const char _logging_paused[] = "Zapis wstrzymany";
  97. __flash const char _starting[] = "Uruchamianie...";
  98. __flash const char _battery_low[] = "Bateria slaba!";
  99. __flash const char _shutting_down[] = "Wylaczanie...";
  100. __flash const char _no_card[] = "Brak karty!";
  101. __flash const char _card_error[] = "Blad karty!";
  102. __flash const char _write_error[] = "Blad zapisu!";
  103. __flash const char _fat_write_error[] = "Blad zapisu FAT!";
  104. __flash const char _file_close_error[] = "Blad zamk.pliku!";
  105. __flash const char _file_open_error[] = "Blad otw. pliku!";
  106. __flash const char _files_closed[] = "Pliki zamkniete";
  107. __flash const char _files_open[] = "Pliki otwarte";
  108. __flash const char _tracking_paused[] = "Wstrzymano!";
  109. __flash const char _tracking_resumed[] = "Wznowiono!";
  110. __flash const char _point_saved[] = "Zapisano!";
  111. __flash const char _point_not_saved[] = "Nie zapisano!";
  112. __flash const char _logging_auto_paused[] = "Autom. wstrzym.";
  113. void display_event(unsigned char event) { /* overrides display with current messages */
  114. switch (event) {
  115. case DISPLAY_EVENT_STARTUP:
  116. strcpy_P(disp.line1, _starting);
  117. break;
  118. case DISPLAY_EVENT_LOW_BATTERY:
  119. strcpy_P(disp.line2, _battery_low);
  120. /* fall through */
  121. case DISPLAY_EVENT_POWEROFF:
  122. strcpy_P(disp.line1, _shutting_down);
  123. break;
  124. case DISPLAY_EVENT_INITIALIZED:
  125. strcpy_P(disp.line1, PSTR("Start"));
  126. switch(System.status){
  127. case STATUS_NO_POWER: case STATUS_OK: case STATUS_NO_GPS: disp.line2[0] = '\0'; break;
  128. case STATUS_NO_DISK: strcpy_P(disp.line2, _no_card); break;
  129. case STATUS_DISK_ERROR: strcpy_P(disp.line2, _card_error); break;
  130. case STATUS_FILE_WRITE_ERROR: strcpy_P(disp.line2, _write_error); break;
  131. case STATUS_FILE_SYNC_ERROR: strcpy_P(disp.line2, _fat_write_error); break;
  132. case STATUS_FILE_CLOSE_ERROR: strcpy_P(disp.line2, _file_close_error); break;
  133. case STATUS_FILE_OPEN_ERROR: strcpy_P(disp.line2, _file_open_error); break;
  134. }
  135. break;
  136. case DISPLAY_EVENT_CARD_INITIALIZED:
  137. strcpy_P(disp.line1, _card_ok);
  138. strcpy_P(disp.line2, _gps_wait);
  139. break;
  140. case DISPLAY_EVENT_FILE_CLOSED:
  141. strcpy_P(disp.line2, _files_closed);
  142. break;
  143. case DISPLAY_EVENT_FILE_OPEN:
  144. strcpy_P(disp.line2, _files_open);
  145. break;
  146. case DISPLAY_EVENT_TRACKING_PAUSED:
  147. strcpy_P(disp.line2, _tracking_paused);
  148. break;
  149. case DISPLAY_EVENT_TRACKING_RESUMED:
  150. strcpy_P(disp.line2, _tracking_resumed);
  151. break;
  152. case DISPLAY_EVENT_POINT_SAVED:
  153. strcpy_P(disp.line2, _point_saved);
  154. break;
  155. case DISPLAY_EVENT_POINT_NOT_SAVED:
  156. strcpy_P(disp.line2, _point_not_saved);
  157. break;
  158. }
  159. display_refresh(1);
  160. }
  161. void disp_func_main_default(void) {
  162. if (FLAGS & F_FILEOPEN) {
  163. if (System.tracking_paused) {
  164. strcpy_P(disp.line1, _logging_paused);
  165. } else if (System.tracking_auto_paused) {
  166. strcpy_P(disp.line1, _logging_auto_paused);
  167. } else {
  168. strcpy_P(disp.line1, _logging_active);
  169. }
  170. } else
  171. strcpy_P(disp.line1, _card_ok);
  172. if (FLAGS & F_GPSOK)
  173. strcpy_P(disp.line2, _gps_ok);
  174. else
  175. strcpy_P(disp.line2, _gps_wait);
  176. }
  177. void disp_func_coord(void) {
  178. if (System.location_valid == LOC_INVALID) {
  179. strcpy_P(disp.line1, PSTR("??? N/S"));
  180. strcpy_P(disp.line2, PSTR("??? E/W"));
  181. return;
  182. }
  183. xsprintf(disp.line1, PSTR("%2.6f%c"), (location.lat < 0)?(-location.lat):location.lat, (location.lat < 0)?'S':'N');
  184. xsprintf(disp.line2, PSTR("%3.6f%c"), (location.lon < 0)?(-location.lon):location.lon, (location.lon < 0)?'W':'E');
  185. }
  186. void disp_func_ele_sat(void) {
  187. if (System.location_valid == LOC_INVALID) {
  188. strcpy_P(disp.line1, PSTR("ele = ???"));
  189. } else {
  190. xsprintf(disp.line1, PSTR("ele = %.1fm"), location.alt);
  191. }
  192. xsprintf(disp.line2, PSTR("%2d satelit"), System.satellites_used);
  193. if (System.sbas)
  194. strcat_P(disp.line2, PSTR(", DGPS"));
  195. }
  196. void disp_distance_and_time(void) {
  197. xsprintf(disp.line1, PSTR("%.2f km"), (float)System.distance / 100000.0);
  198. if (utc > 0 && System.time_start > 0) {
  199. xsprintf(disp.line2, PSTR("t=%u s"), (unsigned int)(utc - System.time_start));
  200. } else {
  201. strcpy_P(disp.line2, PSTR("Czas nieznany"));
  202. }
  203. }
  204. void disp_speed(void) {
  205. strcpy_P(disp.line1, PSTR("Predkosc:"));
  206. if (utc > 0 && System.time_start > 0) {
  207. xsprintf(disp.line2, PSTR("%.2f km/h"), (float)System.distance / (float)(utc - System.time_start) * 0.036);
  208. } else {
  209. strcpy_P(disp.line2, PSTR("nieznana"));
  210. }
  211. }
  212. void disp_time(void) {
  213. if (utc == 0) {
  214. strcpy_P(disp.line1, PSTR("?"));
  215. strcpy_P(disp.line2, PSTR("?"));
  216. return;
  217. }
  218. time_t time = utc;
  219. time += local_time_diff(time) * (signed int)3600;
  220. struct tm *ct = gmtime(&time);
  221. xsprintf(disp.line1, PSTR("%d.%02d.%4d"), ct->tm_mday, ct->tm_mon+1, ct->tm_year+1900);
  222. xsprintf(disp.line2, PSTR("%d:%02d:%02d"), ct->tm_hour, ct->tm_min, ct->tm_sec);
  223. }
  224. void disp_func_temperature(void) {
  225. strcpy_P(disp.line1, PSTR("Temperatura"));
  226. if (System.temperature_ok) {
  227. xsprintf(disp.line2, PSTR("%.1f stC"), System.temperature);
  228. } else {
  229. strcpy_P(disp.line2, PSTR("Blad!"));
  230. }
  231. }
  232. void display_refresh(unsigned char changed) {
  233. if (timer_expired(lcd)) {
  234. changed = 1;
  235. }
  236. /* write to LCD */
  237. if (changed) {
  238. set_timer(lcd, 1000);
  239. battery_state_display();
  240. unsigned char len;
  241. LCD_GoTo(0,0);
  242. len = strlen(disp.line1);
  243. LCD_WriteText(disp.line1);
  244. while (len<15) {
  245. len++;
  246. LCD_WriteData(' ');
  247. }
  248. LCD_WriteData(0); /* battery symbol */
  249. LCD_GoTo(0,1);
  250. len = strlen(disp.line2);
  251. LCD_WriteText(disp.line2);
  252. while (len<16) {
  253. len++;
  254. LCD_WriteData(' ');
  255. }
  256. }
  257. }