displayHandler.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. 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. //int lineNumber,char text[]
  114. ClearLcd();
  115. LcdBackLight(LCD_BACKLIGHT_ON);
  116. LcdArrayLineOne(" Twitter ", 16);
  117. int j = 0;
  118. int i;
  119. char text1[16];
  120. //char text2[140] = text;
  121. // int shift = 0;
  122. //char *text = "Twitter";
  123. for(i = 0; i<140;i++){
  124. if (text[i] != 0){
  125. j++;
  126. }
  127. }
  128. for(i = 0; i < 16; i++){
  129. if (text[Scroller+i]!= 0) {
  130. text1[i] = text[Scroller + i];
  131. } else {
  132. text1[i] = ' ';
  133. }
  134. }
  135. LcdArrayLineTwo(text1,16);
  136. Scroller++;
  137. if (Scroller > j){
  138. Scroller = 0;
  139. }
  140. NutDelay(500);
  141. }
  142. void displayTwitch(char name[], char title[], char game[]) {
  143. if (timerStruct(lastDisplayTime) % 10 < 5) {
  144. ClearLcd();
  145. if(strlen(name) < 16) {
  146. LcdArrayLineOne(name, strlen(name));
  147. }else {
  148. LcdArrayLineOne(name, 16);
  149. }
  150. LcdArrayLineTwo("is streaming ", 16);
  151. }
  152. else {
  153. ClearLcd();
  154. if(strlen(title) > 16) {
  155. LcdArrayLineOne(title, 16);
  156. }else {
  157. LcdArrayLineOne(title, strlen(title));
  158. }if(strlen(game) > 16) {
  159. LcdArrayLineTwo(game, 16);
  160. }else{
  161. LcdArrayLineTwo(game, strlen(game));
  162. }
  163. }
  164. LcdBackLight(LCD_BACKLIGHT_ON);
  165. }
  166. void displayStreamInfo(){
  167. LcdBackLight(LCD_BACKLIGHT_ON);
  168. char offset = getScrollOffset();
  169. if (offset == 0)
  170. (*write_display_ptr[1])(" ", 17);
  171. (*write_display_ptr[0])(" Station Info ", 17);
  172. //char* streamInfo = getStreamInfo();
  173. // I have to copy the StreamInfo buffer, I kept overwriting it.
  174. char streamInfo[48] = " No info ";
  175. char* streamInfoPtr = &streamInfo[0];
  176. strncpy(streamInfo, getStreamInfo(), 48); // copy the streamInfo buffer.
  177. streamInfo[48] = '\0'; // To be sure...
  178. if (offset >= 0)
  179. streamInfoPtr += offset;
  180. streamInfoPtr[16] = '\0';
  181. (*write_display_ptr[1])(streamInfoPtr, 17);
  182. incrementScrollOffset();
  183. NutDelay(500);
  184. }