alarm.c 5.4 KB

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