alarm.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 "httpstream.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. eenmaligAlarmCheck(i);
  24. if (alarm[i].time.tm_year == 0){
  25. alarm[i].state = 0;
  26. }
  27. if (alarm[i].state == 1 || alarm[i].state == 4){
  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. if (alarm[idx].state < 3){
  58. alarm[idx].state = 2;
  59. snooze[idx].snoozeTime = ct;
  60. snooze[idx].snoozeTime.tm_min += alarm[idx].snooze;
  61. stopStream();
  62. LcdBackLight(LCD_BACKLIGHT_OFF);
  63. stopStream();
  64. }
  65. }
  66. int daysInMonth(int m, int y) {
  67. if(m == 2 && isLeapYear(y))
  68. return 29 + (int)(m + floor(m/8)) % 2 + 2 % m + 2 * floor(1/m);
  69. return 28 + (int)(m + floor(m/8)) % 2 + 2 % m + 2 * floor(1/m);
  70. }
  71. int daysInYear(int y){
  72. if(isLeapYear(y))
  73. return 366;
  74. return 365;
  75. }
  76. int isLeapYear(int y){
  77. return (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0));
  78. }
  79. void AddSnoozeMinutes(int idx, int minutes){
  80. if (snooze[idx].snoozeTime.tm_min + minutes >= 60){ //Checks if minutes is >= 60 else minute
  81. snooze[idx].snoozeTime.tm_hour += 1;
  82. snooze[idx].snoozeTime.tm_min = ((snooze[idx].snoozeTime.tm_min + minutes) % 60);
  83. if (snooze[idx].snoozeTime.tm_hour >= 24){ //Checks if hours is >= 24
  84. snooze[idx].snoozeTime.tm_hour = 0;
  85. 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
  86. snooze[idx].snoozeTime.tm_mday += 1;
  87. } else { //If the days+1 is bigger than the amount of days in the month, day = 1 & month is + 1
  88. snooze[idx].snoozeTime.tm_mday = 1;
  89. if (snooze[idx].snoozeTime.tm_mon + 1 > 11){//If month+1 is bigger than 11 (month is 0-11) then month = 0 & year + 1
  90. snooze[idx].snoozeTime.tm_mon = 0;
  91. snooze[idx].snoozeTime.tm_year += 1;
  92. } else {
  93. snooze[idx].snoozeTime.tm_mon += 1;
  94. }
  95. }
  96. }
  97. } else {
  98. snooze[idx].snoozeTime.tm_min += minutes;
  99. }
  100. }
  101. void setState(int idx){
  102. struct _tm ct;
  103. X12RtcGetClock(&ct);
  104. //Set snooze time for snooze alarm
  105. if (alarm[idx].state == 0 && alarm[idx].state < 3){
  106. snooze[idx].snoozeTime = alarm[idx].time;
  107. AddSnoozeMinutes(idx,1);
  108. }
  109. //Check if alarm has to go off for snooze alarm
  110. if (compareTime(ct, alarm[idx].time) >= 1 && alarm[idx].time.tm_year != 0 && alarm[idx].state != 2 && alarm[idx].state < 3){
  111. alarm[idx].state = 1;
  112. } else if (alarm[idx].state != 2 && alarm[idx].state < 3){
  113. alarm[idx].state = 0;
  114. }
  115. //Check if alarm has to snooze
  116. if (compareTime(alarm[idx].time,snooze[idx].snoozeTime) >= 1 && alarm[idx].state < 3){
  117. alarm[idx].state = 2;
  118. }
  119. //Check if alarm has to snooze
  120. if (alarm[idx].state == 1 && compareTime(ct, snooze[idx].snoozeTime) >= 1 && alarm[idx].state < 3){
  121. alarm[idx].state = 2;
  122. snooze[idx].snoozeTime = ct;
  123. AddSnoozeMinutes(idx, alarm[idx].snooze);
  124. LcdBackLight(LCD_BACKLIGHT_OFF);
  125. stopStream();
  126. }
  127. //Check if snooze is done, and alarm goes off again
  128. if (alarm[idx].state == 2 && compareTime(ct, snooze[idx].snoozeTime) >= 1 && alarm[idx].state < 3){
  129. alarm[idx].state = 1;
  130. AddSnoozeMinutes(idx, 1);
  131. }
  132. }
  133. /*void getAlarm(struct _alarm *am){
  134. int i = 0;
  135. for (i = 0; i < n; i++){
  136. am[i] = alarm[i];
  137. }
  138. }*/
  139. void setAlarm(struct _tm time, char* name, char* ip, u_short port, char* url, int snooze, int id, int idx){
  140. alarm[idx].time = time;
  141. strncpy(alarm[idx].name, name, sizeof(alarm[idx].name));
  142. strncpy(alarm[idx].ip, ip, sizeof(alarm[idx].ip));
  143. alarm[idx].port = port;
  144. strncpy(alarm[idx].url, url, sizeof(alarm[idx].url));
  145. alarm[idx].snooze = snooze;
  146. alarm[idx].id = id;
  147. alarm[idx].state = 0;
  148. }
  149. void eenmaligAlarm(struct _tm time, char* name, char* ip, u_short port, char* url, int snooze, int id, int idx){
  150. alarm[idx].time = time;
  151. strncpy(alarm[idx].name, name, sizeof(alarm[idx].name));
  152. strncpy(alarm[idx].ip, ip, sizeof(alarm[idx].ip));
  153. alarm[idx].port = port;
  154. strncpy(alarm[idx].url, url, sizeof(alarm[idx].url));
  155. alarm[idx].id = id;
  156. alarm[idx].state = 3;
  157. }
  158. //Checks if there is an alarm that has to go off
  159. void eenmaligAlarmCheck(int idx){
  160. struct _tm ct;
  161. X12RtcGetClock(&ct);
  162. //Check if alarm goes off, compares the RTC time to the alarm time
  163. if (compareTime(ct, alarm[idx].time) >= 1 && alarm[idx].time.tm_year != 0 && alarm[idx].state == 3){
  164. alarm[idx].state = 4;
  165. snooze[idx].snoozeTime = ct;
  166. AddSnoozeMinutes(idx,2);
  167. }
  168. //Delete alarm after 30 minutes, compares the RTC time to the snooze
  169. if (compareTime(ct, snooze[idx].snoozeTime) >= 1 && alarm[idx].state == 4){
  170. deleteAlarm(idx);
  171. LcdBackLight(LCD_BACKLIGHT_OFF);
  172. stopStream();
  173. }
  174. }
  175. void deleteAlarm(int idx){
  176. struct _tm tm;
  177. tm.tm_year = 0;
  178. alarm[idx].time = tm;
  179. alarm[idx].port = 0;
  180. alarm[idx].snooze = 5;
  181. alarm[idx].id = -1;
  182. alarm[idx].state = -1;
  183. }
  184. void handleAlarm(int idx){
  185. if (alarm[idx].state < 3){
  186. alarm[idx].state = 0;
  187. alarm[idx].time.tm_mday += 1;
  188. } else if (alarm[idx].state == 4){
  189. deleteAlarm(idx);
  190. }
  191. }
  192. int compareTime(tm t1,tm t2){
  193. if (t1.tm_year > t2.tm_year){
  194. return 1;
  195. }
  196. if (t1.tm_year == t2.tm_year && t1.tm_mon > t2.tm_mon){
  197. return 2;
  198. }
  199. if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday > t2.tm_mday){
  200. return 3;
  201. }
  202. 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){
  203. return 4;
  204. }
  205. 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){
  206. return 5;
  207. }
  208. 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){
  209. return 6;
  210. }
  211. return 0;
  212. }