123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- #include "usr_config.h"
- #include "bsp_time.h"
- #include "system.h"
- #include "hal_led.h"
- #include "nrf_gpio.h"
- #include "hal_ble_client.h"
- #include "hal_ble_host.h"
- #include "app_charge.h"
- #include "app_flash.h"
- #include "hal_battery.h"
- #include "ble_comm.h"
- #include "hal_ble_uart0.h"
- /************************ 函数声明 ***********************************/
- /************************ 变量 ***********************************/
- static uint8_t app_charge_state = BLE_CHARGE_PULLOUT;
- /********************************************************/
- uint8_t app_charge_Getstate(void)
- {
- return app_charge_state;
- }
- static void app_send_charge(void)
- {
- uint8_t sbuf[2]={0};
- uint8_t L = 0;
- if(mFlash.isHost) sbuf[L++] = 0;
- else sbuf[L++] = 1;
- sbuf[L++] = app_charge_state;
- BLE_Client_Tx_Send(0,BLE_CHARGE,sbuf,L);
- }
- void cb_BLE_Client_R_CHARGE(void* handle)
- {
- BLE_Client_Rx_t* target = handle;
- DEBUG_LOG("!!!!!!!!cb_BLE_Client_R_CHARGE\n");
- app_send_charge();
- BLE_Host_Tx_Send(0,BLE_CHARGE,target->pDat,target->datLen);
- }
- void cb_BLE_Host_R_CHARGE(void* handle)
- {
- BLE_Host_Rx_t* target = handle;
- BLE_Client_Tx_Send(0,BLE_CHARGE,target->pDat,target->datLen);
- DEBUG_LOG("cb_BLE_Host_R_CHARGE:%d,%d,%d\n",target->pDat[0],target->pDat[1],target->datLen);
- }
- static BLE_Host_Tx_t mBLE_Host_BLE_CHAR_VOL_R = {
- .n = 15,
- .t = 200,
- .cb = 0,
- };
- static BLE_Client_Tx_t mBLE_Client_BLE_CHAR_VOL_R = {
- .n = 15,
- .t = 200,
- .cb = 0,
- };
- static void app_charge_Vol_Charge_Send(void)
- {
- uint8_t buf[10];
- uint8_t L = 0;
- buf[L++] = GetBatteryPersent();
- buf[L++] = app_charge_state;
- if(mFlash.isHost)BLE_Host_Tx_Send(0,BLE_CHAR_VOL,buf,L);
- else BLE_Client_Tx_Send(0,BLE_CHAR_VOL,buf,L);
- }
- static uint8_t OtherShoesVol = 0;
- static uint8_t otherShoes_charge = BLE_CHARGE_PULLOUT;
- static void cb_BLE_R_CHAR_VOL(void* handle)
- {
- UART0_Rx_t* target = handle;
- OtherShoesVol = target->pDat[0];
- otherShoes_charge = target->pDat[1];
- if(!mFlash.isHost)app_charge_Vol_Charge_Send();
- // DEBUG_LOG("OtherShoesVol=%d\n",OtherShoesVol);
- }
- static void cb_BLE_Host_R_CHAR_VOL(void* handle)
- {
- UART0_Rx_t* target = handle;
- OtherShoesVol = target->pDat[0];
- otherShoes_charge = target->pDat[1];
- // DEBUG_LOG("OtherShoesVol=%d\n",OtherShoesVol);
- }
- static void app_charge_Process(void)
- {
- static uint8_t client_connect =0;
- static uint8_t blink_state =0;
-
- if(slave_isconnect() && !client_connect){
- app_send_charge();
- uint8_t data =0;
- BLE_Host_Tx_Send(0,BLE_CHARGE,&data,1);
- }
- else if(!slave_isconnect() && client_connect && (!mFlash.isHost)){
- OtherShoesVol = 0;
- otherShoes_charge =0;
- }
-
- if(client_connect != slave_isconnect())client_connect = slave_isconnect();
-
- uint32_t ch = nrf_gpio_pin_read(PIN_CHARGING);// DEBUG_LOG("charge state:%d\n",ch);
- if(mFlash.isHost)app_charge_Vol_Charge_Send(); //共享充电电量和充电状态
-
- if(!ch){ //没充电
- if(app_charge_state!=BLE_CHARGE_PULLOUT){ DEBUG_LOG("charge out...\n");
- app_charge_state = BLE_CHARGE_PULLOUT;
- Process_SetHoldOn(app_charge_Process,0);
- LED_Stop(LED_CHARGE);
- app_send_charge();
- OtherShoesVol = 0;
- otherShoes_charge =0;
- blink_state =0;
- }
- return;
- }
-
- if(GetBatteryPersent() >=100 ){ //左右鞋都充满电的情况下
- if(app_charge_state!=BLE_CHARGE_DONE){ DEBUG_LOG("charge done:%d...\n",OtherShoesVol);
- app_charge_state = BLE_CHARGE_DONE;
- app_send_charge();
- Process_SetHoldOn(app_charge_Process,1);
- }
- }else{ //正在充电
- if(app_charge_state!=BLE_CHARGE_INSERT){ DEBUG_LOG("charge in...\n");
- app_charge_state = BLE_CHARGE_INSERT;
- app_send_charge();
- Process_SetHoldOn(app_charge_Process,1);
- }
- }
-
- if((otherShoes_charge == BLE_CHARGE_INSERT || otherShoes_charge == BLE_CHARGE_DONE)){
- if(OtherShoesVol >=100 ){
- if(1 != blink_state){blink_state = 1;
- LED_Start(LED_CHARGE,COLOR_GREEN);
- }
- }
- else {
- if(2 != blink_state){blink_state = 2;
- LED_Start(LED_CHARGE,COLOR_ORANGE);
- }
- }
- }
- else{
- if(0 != blink_state){blink_state = 0;
- LED_Stop(LED_CHARGE);
- }
- }
- }
- static void app_charge_monitor_battery_charge_data_process(void)
- {
- #define BATTERY_VOL_THRESHOLD_MIN 2500 //2.5V
- #define BATTERY_VOL_THRESHOLD_MAX 4000 //4V
- #define CHARGE_VOL_THRESHOLD 80 //充电前和充电期间的充电电压变化值,单位mv
-
- //监测电池和充电数据
- int16_t bat_vol = 0;
- int16_t charge_vol = 0;
- int16_t charge_threshold = 0;
- uint32_t ch = nrf_gpio_pin_read(PIN_CHARGING);
- static uint32_t charge_cycle = 50;
- static int16_t before_charge_vol = 0; //充电前的电压值
- static int16_t charge_vol_max = 0; //充电期间最大的电压值
-
- if(!ch)//没充电
- {
- charge_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL));
- before_charge_vol = before_charge_vol > charge_vol ? charge_vol : before_charge_vol;
- charge_vol_max = 0;
- charge_cycle = 50;
- }else //充电
- {
- /* 过筛50轮 */
- if(charge_cycle != 0){
- charge_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL));
- charge_vol_max = charge_vol_max < charge_vol ? charge_vol : charge_vol_max;
- charge_cycle--;
- return;
- }
- charge_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL));
- charge_vol_max = charge_vol_max < charge_vol ? charge_vol : charge_vol_max;
- bat_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_BAT_CHANNEL))*5/3;
-
- //当电池电量没满,充电(经测试,电量没满的充电电压跟电池电压有关,最小充电电压100+mv)
- charge_threshold = charge_vol_max - before_charge_vol; //充电前和充电期间的充电电压变化值
- // DEBUG_LOG("charge_threshold:%d,%d,%d,%d\n",charge_vol_max,before_charge_vol,charge_threshold,CHARGE_VOL_THRESHOLD);
- // if(charge_threshold < CHARGE_VOL_THRESHOLD && bat_vol < BATTERY_VOL_THRESHOLD_MAX)
- // {
- // Except_SetExceptype(EXCEPT_DATA_CHARGE);
- // Except_TxError(EXCEPT_DATA_CHARGE,"except_charging_chip");
- // }
- // else
- // {
- // Except_ClearExceptype(EXCEPT_DATA_CHARGE);
- // }
-
- // //电池小于2.5V或充电电压变化小于阈值且电池电压大于4V
- // if(bat_vol <= BATTERY_VOL_THRESHOLD_MIN || (charge_threshold < CHARGE_VOL_THRESHOLD && bat_vol > BATTERY_VOL_THRESHOLD_MAX))
- // {
- // Except_SetExceptype(EXCEPT_DATA_BATTERY);
- // Except_TxError(EXCEPT_DATA_BATTERY,"except_battery");
- // }
- // else
- // {
- // Except_ClearExceptype(EXCEPT_DATA_BATTERY);
- // }
- }
- }
- void app_charge_Init(void)
- {
- nrf_gpio_cfg_input(PIN_CHARGING,NRF_GPIO_PIN_NOPULL);
-
- BLE_Client_Rx_Regist(BLE_CHAR_VOL,cb_BLE_R_CHAR_VOL);
- BLE_Host_Rx_Regist(BLE_CHAR_VOL,cb_BLE_Host_R_CHAR_VOL);
-
- BLE_Client_Rx_Regist(BLE_CHARGE,cb_BLE_Client_R_CHARGE);
- BLE_Host_Rx_Regist(BLE_CHARGE,cb_BLE_Host_R_CHARGE);
- Process_Start(500,"app_charge",app_charge_Process);
- app_charge_Process();
-
- Process_Start(10,"app_charge_monitor_battery_charge_data_process",app_charge_monitor_battery_charge_data_process);
- }
|