app_charge - 副本 (2).c 6.7 KB

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