displayHandler.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. }
  45. }
  46. long timerStruct(struct _tm s){
  47. struct _tm ct;
  48. X12RtcGetClock(&ct);
  49. long stime = (s.tm_hour * 3600) + (s.tm_min * 60) + s.tm_sec;
  50. long ctime = (ct.tm_hour * 3600) + (ct.tm_min * 60) + ct.tm_sec;
  51. return ctime - stime;
  52. }
  53. void (*write_display_ptr[2])(char*, int) = {LcdArrayLineOne, LcdArrayLineTwo};
  54. void displayDateTime(void){
  55. tm time;
  56. X12RtcGetClock(&time);
  57. char str1[16];
  58. char str2[16];
  59. if (1){
  60. sprintf(str1, " %02d:%02d:%02d ", time.tm_hour, time.tm_min, time.tm_sec);
  61. sprintf(str2, " %02d-%02d-%04d ", time.tm_mday, time.tm_mon + MONTH_OFFSET, time.tm_year + YEAR_OFFSET);
  62. }else {
  63. sprintf(str1, " ??:??:?? ");
  64. sprintf(str2, " ??:??:?? ");
  65. }
  66. if (NtpIsSyncing()) {
  67. str2[1] = 'S';
  68. }else if(NetworkIsReceiving()){
  69. str2[1] = 'N';
  70. }
  71. (*write_display_ptr[0])(str1, 16);
  72. (*write_display_ptr[1])(str2, 16);
  73. }
  74. void displayAlarm(char idx)
  75. {
  76. if(idx == -1){
  77. currentViewDisplay = DISPLAY_DateTime;
  78. }
  79. int i;
  80. int j;
  81. int startidx;
  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. j = 0;
  87. char str2[16];
  88. for (i = 0; i < 16;i++){
  89. if (am->name[i] != 0){
  90. j = j + 1;
  91. }
  92. }
  93. if (j != 16){
  94. startidx = (8-(j/2));
  95. }
  96. j = 0;
  97. for(i = 0; i < 16; i++){
  98. if (i >= startidx){
  99. if (am->name[j] != 0){
  100. str2[i] = am->name[j];
  101. } else {
  102. str2[i] = ' ';
  103. }
  104. j++;
  105. } else {
  106. str2[i] = ' ';
  107. }
  108. }
  109. (*write_display_ptr[1])(str2, 16);
  110. }
  111. void displayVolume()
  112. {
  113. u_char pos = getVolume();
  114. int i;
  115. LcdArrayLineOne(" Volume ", 16);
  116. char characters[17];
  117. for(i = 0; i < 17; i++)
  118. {
  119. if(i < pos) {
  120. characters[i] = 0xFF;
  121. }else {
  122. characters[i] = ' ';
  123. }
  124. }
  125. LcdArrayLineTwo(characters,16);
  126. }
  127. void displayTwitter(char* text)
  128. {
  129. //int lineNumber,char text[]
  130. ClearLcd();
  131. LcdBackLight(LCD_BACKLIGHT_ON);
  132. LcdArrayLineOne(" Twitter ", 16);
  133. int j = 0;
  134. int i;
  135. char text1[16];
  136. //char text2[140] = text;
  137. // int shift = 0;
  138. //char *text = "Twitter";
  139. for(i = 0; i<140;i++){
  140. if (text[i] != 0){
  141. j++;
  142. }
  143. }
  144. for(i = 0; i < 16; i++){
  145. if (text[Scroller+i]!= 0) {
  146. text1[i] = text[Scroller + i];
  147. } else {
  148. text1[i] = ' ';
  149. }
  150. }
  151. LcdArrayLineTwo(text1,16);
  152. Scroller++;
  153. if (Scroller > j){
  154. Scroller = 0;
  155. }
  156. NutDelay(500);
  157. }
  158. void displayTwitch(char name[], char title[], char game[])
  159. {
  160. ClearLcd();
  161. LcdArrayLineOne(name, strlen(name));
  162. LcdArrayLineTwo("Streaming", 9);
  163. LcdBackLight(LCD_BACKLIGHT_ON);
  164. }