hal_charge.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "app_chargerpin_conn_detect.h"
  2. #include "usr_config.h"
  3. #include "bsp_time.h"
  4. #include "system.h"
  5. #include "hal_led.h"
  6. #include "nrf_gpio.h"
  7. #include "hal_ble_client.h"
  8. #include "hal_ble_host.h"
  9. #include "app_flash.h"
  10. #include "hal_battery.h"
  11. #include "ble_comm.h"
  12. #include "app_pair_chargerpin.h"
  13. #include "hal_charge.h"
  14. #include "fml_adc.h"
  15. /************************ 函数声明 ***********************************/
  16. /************************ 变量 ***********************************/
  17. static uint8_t hal_charge_state = BLE_CHARGE_PULLOUT;
  18. static uint8_t pair_state =0;
  19. static void pair_start_cb(){
  20. hal_charge_state = BLE_CHARGE_PULLOUT;
  21. pair_state =1;
  22. }
  23. static void pair_done_cb(){
  24. pair_state =0;
  25. }
  26. PAIR_START_REGISTER(pair_start_cb);
  27. PAIR_DONE_REGISTER(pair_done_cb);
  28. /********************************************************/
  29. uint8_t hal_charge_Getstate(void)
  30. {
  31. return hal_charge_state;
  32. }
  33. static void hal_send_charge(void)
  34. {
  35. uint8_t sbuf[2]={0};
  36. uint8_t L = 0;
  37. if(mFlash.isHost) sbuf[L++] = 0;
  38. else sbuf[L++] = 1;
  39. sbuf[L++] = hal_charge_state;
  40. BLE_Client_Tx_Send(0,BLE_CHARGE,sbuf,L);
  41. }
  42. void cb_BLE_Client_R_CHARGE(void* handle)
  43. {
  44. BLE_Client_Rx_t* target = handle;
  45. DEBUG_LOG("!!!!!!!!cb_BLE_Client_R_CHARGE\n");
  46. hal_send_charge();
  47. BLE_Host_Tx_Send(0,BLE_CHARGE,target->pDat,target->datLen);
  48. }
  49. void cb_BLE_Host_R_CHARGE(void* handle)
  50. {
  51. BLE_Host_Rx_t* target = handle;
  52. BLE_Client_Tx_Send(0,BLE_CHARGE,target->pDat,target->datLen);
  53. // DEBUG_LOG("cb_BLE_Host_R_CHARGE:%d,%d,%d\n",target->pDat[0],target->pDat[1],target->datLen);
  54. }
  55. static void hal_charge_Vol_Charge_Send(void)
  56. {
  57. uint8_t buf[10]={0};
  58. uint8_t L = 0;
  59. buf[L++] = GetBatteryPersent();
  60. buf[L++] = hal_charge_state;
  61. if(mFlash.isHost)BLE_Host_Tx_Send(0,BLE_CHAR_VOL,buf,L);
  62. else BLE_Client_Tx_Send(0,BLE_CHAR_VOL,buf,L);
  63. }
  64. static uint8_t OtherShoesVol = 0;
  65. static uint8_t otherShoes_charge = BLE_CHARGE_PULLOUT;
  66. static void cb_BLE_R_CHAR_VOL(void* handle)
  67. {
  68. BLE_Client_Rx_t* target = handle;
  69. OtherShoesVol = target->pDat[0];
  70. otherShoes_charge = target->pDat[1];
  71. if(!mFlash.isHost)hal_charge_Vol_Charge_Send();
  72. DEBUG_LOG("OtherShoesVol=%d\n",OtherShoesVol);
  73. }
  74. static void cb_BLE_Host_R_CHAR_VOL(void* handle)
  75. {
  76. BLE_Host_Rx_t* target = handle;
  77. OtherShoesVol = target->pDat[0];
  78. otherShoes_charge = target->pDat[1];
  79. // DEBUG_LOG("OtherShoesVol=%d\n",OtherShoesVol);
  80. }
  81. short ChargeADC_value =0;
  82. static void hal_charge_Process(void)
  83. {
  84. static uint8_t client_connect =0;
  85. if(slave_isconnect() && !client_connect){
  86. hal_send_charge();
  87. uint8_t data =0;
  88. BLE_Host_Tx_Send(0,BLE_CHARGE,&data,1);
  89. }
  90. else if(!slave_isconnect() && client_connect && (!mFlash.isHost)){
  91. OtherShoesVol = 0;
  92. otherShoes_charge =0;
  93. }
  94. if(client_connect != slave_isconnect())client_connect = slave_isconnect();
  95. pair_line_t CHARGE_state = app_chargepin_pairline();
  96. if(1 == pair_state)return;//配对不检测充电
  97. if(CHARGE != CHARGE_state){ //没充电
  98. if(hal_charge_state!=BLE_CHARGE_PULLOUT){ DEBUG_LOG("charge out...\n");
  99. hal_charge_state = BLE_CHARGE_PULLOUT;
  100. Process_SetHoldOn(hal_charge_Process,0);
  101. hal_send_charge();
  102. OtherShoesVol = 0;
  103. otherShoes_charge =0;
  104. if(mFlash.isHost)hal_charge_Vol_Charge_Send(); //共享充电电量和充电状态
  105. }
  106. return;
  107. }
  108. if(mFlash.isHost)hal_charge_Vol_Charge_Send(); //共享充电电量和充电状态
  109. if((otherShoes_charge == BLE_CHARGE_INSERT || otherShoes_charge == BLE_CHARGE_DONE) &&
  110. GetBatteryPersent() >=100 &&
  111. OtherShoesVol >=100 ){ //左右鞋都充满电的情况下
  112. if(hal_charge_state!=BLE_CHARGE_DONE){ DEBUG_LOG("charge done:%d...\n",OtherShoesVol);
  113. hal_charge_state = BLE_CHARGE_DONE;
  114. Process_SetHoldOn(hal_charge_Process,1);
  115. hal_send_charge();
  116. }
  117. }else{ //正在充电
  118. if(hal_charge_state!=BLE_CHARGE_INSERT){ DEBUG_LOG("charge in...\n");
  119. hal_charge_state = BLE_CHARGE_INSERT;
  120. Process_SetHoldOn(hal_charge_Process,1);
  121. hal_send_charge();
  122. }
  123. }
  124. }
  125. static void hal_charge_monitor_process(void)
  126. {
  127. #define BATTERY_VOL_THRESHOLD_MIN 2500 //2.5V
  128. #define BATTERY_VOL_THRESHOLD_MAX 4000 //4V
  129. #define CHARGE_VOL_THRESHOLD 80 //充电前和充电期间的充电电压变化值,单位mv
  130. //监测电池和充电数据
  131. int16_t charge_vol = 0;
  132. // int16_t charge_threshold = 0;
  133. static uint32_t charge_cycle = 50;
  134. static int16_t before_charge_vol = 0; //充电前的电压值
  135. static int16_t charge_vol_max = 0; //充电期间最大的电压值
  136. int16_t adcval =0;
  137. if(BLE_CHARGE_PULLOUT == hal_charge_state)//没充电
  138. {
  139. if(-1 ==fml_adc_get_value(PIN_ADC_CHARGMEASURE_CHANNEL,&adcval))return;
  140. charge_vol = ADC_RESULT_IN_MILLI_VOLTS(adcval);
  141. before_charge_vol = before_charge_vol > charge_vol ? charge_vol : before_charge_vol;
  142. charge_vol_max = 0;
  143. charge_cycle = 50;
  144. }else //充电
  145. {
  146. /* 过筛50轮 */
  147. if(charge_cycle != 0){
  148. if(-1 ==fml_adc_get_value(PIN_ADC_CHARGMEASURE_CHANNEL,&adcval))return;
  149. charge_vol = ADC_RESULT_IN_MILLI_VOLTS(adcval);
  150. charge_vol_max = charge_vol_max < charge_vol ? charge_vol : charge_vol_max;
  151. charge_cycle--;
  152. return;
  153. }
  154. if(-1 == fml_adc_get_value(PIN_ADC_CHARGMEASURE_CHANNEL,&adcval))return;
  155. charge_vol = ADC_RESULT_IN_MILLI_VOLTS(adcval);
  156. charge_vol_max = charge_vol_max < charge_vol ? charge_vol : charge_vol_max;
  157. // bat_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_BAT_CHANNEL))*5/3;
  158. //当电池电量没满,充电(经测试,电量没满的充电电压跟电池电压有关,最小充电电压100+mv)
  159. // charge_threshold = charge_vol_max - before_charge_vol; //充电前和充电期间的充电电压变化值
  160. }
  161. }
  162. void hal_charge_init(void)
  163. {
  164. BLE_Client_Rx_Regist(BLE_CHAR_VOL,cb_BLE_R_CHAR_VOL);
  165. BLE_Host_Rx_Regist(BLE_CHAR_VOL,cb_BLE_Host_R_CHAR_VOL);
  166. BLE_Client_Rx_Regist(BLE_CHARGE,cb_BLE_Client_R_CHARGE);
  167. BLE_Host_Rx_Regist(BLE_CHARGE,cb_BLE_Host_R_CHARGE);
  168. Process_Start(1000,"charge",hal_charge_Process);
  169. hal_charge_Process();
  170. Process_Start(10,"charge_monitor",hal_charge_monitor_process);
  171. }