alarm.c 4.6 KB

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