displayHandler.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #include "twitch.h"
  16. struct _tm lastDisplayTime;
  17. viewDisplays currentViewDisplay;
  18. u_long displayTime;
  19. void setCurrentDisplay(viewDisplays d, u_long dt){
  20. X12RtcGetClock(&lastDisplayTime);
  21. LcdBackLight(LCD_BACKLIGHT_ON);
  22. currentViewDisplay = d;
  23. displayTime = dt;
  24. }
  25. viewDisplays getCurrentDisplay(void){
  26. return currentViewDisplay;
  27. }
  28. void refreshScreen(){
  29. if(timerStruct(lastDisplayTime) > displayTime){
  30. currentViewDisplay = DISPLAY_DateTime;
  31. LcdBackLight(LCD_BACKLIGHT_OFF);
  32. }
  33. if(currentViewDisplay == DISPLAY_DateTime){
  34. displayDateTime();
  35. }else if(currentViewDisplay == DISPLAY_Volume){
  36. displayVolume();
  37. }else if(currentViewDisplay == DISPLAY_Alarm){
  38. displayAlarm(getRunningAlarmID());
  39. }else if(currentViewDisplay == DISPLAY_Twitch){
  40. displayTwitch(data.name, data.title, data.game);
  41. }
  42. }
  43. long timerStruct(struct _tm s){
  44. struct _tm ct;
  45. X12RtcGetClock(&ct);
  46. long stime = (s.tm_hour * 3600) + (s.tm_min * 60) + s.tm_sec;
  47. long ctime = (ct.tm_hour * 3600) + (ct.tm_min * 60) + ct.tm_sec;
  48. return ctime - stime;
  49. }
  50. void (*write_display_ptr[2])(char*, int) = {LcdArrayLineOne, LcdArrayLineTwo};
  51. void displayDateTime(void){
  52. tm time;
  53. X12RtcGetClock(&time);
  54. char str1[16];
  55. char str2[16];
  56. if (1){
  57. sprintf(str1, " %02d:%02d:%02d ", time.tm_hour, time.tm_min, time.tm_sec);
  58. sprintf(str2, " %02d-%02d-%04d ", time.tm_mday, time.tm_mon + MONTH_OFFSET, time.tm_year + YEAR_OFFSET);
  59. }else {
  60. sprintf(str1, " ??:??:?? ");
  61. sprintf(str2, " ??:??:?? ");
  62. }
  63. if (NtpIsSyncing()) {
  64. str2[1] = 'S';
  65. }else if(NetworkIsReceiving()){
  66. str2[1] = 'N';
  67. }
  68. (*write_display_ptr[0])(str1, 16);
  69. (*write_display_ptr[1])(str2, 16);
  70. }
  71. void displayAlarm(char idx)
  72. {
  73. if(idx == -1){
  74. currentViewDisplay = DISPLAY_DateTime;
  75. }
  76. int i;
  77. int j;
  78. int startidx;
  79. char str[16];
  80. struct _alarm *am = getAlarm(idx);
  81. sprintf(str, " %02d:%02d:%02d ", am->time.tm_hour, am->time.tm_min, am->time.tm_sec);
  82. (*write_display_ptr[0])(str, 16);
  83. j = 0;
  84. char str2[16];
  85. for (i = 0; i < 16;i++){
  86. if (am->name[i] != 0){
  87. j = j + 1;
  88. }
  89. }
  90. if (j != 16){
  91. startidx = (8-(j/2));
  92. }
  93. j = 0;
  94. for(i = 0; i < 16; i++){
  95. if (i >= startidx){
  96. if (am->name[j] != 0){
  97. str2[i] = am->name[j];
  98. } else {
  99. str2[i] = ' ';
  100. }
  101. j++;
  102. } else {
  103. str2[i] = ' ';
  104. }
  105. }
  106. (*write_display_ptr[1])(str2, 16);
  107. }
  108. void displayVolume()
  109. {
  110. u_char pos = getVolume();
  111. int i;
  112. LcdArrayLineOne(" Volume ", 16);
  113. char characters[17];
  114. for(i = 0; i < 17; i++)
  115. {
  116. if(i < pos) {
  117. characters[i] = 0xFF;
  118. }else {
  119. characters[i] = ' ';
  120. }
  121. }
  122. LcdArrayLineTwo(characters,16);
  123. }
  124. void displayTwitter(int lineNumber,char text[])
  125. {
  126. ClearLcd();
  127. int i;
  128. if (lineNumber > -1 && lineNumber < 2){
  129. (*write_display_ptr[lineNumber])(text,strlen(text));
  130. }
  131. }
  132. void displayTwitch(char name[], char title[], char game[]) {
  133. if (timerStruct(lastDisplayTime) % 10 < 5) {
  134. ClearLcd();
  135. if(strlen(name) > 16) {
  136. LcdArrayLineOne(name, strlen(name));
  137. }else {
  138. LcdArrayLineTwo(name, 16);
  139. }
  140. LcdArrayLineTwo("is streaming", 12);
  141. }
  142. else {
  143. ClearLcd();
  144. if(strlen(title) > 16) {
  145. LcdArrayLineOne(title, 16);
  146. }else {
  147. LcdArrayLineOne(title, strlen(title));
  148. }if(strlen(game) > 16) {
  149. LcdArrayLineTwo(game, 16);
  150. }else{
  151. LcdArrayLineTwo(game, strlen(game));
  152. }
  153. }
  154. LcdBackLight(LCD_BACKLIGHT_ON);
  155. }