displayHandler.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // Created by Jordy Sipkema on 26/02/16.
  3. //
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include "display.h"
  8. #include "displayHandler.h"
  9. #include "ntp.h"
  10. #include "log.h"
  11. #include "mp3stream.h"
  12. #include "rtc.h"
  13. #include "alarm.h"
  14. #include "network.h"
  15. struct _tm lastDisplayTime;
  16. viewDisplays currentViewDisplay;
  17. u_long displayTime;
  18. void setCurrentDisplay(viewDisplays d, u_long dt){
  19. X12RtcGetClock(&lastDisplayTime);
  20. LcdBackLight(LCD_BACKLIGHT_ON);
  21. currentViewDisplay = d;
  22. displayTime = dt;
  23. }
  24. viewDisplays getCurrentDisplay(void){
  25. return currentViewDisplay;
  26. }
  27. void refreshScreen(){
  28. if(timerStruct(lastDisplayTime) > displayTime){
  29. currentViewDisplay = DISPLAY_DateTime;
  30. LcdBackLight(LCD_BACKLIGHT_OFF);
  31. }
  32. if(currentViewDisplay == DISPLAY_DateTime){
  33. displayDateTime();
  34. }else if(currentViewDisplay == DISPLAY_Volume){
  35. displayVolume();
  36. }else if(currentViewDisplay == DISPLAY_Alarm){
  37. displayAlarm(getRunningAlarmID());
  38. }
  39. }
  40. long timerStruct(struct _tm s){
  41. struct _tm ct;
  42. X12RtcGetClock(&ct);
  43. long stime = (s.tm_hour * 3600) + (s.tm_min * 60) + s.tm_sec;
  44. long ctime = (ct.tm_hour * 3600) + (ct.tm_min * 60) + ct.tm_sec;
  45. return ctime - stime;
  46. }
  47. void (*write_display_ptr[2])(char*, int) = {LcdArrayLineOne, LcdArrayLineTwo};
  48. void displayDateTime(void){
  49. tm time;
  50. X12RtcGetClock(&time);
  51. char str1[16];
  52. char str2[16];
  53. if (1){
  54. sprintf(str1, " %02d:%02d:%02d ", time.tm_hour, time.tm_min, time.tm_sec);
  55. sprintf(str2, " %02d-%02d-%04d ", time.tm_mday, time.tm_mon + MONTH_OFFSET, time.tm_year + YEAR_OFFSET);
  56. }else {
  57. sprintf(str1, " ??:??:?? ");
  58. sprintf(str2, " ??:??:?? ");
  59. }
  60. if (NtpIsSyncing()) {
  61. str2[1] = 'S';
  62. }else if(NetworkIsReceiving()){
  63. str2[1] = 'N';
  64. }
  65. (*write_display_ptr[0])(str1, 16);
  66. (*write_display_ptr[1])(str2, 16);
  67. }
  68. void displayAlarm(char idx)
  69. {
  70. if(idx == -1){
  71. currentViewDisplay = DISPLAY_DateTime;
  72. }
  73. int i;
  74. int j;
  75. int startidx;
  76. char str[16];
  77. struct _alarm *am = getAlarm(idx);
  78. sprintf(str, " %02d:%02d:%02d ", am->time.tm_hour, am->time.tm_min, am->time.tm_sec);
  79. (*write_display_ptr[0])(str, 16);
  80. j = 0;
  81. char str2[16];
  82. for (i = 0; i < 16;i++){
  83. if (am->name[i] != 0){
  84. j = j + 1;
  85. }
  86. }
  87. if (j != 16){
  88. startidx = (8-(j/2));
  89. }
  90. j = 0;
  91. for(i = 0; i < 16; i++){
  92. if (i >= startidx){
  93. if (am->name[j] != 0){
  94. str2[i] = am->name[j];
  95. } else {
  96. str2[i] = ' ';
  97. }
  98. j++;
  99. } else {
  100. str2[i] = ' ';
  101. }
  102. }
  103. (*write_display_ptr[1])(str2, 16);
  104. }
  105. void displayVolume()
  106. {
  107. u_char pos = getVolume();
  108. ClearLcd();
  109. int i;
  110. LcdArrayLineOne(" Volume ", 16);
  111. char characters[17];
  112. for(i = 0; i < 17; i++)
  113. {
  114. characters[i] = 0xFF;
  115. }
  116. LcdArrayLineTwo(characters,pos);
  117. }