alarm.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. 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. stopStream();
  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. alarm[idx].state = 1;
  105. } else if (alarm[idx].state != 2){
  106. alarm[idx].state = 0;
  107. }
  108. if (compareTime(alarm[idx].time,snooze[idx].snoozeTime) >= 1){
  109. alarm[idx].state = 2;
  110. }
  111. if (alarm[idx].state == 1 && compareTime(ct, snooze[idx].snoozeTime) >= 1){
  112. alarm[idx].state = 2;
  113. snooze[idx].snoozeTime = ct;
  114. AddSnoozeMinutes(idx, alarm[idx].snooze);
  115. LcdBackLight(LCD_BACKLIGHT_OFF);
  116. }
  117. if (alarm[idx].state == 2 && compareTime(ct, snooze[idx].snoozeTime) >= 1){
  118. alarm[idx].state = 1;
  119. AddSnoozeMinutes(idx, 1);
  120. }
  121. }
  122. /*void getAlarm(struct _alarm *am){
  123. int i = 0;
  124. for (i = 0; i < n; i++){
  125. am[i] = alarm[i];
  126. }
  127. }*/
  128. void setAlarm(struct _tm time, char* name, char* ip, u_short port, char* url, int snooze, int id, int idx){
  129. alarm[idx].time = time;
  130. strncpy(alarm[idx].name, name, sizeof(alarm[idx].name));
  131. strncpy(alarm[idx].ip, ip, sizeof(alarm[idx].ip));
  132. alarm[idx].port = port;
  133. strncpy(alarm[idx].url, url, sizeof(alarm[idx].url));
  134. alarm[idx].snooze = snooze;
  135. alarm[idx].id = id;
  136. alarm[idx].state = 0;
  137. }
  138. void deleteAlarm(int idx){
  139. struct _tm tm;
  140. tm.tm_year = 0;
  141. alarm[idx].time = tm;
  142. alarm[idx].port = 0;
  143. alarm[idx].snooze = 5;
  144. alarm[idx].id = -1;
  145. alarm[idx].state = -1;
  146. }
  147. void handleAlarm(int idx){
  148. alarm[idx].state = 0;
  149. alarm[idx].time.tm_mday += 1;
  150. printf("state is %d \n",alarm[idx].state);
  151. }
  152. int compareTime(tm t1,tm t2){
  153. if (t1.tm_year > t2.tm_year){
  154. return 1;
  155. }
  156. if (t1.tm_year == t2.tm_year && t1.tm_mon > t2.tm_mon){
  157. return 2;
  158. }
  159. if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday > t2.tm_mday){
  160. return 3;
  161. }
  162. 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){
  163. return 4;
  164. }
  165. 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){
  166. return 5;
  167. }
  168. 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){
  169. return 6;
  170. }
  171. return 0;
  172. }