gotosleep.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <assert.h>
  4. #include <sys/thread.h>
  5. #include <sys/timer.h>
  6. #include <sys/version.h>
  7. #include <dev/irqreg.h>
  8. #include "log.h"
  9. #include "rtc.h"
  10. #include "alarm.h"
  11. #include "display.h"
  12. #include "mp3stream.h"
  13. #include "gotosleep.h"
  14. struct _tm sleepTime;
  15. struct _tm count;
  16. int i = 1;
  17. int x = 0;
  18. bool sleepOn = false;
  19. void setSleep(void)
  20. {
  21. if (sleepOn == true)
  22. {
  23. killPlayerThread();
  24. sleepOn = false;
  25. }
  26. else{
  27. X12RtcGetClock(&count);
  28. sleepTime = count;
  29. AddMinutes(60);
  30. changeChanel();
  31. x = sleepTime.tm_min;
  32. sleepOn = true;
  33. }
  34. }
  35. void AddMinutes(int minutes){
  36. if (sleepTime.tm_min + minutes >= 60){ //Checks if minutes is >= 60 else minute
  37. sleepTime.tm_hour += 1;
  38. sleepTime.tm_min = ((sleepTime.tm_min + minutes) % 60);
  39. if (sleepTime.tm_hour >= 24){ //Checks if hours is >= 24
  40. sleepTime.tm_hour = 0;
  41. if ((sleepTime.tm_mday + 1) <= daysInMonth((sleepTime.tm_mon+1), (sleepTime.tm_year+1900))){ //Checks if day+1 smaller or even is to the amount of days in the month
  42. sleepTime.tm_mday += 1;
  43. } else { //If the days+1 is bigger than the amount of days in the month, day = 1 & month is + 1
  44. sleepTime.tm_mday = 1;
  45. if (sleepTime.tm_mon + 1 > 11){//If month+1 is bigger than 11 (month is 0-11) then month = 0 & year + 1
  46. sleepTime.tm_mon = 0;
  47. sleepTime.tm_year += 1;
  48. } else {
  49. sleepTime.tm_mon += 1;
  50. }
  51. }
  52. }
  53. } else {
  54. sleepTime.tm_min += minutes;
  55. }
  56. }
  57. void checkSleep(void)
  58. {
  59. if(sleepOn == true){
  60. X12RtcGetClock(&count);
  61. if(compareTime(count, sleepTime) == 0){
  62. killPlayerThread();
  63. sleepOn = false;
  64. }
  65. if(compareTime(count, sleepTime) == 5 && sleepTime.tm_min == x){
  66. volumeDown();
  67. if (x >= 60){
  68. x = 0;
  69. }
  70. x += 2;
  71. }
  72. }
  73. }
  74. void changeChanel(void){
  75. if (i > 2){
  76. i =1;
  77. }
  78. switch(i) {
  79. case 1 :
  80. killPlayerThread();
  81. connectToStream("62.195.226.247",80,"/test.mp3");
  82. play();
  83. i++;
  84. break;
  85. case 2 :
  86. killPlayerThread();
  87. connectToStream("62.195.226.247",80,"/test2.mp3");
  88. play();
  89. i++;
  90. break;
  91. }
  92. }