displayHandler.c 4.6 KB

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