display.c 7.0 KB

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