alarm.c 5.3 KB

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