app_charge.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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_charge.h"
  9. #include "app_flash.h"
  10. #include "hal_battery.h"
  11. #include "hal_ble_uart0.h"
  12. #include "ble_comm.h"
  13. #include "hal_ble_uart0.h"
  14. /************************ 函数声明 ***********************************/
  15. /************************ 变量 ***********************************/
  16. static uint8_t app_charge_state = BLE_CHARGE_PULLOUT;
  17. /********************************************************/
  18. uint8_t app_charge_Getstate(void)
  19. {
  20. return app_charge_state;
  21. }
  22. static void app_send_charge(void)
  23. {
  24. uint8_t sbuf[2]={0};
  25. uint8_t L = 0;
  26. if(mFlash.isHost) sbuf[L++] = 0;
  27. else sbuf[L++] = 1;
  28. sbuf[L++] = app_charge_state;
  29. BLE_Client_Tx_Send(0,BLE_CHARGE,sbuf,L);
  30. }
  31. void cb_BLE_Client_R_CHARGE(void* handle)
  32. {
  33. BLE_Client_Rx_t* target = handle;
  34. DEBUG_LOG("!!!!!!!!cb_BLE_Client_R_CHARGE\n");
  35. app_send_charge();
  36. BLE_Host_Tx_Send(0,BLE_CHARGE,target->pDat,target->datLen);
  37. }
  38. void cb_BLE_Host_R_CHARGE(void* handle)
  39. {
  40. BLE_Host_Rx_t* target = handle;
  41. BLE_Client_Tx_Send(0,BLE_CHARGE,target->pDat,target->datLen);
  42. DEBUG_LOG("cb_BLE_Host_R_CHARGE:%d,%d,%d\n",target->pDat[0],target->pDat[1],target->datLen);
  43. }
  44. static void app_charge_Vol_Charge_Send(void)
  45. {
  46. uint8_t buf[10]={0};
  47. uint8_t L = 0;
  48. buf[L++] = GetBatteryPersent();
  49. buf[L++] = app_charge_state;
  50. if(mFlash.isHost)BLE_Host_Tx_Send(0,BLE_CHAR_VOL,buf,L);
  51. else BLE_Client_Tx_Send(0,BLE_CHAR_VOL,buf,L);
  52. }
  53. static uint8_t OtherShoesVol = 0;
  54. static uint8_t otherShoes_charge = BLE_CHARGE_PULLOUT;
  55. static void cb_BLE_R_CHAR_VOL(void* handle)
  56. {
  57. UART0_Rx_t* target = handle;
  58. OtherShoesVol = target->pDat[0];
  59. otherShoes_charge = target->pDat[1];
  60. if(!mFlash.isHost)app_charge_Vol_Charge_Send();
  61. // DEBUG_LOG("OtherShoesVol=%d\n",OtherShoesVol);
  62. }
  63. static void cb_BLE_Host_R_CHAR_VOL(void* handle)
  64. {
  65. UART0_Rx_t* target = handle;
  66. OtherShoesVol = target->pDat[0];
  67. otherShoes_charge = target->pDat[1];
  68. // DEBUG_LOG("OtherShoesVol=%d\n",OtherShoesVol);
  69. }
  70. static void app_charge_Process(void)
  71. {
  72. static uint8_t client_connect =0;
  73. if(slave_isconnect() && !client_connect){
  74. app_send_charge();
  75. uint8_t data =0;
  76. BLE_Host_Tx_Send(0,BLE_CHARGE,&data,1);
  77. }
  78. else if(!slave_isconnect() && client_connect && (!mFlash.isHost)){
  79. OtherShoesVol = 0;
  80. otherShoes_charge =0;
  81. }
  82. if(client_connect != slave_isconnect())client_connect = slave_isconnect();
  83. uint32_t ch = nrf_gpio_pin_read(PIN_CHARGING);// DEBUG_LOG("charge state:%d\n",ch);
  84. if(!ch){ //没充电
  85. if(app_charge_state!=BLE_CHARGE_PULLOUT){ DEBUG_LOG("charge out...\n");
  86. app_charge_state = BLE_CHARGE_PULLOUT;
  87. Process_SetHoldOn(app_charge_Process,0);
  88. LED_Stop(LED_CHARGE);
  89. app_send_charge();
  90. OtherShoesVol = 0;
  91. otherShoes_charge =0;
  92. }
  93. return;
  94. }
  95. if(mFlash.isHost)app_charge_Vol_Charge_Send(); //共享充电电量和充电状态
  96. if((otherShoes_charge == BLE_CHARGE_INSERT || otherShoes_charge == BLE_CHARGE_DONE) &&
  97. GetBatteryPersent() >=100 &&
  98. OtherShoesVol >=100 ){ //左右鞋都充满电的情况下
  99. if(app_charge_state!=BLE_CHARGE_DONE){ DEBUG_LOG("charge done:%d...\n",OtherShoesVol);
  100. app_charge_state = BLE_CHARGE_DONE;
  101. Process_SetHoldOn(app_charge_Process,1);
  102. LED_Start(LED_CHARGE,COLOR_GREEN);
  103. app_send_charge();
  104. }
  105. }else{ //正在充电
  106. if(app_charge_state!=BLE_CHARGE_INSERT){ DEBUG_LOG("charge in...\n");
  107. app_charge_state = BLE_CHARGE_INSERT;
  108. Process_SetHoldOn(app_charge_Process,1);
  109. LED_Start(LED_CHARGE,COLOR_ORANGE);
  110. app_send_charge();
  111. }
  112. }
  113. }
  114. static void app_charge_monitor_battery_charge_data_process(void)
  115. {
  116. #define BATTERY_VOL_THRESHOLD_MIN 2500 //2.5V
  117. #define BATTERY_VOL_THRESHOLD_MAX 4000 //4V
  118. #define CHARGE_VOL_THRESHOLD 80 //充电前和充电期间的充电电压变化值,单位mv
  119. //监测电池和充电数据
  120. int16_t bat_vol = 0;
  121. int16_t charge_vol = 0;
  122. int16_t charge_threshold = 0;
  123. uint32_t ch = nrf_gpio_pin_read(PIN_CHARGING);
  124. static uint32_t charge_cycle = 50;
  125. static int16_t before_charge_vol = 0; //充电前的电压值
  126. static int16_t charge_vol_max = 0; //充电期间最大的电压值
  127. if(!ch)//没充电
  128. {
  129. charge_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL));
  130. before_charge_vol = before_charge_vol > charge_vol ? charge_vol : before_charge_vol;
  131. charge_vol_max = 0;
  132. charge_cycle = 50;
  133. }else //充电
  134. {
  135. /* 过筛50轮 */
  136. if(charge_cycle != 0){
  137. charge_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL));
  138. charge_vol_max = charge_vol_max < charge_vol ? charge_vol : charge_vol_max;
  139. charge_cycle--;
  140. return;
  141. }
  142. charge_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL));
  143. charge_vol_max = charge_vol_max < charge_vol ? charge_vol : charge_vol_max;
  144. bat_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_BAT_CHANNEL))*5/3;
  145. //当电池电量没满,充电(经测试,电量没满的充电电压跟电池电压有关,最小充电电压100+mv)
  146. charge_threshold = charge_vol_max - before_charge_vol; //充电前和充电期间的充电电压变化值
  147. // DEBUG_LOG("charge_threshold:%d,%d,%d,%d\n",charge_vol_max,before_charge_vol,charge_threshold,CHARGE_VOL_THRESHOLD);
  148. if(charge_threshold < CHARGE_VOL_THRESHOLD && bat_vol < BATTERY_VOL_THRESHOLD_MAX)
  149. {
  150. Except_SetExceptype(EXCEPT_DATA_CHARGE);
  151. Except_TxError(EXCEPT_DATA_CHARGE,"except_charging_chip");
  152. }
  153. else
  154. {
  155. Except_ClearExceptype(EXCEPT_DATA_CHARGE);
  156. }
  157. // //电池小于2.5V或充电电压变化小于阈值且电池电压大于4V
  158. // if(bat_vol <= BATTERY_VOL_THRESHOLD_MIN || (charge_threshold < CHARGE_VOL_THRESHOLD && bat_vol > BATTERY_VOL_THRESHOLD_MAX))
  159. // {
  160. // Except_SetExceptype(EXCEPT_DATA_BATTERY);
  161. // Except_TxError(EXCEPT_DATA_BATTERY,"except_battery");
  162. // }
  163. // else
  164. // {
  165. // Except_ClearExceptype(EXCEPT_DATA_BATTERY);
  166. // }
  167. }
  168. }
  169. void app_charge_Init(void)
  170. {
  171. nrf_gpio_cfg_input(PIN_CHARGING,NRF_GPIO_PIN_NOPULL);
  172. BLE_Client_Rx_Regist(BLE_CHAR_VOL,cb_BLE_R_CHAR_VOL);
  173. BLE_Host_Rx_Regist(BLE_CHAR_VOL,cb_BLE_Host_R_CHAR_VOL);
  174. BLE_Client_Rx_Regist(BLE_CHARGE,cb_BLE_Client_R_CHARGE);
  175. BLE_Host_Rx_Regist(BLE_CHARGE,cb_BLE_Host_R_CHARGE);
  176. Process_Start(1000,"app_charge",app_charge_Process);
  177. app_charge_Process();
  178. Process_Start(10,"app_charge_monitor_battery_charge_data_process",app_charge_monitor_battery_charge_data_process);
  179. }