displayHandler.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 && currentViewDisplay != DISPLAY_Alarm){
  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. /* if(ctime < 0){
  54. return 0;
  55. }*/
  56. return ctime - stime;
  57. }
  58. void (*write_display_ptr[2])(char*, int) = {LcdArrayLineOne, LcdArrayLineTwo};
  59. void displayDateTime(void){
  60. tm time;
  61. X12RtcGetClock(&time);
  62. char str1[16];
  63. char str2[16];
  64. if (1){
  65. sprintf(str1, " %02d:%02d:%02d ", time.tm_hour, time.tm_min, time.tm_sec);
  66. sprintf(str2, " %02d-%02d-%04d ", time.tm_mday, time.tm_mon + MONTH_OFFSET, time.tm_year + YEAR_OFFSET);
  67. } else {
  68. sprintf(str1, " ??:??:?? ");
  69. sprintf(str2, " ??:??:?? ");
  70. }
  71. if (NtpIsSyncing()) {
  72. str2[1] = 'S';
  73. } else if(NetworkIsReceiving()){
  74. str2[1] = 'N';
  75. }
  76. (*write_display_ptr[0])(str1, 16);
  77. (*write_display_ptr[1])(str2, 16);
  78. }
  79. void displayAlarm(char idx)
  80. {
  81. if (idx == -1){
  82. currentViewDisplay = DISPLAY_DateTime;
  83. }
  84. int i;
  85. char str[16];
  86. struct _alarm *am = getAlarm(idx);
  87. sprintf(str, " %02d:%02d:%02d ", am->time.tm_hour, am->time.tm_min, am->time.tm_sec);
  88. (*write_display_ptr[0])(str, 16);
  89. char str2[16];
  90. for (i = 0; i < 16;i++){
  91. str2[i] = am->name[i];
  92. }
  93. (*write_display_ptr[1])(str2, 16);
  94. }
  95. void displayVolume()
  96. {
  97. u_char pos = getVolume();
  98. int i;
  99. LcdArrayLineOne(" Volume ", 16);
  100. char characters[17];
  101. for(i = 0; i < 17; i++)
  102. {
  103. if(i < pos) {
  104. characters[i] = 0xFF;
  105. }else {
  106. characters[i] = ' ';
  107. }
  108. }
  109. LcdArrayLineTwo(characters,16);
  110. }
  111. void displayTwitter(char* text)
  112. {
  113. ClearLcd();
  114. LcdBackLight(LCD_BACKLIGHT_ON);
  115. LcdArrayLineOne(" Twitter ", 16);
  116. int j = 0;
  117. int i;
  118. char text1[16];
  119. for(i = 0; i<140;i++){
  120. if (text[i] != 0){
  121. j++;
  122. }
  123. }
  124. for(i = 0; i < 16; i++){
  125. if (text[Scroller+i]!= 0) {
  126. text1[i] = text[Scroller + i];
  127. } else {
  128. text1[i] = ' ';
  129. }
  130. }
  131. LcdArrayLineTwo(text1,16);
  132. Scroller++;
  133. if (Scroller > j){
  134. Scroller = 0;
  135. }
  136. }
  137. void displayTwitch(char name[], char title[], char game[]) {
  138. if (timerStruct(lastDisplayTime) % 10 < 5) {
  139. ClearLcd();
  140. if(strlen(name) < 16) {
  141. LcdArrayLineOne(name, strlen(name));
  142. }else {
  143. LcdArrayLineOne(name, 16);
  144. }
  145. LcdArrayLineTwo("is streaming ", 16);
  146. }
  147. else {
  148. ClearLcd();
  149. if(strlen(title) > 16) {
  150. LcdArrayLineOne(title, 16);
  151. }else {
  152. LcdArrayLineOne(title, strlen(title));
  153. }if(strlen(game) > 16) {
  154. LcdArrayLineTwo(game, 16);
  155. }else{
  156. LcdArrayLineTwo(game, strlen(game));
  157. }
  158. }
  159. LcdBackLight(LCD_BACKLIGHT_ON);
  160. }
  161. void displayStreamInfo(){
  162. LcdBackLight(LCD_BACKLIGHT_ON);
  163. char offset = getScrollOffset();
  164. if (offset == 0)
  165. (*write_display_ptr[1])(" ", 17);
  166. (*write_display_ptr[0])(" Station Info ", 17);
  167. //char* streamInfo = getStreamInfo();
  168. // I have to copy the StreamInfo buffer, I kept overwriting it.
  169. char streamInfo[48] = " No info ";
  170. char* streamInfoPtr = &streamInfo[0];
  171. strncpy(streamInfo, getStreamInfo(), 48); // copy the streamInfo buffer.
  172. streamInfo[48] = '\0'; // To be sure...
  173. if (offset >= 0)
  174. streamInfoPtr += offset;
  175. streamInfoPtr[16] = '\0';
  176. (*write_display_ptr[1])(streamInfoPtr, 17);
  177. incrementScrollOffset();
  178. NutDelay(500);
  179. }