displayHandler.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. int j;
  83. int startidx;
  84. char str[16];
  85. struct _alarm *am = getAlarm(idx);
  86. sprintf(str, " %02d:%02d:%02d ", am->time.tm_hour, am->time.tm_min, am->time.tm_sec);
  87. (*write_display_ptr[0])(str, 16);
  88. j = 0;
  89. char str2[16];
  90. for (i = 0; i < 16;i++){
  91. if (am->name[i] != 0){
  92. j = j + 1;
  93. }
  94. }
  95. if (j != 16){
  96. startidx = (8-(j/2));
  97. }
  98. j = 0;
  99. for(i = 0; i < 16; i++){
  100. if (i >= startidx){
  101. if (am->name[j] != 0){
  102. str2[i] = am->name[j];
  103. } else {
  104. str2[i] = ' ';
  105. }
  106. j++;
  107. } else {
  108. str2[i] = ' ';
  109. }
  110. }
  111. (*write_display_ptr[1])(str2, 16);
  112. }
  113. void displayVolume()
  114. {
  115. u_char pos = getVolume();
  116. int i;
  117. LcdArrayLineOne(" Volume ", 16);
  118. char characters[17];
  119. for(i = 0; i < 17; i++)
  120. {
  121. if(i < pos) {
  122. characters[i] = 0xFF;
  123. }else {
  124. characters[i] = ' ';
  125. }
  126. }
  127. LcdArrayLineTwo(characters,16);
  128. }
  129. void displayTwitter(char* text)
  130. {
  131. //int lineNumber,char text[]
  132. ClearLcd();
  133. LcdBackLight(LCD_BACKLIGHT_ON);
  134. LcdArrayLineOne(" Twitter ", 16);
  135. int j = 0;
  136. int i;
  137. char text1[16];
  138. //char text2[140] = text;
  139. // int shift = 0;
  140. //char *text = "Twitter";
  141. for(i = 0; i<140;i++){
  142. if (text[i] != 0){
  143. j++;
  144. }
  145. }
  146. for(i = 0; i < 16; i++){
  147. if (text[Scroller+i]!= 0) {
  148. text1[i] = text[Scroller + i];
  149. } else {
  150. text1[i] = ' ';
  151. }
  152. }
  153. LcdArrayLineTwo(text1,16);
  154. Scroller++;
  155. if (Scroller > j){
  156. Scroller = 0;
  157. }
  158. NutDelay(500);
  159. }
  160. void displayTwitch(char name[], char title[], char game[])
  161. {
  162. ClearLcd();
  163. LcdArrayLineOne(name, strlen(name));
  164. LcdArrayLineTwo("Streaming", 9);
  165. LcdBackLight(LCD_BACKLIGHT_ON);
  166. }
  167. void displayStreamInfo(){
  168. LcdBackLight(LCD_BACKLIGHT_ON);
  169. (*write_display_ptr[0])(" Station Info ", 17);
  170. (*write_display_ptr[1])(" ", 17);
  171. (*write_display_ptr[1])(getStreamInfo(), 17);
  172. }