alarm.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #define LOG_MODULE LOG_MAIN_MODULE
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <assert.h>
  6. #include "log.h"
  7. #include "rtc.h"
  8. #include "alarm.h"
  9. #include "display.h"
  10. #include "mp3stream.h"
  11. #define n 5
  12. struct _snooze
  13. {
  14. struct _tm snoozeTime;
  15. };
  16. struct _alarm alarm[n];
  17. struct _snooze snooze[n];
  18. int checkAlarms(){
  19. int i = 0;
  20. int check = 0;
  21. for (i = 0; i < n; i++){
  22. setState(i);
  23. if (alarm[i].time.tm_year == 0){
  24. alarm[i].state = 0;
  25. }
  26. if (alarm[i].state == 1){
  27. check = 1;
  28. }
  29. }
  30. if (check == 1){
  31. return 1;
  32. }
  33. return 0;
  34. }
  35. int alarmExist(int id){
  36. int g;
  37. for (g = 0; g < n; g++){
  38. if (alarm[g].id == id){
  39. return g;
  40. }
  41. }
  42. return -1;
  43. }
  44. struct _alarm getAlarm(int idx){
  45. return alarm[idx];
  46. }
  47. int getState(int idx){
  48. return alarm[idx].state;
  49. }
  50. int maxAlarms(){
  51. return n;
  52. }
  53. void setSnooze(int idx){
  54. struct _tm ct;
  55. X12RtcGetClock(&ct);
  56. alarm[idx].state = 2;
  57. snooze[idx].snoozeTime = ct;
  58. snooze[idx].snoozeTime.tm_min += alarm[idx].snooze;
  59. }
  60. int daysInMonth(int m, int y) {
  61. if(m == 2 && isLeapYear(y))
  62. return 29 + (int)(m + floor(m/8)) % 2 + 2 % m + 2 * floor(1/m);
  63. return 28 + (int)(m + floor(m/8)) % 2 + 2 % m + 2 * floor(1/m);
  64. }
  65. int daysInYear(int y){
  66. if(isLeapYear(y))
  67. return 366;
  68. return 365;
  69. }
  70. int isLeapYear(int y){
  71. return (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0));
  72. }
  73. void AddSnoozeMinutes(int idx, int minutes){
  74. if (snooze[idx].snoozeTime.tm_min + minutes >= 60){ //Checks if minutes is >= 60 else minute
  75. snooze[idx].snoozeTime.tm_hour += 1;
  76. snooze[idx].snoozeTime.tm_min = ((snooze[idx].snoozeTime.tm_min + minutes) % 60);
  77. if (snooze[idx].snoozeTime.tm_hour >= 24){ //Checks if hours is >= 24
  78. snooze[idx].snoozeTime.tm_hour = 0;
  79. if ((snooze[idx].snoozeTime.tm_mday + 1) <= daysInMonth((snooze[idx].snoozeTime.tm_mon+1), (snooze[idx].snoozeTime.tm_year+1900))){ //Checks if day+1 smaller or even is to the amount of days in the month
  80. snooze[idx].snoozeTime.tm_mday += 1;
  81. } else { //If the days+1 is bigger than the amount of days in the month, day = 1 & month is + 1
  82. snooze[idx].snoozeTime.tm_mday = 1;
  83. if (snooze[idx].snoozeTime.tm_mon + 1 > 11){//If month+1 is bigger than 11 (month is 0-11) then month = 0 & year + 1
  84. snooze[idx].snoozeTime.tm_mon = 0;
  85. snooze[idx].snoozeTime.tm_year += 1;
  86. } else {
  87. snooze[idx].snoozeTime.tm_mon += 1;
  88. }
  89. }
  90. }
  91. } else {
  92. snooze[idx].snoozeTime.tm_min += minutes;
  93. }
  94. }
  95. void setState(int idx){
  96. struct _tm ct;
  97. X12RtcGetClock(&ct);
  98. if (alarm[idx].state == 0){
  99. snooze[idx].snoozeTime = alarm[idx].time;
  100. AddSnoozeMinutes(idx,1);
  101. }
  102. if (compareTime(ct, alarm[idx].time) >= 1 && alarm[idx].time.tm_year != 0 && alarm[idx].state != 2){
  103. if(alarm[idx].state != 1) {
  104. alarm[idx].state = 1;
  105. printf("\n\nAlarm gaat nu af!\n\n");
  106. bool success = connectToStream(alarm[idx].ip, alarm[idx].port, alarm[idx].url);
  107. if (success == true){
  108. play();
  109. }else {
  110. printf("ConnectToStream failed. Aborting.\n\n");
  111. }
  112. }
  113. } else if (alarm[idx].state != 2){
  114. alarm[idx].state = 0;
  115. }
  116. if (compareTime(alarm[idx].time,snooze[idx].snoozeTime) >= 1){
  117. alarm[idx].state = 2;
  118. }
  119. if (alarm[idx].state == 1 && compareTime(ct, snooze[idx].snoozeTime) >= 1){
  120. alarm[idx].state = 2;
  121. snooze[idx].snoozeTime = ct;
  122. AddSnoozeMinutes(idx, alarm[idx].snooze);
  123. LcdBackLight(LCD_BACKLIGHT_OFF);
  124. killPlayerThread();
  125. }
  126. if (alarm[idx].state == 2 && compareTime(ct, snooze[idx].snoozeTime) >= 1){
  127. if(alarm[idx].state != 1){
  128. printf("Alarm komt nu uit snooze!!");
  129. bool success = connectToStream(alarm[idx].ip, alarm[idx].port, alarm[idx].url);
  130. if (success == true){
  131. play();
  132. }else {
  133. printf("ConnectToStream failed. Aborting.\n\n");
  134. }
  135. alarm[idx].state = 1;
  136. }
  137. AddSnoozeMinutes(idx, 1);
  138. }
  139. }
  140. /*void getAlarm(struct _alarm *am){
  141. int i = 0;
  142. for (i = 0; i < n; i++){
  143. am[i] = alarm[i];
  144. }
  145. }*/
  146. void setAlarm(struct _tm time, char* name, char* ip, u_short port, char* url, int snooze, int id, int idx){
  147. alarm[idx].time = time;
  148. strncpy(alarm[idx].name, name, sizeof(alarm[idx].name));
  149. strncpy(alarm[idx].ip, ip, sizeof(alarm[idx].ip));
  150. alarm[idx].port = port;
  151. strncpy(alarm[idx].url, url, sizeof(alarm[idx].url));
  152. alarm[idx].snooze = snooze;
  153. alarm[idx].id = id;
  154. alarm[idx].state = 0;
  155. }
  156. void deleteAlarm(int idx){
  157. struct _tm tm;
  158. tm.tm_year = 0;
  159. alarm[idx].time = tm;
  160. alarm[idx].port = 0;
  161. alarm[idx].snooze = 5;
  162. alarm[idx].id = -1;
  163. alarm[idx].state = -1;
  164. }
  165. void handleAlarm(int idx){
  166. alarm[idx].state = 0;
  167. alarm[idx].time.tm_mday += 1;
  168. killPlayerThread();
  169. printf("state is %d \n",alarm[idx].state);
  170. }
  171. int compareTime(tm t1,tm t2){
  172. if (t1.tm_year > t2.tm_year){
  173. return 1;
  174. }
  175. if (t1.tm_year == t2.tm_year && t1.tm_mon > t2.tm_mon){
  176. return 2;
  177. }
  178. if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday > t2.tm_mday){
  179. return 3;
  180. }
  181. if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour > t2.tm_hour){
  182. return 4;
  183. }
  184. if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour == t2.tm_hour && t1.tm_min > t2.tm_min){
  185. return 5;
  186. }
  187. if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour == t2.tm_hour && t1.tm_min == t2.tm_min &&t1.tm_sec > t2.tm_sec){
  188. return 6;
  189. }
  190. return 0;
  191. }