app_charge - 副本.c 5.9 KB

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