123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- #include "u_time.h"
- #include "u_main.h"
- #include "time.h"
- typedef struct
- {
- uint8_t week;
- uint8_t hour;
- uint8_t minute;
- uint8_t second;
- }CurrTime_t;
- //AlarmTime_t m_count_down = {0x00}; //倒计时结构体定义
- AlarmTime_t m_alarm_cfg[TIME_ALIRM_MUN_MAX] = {0x00}; //3个闹钟结构体定义
- CurrTime_t m_curr_time = {0x00};
- void time_alarm_set(uint8_t *data)
- {
- m_alarm_cfg[data[0]-1].alarm_state = data[1];
- m_alarm_cfg[data[0]-1].week = data[2];
- m_alarm_cfg[data[0]-1].hour = data[3];
- m_alarm_cfg[data[0]-1].minute = data[4];
- m_alarm_cfg[data[0]-1].second = data[5];
- m_alarm_cfg[data[0]-1].led_onoff = data[6];
- }
- /*
- void time_count_down_set(uint8_t *data)
- {
- m_count_down.second = 0;
- m_count_down.minute = 0;
- m_count_down.hour = 0;
-
- m_count_down.alarm_state = data[0];
- m_count_down.hour_set = data[1];
- m_count_down.minute_set = data[2];
- m_count_down.second_set = data[3];
-
- m_count_down.second = m_count_down.second + m_curr_time.second + m_count_down.second_set;
- if(m_count_down.second >= 60)
- {
- m_count_down.minute = 1;
- m_count_down.second = m_count_down.second - 60 ;
- }
-
- m_count_down.minute = m_count_down.minute + m_curr_time.minute + m_count_down.minute_set;
- if(m_count_down.minute >= 60)
- {
- m_count_down.hour = 1;
- m_count_down.minute = m_count_down.minute - 60 ;
- }
-
- m_count_down.hour = m_count_down.hour + m_curr_time.hour + m_count_down.hour_set;
- if(m_count_down.hour >= 24)
- {
- m_count_down.week = m_curr_time.week + 1;
- if(m_count_down.week > 7)
- m_count_down.week = 1;
- m_count_down.hour = m_count_down.hour - 24 ;
- }
- else
- {
- m_count_down.week = m_curr_time.week;
- }
- m_count_down.led_onoff = data[4];
- 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);
- }
- void time_count_dowm_state_query(uint8_t *count_down_state)
- {
- struct tm set_date, curr_date , tran_date;
- time_t set_sec = 0, curr_sec = 0, tran_sec = 0;
-
- uint8_t count_borrow = 0; //计数借位
- //uint8_t count_down_state[5] = {0x00};
-
- count_down_state[0] = m_count_down.alarm_state;
- count_down_state[4] = m_count_down.led_onoff;
- if (m_count_down.alarm_state == GLO_DIS)
- {
- return;
- }
- //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)
- U_UART_PRINTF("set_dow = %d , %d, %d;\n count_down = %d , %d, %d\n",
- m_count_down.hour, m_count_down.minute, m_count_down.second,
- m_curr_time.hour, m_curr_time.minute, m_curr_time.second);
-
-
- memset(&set_date, 0, sizeof(struct tm));
- if (m_count_down.week == m_curr_time.week) //不跨天
- set_date.tm_mday = 1;
- else
- set_date.tm_mday = 2;
- set_date.tm_hour = m_count_down.hour;
- set_date.tm_min = m_count_down.minute;
- set_date.tm_sec = m_count_down.second;
- set_sec = mktime(&set_date);
- U_UART_PRINTF("set_sec = %d \n", set_sec);
-
- memset(&curr_date, 0, sizeof(struct tm));
- curr_date.tm_mday = 1;
- curr_date.tm_hour = m_curr_time.hour;
- curr_date.tm_min = m_curr_time.minute;
- curr_date.tm_sec = m_curr_time.second;
- curr_sec = mktime(&curr_date);
- U_UART_PRINTF("curr_sec = %d \n", curr_sec);
-
- memset(&tran_date, 0, sizeof(struct tm));
- tran_sec = difftime(set_sec, curr_sec);//set_sec - curr_sec;
-
- tran_date = *localtime(&tran_sec);
- U_UART_PRINTF("tran_date = %d, %d, %d \n", tran_date.tm_hour, tran_date.tm_min, tran_date.tm_sec);
-
- count_down_state[1] = tran_date.tm_hour;
- count_down_state[2] = tran_date.tm_min;
- count_down_state[3] = tran_date.tm_sec;
- }
- */
- void time_app_driver_sync(uint8_t *data)
- {
- m_curr_time.hour = data[4];
- m_curr_time.minute = data[5];
- m_curr_time.second = data[6];
- m_curr_time.week = data[7] + 1;
- U_UART_PRINTF("CURR_TIME %d %d %d %d\r\n", data[4], data[5], data[6], data[7]);
- }
- void time_alarm_state_query(uint8_t *send, uint8_t *reply_len)
- {
- uint8_t index = 0, i = 0;
- *reply_len = 0;
-
- for(i = 0; i < TIME_ALIRM_MUN_MAX ; i++) //循环3次判断3个闹钟是否存在
- {
- if(m_alarm_cfg[i].alarm_state != ALARM_ST_NULL)
- {
- U_UART_PRINTF("1index%d\r\n",index);
- send[index++] = i+1;
- U_UART_PRINTF("2index%d\r\n",index);
- send[index++] = m_alarm_cfg[i].alarm_state;
- send[index++] = m_alarm_cfg[i].week;
- send[index++] = m_alarm_cfg[i].hour;
- send[index++] = m_alarm_cfg[i].minute;
- send[index++] = m_alarm_cfg[i].second;
- send[index++] = m_alarm_cfg[i].led_onoff;
- *reply_len += 7;
- U_UART_PRINTF("2index%d\r\n",index);
- U_UART_PRINTF("3len%d\r\n",*reply_len);
- U_UART_PRINTF("sent %d %d %d %d\r\n",send[1],send[2],send[3],send[4]);
-
- }
- }
- }
- void time_delete_alarm(uint8_t delete_ind)
- {
- memset(&m_alarm_cfg[delete_ind - 1].alarm_state, 0, sizeof(AlarmTime_t));
- m_alarm_cfg[delete_ind - 1].alarm_state = ALARM_ST_NULL;
- }
- //void time_count_down_off(uint8_t hour, uint8_t min, uint8_t sec)
- //{
- // uint8_t set_buff[5] = {0x00};
- //
- // set_buff[1] = hour;
- // set_buff[2] = min;
- // set_buff[3] = sec;
- // set_buff[4] = GLO_DIS; //关灯
- //
- // if (hour == 0 && min == 0 && sec == 0)
- // {
- // set_buff[0] = GLO_DIS;
- // }
- // else
- // {
- // set_buff[0] = GLO_EN;
- // }
- // time_count_down_set(set_buff);
- //}
- void alarm_time_task(void) //倒计时和闹钟1秒钟事件处理
- {
- uint8_t i = 0;
- m_curr_time.second ++; //当前的时间计数,秒,分,时,周
- if(m_curr_time.second >= 60)
- {
- m_curr_time.second = 0;
- m_curr_time.minute ++;
- if(m_curr_time.minute >= 60)
- {
- m_curr_time.minute = 0;
- m_curr_time.hour ++;
- if(m_curr_time.hour >= 24)
- {
- m_curr_time.hour = 0;
- m_curr_time.week ++;
- if(m_curr_time.week > 7)
- {
- m_curr_time.week = 1;
- }
- }
- }
- }
- //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);
- // if(m_count_down.alarm_state) //如果手机设置倒计时标志位设置成功
- // {
- // //如果当前时间等于倒计时设置的时间
- // 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)
- // {
- // m_count_down.alarm_state = ALARM_ST_OFF;
- // count_down_reach_cb(m_count_down.led_onoff);
- // //倒计时到
- // //user_send_light_state(); //倒计时时间到了以后需要发送灯的状态给app
- // //user_send_count_down_state();
- // }
- // 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);
- // 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);
- // }
-
- for(i = 0; i < TIME_ALIRM_MUN_MAX; i++)
- {
- if(m_alarm_cfg[i].alarm_state == ALARM_ST_ON) //如果手机设置闹钟的标志位设置成功
- {
- if(m_alarm_cfg[i].week & (0x1 <<(m_curr_time.week -1))) //如果闹钟的星期等于今天的星期
- { //如果当前时间等于闹钟设置的时间
- 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)
- {
-
- alarm_time_reach_cb(m_alarm_cfg[i].led_onoff);
- if(m_alarm_cfg[i].week & (0x1 << 7)) //如果星期只执行一次,不是每周都执行
- {
- m_alarm_cfg[i].alarm_state = ALARM_ST_OFF; //清楚闹钟本次执行后的状态
- }
- }
- }
- 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);
- 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);
- }
- }
- // 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);
- // 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]);
- }
- /***************************************** adaptor *************************************************************/
- void light_time_init(void)
- {
- light_time_timer_start();
-
- }
|