123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- #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_flash.h"
- #include "hal_battery.h"
- #include "ble_comm.h"
- #include "app_pair_chargerpin.h"
- #include "hal_charge.h"
- static uint8_t hal_charge_state = BLE_CHARGE_PULLOUT;
- static uint8_t pair_state =0;
- static void pair_start_cb(){
- hal_charge_state = BLE_CHARGE_PULLOUT;
- pair_state =1;
- }
- static void pair_done_cb(){
- pair_state =0;
- }
- PAIR_START_REGISTER(pair_start_cb);
- PAIR_DONE_REGISTER(pair_done_cb);
- uint8_t hal_charge_Getstate(void)
- {
- return hal_charge_state;
- }
- static void hal_send_charge(void)
- {
- uint8_t sbuf[2]={0};
- uint8_t L = 0;
- if(mFlash.isHost) sbuf[L++] = 0;
- else sbuf[L++] = 1;
- sbuf[L++] = hal_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");
- hal_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);
- }
- static void hal_charge_Vol_Charge_Send(void)
- {
- uint8_t buf[10]={0};
- uint8_t L = 0;
- buf[L++] = GetBatteryPersent();
- buf[L++] = hal_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)
- {
- BLE_Client_Rx_t* target = handle;
- OtherShoesVol = target->pDat[0];
- otherShoes_charge = target->pDat[1];
- if(!mFlash.isHost)hal_charge_Vol_Charge_Send();
- DEBUG_LOG("OtherShoesVol=%d\n",OtherShoesVol);
- }
- static void cb_BLE_Host_R_CHAR_VOL(void* handle)
- {
- BLE_Host_Rx_t* target = handle;
- OtherShoesVol = target->pDat[0];
- otherShoes_charge = target->pDat[1];
- }
- short ChargeADC_value =0;
- static void hal_charge_Process(void)
- {
- static uint8_t client_connect =0;
- short ADC_value =0;
-
- if(slave_isconnect() && !client_connect){
- hal_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();
- if(ADC_OP_SUCCESS != ADC_Read(PIN_CHARGING_CHANNEL,&ADC_value))return;
-
- if(1 == pair_state)return;
-
- if(ADC_value < CHARGE_ADC){
- if(hal_charge_state!=BLE_CHARGE_PULLOUT){ DEBUG_LOG("charge out...\n");
- hal_charge_state = BLE_CHARGE_PULLOUT;
- Process_SetHoldOn(hal_charge_Process,0);
- hal_send_charge();
- OtherShoesVol = 0;
- otherShoes_charge =0;
- if(mFlash.isHost)hal_charge_Vol_Charge_Send();
- }
- return;
- }
-
- if(mFlash.isHost)hal_charge_Vol_Charge_Send();
- if((otherShoes_charge == BLE_CHARGE_INSERT || otherShoes_charge == BLE_CHARGE_DONE) &&
- GetBatteryPersent() >=100 &&
- OtherShoesVol >=100 ){
- if(hal_charge_state!=BLE_CHARGE_DONE){ DEBUG_LOG("charge done:%d...\n",OtherShoesVol);
- hal_charge_state = BLE_CHARGE_DONE;
- Process_SetHoldOn(hal_charge_Process,1);
- hal_send_charge();
- }
- }else{
- if(hal_charge_state!=BLE_CHARGE_INSERT){ DEBUG_LOG("charge in...\n");
- hal_charge_state = BLE_CHARGE_INSERT;
- Process_SetHoldOn(hal_charge_Process,1);
- hal_send_charge();
- }
- }
- }
- static void hal_charge_monitor_process(void)
- {
- #define BATTERY_VOL_THRESHOLD_MIN 2500
- #define BATTERY_VOL_THRESHOLD_MAX 4000
- #define CHARGE_VOL_THRESHOLD 80
-
-
- int16_t charge_vol = 0;
- static uint32_t charge_cycle = 50;
- static int16_t before_charge_vol = 0;
- static int16_t charge_vol_max = 0;
- int16_t adcval =0;
- if(BLE_CHARGE_PULLOUT == hal_charge_state)
- {
- if(!ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL,&adcval)) return;
- charge_vol = ADC_RESULT_IN_MILLI_VOLTS(adcval);
- before_charge_vol = before_charge_vol > charge_vol ? charge_vol : before_charge_vol;
- charge_vol_max = 0;
- charge_cycle = 50;
- }else
- {
-
- if(charge_cycle != 0){
- if(!ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL,&adcval)) return;
- charge_vol = ADC_RESULT_IN_MILLI_VOLTS(adcval);
- charge_vol_max = charge_vol_max < charge_vol ? charge_vol : charge_vol_max;
- charge_cycle--;
- return;
- }
- if(!ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL,&adcval)) return;
- charge_vol = ADC_RESULT_IN_MILLI_VOLTS(adcval);
- charge_vol_max = charge_vol_max < charge_vol ? charge_vol : charge_vol_max;
-
-
- }
- }
- void hal_charge_init(void)
- {
- 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(1000,"app_charge",hal_charge_Process);
- hal_charge_Process();
-
- Process_Start(10,"hal_charge_monitor",hal_charge_monitor_process);
- }
|