u_time.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #ifdef DEBUG_DRV_APP
  109. U_UART_PRINTF("CURR_TIME %d %d %d %d\r\n", data[4], data[5], data[6], data[7]);
  110. #endif
  111. }
  112. void time_alarm_state_query(uint8_t *send, uint8_t *reply_len)
  113. {
  114. uint8_t index = 0, i = 0;
  115. *reply_len = 0;
  116. for(i = 0; i < TIME_ALIRM_MUN_MAX ; i++) //循环3次判断3个闹钟是否存在
  117. {
  118. if(m_alarm_cfg[i].alarm_state != ALARM_ST_NULL)
  119. {
  120. #ifdef DEBUG_DRV_APP
  121. U_UART_PRINTF("1index%d\r\n",index);
  122. #endif
  123. send[index++] = i+1;
  124. #ifdef DEBUG_DRV_APP
  125. U_UART_PRINTF("2index%d\r\n",index);
  126. #endif
  127. send[index++] = m_alarm_cfg[i].alarm_state;
  128. send[index++] = m_alarm_cfg[i].week;
  129. send[index++] = m_alarm_cfg[i].hour;
  130. send[index++] = m_alarm_cfg[i].minute;
  131. send[index++] = m_alarm_cfg[i].second;
  132. send[index++] = m_alarm_cfg[i].led_onoff;
  133. *reply_len += 7;
  134. #ifdef DEBUG_DRV_APP
  135. U_UART_PRINTF("2index%d\r\n",index);
  136. U_UART_PRINTF("3len%d\r\n",*reply_len);
  137. U_UART_PRINTF("sent %d %d %d %d\r\n",send[1],send[2],send[3],send[4]);
  138. #endif
  139. }
  140. }
  141. }
  142. void time_delete_alarm(uint8_t delete_ind)
  143. {
  144. memset(&m_alarm_cfg[delete_ind - 1].alarm_state, 0, sizeof(AlarmTime_t));
  145. m_alarm_cfg[delete_ind - 1].alarm_state = ALARM_ST_NULL;
  146. }
  147. //void time_count_down_off(uint8_t hour, uint8_t min, uint8_t sec)
  148. //{
  149. // uint8_t set_buff[5] = {0x00};
  150. //
  151. // set_buff[1] = hour;
  152. // set_buff[2] = min;
  153. // set_buff[3] = sec;
  154. // set_buff[4] = GLO_DIS; //关灯
  155. //
  156. // if (hour == 0 && min == 0 && sec == 0)
  157. // {
  158. // set_buff[0] = GLO_DIS;
  159. // }
  160. // else
  161. // {
  162. // set_buff[0] = GLO_EN;
  163. // }
  164. // time_count_down_set(set_buff);
  165. //}
  166. void alarm_time_task(void) //倒计时和闹钟1秒钟事件处理
  167. {
  168. uint8_t i = 0;
  169. m_curr_time.second ++; //当前的时间计数,秒,分,时,周
  170. if(m_curr_time.second >= 60)
  171. {
  172. m_curr_time.second = 0;
  173. m_curr_time.minute ++;
  174. if(m_curr_time.minute >= 60)
  175. {
  176. m_curr_time.minute = 0;
  177. m_curr_time.hour ++;
  178. if(m_curr_time.hour >= 24)
  179. {
  180. m_curr_time.hour = 0;
  181. m_curr_time.week ++;
  182. if(m_curr_time.week > 7)
  183. {
  184. m_curr_time.week = 1;
  185. }
  186. }
  187. }
  188. }
  189. //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);
  190. // if(m_count_down.alarm_state) //如果手机设置倒计时标志位设置成功
  191. // {
  192. // //如果当前时间等于倒计时设置的时间
  193. // 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)
  194. // {
  195. // m_count_down.alarm_state = ALARM_ST_OFF;
  196. // count_down_reach_cb(m_count_down.led_onoff);
  197. // //倒计时到
  198. // //user_send_light_state(); //倒计时时间到了以后需要发送灯的状态给app
  199. // //user_send_count_down_state();
  200. // }
  201. // 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);
  202. // 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);
  203. // }
  204. for(i = 0; i < TIME_ALIRM_MUN_MAX; i++)
  205. {
  206. if(m_alarm_cfg[i].alarm_state == ALARM_ST_ON) //如果手机设置闹钟的标志位设置成功
  207. {
  208. if(m_alarm_cfg[i].week & (0x1 <<(m_curr_time.week -1))) //如果闹钟的星期等于今天的星期
  209. { //如果当前时间等于闹钟设置的时间
  210. 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)
  211. {
  212. alarm_time_reach_cb(m_alarm_cfg[i].led_onoff);
  213. if(m_alarm_cfg[i].week & (0x1 << 7)) //如果星期只执行一次,不是每周都执行
  214. {
  215. m_alarm_cfg[i].alarm_state = ALARM_ST_OFF; //清楚闹钟本次执行后的状态
  216. }
  217. }
  218. }
  219. #ifdef DEBUG_DRV_APP
  220. 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);
  221. 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);
  222. #endif
  223. }
  224. }
  225. // 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);
  226. // 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]);
  227. }
  228. /***************************************** adaptor *************************************************************/
  229. void light_time_init(void)
  230. {
  231. light_time_timer_start();
  232. }