hal_charge.c 5.8 KB

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