#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 "hal_ble_uart0.h" #include "ble_comm.h" #include "hal_ble_uart0.h" /************************ 函数声明 ***********************************/ void app_charge_Process(void); /************************ 变量 ***********************************/ static uint8_t app_charge_state = BLE_CHARGE_PULLOUT; /********************************************************/ uint8_t app_charge_Getstate(void) { return app_charge_state; } void send_charge(void) { static uint8_t sbuf[2]; 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; 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); } void cb_BLE_Client_T_CHARGE(void* handle) { DEBUG_LOG("cb_BLE_Client_T_CHARGE\n"); } static void app_charge_Vol_Charge_Send(BLE_CMD_n cmd) { uint8_t buf[32]; uint8_t L = 0; buf[L++] = GetBatteryPersent(); buf[L++] = app_charge_state; if(mFlash.isHost)BLE_Host_Tx_Send(0,cmd,buf,L); else BLE_Client_Tx_Send(0,cmd,buf,L); } static uint8_t OtherShoesVol = 0; static uint8_t otherShoes_charge =0; static void cb_BLE_R_CHAR_VOL_CHARGE(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) { uint8_t client_connect =0; static uint8_t ResponseFlag =0; app_charge_Vol_Charge_Send(BLE_CHAR_VOL); //共享充电电量和充电状态 uint32_t ch = nrf_gpio_pin_read(PIN_CHARGING); client_connect = slave_isconnect(); // DEBUG_LOG("charge state:%d\n",ch); 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); if(client_connect){ ResponseFlag = 0; send_charge(); } else ResponseFlag =1; } return; } if((otherShoes_charge == BLE_CHARGE_INSERT || otherShoes_charge == BLE_CHARGE_DONE) && GetBatteryPersent() >=100 && OtherShoesVol >=100 ){ //左右鞋都充满电的情况下 if(app_charge_state!=BLE_CHARGE_DONE){ DEBUG_LOG("charge done:%d...\n",OtherShoesVol); app_charge_state = BLE_CHARGE_DONE; Process_SetHoldOn(app_charge_Process,1); LED_Start(LED_CHARGE,COLOR_GREEN); send_charge(); } }else{ //正在充电 if(app_charge_state!=BLE_CHARGE_INSERT){ DEBUG_LOG("charge in...\n"); app_charge_state = BLE_CHARGE_INSERT; Process_SetHoldOn(app_charge_Process,1); LED_Start(LED_CHARGE,COLOR_ORANGE); if(client_connect){ send_charge(); ResponseFlag = 0; } else ResponseFlag =1; } } if(client_connect && ResponseFlag){ ResponseFlag =0; send_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; int16_t charge_vol; int16_t charge_threshold; uint32_t ch = nrf_gpio_pin_read(PIN_CHARGING); static uint32_t charge_cycle = 50; static int16_t before_charge_vol; //充电前的电压值 static int16_t charge_vol_max; //充电期间最大的电压值 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,bat:%d\n",charge_threshold,ob_monitor.charge_vol_max,ob_monitor.before_charge_vol,bat_vol); if(charge_threshold < CHARGE_VOL_THRESHOLD) { 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); if(mFlash.isHost) BLE_Host_Rx_Regist(BLE_CHAR_VOL,cb_BLE_R_CHAR_VOL_CHARGE); else BLE_Client_Rx_Regist(BLE_CHAR_VOL,cb_BLE_R_CHAR_VOL_CHARGE); BLE_Client_Rx_Regist(BLE_CHARGE,cb_BLE_Client_R_CHARGE); BLE_Host_Rx_Regist(BLE_CHARGE,cb_BLE_Host_R_CHARGE); Process_Start(1000,"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); }