u_time.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include "u_time.h"
  2. #include "u_main.h"
  3. #include "time.h"
  4. typedef struct
  5. {
  6. uint8_t week;
  7. uint8_t hour;
  8. uint8_t minute;
  9. uint8_t second;
  10. }CurrTime_t;
  11. //AlarmTime_t m_count_down = {0x00}; //倒计时结构体定义
  12. AlarmTime_t m_alarm_cfg[TIME_ALIRM_MUN_MAX] = {0x00}; //3个闹钟结构体定义
  13. CurrTime_t m_curr_time = {0x00};
  14. void time_alarm_set(uint8_t *data)
  15. {
  16. m_alarm_cfg[data[0]-1].alarm_state = data[1];
  17. m_alarm_cfg[data[0]-1].week = data[2];
  18. m_alarm_cfg[data[0]-1].hour = data[3];
  19. m_alarm_cfg[data[0]-1].minute = data[4];
  20. m_alarm_cfg[data[0]-1].second = data[5];
  21. m_alarm_cfg[data[0]-1].led_onoff = data[6];
  22. }
  23. /*
  24. void time_count_down_set(uint8_t *data)
  25. {
  26. m_count_down.second = 0;
  27. m_count_down.minute = 0;
  28. m_count_down.hour = 0;
  29. m_count_down.alarm_state = data[0];
  30. m_count_down.hour_set = data[1];
  31. m_count_down.minute_set = data[2];
  32. m_count_down.second_set = data[3];
  33. m_count_down.second = m_count_down.second + m_curr_time.second + m_count_down.second_set;
  34. if(m_count_down.second >= 60)
  35. {
  36. m_count_down.minute = 1;
  37. m_count_down.second = m_count_down.second - 60 ;
  38. }
  39. m_count_down.minute = m_count_down.minute + m_curr_time.minute + m_count_down.minute_set;
  40. if(m_count_down.minute >= 60)
  41. {
  42. m_count_down.hour = 1;
  43. m_count_down.minute = m_count_down.minute - 60 ;
  44. }
  45. m_count_down.hour = m_count_down.hour + m_curr_time.hour + m_count_down.hour_set;
  46. if(m_count_down.hour >= 24)
  47. {
  48. m_count_down.week = m_curr_time.week + 1;
  49. if(m_count_down.week > 7)
  50. m_count_down.week = 1;
  51. m_count_down.hour = m_count_down.hour - 24 ;
  52. }
  53. else
  54. {
  55. m_count_down.week = m_curr_time.week;
  56. }
  57. m_count_down.led_onoff = data[4];
  58. U_UART_PRINTF("m_count_down %d %d %d %d %d\r\n",m_count_down.week, m_count_down.hour,m_count_down.minute, m_count_down.second,m_count_down.led_onoff);
  59. }
  60. void time_count_dowm_state_query(uint8_t *count_down_state)
  61. {
  62. struct tm set_date, curr_date , tran_date;
  63. time_t set_sec = 0, curr_sec = 0, tran_sec = 0;
  64. uint8_t count_borrow = 0; //计数借位
  65. //uint8_t count_down_state[5] = {0x00};
  66. count_down_state[0] = m_count_down.alarm_state;
  67. count_down_state[4] = m_count_down.led_onoff;
  68. if (m_count_down.alarm_state == GLO_DIS)
  69. {
  70. return;
  71. }
  72. //if(m_curr_time.week == m_count_down.week && m_curr_time.hour == m_count_down.hour && m_curr_time.minute == m_count_down.minute && m_curr_time.second == m_count_down.second)
  73. U_UART_PRINTF("set_dow = %d , %d, %d;\n count_down = %d , %d, %d\n",
  74. m_count_down.hour, m_count_down.minute, m_count_down.second,
  75. m_curr_time.hour, m_curr_time.minute, m_curr_time.second);
  76. memset(&set_date, 0, sizeof(struct tm));
  77. if (m_count_down.week == m_curr_time.week) //不跨天
  78. set_date.tm_mday = 1;
  79. else
  80. set_date.tm_mday = 2;
  81. set_date.tm_hour = m_count_down.hour;
  82. set_date.tm_min = m_count_down.minute;
  83. set_date.tm_sec = m_count_down.second;
  84. set_sec = mktime(&set_date);
  85. U_UART_PRINTF("set_sec = %d \n", set_sec);
  86. memset(&curr_date, 0, sizeof(struct tm));
  87. curr_date.tm_mday = 1;
  88. curr_date.tm_hour = m_curr_time.hour;
  89. curr_date.tm_min = m_curr_time.minute;
  90. curr_date.tm_sec = m_curr_time.second;
  91. curr_sec = mktime(&curr_date);
  92. U_UART_PRINTF("curr_sec = %d \n", curr_sec);
  93. memset(&tran_date, 0, sizeof(struct tm));
  94. tran_sec = difftime(set_sec, curr_sec);//set_sec - curr_sec;
  95. tran_date = *localtime(&tran_sec);
  96. U_UART_PRINTF("tran_date = %d, %d, %d \n", tran_date.tm_hour, tran_date.tm_min, tran_date.tm_sec);
  97. count_down_state[1] = tran_date.tm_hour;
  98. count_down_state[2] = tran_date.tm_min;
  99. count_down_state[3] = tran_date.tm_sec;
  100. }
  101. */
  102. void time_app_driver_sync(uint8_t *data)
  103. {
  104. m_curr_time.hour = data[4];
  105. m_curr_time.minute = data[5];
  106. m_curr_time.second = data[6];
  107. m_curr_time.week = data[7] + 1;
  108. U_UART_PRINTF("CURR_TIME %d %d %d %d\r\n", data[4], data[5], data[6], data[7]);
  109. }
  110. void time_alarm_state_query(uint8_t *send, uint8_t *reply_len)
  111. {
  112. uint8_t index = 0, i = 0;
  113. *reply_len = 0;
  114. for(i = 0; i < TIME_ALIRM_MUN_MAX ; i++) //循环3次判断3个闹钟是否存在
  115. {
  116. if(m_alarm_cfg[i].alarm_state != ALARM_ST_NULL)
  117. {
  118. U_UART_PRINTF("1index%d\r\n",index);
  119. send[index++] = i+1;
  120. U_UART_PRINTF("2index%d\r\n",index);
  121. send[index++] = m_alarm_cfg[i].alarm_state;
  122. send[index++] = m_alarm_cfg[i].week;
  123. send[index++] = m_alarm_cfg[i].hour;
  124. send[index++] = m_alarm_cfg[i].minute;
  125. send[index++] = m_alarm_cfg[i].second;
  126. send[index++] = m_alarm_cfg[i].led_onoff;
  127. *reply_len += 7;
  128. U_UART_PRINTF("2index%d\r\n",index);
  129. U_UART_PRINTF("3len%d\r\n",*reply_len);
  130. U_UART_PRINTF("sent %d %d %d %d\r\n",send[1],send[2],send[3],send[4]);
  131. }
  132. }
  133. }
  134. void time_delete_alarm(uint8_t delete_ind)
  135. {
  136. memset(&m_alarm_cfg[delete_ind - 1].alarm_state, 0, sizeof(AlarmTime_t));
  137. m_alarm_cfg[delete_ind - 1].alarm_state = ALARM_ST_NULL;
  138. }
  139. //void time_count_down_off(uint8_t hour, uint8_t min, uint8_t sec)
  140. //{
  141. // uint8_t set_buff[5] = {0x00};
  142. //
  143. // set_buff[1] = hour;
  144. // set_buff[2] = min;
  145. // set_buff[3] = sec;
  146. // set_buff[4] = GLO_DIS; //关灯
  147. //
  148. // if (hour == 0 && min == 0 && sec == 0)
  149. // {
  150. // set_buff[0] = GLO_DIS;
  151. // }
  152. // else
  153. // {
  154. // set_buff[0] = GLO_EN;
  155. // }
  156. // time_count_down_set(set_buff);
  157. //}
  158. void alarm_time_task(void) //倒计时和闹钟1秒钟事件处理
  159. {
  160. uint8_t i = 0;
  161. m_curr_time.second ++; //当前的时间计数,秒,分,时,周
  162. if(m_curr_time.second >= 60)
  163. {
  164. m_curr_time.second = 0;
  165. m_curr_time.minute ++;
  166. if(m_curr_time.minute >= 60)
  167. {
  168. m_curr_time.minute = 0;
  169. m_curr_time.hour ++;
  170. if(m_curr_time.hour >= 24)
  171. {
  172. m_curr_time.hour = 0;
  173. m_curr_time.week ++;
  174. if(m_curr_time.week > 7)
  175. {
  176. m_curr_time.week = 1;
  177. }
  178. }
  179. }
  180. }
  181. //U_UART_PRINTF("curr_time: %d %d %d %d\r\n", m_curr_time.week, m_curr_time.hour, m_curr_time.minute, m_curr_time.second);
  182. // if(m_count_down.alarm_state) //如果手机设置倒计时标志位设置成功
  183. // {
  184. // //如果当前时间等于倒计时设置的时间
  185. // if(m_curr_time.week == m_count_down.week && m_curr_time.hour == m_count_down.hour && m_curr_time.minute == m_count_down.minute && m_curr_time.second == m_count_down.second)
  186. // {
  187. // m_count_down.alarm_state = ALARM_ST_OFF;
  188. // count_down_reach_cb(m_count_down.led_onoff);
  189. // //倒计时到
  190. // //user_send_light_state(); //倒计时时间到了以后需要发送灯的状态给app
  191. // //user_send_count_down_state();
  192. // }
  193. // U_UART_PRINTF("curr_time: %d %d %d %d\r\n", m_curr_time.week, m_curr_time.hour, m_curr_time.minute, m_curr_time.second);
  194. // U_UART_PRINTF("m_count_down %d %d %d %d %d\r\n", m_count_down.week, m_count_down.hour, m_count_down.minute, m_count_down.second, m_count_down.led_onoff);
  195. // }
  196. for(i = 0; i < TIME_ALIRM_MUN_MAX; i++)
  197. {
  198. if(m_alarm_cfg[i].alarm_state == ALARM_ST_ON) //如果手机设置闹钟的标志位设置成功
  199. {
  200. if(m_alarm_cfg[i].week & (0x1 <<(m_curr_time.week -1))) //如果闹钟的星期等于今天的星期
  201. { //如果当前时间等于闹钟设置的时间
  202. if(m_curr_time.hour == m_alarm_cfg[i].hour && m_curr_time.minute == m_alarm_cfg[i].minute && m_curr_time.second == m_alarm_cfg[i].second)
  203. {
  204. alarm_time_reach_cb(m_alarm_cfg[i].led_onoff);
  205. if(m_alarm_cfg[i].week & (0x1 << 7)) //如果星期只执行一次,不是每周都执行
  206. {
  207. m_alarm_cfg[i].alarm_state = ALARM_ST_OFF; //清楚闹钟本次执行后的状态
  208. }
  209. }
  210. }
  211. U_UART_PRINTF("curr_time: %d %d %d %d\r\n", m_curr_time.week, m_curr_time.hour, m_curr_time.minute, m_curr_time.second);
  212. U_UART_PRINTF("m_alarm_cfg %d %d %d %d %d\r\n",m_alarm_cfg[i].week, m_alarm_cfg[i].hour, m_alarm_cfg[i].minute, m_alarm_cfg[i].second, m_alarm_cfg[i].led_onoff);
  213. }
  214. }
  215. // UART_PRINTF("m_alarm_cfg %d %d %d %d\r\n",m_curr_time.week,m_curr_time.hour,m_curr_time.minute,m_curr_time.second);
  216. // UART_PRINTF("COLOR briht = %d ,R G B = %x %x %x\n",Rgb_Para.rgb_light, Rgb_Para.color_arr[0], Rgb_Para.color_arr[1], Rgb_Para.color_arr[2]);
  217. }
  218. /***************************************** adaptor *************************************************************/
  219. void light_time_init(void)
  220. {
  221. light_time_timer_start();
  222. }