app.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. #include "app.h"
  2. struct
  3. {
  4. unsigned char systemtime_minute;//距离上一个整点的分钟数
  5. unsigned char xiema;//鞋码
  6. unsigned char software_ver[2];//软件版本
  7. unsigned char hardware_ver[2];//硬件版本
  8. unsigned char power;//电量
  9. unsigned char time_of_end;//续航时间
  10. unsigned char inner_temperature;//内部温度
  11. unsigned char pressure[4];//压力
  12. }pm;
  13. struct
  14. {
  15. unsigned char power;//电量
  16. unsigned char time_of_end;//续航时间
  17. unsigned char inner_temperature;//内部温度
  18. unsigned char pressure[4];//压力
  19. }pm_other;
  20. //。。。。。。。。。。。。。。。。。。。。。。。。。。。。
  21. //
  22. const char *fds_err_str(ret_code_t ret)
  23. {
  24. /* Array to map FDS return values to strings. */
  25. static char const * err_str[] =
  26. {
  27. "FDS_ERR_OPERATION_TIMEOUT",
  28. "FDS_ERR_NOT_INITIALIZED",
  29. "FDS_ERR_UNALIGNED_ADDR",
  30. "FDS_ERR_INVALID_ARG",
  31. "FDS_ERR_NULL_ARG",
  32. "FDS_ERR_NO_OPEN_RECORDS",
  33. "FDS_ERR_NO_SPACE_IN_FLASH",
  34. "FDS_ERR_NO_SPACE_IN_QUEUES",
  35. "FDS_ERR_RECORD_TOO_LARGE",
  36. "FDS_ERR_NOT_FOUND",
  37. "FDS_ERR_NO_PAGES",
  38. "FDS_ERR_USER_LIMIT_REACHED",
  39. "FDS_ERR_CRC_CHECK_FAILED",
  40. "FDS_ERR_BUSY",
  41. "FDS_ERR_INTERNAL",
  42. };
  43. return err_str[ret - NRF_ERROR_FDS_ERR_BASE];
  44. }
  45. typedef struct
  46. {
  47. uint32_t device_name;
  48. unsigned char model_number[64];//设备型号
  49. unsigned char systemtime[8];//系统时间
  50. } configuration_t;//需要写到flish中的数据
  51. /* 出工厂参数 */
  52. static const configuration_t m_dummy_cfg =
  53. {
  54. .device_name =0x123456,
  55. .model_number = "smart shose",
  56. };
  57. configuration_t cf;
  58. /* A record containing dummy configuration data. */
  59. static fds_record_t const m_dummy_record =
  60. {
  61. .file_id = CONFIG_FILE,
  62. .key = CONFIG_REC_KEY,
  63. .data.p_data = &m_dummy_cfg,
  64. /* The length of a record is always expressed in 4-byte units (words). */
  65. .data.length_words = (sizeof(m_dummy_cfg) + 3) / sizeof(uint32_t),
  66. };
  67. /* Array to map FDS events to strings. */
  68. static char const * fds_evt_str[] =
  69. {
  70. "FDS_EVT_INIT",
  71. "FDS_EVT_WRITE",
  72. "FDS_EVT_UPDATE",
  73. "FDS_EVT_DEL_RECORD",
  74. "FDS_EVT_DEL_FILE",
  75. "FDS_EVT_GC",
  76. };
  77. extern uint32_t app_uart_put(uint8_t byte);
  78. void send_uart(unsigned char *bytes,int len)
  79. {
  80. for(int i=0;i<len;i++)
  81. {
  82. app_uart_put(bytes[i]);
  83. }
  84. }
  85. //加上帧头发送到串口
  86. void send_to_uart_AABBCC(uint8_t index,uint8_t cmd,uint8_t* dat,uint8_t datLen)
  87. {
  88. // uint32_t err_code;
  89. uint8_t buf[50];
  90. uint16_t Len = datLen+5;
  91. uint16_t L=0;
  92. uint8_t i;
  93. uint8_t ver = 0;
  94. if(Len>50) return;
  95. buf[L++] = 0xAA; ver += 0xAA; //帧头
  96. buf[L++] = Len; ver += Len; //长度
  97. buf[L++] = ~Len; ver += (~Len);//长度反码
  98. buf[L++] = cmd; ver += cmd; //命令
  99. for(i=0;i<datLen;i++){ buf[L++] = dat[i]; ver += dat[i];} //数据
  100. buf[L++] = ver; //校验
  101. for(i=0;i<L;i++) app_uart_put(buf[i]); //串口输出数据
  102. }
  103. void showflishinfo(void)
  104. {
  105. ret_code_t rc;
  106. fds_stat_t stat = {0};
  107. rc = fds_stat(&stat);//设置统计数据
  108. APP_ERROR_CHECK(rc);
  109. //清风带你学蓝牙下册270页有详细讲解
  110. NRF_LOG_INFO("pages_available = %d",stat.pages_available);
  111. NRF_LOG_INFO("open_records = %d",stat.open_records);
  112. NRF_LOG_INFO("valid_records = %d",stat.valid_records);
  113. NRF_LOG_INFO("dirty_records = %d",stat.dirty_records);
  114. NRF_LOG_INFO("words_reserved = %d",stat.words_reserved);
  115. NRF_LOG_INFO("words_used = %d",stat.words_used);
  116. NRF_LOG_INFO("largest_contig = %d",stat.largest_contig);
  117. NRF_LOG_INFO("freeable_words = %d",stat.freeable_words);
  118. NRF_LOG_INFO("done.\r\n");
  119. }
  120. ///* Keep track of the progress of a delete_all operation. */
  121. //static struct
  122. //{
  123. // bool delete_next; //!< Delete next record.
  124. // bool pending; //!< Waiting for an fds FDS_EVT_DEL_RECORD event, to delete the next record.
  125. //} m_delete_all;
  126. char flashbusy=0;
  127. static void fds_evt_handler(fds_evt_t const * p_evt)
  128. {
  129. if (p_evt->result == NRF_SUCCESS)
  130. {
  131. flashbusy=0;
  132. NRF_LOG_INFO("Event: %s received (NRF_SUCCESS)",
  133. fds_evt_str[p_evt->id]);
  134. }
  135. else
  136. {
  137. NRF_LOG_INFO("Event: %s received (%s)",
  138. fds_evt_str[p_evt->id],
  139. fds_err_str(p_evt->result));
  140. }
  141. switch (p_evt->id)
  142. {
  143. case FDS_EVT_INIT:
  144. if (p_evt->result == NRF_SUCCESS)
  145. {
  146. m_fds_initialized = true;
  147. }
  148. break;
  149. case FDS_EVT_WRITE:
  150. flashbusy=0;
  151. {
  152. if (p_evt->result == NRF_SUCCESS)
  153. {
  154. NRF_LOG_INFO("Record ID:\t0x%04x", p_evt->write.record_id);
  155. NRF_LOG_INFO("File ID:\t0x%04x", p_evt->write.file_id);
  156. NRF_LOG_INFO("Record key:\t0x%04x", p_evt->write.record_key);
  157. }
  158. } break;
  159. case FDS_EVT_DEL_RECORD:
  160. flashbusy=0;
  161. {
  162. if (p_evt->result == NRF_SUCCESS)
  163. {
  164. NRF_LOG_INFO("Record ID:\t0x%04x", p_evt->del.record_id);
  165. NRF_LOG_INFO("File ID:\t0x%04x", p_evt->del.file_id);
  166. NRF_LOG_INFO("Record key:\t0x%04x", p_evt->del.record_key);
  167. }
  168. // m_delete_all.pending = false;
  169. } break;
  170. default:
  171. break;
  172. }
  173. }
  174. bool record_delete_next(void)
  175. {
  176. ret_code_t rc;
  177. fds_find_token_t tok = {0};
  178. fds_record_desc_t desc = {0};
  179. rc = fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &desc, &tok);//查找配置文件记录
  180. if (rc == NRF_SUCCESS)
  181. {
  182. ret_code_t rc = fds_record_delete(&desc);
  183. if (rc != NRF_SUCCESS)
  184. {
  185. return false;
  186. }
  187. return true;
  188. }
  189. else
  190. {
  191. /* No records left to delete. */
  192. return false;
  193. }
  194. }
  195. //每个Record最大保存1019个words 即4076byte
  196. //FDS_VIRTUAL_PAGES 宏中记录了整个FDS的空间 = (FDS_VIRTUAL_PAGES - 1)*4076 byte
  197. void config_file_init(void)
  198. {
  199. ret_code_t rc;
  200. // uint32_t *data;
  201. (void) fds_register(fds_evt_handler);//FDS注册
  202. rc = fds_init();//fds初始化
  203. APP_ERROR_CHECK(rc);
  204. while (!m_fds_initialized)//等待初始化完成
  205. {
  206. sd_app_evt_wait();//等待过程中待机
  207. }
  208. fds_stat_t stat = {0};
  209. rc = fds_stat(&stat);//设置统计数据
  210. APP_ERROR_CHECK(rc);
  211. //清风带你学蓝牙下册270页有详细讲解
  212. // NRF_LOG_INFO("\r\n");
  213. // NRF_LOG_INFO("fds info");
  214. // NRF_LOG_INFO("pages_available = %d",stat.pages_available);
  215. // NRF_LOG_INFO("open_records = %d",stat.open_records);
  216. // NRF_LOG_INFO("valid_records = %d",stat.valid_records);
  217. // NRF_LOG_INFO("dirty_records = %d",stat.dirty_records);
  218. // NRF_LOG_INFO("words_reserved = %d",stat.words_reserved);
  219. // NRF_LOG_INFO("words_used = %d",stat.words_used);
  220. // NRF_LOG_INFO("largest_contig = %d",stat.largest_contig);
  221. // NRF_LOG_INFO("freeable_words = %d",stat.freeable_words);
  222. // NRF_LOG_INFO("done.\r\n");
  223. fds_record_desc_t desc = {0};//用来操作记录的描述符结构清零
  224. fds_find_token_t tok = {0};//保存秘钥的令牌清零
  225. rc = fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &desc, &tok);//对应KEY记录查找数据
  226. if (rc == NRF_SUCCESS)//如果查找成功
  227. {
  228. /* A config file is in flash. Let's update it. */
  229. fds_flash_record_t config = {0};//把配置清零
  230. /* Open the record and read its contents. */
  231. rc = fds_record_open(&desc, &config);//打开记录读取数据
  232. APP_ERROR_CHECK(rc);
  233. // NRF_LOG_INFO("Read DataLen = %d,config file len = %d",config.p_header->length_words,(sizeof(configuration_t)+3)/4);
  234. if(config.p_header->length_words != (sizeof(configuration_t)+3)/4)//判断一下读出来的参数结构体长度是否正确
  235. {
  236. /* Close the record when done reading. */
  237. rc = fds_record_close(&desc);//关闭记录
  238. APP_ERROR_CHECK(rc);
  239. // NRF_LOG_INFO("Update config file...");
  240. //参数结构体不正确
  241. record_delete_next();//把所有记录清零
  242. rc = fds_record_write(&desc, &m_dummy_record);//重新写记录
  243. APP_ERROR_CHECK(rc);
  244. // NRF_LOG_INFO("Update config file..done.");
  245. memcpy(&cf, &m_dummy_cfg,sizeof(configuration_t));//以出厂设置作为当前运行参数
  246. }
  247. else//读出来的长度一样,关闭记录
  248. {
  249. memcpy(&cf, config.p_data,sizeof(configuration_t));
  250. rc = fds_record_close(&desc);//关闭记录
  251. APP_ERROR_CHECK(rc);
  252. // NRF_LOG_INFO("Data = ");
  253. // data = (uint32_t *)config.p_data;
  254. // for (uint16_t i=0;i<config.p_header->length_words;i++)
  255. // {
  256. // NRF_LOG_INFO("0x%8x ",data[i]);//打印输出数据
  257. // }
  258. // NRF_LOG_INFO("\r\n");
  259. }
  260. }
  261. else
  262. {
  263. /* System config not found; write a new one. */
  264. NRF_LOG_INFO("Writing config file...");
  265. rc = fds_record_write(&desc, &m_dummy_record);//重新写记录
  266. APP_ERROR_CHECK(rc);
  267. }
  268. pm.software_ver[0]=1;
  269. pm.software_ver[1]=2;
  270. pm.hardware_ver[0]=1;
  271. pm.hardware_ver[1]=4;
  272. }
  273. //。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
  274. #define sportdatakey_file_id 0x2111
  275. #define sportdatakey_rec_key 0x2323
  276. #define step_number_length 24*30
  277. unsigned short step_journey_cun=0;//总共保存的数据量
  278. unsigned int step_number[step_number_length];//用于记录步数和路程,高16位是步数,低16位是路程
  279. //void write_step_journey(unsigned short step_num,unsigned short jour)
  280. //{
  281. // step_number[step_journey_cun] = (unsigned int)jour;
  282. // step_number[step_journey_cun] |= ((unsigned int)step_num)<<16;
  283. // step_journey_cun++;
  284. // if(step_journey_cun >= step_number_length)step_journey_cun=0;
  285. //}
  286. //以增量方式添加步数和路程
  287. void add_step_journey(unsigned char step_c,unsigned short jour_c)
  288. {
  289. unsigned short step = (step_number[step_journey_cun]&0xffff0000)>>16;
  290. unsigned short journey = step_number[step_journey_cun]&0x0000ffff;
  291. step += step_c;
  292. journey += jour_c;
  293. step_number[step_journey_cun] = (unsigned int)journey;
  294. step_number[step_journey_cun] |= ((unsigned int)step)<<16;
  295. NRF_LOG_INFO("add_step_journey -> step_number[step_journey_cun]=0x%8x ,step_journey_cun = %d",step_number[step_journey_cun],step_journey_cun);
  296. }
  297. int update_step_journey_work_statu=0;
  298. void update_step_journey_work(void)
  299. {
  300. ret_code_t rc;
  301. fds_record_desc_t desc = {0};//用来操作记录的描述符结构清零
  302. fds_find_token_t tok = {0};//保存秘钥的令牌清零
  303. fds_record_t sportdatakey_record={0};
  304. if(flashbusy==1)
  305. {
  306. NRF_LOG_INFO("flashbusy.....return ");
  307. return;//flish在忙,什么都不做
  308. }
  309. if(update_step_journey_work_statu==0)return;
  310. NRF_LOG_INFO("update_step_journey_work_statu= %d ",update_step_journey_work_statu);
  311. switch(update_step_journey_work_statu)
  312. {
  313. case 0://状态0,什么都不用做
  314. break;
  315. case 1://状态1,需要发送一次查询
  316. rc = fds_record_find(sportdatakey_file_id, sportdatakey_rec_key, &desc, &tok);//对应KEY记录查找数据
  317. switch(rc)
  318. {
  319. case FDS_ERR_NOT_FOUND:
  320. NRF_LOG_INFO("->fds_record_find = FDS_ERR_NOT_FOUND");
  321. update_step_journey_work_statu=2;
  322. break;
  323. case FDS_ERR_NOT_INITIALIZED:
  324. NRF_LOG_INFO("->fds_record_find = FDS_ERR_NOT_INITIALIZED");
  325. break;
  326. case FDS_ERR_NULL_ARG:
  327. NRF_LOG_INFO("->fds_record_find = FDS_ERR_NULL_ARG");
  328. break;
  329. case NRF_SUCCESS:
  330. NRF_LOG_INFO("->fds_record_find = NRF_SUCCESS");
  331. flashbusy = 1;
  332. rc = fds_record_delete(&desc);
  333. APP_ERROR_CHECK(rc);
  334. update_step_journey_work_statu=2;
  335. break;
  336. default:NRF_LOG_INFO("->fds_record_find = 0x%8x \n",rc);
  337. break;
  338. }
  339. break;
  340. case 2://状态2,写
  341. memset(&desc,0,sizeof(fds_record_desc_t));
  342. sportdatakey_record.file_id = sportdatakey_file_id;
  343. sportdatakey_record.key = sportdatakey_rec_key;
  344. sportdatakey_record.data.p_data = (void*)step_number;
  345. sportdatakey_record.data.length_words = (unsigned int)step_journey_cun;
  346. rc = fds_record_write(&desc, &sportdatakey_record);//重新写记录
  347. switch(rc)
  348. {
  349. case NRF_SUCCESS:
  350. NRF_LOG_INFO("->fds_record_write = NRF_SUCCESS");
  351. update_step_journey_work_statu=3;
  352. flashbusy = 1;
  353. break;
  354. case FDS_ERR_NOT_INITIALIZED:
  355. NRF_LOG_INFO("->fds_record_write = FDS_ERR_NOT_INITIALIZED");
  356. break;
  357. case FDS_ERR_NULL_ARG:
  358. NRF_LOG_INFO("->fds_record_write = FDS_ERR_NULL_ARG");
  359. break;
  360. case FDS_ERR_INVALID_ARG:
  361. NRF_LOG_INFO("->fds_record_write = FDS_ERR_INVALID_ARG");
  362. break;
  363. case FDS_ERR_UNALIGNED_ADDR:
  364. NRF_LOG_INFO("->fds_record_write = FDS_ERR_UNALIGNED_ADDR");
  365. update_step_journey_work_statu=3;
  366. break;
  367. case FDS_ERR_RECORD_TOO_LARGE:
  368. NRF_LOG_INFO("->fds_record_write = FDS_ERR_RECORD_TOO_LARGE");
  369. break;
  370. case FDS_ERR_NO_SPACE_IN_QUEUES:
  371. NRF_LOG_INFO("->fds_record_write = FDS_ERR_NO_SPACE_IN_QUEUES");
  372. break;
  373. case FDS_ERR_NO_SPACE_IN_FLASH:
  374. NRF_LOG_INFO("->fds_record_write = FDS_ERR_NO_SPACE_IN_FLASH");
  375. update_step_journey_work_statu = 4;
  376. break;
  377. default:NRF_LOG_INFO("->fds_record_write = 0x%8x \n",rc);
  378. break;
  379. }
  380. break;
  381. case 3://状态3,读取写完的结果并校验数据
  382. rc = fds_record_find(sportdatakey_file_id, sportdatakey_rec_key, &desc, &tok);//对应KEY记录查找数据
  383. switch(rc)
  384. {
  385. case FDS_ERR_NOT_FOUND:
  386. NRF_LOG_INFO("->fds_record_find = FDS_ERR_NOT_FOUND");
  387. break;
  388. case FDS_ERR_NOT_INITIALIZED:
  389. NRF_LOG_INFO("->fds_record_find = FDS_ERR_NOT_INITIALIZED");
  390. break;
  391. case FDS_ERR_NULL_ARG:
  392. NRF_LOG_INFO("->fds_record_find = FDS_ERR_NULL_ARG");
  393. break;
  394. case NRF_SUCCESS: NRF_LOG_INFO("->fds_record_find = NRF_SUCCESS");
  395. {
  396. fds_flash_record_t config = {0};//把配置清零
  397. rc = fds_record_open(&desc, &config);//打开记录读取数据
  398. APP_ERROR_CHECK(rc);
  399. step_journey_cun = config.p_header->length_words;
  400. // memcpy(step_number,config.p_data,step_journey_cun*4);
  401. // for(int i=0;i<step_journey_cun;i++)
  402. // {
  403. // NRF_LOG_INFO("0x%x",step_number[i]);
  404. // }
  405. rc = fds_record_close(&desc);//关闭记录
  406. APP_ERROR_CHECK(rc);
  407. update_step_journey_work_statu=0;
  408. }
  409. break;
  410. default:NRF_LOG_INFO("->fds_record_find = 0x%8x \n",rc);
  411. break;
  412. }
  413. break;
  414. case 4:////状态4,垃圾回收
  415. NRF_LOG_INFO("fds_gc");
  416. rc = fds_gc();//垃圾回收
  417. switch(rc)
  418. {
  419. case NRF_SUCCESS:
  420. NRF_LOG_INFO("->fds_gc = NRF_SUCCESS");
  421. update_step_journey_work_statu=2;
  422. flashbusy = 1;
  423. break;
  424. case FDS_ERR_NOT_INITIALIZED:
  425. NRF_LOG_INFO("->fds_gc = FDS_ERR_NOT_INITIALIZED");
  426. break;
  427. case FDS_ERR_NO_SPACE_IN_QUEUES:
  428. NRF_LOG_INFO("->fds_gc = FDS_ERR_NO_SPACE_IN_QUEUES");
  429. break;
  430. default:NRF_LOG_INFO("->fds_gc = 0x%8x \n",rc);
  431. break;
  432. }
  433. break;
  434. default :
  435. break;
  436. }
  437. }
  438. //把记录保存到中
  439. void update_step_journey(void)
  440. {
  441. update_step_journey_work_statu=1;
  442. }
  443. //把flish中的数据加载到内存
  444. void load_step_journey(void)
  445. {
  446. ret_code_t rc;
  447. fds_record_desc_t desc = {0};//用来操作记录的描述符结构清零
  448. fds_find_token_t tok = {0};//保存秘钥的令牌清零
  449. rc = fds_record_find(sportdatakey_file_id, sportdatakey_rec_key, &desc, &tok);//对应KEY记录查找数据
  450. APP_ERROR_CHECK(rc);
  451. if (rc == NRF_SUCCESS)//如果查找成功
  452. {
  453. // printf("load_step_journey rc == NRF_SUCCESS\n");
  454. /* A config file is in flash. Let's update it. */
  455. fds_flash_record_t config = {0};//把配置清零
  456. /* Open the record and read its contents. */
  457. rc = fds_record_open(&desc, &config);//打开记录读取数据
  458. APP_ERROR_CHECK(rc);
  459. step_journey_cun = config.p_header->length_words;
  460. memcpy(step_number,config.p_data,step_journey_cun*4);
  461. rc = fds_record_close(&desc);//关闭记录
  462. APP_ERROR_CHECK(rc);
  463. }
  464. else
  465. {
  466. }
  467. }
  468. void send_ble_data_A0_0(void)
  469. {
  470. uint8_t buf[100];
  471. uint8_t L=0;
  472. buf[L++] = 0;
  473. for(int i=0;i<8;i++)
  474. {
  475. buf[L++] = cf.systemtime[i];
  476. }
  477. send_to_ble_nus(DEX_NUM,0xa0,buf,L);
  478. }
  479. //发送数据到串口查询压力数据
  480. void send_uart_data_A1_1(void)
  481. {
  482. uint8_t buf[100];
  483. uint8_t L=0;
  484. buf[L++] = 0;
  485. for(int i=0;i<8;i++)
  486. {
  487. buf[L++] = cf.systemtime[i];
  488. }
  489. send_to_uart_AABBCC(DEX_NUM,0xa0,buf,L);
  490. }
  491. void send_ble_data_A1_0(void)
  492. {
  493. uint8_t buf[100];
  494. uint8_t L=0;
  495. buf[L++] = 0;
  496. for(int i=0;i<64;i++)
  497. {
  498. buf[L++] = cf.model_number[i];
  499. }
  500. buf[L++] = pm.software_ver[0];
  501. buf[L++] = pm.software_ver[1];
  502. buf[L++] = pm.hardware_ver[0];
  503. buf[L++] = pm.hardware_ver[1];
  504. send_to_ble_nus(DEX_NUM,0xa1,buf,L);
  505. }
  506. void send_ble_data_A1_1(void)
  507. {
  508. uint8_t buf[100];
  509. uint8_t L=0;
  510. buf[L++] = 1;
  511. buf[L++] = pm.power;
  512. buf[L++] = pm.time_of_end;
  513. buf[L++] = pm.inner_temperature;
  514. for(int i=0;i<4;i++)
  515. {
  516. buf[L++] = pm.pressure[i];
  517. }
  518. buf[L++] = pm_other.power;
  519. buf[L++] = pm_other.time_of_end;
  520. buf[L++] = pm_other.inner_temperature;
  521. for(int i=0;i<4;i++)
  522. {
  523. buf[L++] = pm_other.pressure[i];
  524. }
  525. send_to_ble_nus(DEX_NUM,0xa1,buf,L);
  526. }
  527. #define PAGE_E 50
  528. #define Pagesendtimes 20;//同一条命令最多发送的次数
  529. unsigned char PagesendtimesIndex=0;
  530. unsigned char pageindex=0;//标记这次要发的是第几包数据
  531. unsigned char allpagecun=0;//一共要发多少包数据
  532. void send_ble_data_A1_2(void)
  533. {
  534. uint8_t buf[255];
  535. uint8_t L=0;
  536. buf[L++] = 2;
  537. buf[L++] = pageindex;
  538. if(pageindex >= allpagecun)//表示这次发的是最后一包数据
  539. {
  540. for(int i=0;PAGE_E*(pageindex-1)+i <= step_journey_cun ;i++)
  541. {
  542. buf[L++] = step_number[PAGE_E*(pageindex-1)+i]>>24;
  543. buf[L++] = step_number[PAGE_E*(pageindex-1)+i]>>16;
  544. buf[L++] = step_number[PAGE_E*(pageindex-1)+i]>>8;
  545. buf[L++] = step_number[PAGE_E*(pageindex-1)+i]>>0;
  546. NRF_LOG_INFO("send_ble_data_A1_2 -> step_number[step_journey_cun]=0x%8x ,step_journey_cun = %d",step_number[step_journey_cun],step_journey_cun);
  547. NRF_LOG_INFO("send_ble_data_A1_2 -> for %d 0x%x",PAGE_E*(pageindex-1)+i,step_number[PAGE_E*(pageindex-1)+i]);
  548. }
  549. NRF_LOG_INFO("send_ble_data_A1_2 -> end");
  550. }
  551. else
  552. {
  553. for(int i=0;i<PAGE_E;i++)
  554. {
  555. buf[L++] = step_number[PAGE_E*(pageindex-1)+i]>>24;
  556. buf[L++] = step_number[PAGE_E*(pageindex-1)+i]>>16;
  557. buf[L++] = step_number[PAGE_E*(pageindex-1)+i]>>8;
  558. buf[L++] = step_number[PAGE_E*(pageindex-1)+i]>>0;
  559. }
  560. NRF_LOG_INFO("send_ble_data_A1_2 -> pageindex");
  561. }
  562. send_to_ble_nus(DEX_NUM,0xa1,buf,L);
  563. }
  564. //发送查询命令的反馈
  565. void send_ble_data_A1_2_0(unsigned char cun)
  566. {
  567. uint8_t buf[255];
  568. uint8_t L=0;
  569. buf[L++] = 2;
  570. buf[L++] = 0;
  571. for(int i=0;i<8;i++)
  572. {
  573. buf[L++] = cf.systemtime[i];
  574. }
  575. buf[L++] = cun;
  576. send_to_ble_nus(DEX_NUM,0xa1,buf,L);
  577. NRF_LOG_INFO("send_ble_data_A1_2_0 -> cun == %d",cun);
  578. }
  579. //处理手机发过来的命令
  580. //串口协议
  581. char anilicedata(const unsigned char *buff,unsigned char *buffout,int len)
  582. {
  583. int i=0,j=0;
  584. unsigned char crc=0;
  585. if(len<3)return 0;
  586. for(i=0;i<len;i++)
  587. {
  588. if(len-i>4)
  589. {
  590. if((buff[i]==0xaa)&&(buff[i+1]+buff[i+2]==0xff))//判断桢头和长度
  591. {
  592. if(len - i >= buff[i+1])//数据包长度符合
  593. {
  594. for(j=i;j < i+buff[i+1]-1;j++)//计算校验
  595. {
  596. crc += buff[j];
  597. }
  598. if( buff[i + buff[i+1] - 1] == crc)
  599. {
  600. for(j = 0;j < i+buff[i+1]; j++)
  601. {
  602. buffout[j] = buff[j+i];
  603. }
  604. return 1;
  605. }
  606. }
  607. }
  608. }
  609. }
  610. return 0;
  611. }
  612. unsigned char uart_command_buff[100];
  613. int uart_command_callback(const unsigned char *data,int length)
  614. {
  615. memcpy(uart_command_buff,data,length);
  616. switch(uart_command_buff[3])//处理命令号
  617. {
  618. case CMD_HEART:
  619. case 0x03:
  620. case 0x04:
  621. case CMD_MOTION:
  622. Send_bytes_to_Ble(uart_command_buff,length);
  623. NRF_LOG_INFO("uart_command_callback -> CMD_MOTION");
  624. break;
  625. case CMD_UPDATA:
  626. switch(uart_command_buff[4])
  627. {
  628. case UPDATE_NONE:
  629. break;
  630. case UPDATE_RUN://主机上报步数和路程 ,前1个字节是步数增量,路程两个字节(无符号16位,单位是厘米)
  631. {
  632. unsigned short temp=(((unsigned short)uart_command_buff[6])<<8) | ((unsigned short)uart_command_buff[7]);
  633. add_step_journey(uart_command_buff[5],temp);
  634. NRF_LOG_INFO("uart_command_callback -> UPDATE_RUN %d,%d",uart_command_buff[8],temp);
  635. }break;
  636. case UPDATE_BASEINFO:
  637. {
  638. int i=5;
  639. pm.power = uart_command_buff[i++];
  640. pm.inner_temperature = uart_command_buff[i++];
  641. pm.pressure[0]=uart_command_buff[i++];
  642. pm.pressure[1]=uart_command_buff[i++];
  643. pm.pressure[2]=uart_command_buff[i++];
  644. pm.pressure[3]=uart_command_buff[i++];
  645. pm_other.power = uart_command_buff[i++];
  646. pm_other.inner_temperature = uart_command_buff[i++];
  647. pm_other.pressure[0]=uart_command_buff[i++];
  648. pm_other.pressure[1]=uart_command_buff[i++];
  649. pm_other.pressure[2]=uart_command_buff[i++];
  650. pm_other.pressure[3]=uart_command_buff[i++];
  651. // NRF_LOG_INFO("uart_command_callback -> UPDATE_BASEINFO ");
  652. //SEGGER_RTT_printf(0,"%d%%,%d,0x%x 0x%x 0x%x 0x%x ;%d%%,%d,0x%x 0x%x 0x%x 0x%x\n",pm.power,pm.inner_temperature,pm.pressure[0],pm.pressure[1],pm.pressure[2],pm.pressure[3],pm_other.power,pm_other.inner_temperature,pm_other.pressure[0],pm_other.pressure[1],pm_other.pressure[2],pm_other.pressure[3]);
  653. }break;
  654. default:NRF_LOG_INFO("uart_command_callback -> default %d",uart_command_buff[4]);
  655. break;
  656. }
  657. break;
  658. default:
  659. break;
  660. }
  661. return uart_command_buff[3];
  662. }
  663. void send_uart_data_UPDATE_BASEINFO(void)
  664. {
  665. uint8_t buf[100];
  666. uint8_t L=0;
  667. buf[L++]=UPDATE_BASEINFO;
  668. send_to_uart_AABBCC(DEX_NUM,UPDATE_BASEINFO,buf,L);
  669. }
  670. /**********************************************************
  671. * ????:Reply_GameCmd_Host
  672. * ????:??????
  673. * ????:?
  674. * ?????:?
  675. ***********************************************************/
  676. void Reply_GameCmd_Host(void)
  677. {
  678. uint8_t buf=0x01;
  679. uint8_t length =1;
  680. send_to_ble_nus(0x00,0xA2,&buf,length);
  681. }
  682. unsigned char buffrecdata[100];
  683. int ble_phone_command_callback(const unsigned char *data,int length)
  684. {
  685. if(anilicedata(data,buffrecdata,length)==0)
  686. {
  687. return -1;
  688. }
  689. switch(buffrecdata[3])//处理命令号
  690. {
  691. case 0xA2:
  692. Reply_GameCmd_Host();
  693. break;
  694. case 0xA1://查询
  695. switch(buffrecdata[4])
  696. {
  697. case 0: //查询设备基本信息
  698. NRF_LOG_INFO("ble_phone_command_callback -> 0xA1_0");
  699. send_ble_data_A1_0();
  700. break;
  701. case 1: //查询数据
  702. NRF_LOG_INFO("ble_phone_command_callback -> 0xA1_1");
  703. send_ble_data_A1_1();//转发给手机端
  704. break;
  705. case 2: //查询步数距离
  706. NRF_LOG_INFO("ble_phone_command_callback -> 0xA1_2_%d",buffrecdata[5]);
  707. if(buffrecdata[5]==0)//这是手机发过来的查询请求
  708. {
  709. for(int i=0;i<8;i++)
  710. {
  711. cf.systemtime[i] = buffrecdata[6+i];
  712. }
  713. pm.systemtime_minute=buffrecdata[14];//距离上一个整点的分钟数
  714. NRF_LOG_INFO("ble_phone_command_callback -> pm.systemtime_minute = %d",pm.systemtime_minute);
  715. if((step_journey_cun==0)&&(step_number[step_journey_cun]==0))//没有数据的时候回一个包的数量为0
  716. {
  717. send_ble_data_A1_2_0(0);
  718. }
  719. else if((step_journey_cun==0)&&(step_number[step_journey_cun]!=0))
  720. {
  721. allpagecun=1;
  722. send_ble_data_A1_2_0(1);
  723. pageindex = 1;//设个标志让定时器发送第一包数据
  724. PagesendtimesIndex = Pagesendtimes;
  725. }
  726. else
  727. {
  728. //计算总共要发的包数
  729. if(step_journey_cun%PAGE_E>0)
  730. allpagecun = step_journey_cun/PAGE_E + 1;
  731. else allpagecun = step_journey_cun/PAGE_E;
  732. send_ble_data_A1_2_0(allpagecun); //发送反馈告诉手机一共有多少包数据要发
  733. pageindex = 1;//设个标志让定时器发送第一包数据
  734. PagesendtimesIndex = Pagesendtimes;
  735. }
  736. }
  737. else
  738. {
  739. if(buffrecdata[5] == pageindex)//判断一下是不是当前发送包的反馈数据
  740. {
  741. if(pageindex >= allpagecun)
  742. {
  743. pageindex = 0;//表示发送已经结束了,这个时候可以清理保存的数据
  744. step_journey_cun = 0;//清理保存的数据
  745. step_number[step_journey_cun]=0;
  746. }
  747. else
  748. {
  749. pageindex++;//发送下一包,发送完成后
  750. PagesendtimesIndex = Pagesendtimes;
  751. }
  752. }
  753. }
  754. break;
  755. default :
  756. break;
  757. }
  758. break;
  759. default:
  760. break;
  761. }
  762. return 0;
  763. }
  764. void send_ble_data_temp(void)
  765. {
  766. uint8_t buf[255];
  767. uint8_t L=0;
  768. buf[L++] = 2;
  769. buf[L++] = 1;
  770. // for(int i=0;i<8;i++)
  771. // {
  772. // buf[L++] = 0xff;
  773. // }
  774. // buf[L++] = 30;
  775. send_to_ble_nus(DEX_NUM,0xa1,buf,L);
  776. }
  777. unsigned short iiiiiii=500;
  778. unsigned short timecun=0;
  779. void timer_callback_Chen(void)
  780. {
  781. if(timecun%100 == 0)
  782. {
  783. update_step_journey_work();//1s
  784. }
  785. if(timecun%500 == 0)NRF_LOG_INFO("timer_callback_Chen -> step_number[step_journey_cun]=0x%8x ,step_journey_cun = %d",step_number[step_journey_cun],step_journey_cun);
  786. // send_ble_data_temp();
  787. if((pageindex > 0) && ( pageindex <= allpagecun) && ( timecun%50 == 0))
  788. {
  789. if(PagesendtimesIndex>0)
  790. {
  791. send_ble_data_A1_2();//发送数据
  792. PagesendtimesIndex--;
  793. }
  794. else
  795. {
  796. //在这里报错,发送超时错误
  797. }
  798. }
  799. if(timecun%6000==0)pm.systemtime_minute++;
  800. if(pm.systemtime_minute>=60)//到达一个小时
  801. {
  802. step_journey_cun++;
  803. step_number[step_journey_cun] = 0;//清0
  804. if(step_journey_cun >= step_number_length)step_journey_cun=0;
  805. pm.systemtime_minute=0;
  806. NRF_LOG_INFO("timer_callback_Chen -> pm.systemtime_minute>=60 , step_journey_cun = %d",step_journey_cun);
  807. }
  808. timecun++;
  809. }