alarm.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. if (alarm[i].time.tm_year == 0){
  24. alarm[i].state = 0;
  25. }else{
  26. setState(i);
  27. eenmaligAlarmCheck(i);
  28. }
  29. if (alarm[i].state == 1 || alarm[i].state == 4){
  30. check = 1;
  31. }
  32. }
  33. if (check == 1){
  34. return 1;
  35. }
  36. return 0;
  37. }
  38. int alarmExist(int id){
  39. int g;
  40. for (g = 0; g < n; g++){
  41. if (alarm[g].id == id){
  42. return g;
  43. }
  44. }
  45. return -1;
  46. }
  47. struct _alarm* getAlarm(int idx){
  48. return &alarm[idx];
  49. }
  50. char getRunningAlarmID(){
  51. char idx;
  52. for (idx = 0; idx < 5; idx++) {
  53. if (getState(idx) == 1 || getState(idx) == 4) {
  54. return idx;
  55. }
  56. }
  57. return -1;
  58. }
  59. int getState(int idx){
  60. return alarm[idx].state;
  61. }
  62. int maxAlarms(){
  63. return n;
  64. }
  65. void setSnooze(int idx){
  66. struct _tm ct;
  67. X12RtcGetClock(&ct);
  68. if (alarm[idx].state < 3){
  69. alarm[idx].state = 2;
  70. snooze[idx].snoozeTime = ct;
  71. snooze[idx].snoozeTime.tm_min += alarm[idx].snooze;
  72. }
  73. }
  74. int daysInMonth(int m, int y) {
  75. if(m == 2 && isLeapYear(y))
  76. return 29 + (int)(m + floor(m/8)) % 2 + 2 % m + 2 * floor(1/m);
  77. return 28 + (int)(m + floor(m/8)) % 2 + 2 % m + 2 * floor(1/m);
  78. }
  79. int daysInYear(int y){
  80. if(isLeapYear(y))
  81. return 366;
  82. return 365;
  83. }
  84. int isLeapYear(int y){
  85. return (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0));
  86. }
  87. void AddSnoozeMinutes(int idx, int minutes){
  88. if (snooze[idx].snoozeTime.tm_min + minutes >= 60){ //Checks if minutes is >= 60 else minute
  89. snooze[idx].snoozeTime.tm_hour += 1;
  90. snooze[idx].snoozeTime.tm_min = ((snooze[idx].snoozeTime.tm_min + minutes) % 60);
  91. if (snooze[idx].snoozeTime.tm_hour >= 24){ //Checks if hours is >= 24
  92. snooze[idx].snoozeTime.tm_hour = 0;
  93. 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
  94. snooze[idx].snoozeTime.tm_mday += 1;
  95. } else { //If the days+1 is bigger than the amount of days in the month, day = 1 & month is + 1
  96. snooze[idx].snoozeTime.tm_mday = 1;
  97. if (snooze[idx].snoozeTime.tm_mon + 1 > 11){//If month+1 is bigger than 11 (month is 0-11) then month = 0 & year + 1
  98. snooze[idx].snoozeTime.tm_mon = 0;
  99. snooze[idx].snoozeTime.tm_year += 1;
  100. } else {
  101. snooze[idx].snoozeTime.tm_mon += 1;
  102. }
  103. }
  104. }
  105. } else {
  106. snooze[idx].snoozeTime.tm_min += minutes;
  107. }
  108. }
  109. void setState(int idx){
  110. struct _tm ct;
  111. X12RtcGetClock(&ct);
  112. //Set snooze time for snooze alarm
  113. if (alarm[idx].state == 0 && alarm[idx].state < 3){
  114. snooze[idx].snoozeTime = alarm[idx].time;
  115. AddSnoozeMinutes(idx,1);
  116. }
  117. if (compareTime(ct, alarm[idx].time) >= 1 && alarm[idx].time.tm_year != 0 && alarm[idx].state != 2 && alarm[idx].state < 3){
  118. if(alarm[idx].state != 1) {
  119. alarm[idx].state = 1;
  120. printf("\n\nAlarm gaat nu af!\n\n");
  121. bool success = connectToStream(alarm[idx].ip, alarm[idx].port, alarm[idx].url);
  122. if (success == true){
  123. play();
  124. }else {
  125. printf("ConnectToStream failed. Aborting.\n\n");
  126. }
  127. }
  128. } else if (alarm[idx].state != 2 && alarm[idx].state < 3 ){
  129. alarm[idx].state = 0;
  130. }
  131. //Check if alarm has to snooze
  132. if (compareTime(alarm[idx].time,snooze[idx].snoozeTime) >= 1 && alarm[idx].state < 3){
  133. alarm[idx].state = 2;
  134. }
  135. //Check if alarm has to snooze
  136. if (alarm[idx].state == 1 && compareTime(ct, snooze[idx].snoozeTime) >= 1 && alarm[idx].state < 3){
  137. printf("Alarm stopt nu!");
  138. alarm[idx].state = 2;
  139. snooze[idx].snoozeTime = ct;
  140. AddSnoozeMinutes(idx, alarm[idx].snooze);
  141. LcdBackLight(LCD_BACKLIGHT_OFF);
  142. killPlayerThread();
  143. }
  144. if (alarm[idx].state == 2 && compareTime(ct, snooze[idx].snoozeTime) >= 1 && alarm[idx].state < 3){
  145. AddSnoozeMinutes(idx, 1);
  146. printf("Alarm komt nu uit snooze!!");
  147. if(alarm[idx].state != 1){
  148. bool success = connectToStream(alarm[idx].ip, alarm[idx].port, alarm[idx].url);
  149. if (success == true){
  150. play();
  151. }else {
  152. printf("ConnectToStream failed. Aborting.\n\n");
  153. }
  154. alarm[idx].state = 1;
  155. }
  156. }
  157. }
  158. void setAlarm(struct _tm time, char* name, char* ip, u_short port, char* url, int snooze, int id, int idx){
  159. alarm[idx].time = time;
  160. strncpy(alarm[idx].name, name, sizeof(alarm[idx].name));
  161. strncpy(alarm[idx].ip, ip, sizeof(alarm[idx].ip));
  162. alarm[idx].port = port;
  163. strncpy(alarm[idx].url, url, sizeof(alarm[idx].url));
  164. alarm[idx].snooze = snooze;
  165. alarm[idx].id = id;
  166. alarm[idx].state = 0;
  167. }
  168. void eenmaligAlarm(struct _tm time, char* name, char* ip, u_short port, char* url, int snooze, int id, int idx){
  169. alarm[idx].time = time;
  170. strncpy(alarm[idx].name, name, sizeof(alarm[idx].name));
  171. strncpy(alarm[idx].ip, ip, sizeof(alarm[idx].ip));
  172. alarm[idx].port = port;
  173. strncpy(alarm[idx].url, url, sizeof(alarm[idx].url));
  174. alarm[idx].id = id;
  175. alarm[idx].state = 3;
  176. }
  177. //Checks if there is an alarm that has to go off
  178. void eenmaligAlarmCheck(int idx){
  179. struct _tm ct;
  180. X12RtcGetClock(&ct);
  181. //Check if alarm goes off, compares the RTC time to the alarm time
  182. if (compareTime(ct, alarm[idx].time) >= 1 && alarm[idx].time.tm_year != 0 && alarm[idx].state == 3){
  183. alarm[idx].state = 4;
  184. snooze[idx].snoozeTime = ct;
  185. AddSnoozeMinutes(idx,2);
  186. }
  187. //Delete alarm after 30 minutes, compares the RTC time to the snooze
  188. if (compareTime(ct, snooze[idx].snoozeTime) >= 1 && alarm[idx].state == 4){
  189. deleteAlarm(idx);
  190. LcdBackLight(LCD_BACKLIGHT_OFF);
  191. killPlayerThread();
  192. }
  193. }
  194. void deleteAlarm(int idx){
  195. struct _tm tm;
  196. tm.tm_year = 0;
  197. alarm[idx].time = tm;
  198. alarm[idx].port = 0;
  199. alarm[idx].snooze = 5;
  200. alarm[idx].id = -1;
  201. alarm[idx].state = -1;
  202. }
  203. void handleAlarm(int idx){
  204. if (alarm[idx].state < 3){
  205. alarm[idx].state = 0;
  206. alarm[idx].time.tm_mday += 1;
  207. } else if (alarm[idx].state == 4){
  208. deleteAlarm(idx);
  209. }
  210. killPlayerThread();
  211. }
  212. int compareTime(tm t1,tm t2){
  213. if (t1.tm_year > t2.tm_year){
  214. return 1;
  215. }
  216. if (t1.tm_year == t2.tm_year && t1.tm_mon > t2.tm_mon){
  217. return 2;
  218. }
  219. if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday > t2.tm_mday){
  220. return 3;
  221. }
  222. 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){
  223. return 4;
  224. }
  225. 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){
  226. return 5;
  227. }
  228. 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){
  229. return 6;
  230. }
  231. return 0;
  232. }