hal_battery_NoPowerEnPin.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*********************************************************************
  2. * INCLUDES
  3. */
  4. #include "usr_config.h"
  5. #include "hal_battery.h"
  6. #include "cli.h"
  7. #include "hal_charge.h"
  8. #define PRINTBLE 0
  9. #define IIDUAN 1
  10. #include "ble_comm.h"
  11. #include "exception.h"
  12. #include "ringframe.h"
  13. RINGFRAME_DEF(battlog, ringframe_size_4096);
  14. char logbuftemp[50];
  15. int logbuftemp_len = 0;
  16. #define log(...) {logbuftemp_len = sprintf(logbuftemp,__VA_ARGS__); while(ringframe_in(&battlog,logbuftemp,logbuftemp_len)!=0){ringframe_throw(&battlog);}}
  17. #include "hal_ble_client.h"
  18. char print_log = 0;
  19. void cb_BLE_Client_ERR(void* handle)
  20. {
  21. // BLE_Client_Rx_t* target = handle;
  22. DEBUG_LOG("cb_BLE_Client_ERR:%d,%d\n", 1, 1);
  23. print_log = 1;
  24. }
  25. uint8_t ADC_GetValue(uint32_t channel,int16_t *adcval)
  26. {
  27. int16_t temp = 0;
  28. uint32_t errcode =0;
  29. errcode = ADC_Read(channel, &temp);
  30. if(ADC_OP_SUCCESS == errcode){
  31. *adcval = temp;
  32. return 1;
  33. }
  34. else return 0;
  35. }
  36. //typedef struct
  37. //{
  38. // void * p_buffer;
  39. // short size;
  40. // short index;
  41. // float sum;
  42. //} average_filter_t;
  43. //#define AVERAGE_FILTER_DEF( _name, _size ) \
  44. // float _name##_average_filter_buffer[(_size)]; \
  45. // average_filter_t _name= \
  46. // { \
  47. // .index = 0, \
  48. // .sum = 0.0f, \
  49. // .p_buffer = _name##_average_filter_buffer, \
  50. // .size = (_size), \
  51. // }
  52. //float average_filter_init(average_filter_t * p_avgf)
  53. //{
  54. // short i=0;
  55. // for(i=1;i<p_avgf->size;i++)
  56. // {
  57. // p_avgf->p_buffer[i]=0.0f;
  58. // }
  59. // p_avgf->index=0;
  60. // p_avgf->sum=0.0f;
  61. //}
  62. //float average_filter(average_filter_t * p_avgf,float value_in)
  63. //{
  64. // p_avgf->sum=0.0f;
  65. // p_avgf->p_buffer[p_avgf->index++]=value_in;
  66. //}
  67. battercb_t * battercb=NULL;
  68. void printbatter_cb(battercb_t *c,battercb_t *C_flash)
  69. {
  70. char bytes[256];
  71. int len =0;
  72. len=sprintf(bytes,"%f,%f,%f,%f,%f,%f,%f,%f,%d,%d,%d,%d\r\n",
  73. c->preBestResult_Voltage2power ,
  74. c->preBestResult_chargeV2P_f ,
  75. c->P_mAh,
  76. c->kg,
  77. c->P2 ,
  78. c->P1,
  79. c->Battery_capacity_mAh,
  80. c->adc_tp4056_power,
  81. c->init,
  82. c->sta ,
  83. c->chargeV2P_f_init,
  84. c->Voltage2power_init);
  85. SEGGER_RTT_Write(0,bytes, len);
  86. len=sprintf(bytes,"%f,%f,%f,%f,%f,%f,%f,%f,%d,%d,%d,%d\r\n",
  87. C_flash->preBestResult_Voltage2power ,
  88. C_flash->preBestResult_chargeV2P_f ,
  89. C_flash->P_mAh,
  90. C_flash->kg,
  91. C_flash->P2 ,
  92. C_flash->P1,
  93. C_flash->Battery_capacity_mAh,
  94. C_flash->adc_tp4056_power,
  95. C_flash->init,
  96. C_flash->sta ,
  97. C_flash->chargeV2P_f_init,
  98. C_flash->Voltage2power_init);
  99. SEGGER_RTT_Write(0,bytes, len);
  100. DEBUG_LOG("\r\n");
  101. }
  102. void cb_init(void)
  103. {
  104. battercb = Except_Get_Battery_Record_Buff();
  105. if(battercb->init != 3)
  106. {
  107. battercb->init = 3;
  108. battercb->P1=100.0f;
  109. battercb->adc_tp4056_power=0;
  110. battercb->Battery_capacity_mAh=0;
  111. battercb->kg=1;
  112. battercb->P2=0;
  113. battercb->preBestResult_chargeV2P_f=0;
  114. battercb->preBestResult_Voltage2power=0;
  115. battercb->sta=0;
  116. battercb->P_mAh=0;
  117. battercb->Voltage2power_init=1;
  118. battercb->chargeV2P_f_init =1;
  119. }
  120. }
  121. //返回5V信号,有5V的话返回1,没有的话返回0
  122. static char charge_in(void)
  123. {
  124. if ((hal_charge_Getstate() != BLE_CHARGE_PULLOUT))
  125. {
  126. return 1;
  127. }
  128. else
  129. {
  130. return 0;
  131. }
  132. }
  133. static float filter(float value, float kg, float* preBestResult)
  134. {
  135. float new_v = value;
  136. new_v = *preBestResult * (1.0f - kg) + value * kg;
  137. *preBestResult = new_v;
  138. return new_v;
  139. }
  140. #if IIDUAN == 0
  141. //鞋子ADC
  142. static const float poo1o[] = {0, 0, 0.0279893723606430, 0.174605323652602, 0.325796538285416, 0.495164949358988, 0.661918800578876, 0.829024800123971, 1.00225498989324, 1.17936073685608, 1.37258677752597, 1.56525700069634, 1.78680072433224, 2.00361106262195, 2.24466616203811, 2.46699160701705, 2.77834696254638, 3.12186809827754, 3.58442625993982, 4.15025435558636, 4.75855743544068, 5.51189718744822, 6.35834306864975, 7.38461196888009, 8.48997478633724, 9.43096936165977, 10.3817319764220, 11.4116388420216, 12.3939372566211, 13.5048186806524, 14.5904959858255, 15.5237940825920, 16.4790857938893, 17.8137595522187, 18.9982251103467, 20.3392608271850, 21.5817542329461, 22.7218253119165, 23.9444316340532, 25.2939077624602, 26.6264082603126, 27.6802415218000, 29.0022881606974, 30.1783424265851, 31.1179209268523, 32.2887764986448, 33.3732790985050, 34.2380544358441, 35.2041112278740, 36.0163848326001, 36.8624779801428, 37.6634899287154, 38.5186413495501, 39.4878256764553, 40.2471232681709, 41.2081417271725, 42.3322924899204, 43.7047997876243, 44.9058976548061, 46.5044971286874, 47.8927266715832, 49.8558978793141, 51.9022338412845, 54.2586141300707, 56.3903798469888, 58.7696803719223, 60.8764981712366, 62.2358527791606, 63.8383633243999, 65.5021323737117, 67.1556090613014, 69.0159229136298, 70.1420773342446, 71.2282683025524, 72.4548338447843, 73.6556507850819, 74.8128040906371, 75.8695501837768, 77.1323517287879, 78.6365237973046, 80.3752005495001, 82.8468947240450, 86.6163997907370, 91.2910588313494, 93.9702969410882, 95.4930183746766, 96.9114001488224, 97.8493292727541, 98.7169169431273, 99.3270162091455, 99.6869018017068, 99.9917942993789, 99.9689500363163, 99.97, 99.98, 99.98, 99.98, 99.98, 99.99, 99.99, 99.99};
  143. static float interp1(float x)
  144. {
  145. int absx = (int)x - 320;
  146. float max = poo1o[absx + 1];
  147. float min = poo1o[absx];
  148. float temp = x - (float)absx - 320.0f;
  149. return (max - min) * temp + min;
  150. }
  151. #else
  152. #define BAT_100_P 410.0f
  153. #define BAT_90_P 403.0f
  154. #define BAT_80_P 393.0f
  155. #define BAT_70_P 388.0f
  156. #define BAT_60_P 382.0f
  157. #define BAT_50_P 372.0f
  158. #define BAT_40_P 365.0f
  159. #define BAT_30_P 361.0f
  160. #define BAT_20_P 354.0f
  161. #define BAT_10_P 345.0f
  162. #define BAT_0_P 320.0f
  163. static float interp1(float x)
  164. {
  165. if(x > BAT_100_P)return 100.0f;
  166. else if((x <= BAT_100_P)&&(x > BAT_90_P)){return (x-BAT_90_P)/(BAT_100_P-BAT_90_P)+90.0f;}
  167. else if((x <= BAT_90_P)&&(x > BAT_80_P)){return (x-BAT_80_P)/(BAT_90_P-BAT_80_P)+80.0f;}
  168. else if((x <= BAT_80_P)&&(x > BAT_70_P)){return (x-BAT_70_P)/(BAT_80_P-BAT_70_P)+70.0f;}
  169. else if((x <= BAT_70_P)&&(x > BAT_60_P)){return (x-BAT_60_P)/(BAT_70_P-BAT_60_P)+60.0f;}
  170. else if((x <= BAT_60_P)&&(x > BAT_50_P)){return (x-BAT_50_P)/(BAT_60_P-BAT_50_P)+50.0f;}
  171. else if((x <= BAT_50_P)&&(x > BAT_40_P)){return (x-BAT_40_P)/(BAT_50_P-BAT_40_P)+40.0f;}
  172. else if((x <= BAT_40_P)&&(x > BAT_30_P)){return (x-BAT_30_P)/(BAT_40_P-BAT_30_P)+30.0f;}
  173. else if((x <= BAT_30_P)&&(x > BAT_20_P)){return (x-BAT_20_P)/(BAT_30_P-BAT_20_P)+20.0f;}
  174. else if((x <= BAT_20_P)&&(x > BAT_10_P)){return (x-BAT_10_P)/(BAT_20_P-BAT_10_P)+10.0f;}
  175. else if((x <= BAT_10_P)&&(x > BAT_0_P)){return (x-BAT_0_P)/(BAT_10_P-BAT_0_P)+0.0f;}
  176. else return 0.0f;
  177. }
  178. #endif
  179. //返回电压百分比
  180. static float Voltage2power(float mV)
  181. {
  182. float rev = 0;
  183. float k = 0;
  184. if (battercb->Voltage2power_init)
  185. {
  186. battercb->preBestResult_Voltage2power = mV / 10;
  187. battercb->Voltage2power_init = 0;
  188. }
  189. k = filter(mV / 10, 0.01, &battercb->preBestResult_Voltage2power);
  190. if (k < 320.0f)
  191. {
  192. rev = 0;
  193. }
  194. else if (k > 420.0f)
  195. {
  196. rev = 100;
  197. }
  198. else
  199. {
  200. rev = interp1(k);
  201. }
  202. return rev;
  203. }
  204. #if IIDUAN == 0
  205. static const float chargeV2P[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0.606184684199367, 0.938307744774148, 1.30769659743727, 1.87378869014097, 2.39210582883903, 2.92471759425620, 3.79782257541779, 4.54241638183623, 5.09886597521223, 5.98930945903392, 6.49613916536905, 7.57967916311061, 8.42757721701290, 9.26589943580526, 10.3408809248851, 11.4278317273525, 12.3098089504989, 13.8095299712012, 15.4373756551577, 16.8805115746597, 18.4871688551768, 20.0578462608782, 21.6336381358003, 22.9916066660508, 24.4652374859292, 25.6565902258290, 26.8453581267414, 27.9070922567514, 28.9713295466563, 30.0209078713242, 30.9380166385130, 32.0419258329631, 33.1827748787620, 33.8691297160915, 34.8949274023278, 35.8840553861847, 36.9638277084030, 37.8687458690322, 38.9939274175310, 40.4336758333268, 41.1114865403869, 42.6160733592240, 44.2493873543177, 45.6026105469954, 47.4664557187522, 49.0734043728910, 51.2998758679562, 52.8880367841713, 54.7587352342972, 57.0178625862682, 58.8608281071146, 60.3657347075748, 62.4281274954232, 64.3086188700345, 65.8044424890286, 68.6475622104224, 72.7352289117192, 76.9893033499930, 84.0930819869950, 92.2923770276700, 96.3286261252036, 99.7542287364423};
  206. static float interp1_chargeV2P(float x)
  207. {
  208. int absx = (int)x - 350;
  209. float max = chargeV2P[absx + 1];
  210. float min = chargeV2P[absx];
  211. float temp = x - (float)absx - 350.0f;
  212. return (max - min) * temp + min;
  213. }
  214. #else
  215. #define CHA_100_P BAT_100_P+0.5f
  216. #define CHA_90_P BAT_90_P+0.5f
  217. #define CHA_80_P BAT_80_P+0.2f
  218. #define CHA_70_P BAT_70_P+0.2f
  219. #define CHA_60_P BAT_60_P+0.2f
  220. #define CHA_50_P BAT_50_P+0.2f
  221. #define CHA_40_P BAT_40_P+0.2f
  222. #define CHA_30_P BAT_30_P+0.2f
  223. #define CHA_20_P BAT_20_P+0.2f
  224. #define CHA_10_P BAT_10_P+0.2f
  225. #define CHA_0_P BAT_0_P+0.2f
  226. static float interp1_chargeV2P(float x)
  227. {
  228. if(x > CHA_100_P)return 100.0f;
  229. else if((x <= CHA_100_P)&&(x > CHA_90_P)){return (x-CHA_90_P)/(CHA_100_P-CHA_90_P)+90.0f;}
  230. else if((x <= CHA_90_P)&&(x > CHA_80_P)){return (x-CHA_80_P)/(CHA_90_P-CHA_80_P)+80.0f;}
  231. else if((x <= CHA_80_P)&&(x > CHA_70_P)){return (x-CHA_70_P)/(CHA_80_P-CHA_70_P)+70.0f;}
  232. else if((x <= CHA_70_P)&&(x > CHA_60_P)){return (x-CHA_60_P)/(CHA_70_P-CHA_60_P)+60.0f;}
  233. else if((x <= CHA_60_P)&&(x > CHA_50_P)){return (x-CHA_50_P)/(CHA_60_P-CHA_50_P)+50.0f;}
  234. else if((x <= CHA_50_P)&&(x > CHA_40_P)){return (x-CHA_40_P)/(CHA_50_P-CHA_40_P)+40.0f;}
  235. else if((x <= CHA_40_P)&&(x > CHA_30_P)){return (x-CHA_30_P)/(CHA_40_P-CHA_30_P)+30.0f;}
  236. else if((x <= CHA_30_P)&&(x > CHA_20_P)){return (x-CHA_20_P)/(CHA_30_P-CHA_20_P)+20.0f;}
  237. else if((x <= CHA_20_P)&&(x > CHA_10_P)){return (x-CHA_10_P)/(CHA_20_P-CHA_10_P)+10.0f;}
  238. else if((x <= CHA_10_P)&&(x > CHA_0_P)){return (x-CHA_0_P)/(BAT_10_P-CHA_0_P)+0.0f;}
  239. else return 0.0f;
  240. }
  241. #endif
  242. //返回电压百分比
  243. static float chargeV2P_f(float mV)
  244. {
  245. float rev = 0;
  246. float k = 0;
  247. if (battercb->chargeV2P_f_init)
  248. {
  249. battercb->preBestResult_chargeV2P_f = mV / 10;
  250. battercb->chargeV2P_f_init = 0;
  251. }
  252. k = filter(mV / 10, 0.05, &battercb->preBestResult_chargeV2P_f);
  253. if (k < 350.0f)
  254. {
  255. rev = 0;
  256. }
  257. else if (k > 415.0f)
  258. {
  259. rev = 100;
  260. }
  261. else
  262. {
  263. rev = interp1_chargeV2P(k);
  264. }
  265. return rev;
  266. }
  267. //返回电量百分比
  268. static float Voltage2mah(float mah, float storage_capacity)
  269. {
  270. return mah / storage_capacity * 100.0f;
  271. }
  272. //返回电压剩余绝对容量
  273. static float mah2Voltage(float P, float storage_capacity)
  274. {
  275. return storage_capacity * P / 100.0f;
  276. }
  277. static void Charge(float mV, float* mAh, float interval_s)
  278. {
  279. float A = mV / 3000.0f * 1.1f;
  280. float dmAh = A * 1000.0f * interval_s / 3600.0f;
  281. *mAh = *mAh + dmAh;
  282. }
  283. static float Power_management(float mV_Battery, float mV_Charge)
  284. {
  285. float storage_capacity = 350;
  286. switch (battercb->sta)
  287. {
  288. case 0:
  289. if (mV_Charge > 20)
  290. {
  291. battercb->sta = 2; //充电过程
  292. battercb->P1 = chargeV2P_f(mV_Battery);
  293. battercb->Battery_capacity_mAh=mah2Voltage(battercb->P1, storage_capacity);
  294. }
  295. else
  296. {
  297. battercb->P1 = Voltage2power(mV_Battery);
  298. battercb->sta = 1; //放电过程
  299. }
  300. break;
  301. case 1://放电
  302. if (mV_Charge > 20)
  303. {
  304. battercb->sta = 2; //充电过程
  305. battercb->kg = 0;
  306. // //解决插上充电没接电池状态下突然接上电池时电量不衔接的问题
  307. // if((mV_Battery<4000)&&(P1>95.0f))
  308. // {
  309. // P1=chargeV2P_f(mV_Battery);
  310. // }
  311. battercb->Battery_capacity_mAh=mah2Voltage(battercb->P1, storage_capacity);
  312. }
  313. else
  314. {
  315. battercb->P2 = Voltage2power(mV_Battery);
  316. if (battercb->P1 > battercb->P2) //过滤刚拔掉充电线时候的虚高
  317. {
  318. battercb->P1 = battercb->P2;
  319. }
  320. //解决充满电后充电器不拔出来的情况显示不到100%的情况
  321. if (charge_in() && (battercb->P1 > 95.0f))
  322. {
  323. battercb->P1 = 100.0f;
  324. }
  325. }
  326. break;
  327. case 2://充电
  328. if (mV_Charge < 20)
  329. {
  330. battercb->sta = 1; //放电过程
  331. //初始化滤波器波器
  332. battercb->preBestResult_Voltage2power = mV_Battery / 10;
  333. #ifdef PIN_BATFULL
  334. if ((battercb->P1 > 99.1f)&&(charge_in())&&(nrf_gpio_pin_read(PIN_BATFULL)==0))
  335. #else
  336. if ((battercb->P1 > 99.1f)&&(charge_in()))
  337. #endif
  338. {
  339. battercb->P1 = 100.0f;
  340. }
  341. }
  342. else
  343. {
  344. //---------------------------------------------------
  345. Charge(mV_Charge, &battercb->Battery_capacity_mAh, 1);
  346. battercb->P_mAh = Voltage2mah(battercb->Battery_capacity_mAh, storage_capacity);
  347. //---------------------------------------------------
  348. battercb->P2 = chargeV2P_f(mV_Battery);
  349. // log("%f\n",P2);
  350. // SEGGER_RTT_Write(0,logbuftemp, logbuftemp_len);
  351. battercb->kg = mV_Charge / 1000.0f;
  352. if (battercb->kg > 1.0f)
  353. {
  354. battercb->kg = 1.0f;
  355. }
  356. battercb->P2 = (1.0f - battercb->kg) * battercb->P2 + battercb->kg * battercb->P_mAh;
  357. if (battercb->P1 < battercb->P2) //过滤刚插上充电线时候的虚低
  358. {
  359. battercb->P1 = battercb->P2;
  360. }
  361. if (battercb->P1 > 100)
  362. {
  363. battercb->P1 = 99.9;
  364. }
  365. }
  366. break;
  367. }
  368. return battercb->P1;
  369. }
  370. static int16_t BatadcVal =0;
  371. int16_t hal_GetBatttery_Adc(void){
  372. return ADC_RESULT_IN_MILLI_VOLTS(BatadcVal) * 5 / 3;
  373. }
  374. static void hal_battery_Process(void)
  375. {
  376. static int count=0;
  377. static int adc_midal=0;
  378. static int cprign = 0;
  379. #if PRINTBLE
  380. char buff[256];
  381. unsigned char len = 0;
  382. #endif
  383. int16_t CHARGMEASURE;
  384. int16_t volTemp;
  385. int16_t volTemp_CHARGMEASURE;
  386. if(!ADC_GetValue(PIN_ADC_BAT_CHANNEL,&BatadcVal)) return;
  387. // DEBUG_LOG("hal_GetBatttery_Adc:%d\n", BatadcVal);
  388. volTemp = ADC_RESULT_IN_MILLI_VOLTS(BatadcVal) * 5 / 3; // 电池电压转换计算
  389. if(!ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL,&CHARGMEASURE))return;
  390. volTemp_CHARGMEASURE = ADC_RESULT_IN_MILLI_VOLTS(CHARGMEASURE);// 电池电压转换计算
  391. if(count<10)
  392. {
  393. adc_midal+=volTemp;
  394. DEBUG_LOG(0,"N %d %5d battery %d mV\r\n",count,adc_midal,volTemp);
  395. count++;
  396. return ;
  397. }
  398. else if(count==10)
  399. {
  400. Process_UpdatePeroid(hal_battery_Process,1000);
  401. volTemp=adc_midal/count;
  402. DEBUG_LOG(0,"average battery %d mV %d\r\n",volTemp,count);
  403. count++;
  404. }
  405. battercb->adc_tp4056_power = Power_management((float)volTemp, (float)volTemp_CHARGMEASURE);
  406. #if PRINTBLE
  407. len = sprintf(buff, "%4d ,%4d ,%4d,%f\r\n", TIME_GetTicks(), volTemp, volTemp_CHARGMEASURE, battercb->adc_tp4056_power);
  408. send_bytes_client((unsigned char*)buff, len);
  409. #endif
  410. if (cprign % 600 == 0)
  411. {
  412. log("%4d,%4d,%4d,%2.1f,%d,%2.1f,%2.1f,%2.1f\n", TIME_GetTicks(), volTemp, volTemp_CHARGMEASURE, battercb->adc_tp4056_power, battercb->sta, battercb->P_mAh, battercb->P2, battercb->kg);
  413. SEGGER_RTT_Write(0,logbuftemp, logbuftemp_len);
  414. logbuftemp[logbuftemp_len] = 0;
  415. Except_TxError(EXCEPT_DATA_BATTERY, logbuftemp);
  416. }
  417. cprign++;
  418. // cli_process(&clirtt);
  419. // uint8_t persent = 0;
  420. // persent = (uint8_t)(adc_tp4056_power+0.5f);
  421. // DEBUG_LOG("hal_battery_Process:%d,%d\n",persent,volTemp);
  422. if(print_log==1)
  423. {
  424. log("%4d,%4d,%4d,%2.1f,%d,%2.1f,%2.1f,%2.1f\n", TIME_GetTicks(), volTemp, volTemp_CHARGMEASURE, battercb->adc_tp4056_power, battercb->sta, battercb->P_mAh, battercb->P2, battercb->kg);
  425. SEGGER_RTT_Write(0,logbuftemp, logbuftemp_len);
  426. logbuftemp[logbuftemp_len] = 0;
  427. Except_TxError(EXCEPT_DATA_BATTERY, logbuftemp);
  428. print_log=2;
  429. }
  430. if (print_log==2)
  431. {
  432. unsigned char length = 0;
  433. while (ringframe_peek(&battlog, logbuftemp, &length) == 0)
  434. {
  435. if (send_bytes_client((unsigned char*)logbuftemp, length) != 0)
  436. {
  437. return;
  438. }
  439. ringframe_throw(&battlog);
  440. SEGGER_RTT_Write(0, logbuftemp, length);
  441. }
  442. print_log = 0;
  443. }
  444. }
  445. //返回的电量范围: 0~100 表示电量百分比
  446. uint8_t GetBatteryPersent(void)
  447. {
  448. uint8_t persent = 0;
  449. persent = (uint8_t)(battercb->adc_tp4056_power + 0.5f);
  450. // if(persent>95)persent=100;
  451. // else if((persent>85)&&(persent<=95))persent=90;
  452. // else if((persent>75)&&(persent<=85))persent=80;
  453. // else if((persent>65)&&(persent<=75))persent=70;
  454. // else if((persent>55)&&(persent<=65))persent=60;
  455. // else if((persent>45)&&(persent<=55))persent=50;
  456. // else if((persent>35)&&(persent<=45))persent=40;
  457. // else if((persent>25)&&(persent<=35))persent=30;
  458. // else if((persent>15)&&(persent<=25))persent=20;
  459. // else if((persent>10)&&(persent<=15))persent=10;
  460. if(persent>10)
  461. {
  462. persent=persent/10;
  463. persent=persent*10;
  464. }
  465. if (persent > 100)
  466. {
  467. persent = 100;
  468. }
  469. else if(persent<=0)
  470. {
  471. persent = 0;
  472. }
  473. return persent;
  474. }
  475. void hal_battery_init(void)
  476. {
  477. cb_init();
  478. hal_battery_Process();
  479. #ifdef PIN_BATFULL
  480. nrf_gpio_cfg_input(PIN_BATFULL,NRF_GPIO_PIN_PULLUP);
  481. #endif
  482. BLE_Client_Rx_Regist(BLE_ERR, cb_BLE_Client_ERR);
  483. Process_Start(10, "hal_battery", hal_battery_Process);
  484. }