123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887 |
- #include "app.h"
- struct
- {
- unsigned char systemtime_minute;//距离上一个整点的分钟数
- unsigned char xiema;//鞋码
- unsigned char software_ver[2];//软件版本
- unsigned char hardware_ver[2];//硬件版本
- unsigned char power;//电量
- unsigned char time_of_end;//续航时间
- unsigned char inner_temperature;//内部温度
- unsigned char pressure[4];//压力
- }pm;
- struct
- {
- unsigned char power;//电量
- unsigned char time_of_end;//续航时间
- unsigned char inner_temperature;//内部温度
- unsigned char pressure[4];//压力
- }pm_other;
- //。。。。。。。。。。。。。。。。。。。。。。。。。。。。
- //
- const char *fds_err_str(ret_code_t ret)
- {
- /* Array to map FDS return values to strings. */
- static char const * err_str[] =
- {
- "FDS_ERR_OPERATION_TIMEOUT",
- "FDS_ERR_NOT_INITIALIZED",
- "FDS_ERR_UNALIGNED_ADDR",
- "FDS_ERR_INVALID_ARG",
- "FDS_ERR_NULL_ARG",
- "FDS_ERR_NO_OPEN_RECORDS",
- "FDS_ERR_NO_SPACE_IN_FLASH",
- "FDS_ERR_NO_SPACE_IN_QUEUES",
- "FDS_ERR_RECORD_TOO_LARGE",
- "FDS_ERR_NOT_FOUND",
- "FDS_ERR_NO_PAGES",
- "FDS_ERR_USER_LIMIT_REACHED",
- "FDS_ERR_CRC_CHECK_FAILED",
- "FDS_ERR_BUSY",
- "FDS_ERR_INTERNAL",
- };
- return err_str[ret - NRF_ERROR_FDS_ERR_BASE];
- }
- typedef struct
- {
- uint32_t device_name;
- unsigned char model_number[64];//设备型号
- unsigned char systemtime[8];//系统时间
- } configuration_t;//需要写到flish中的数据
- /* 出工厂参数 */
- static const configuration_t m_dummy_cfg =
- {
- .device_name =0x123456,
- .model_number = "smart shose",
- };
- configuration_t cf;
- /* A record containing dummy configuration data. */
- static fds_record_t const m_dummy_record =
- {
- .file_id = CONFIG_FILE,
- .key = CONFIG_REC_KEY,
- .data.p_data = &m_dummy_cfg,
- /* The length of a record is always expressed in 4-byte units (words). */
- .data.length_words = (sizeof(m_dummy_cfg) + 3) / sizeof(uint32_t),
- };
- /* Array to map FDS events to strings. */
- static char const * fds_evt_str[] =
- {
- "FDS_EVT_INIT",
- "FDS_EVT_WRITE",
- "FDS_EVT_UPDATE",
- "FDS_EVT_DEL_RECORD",
- "FDS_EVT_DEL_FILE",
- "FDS_EVT_GC",
- };
- extern uint32_t app_uart_put(uint8_t byte);
- void send_uart(unsigned char *bytes,int len)
- {
- for(int i=0;i<len;i++)
- {
- app_uart_put(bytes[i]);
- }
- }
- //加上帧头发送到串口
- void send_to_uart_AABBCC(uint8_t index,uint8_t cmd,uint8_t* dat,uint8_t datLen)
- {
- // uint32_t err_code;
- uint8_t buf[50];
- uint16_t Len = datLen+5;
- uint16_t L=0;
- uint8_t i;
- uint8_t ver = 0;
- if(Len>50) return;
- buf[L++] = 0xAA; ver += 0xAA; //帧头
- buf[L++] = Len; ver += Len; //长度
- buf[L++] = ~Len; ver += (~Len);//长度反码
- buf[L++] = cmd; ver += cmd; //命令
- for(i=0;i<datLen;i++){ buf[L++] = dat[i]; ver += dat[i];} //数据
- buf[L++] = ver; //校验
-
- for(i=0;i<L;i++) app_uart_put(buf[i]); //串口输出数据
- }
- void showflishinfo(void)
- {
- ret_code_t rc;
- fds_stat_t stat = {0};
- rc = fds_stat(&stat);//设置统计数据
- APP_ERROR_CHECK(rc);
-
- //清风带你学蓝牙下册270页有详细讲解
- NRF_LOG_INFO("pages_available = %d",stat.pages_available);
- NRF_LOG_INFO("open_records = %d",stat.open_records);
- NRF_LOG_INFO("valid_records = %d",stat.valid_records);
- NRF_LOG_INFO("dirty_records = %d",stat.dirty_records);
- NRF_LOG_INFO("words_reserved = %d",stat.words_reserved);
- NRF_LOG_INFO("words_used = %d",stat.words_used);
- NRF_LOG_INFO("largest_contig = %d",stat.largest_contig);
- NRF_LOG_INFO("freeable_words = %d",stat.freeable_words);
- NRF_LOG_INFO("done.\r\n");
- }
- ///* Keep track of the progress of a delete_all operation. */
- //static struct
- //{
- // bool delete_next; //!< Delete next record.
- // bool pending; //!< Waiting for an fds FDS_EVT_DEL_RECORD event, to delete the next record.
- //} m_delete_all;
- char flashbusy=0;
- static void fds_evt_handler(fds_evt_t const * p_evt)
- {
- if (p_evt->result == NRF_SUCCESS)
- {
- flashbusy=0;
- NRF_LOG_INFO("Event: %s received (NRF_SUCCESS)",
- fds_evt_str[p_evt->id]);
- }
- else
- {
- NRF_LOG_INFO("Event: %s received (%s)",
- fds_evt_str[p_evt->id],
- fds_err_str(p_evt->result));
- }
- switch (p_evt->id)
- {
- case FDS_EVT_INIT:
- if (p_evt->result == NRF_SUCCESS)
- {
- m_fds_initialized = true;
- }
- break;
- case FDS_EVT_WRITE:
- flashbusy=0;
- {
- if (p_evt->result == NRF_SUCCESS)
- {
- NRF_LOG_INFO("Record ID:\t0x%04x", p_evt->write.record_id);
- NRF_LOG_INFO("File ID:\t0x%04x", p_evt->write.file_id);
- NRF_LOG_INFO("Record key:\t0x%04x", p_evt->write.record_key);
- }
- } break;
- case FDS_EVT_DEL_RECORD:
- flashbusy=0;
- {
- if (p_evt->result == NRF_SUCCESS)
- {
- NRF_LOG_INFO("Record ID:\t0x%04x", p_evt->del.record_id);
- NRF_LOG_INFO("File ID:\t0x%04x", p_evt->del.file_id);
- NRF_LOG_INFO("Record key:\t0x%04x", p_evt->del.record_key);
- }
- // m_delete_all.pending = false;
- } break;
- default:
- break;
- }
- }
- bool record_delete_next(void)
- {
- ret_code_t rc;
- fds_find_token_t tok = {0};
- fds_record_desc_t desc = {0};
- rc = fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &desc, &tok);//查找配置文件记录
- if (rc == NRF_SUCCESS)
- {
- ret_code_t rc = fds_record_delete(&desc);
- if (rc != NRF_SUCCESS)
- {
- return false;
- }
- return true;
- }
- else
- {
- /* No records left to delete. */
- return false;
- }
- }
- //每个Record最大保存1019个words 即4076byte
- //FDS_VIRTUAL_PAGES 宏中记录了整个FDS的空间 = (FDS_VIRTUAL_PAGES - 1)*4076 byte
- void config_file_init(void)
- {
- ret_code_t rc;
- // uint32_t *data;
- (void) fds_register(fds_evt_handler);//FDS注册
- rc = fds_init();//fds初始化
-
- APP_ERROR_CHECK(rc);
-
- while (!m_fds_initialized)//等待初始化完成
- {
- sd_app_evt_wait();//等待过程中待机
- }
-
- fds_stat_t stat = {0};
- rc = fds_stat(&stat);//设置统计数据
- APP_ERROR_CHECK(rc);
-
- //清风带你学蓝牙下册270页有详细讲解
- // NRF_LOG_INFO("\r\n");
- // NRF_LOG_INFO("fds info");
- // NRF_LOG_INFO("pages_available = %d",stat.pages_available);
- // NRF_LOG_INFO("open_records = %d",stat.open_records);
- // NRF_LOG_INFO("valid_records = %d",stat.valid_records);
- // NRF_LOG_INFO("dirty_records = %d",stat.dirty_records);
- // NRF_LOG_INFO("words_reserved = %d",stat.words_reserved);
- // NRF_LOG_INFO("words_used = %d",stat.words_used);
- // NRF_LOG_INFO("largest_contig = %d",stat.largest_contig);
- // NRF_LOG_INFO("freeable_words = %d",stat.freeable_words);
- // NRF_LOG_INFO("done.\r\n");
-
- fds_record_desc_t desc = {0};//用来操作记录的描述符结构清零
- fds_find_token_t tok = {0};//保存秘钥的令牌清零
-
- rc = fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &desc, &tok);//对应KEY记录查找数据
-
-
- if (rc == NRF_SUCCESS)//如果查找成功
- {
- /* A config file is in flash. Let's update it. */
- fds_flash_record_t config = {0};//把配置清零
- /* Open the record and read its contents. */
- rc = fds_record_open(&desc, &config);//打开记录读取数据
- APP_ERROR_CHECK(rc);
-
- // NRF_LOG_INFO("Read DataLen = %d,config file len = %d",config.p_header->length_words,(sizeof(configuration_t)+3)/4);
-
- if(config.p_header->length_words != (sizeof(configuration_t)+3)/4)//判断一下读出来的参数结构体长度是否正确
- {
- /* Close the record when done reading. */
- rc = fds_record_close(&desc);//关闭记录
- APP_ERROR_CHECK(rc);
- // NRF_LOG_INFO("Update config file...");
- //参数结构体不正确
- record_delete_next();//把所有记录清零
- rc = fds_record_write(&desc, &m_dummy_record);//重新写记录
- APP_ERROR_CHECK(rc);
-
- // NRF_LOG_INFO("Update config file..done.");
- memcpy(&cf, &m_dummy_cfg,sizeof(configuration_t));//以出厂设置作为当前运行参数
- }
- else//读出来的长度一样,关闭记录
- {
- memcpy(&cf, config.p_data,sizeof(configuration_t));
- rc = fds_record_close(&desc);//关闭记录
- APP_ERROR_CHECK(rc);
-
- // NRF_LOG_INFO("Data = ");
- // data = (uint32_t *)config.p_data;
- // for (uint16_t i=0;i<config.p_header->length_words;i++)
- // {
- // NRF_LOG_INFO("0x%8x ",data[i]);//打印输出数据
- // }
- // NRF_LOG_INFO("\r\n");
- }
- }
- else
- {
- /* System config not found; write a new one. */
- NRF_LOG_INFO("Writing config file...");
- rc = fds_record_write(&desc, &m_dummy_record);//重新写记录
- APP_ERROR_CHECK(rc);
- }
-
- pm.software_ver[0]=1;
- pm.software_ver[1]=2;
- pm.hardware_ver[0]=1;
- pm.hardware_ver[1]=4;
-
-
- }
- //。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
- #define sportdatakey_file_id 0x2111
- #define sportdatakey_rec_key 0x2323
- #define step_number_length 24*30
- unsigned short step_journey_cun=0;//总共保存的数据量
- unsigned int step_number[step_number_length];//用于记录步数和路程,高16位是步数,低16位是路程
- //void write_step_journey(unsigned short step_num,unsigned short jour)
- //{
- // step_number[step_journey_cun] = (unsigned int)jour;
- // step_number[step_journey_cun] |= ((unsigned int)step_num)<<16;
- // step_journey_cun++;
- // if(step_journey_cun >= step_number_length)step_journey_cun=0;
- //}
- //以增量方式添加步数和路程
- void add_step_journey(unsigned char step_c,unsigned short jour_c)
- {
- unsigned short step = (step_number[step_journey_cun]&0xffff0000)>>16;
- unsigned short journey = step_number[step_journey_cun]&0x0000ffff;
- step += step_c;
- journey += jour_c;
- step_number[step_journey_cun] = (unsigned int)journey;
- step_number[step_journey_cun] |= ((unsigned int)step)<<16;
- 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);
- }
- int update_step_journey_work_statu=0;
- void update_step_journey_work(void)
- {
- ret_code_t rc;
- fds_record_desc_t desc = {0};//用来操作记录的描述符结构清零
- fds_find_token_t tok = {0};//保存秘钥的令牌清零
- fds_record_t sportdatakey_record={0};
- if(flashbusy==1)
- {
-
- NRF_LOG_INFO("flashbusy.....return ");
- return;//flish在忙,什么都不做
- }
- if(update_step_journey_work_statu==0)return;
- NRF_LOG_INFO("update_step_journey_work_statu= %d ",update_step_journey_work_statu);
- switch(update_step_journey_work_statu)
- {
- case 0://状态0,什么都不用做
- break;
- case 1://状态1,需要发送一次查询
- rc = fds_record_find(sportdatakey_file_id, sportdatakey_rec_key, &desc, &tok);//对应KEY记录查找数据
- switch(rc)
- {
- case FDS_ERR_NOT_FOUND:
- NRF_LOG_INFO("->fds_record_find = FDS_ERR_NOT_FOUND");
- update_step_journey_work_statu=2;
- break;
- case FDS_ERR_NOT_INITIALIZED:
- NRF_LOG_INFO("->fds_record_find = FDS_ERR_NOT_INITIALIZED");
- break;
- case FDS_ERR_NULL_ARG:
- NRF_LOG_INFO("->fds_record_find = FDS_ERR_NULL_ARG");
- break;
- case NRF_SUCCESS:
- NRF_LOG_INFO("->fds_record_find = NRF_SUCCESS");
- flashbusy = 1;
- rc = fds_record_delete(&desc);
- APP_ERROR_CHECK(rc);
- update_step_journey_work_statu=2;
- break;
- default:NRF_LOG_INFO("->fds_record_find = 0x%8x \n",rc);
- break;
- }
- break;
- case 2://状态2,写
- memset(&desc,0,sizeof(fds_record_desc_t));
- sportdatakey_record.file_id = sportdatakey_file_id;
- sportdatakey_record.key = sportdatakey_rec_key;
- sportdatakey_record.data.p_data = (void*)step_number;
- sportdatakey_record.data.length_words = (unsigned int)step_journey_cun;
- rc = fds_record_write(&desc, &sportdatakey_record);//重新写记录
- switch(rc)
- {
- case NRF_SUCCESS:
- NRF_LOG_INFO("->fds_record_write = NRF_SUCCESS");
- update_step_journey_work_statu=3;
- flashbusy = 1;
- break;
- case FDS_ERR_NOT_INITIALIZED:
- NRF_LOG_INFO("->fds_record_write = FDS_ERR_NOT_INITIALIZED");
- break;
- case FDS_ERR_NULL_ARG:
- NRF_LOG_INFO("->fds_record_write = FDS_ERR_NULL_ARG");
- break;
- case FDS_ERR_INVALID_ARG:
- NRF_LOG_INFO("->fds_record_write = FDS_ERR_INVALID_ARG");
- break;
- case FDS_ERR_UNALIGNED_ADDR:
- NRF_LOG_INFO("->fds_record_write = FDS_ERR_UNALIGNED_ADDR");
- update_step_journey_work_statu=3;
- break;
- case FDS_ERR_RECORD_TOO_LARGE:
- NRF_LOG_INFO("->fds_record_write = FDS_ERR_RECORD_TOO_LARGE");
- break;
- case FDS_ERR_NO_SPACE_IN_QUEUES:
- NRF_LOG_INFO("->fds_record_write = FDS_ERR_NO_SPACE_IN_QUEUES");
- break;
- case FDS_ERR_NO_SPACE_IN_FLASH:
- NRF_LOG_INFO("->fds_record_write = FDS_ERR_NO_SPACE_IN_FLASH");
- update_step_journey_work_statu = 4;
- break;
- default:NRF_LOG_INFO("->fds_record_write = 0x%8x \n",rc);
- break;
- }
- break;
- case 3://状态3,读取写完的结果并校验数据
- rc = fds_record_find(sportdatakey_file_id, sportdatakey_rec_key, &desc, &tok);//对应KEY记录查找数据
- switch(rc)
- {
- case FDS_ERR_NOT_FOUND:
- NRF_LOG_INFO("->fds_record_find = FDS_ERR_NOT_FOUND");
- break;
- case FDS_ERR_NOT_INITIALIZED:
- NRF_LOG_INFO("->fds_record_find = FDS_ERR_NOT_INITIALIZED");
- break;
- case FDS_ERR_NULL_ARG:
- NRF_LOG_INFO("->fds_record_find = FDS_ERR_NULL_ARG");
- break;
- case NRF_SUCCESS: NRF_LOG_INFO("->fds_record_find = NRF_SUCCESS");
- {
- fds_flash_record_t config = {0};//把配置清零
- rc = fds_record_open(&desc, &config);//打开记录读取数据
- APP_ERROR_CHECK(rc);
- step_journey_cun = config.p_header->length_words;
- // memcpy(step_number,config.p_data,step_journey_cun*4);
- // for(int i=0;i<step_journey_cun;i++)
- // {
- // NRF_LOG_INFO("0x%x",step_number[i]);
- // }
- rc = fds_record_close(&desc);//关闭记录
- APP_ERROR_CHECK(rc);
-
- update_step_journey_work_statu=0;
- }
- break;
- default:NRF_LOG_INFO("->fds_record_find = 0x%8x \n",rc);
- break;
- }
- break;
- case 4:////状态4,垃圾回收
- NRF_LOG_INFO("fds_gc");
- rc = fds_gc();//垃圾回收
- switch(rc)
- {
- case NRF_SUCCESS:
- NRF_LOG_INFO("->fds_gc = NRF_SUCCESS");
- update_step_journey_work_statu=2;
- flashbusy = 1;
- break;
- case FDS_ERR_NOT_INITIALIZED:
- NRF_LOG_INFO("->fds_gc = FDS_ERR_NOT_INITIALIZED");
- break;
- case FDS_ERR_NO_SPACE_IN_QUEUES:
- NRF_LOG_INFO("->fds_gc = FDS_ERR_NO_SPACE_IN_QUEUES");
- break;
- default:NRF_LOG_INFO("->fds_gc = 0x%8x \n",rc);
- break;
- }
- break;
- default :
- break;
- }
- }
- //把记录保存到中
- void update_step_journey(void)
- {
- update_step_journey_work_statu=1;
- }
- //把flish中的数据加载到内存
- void load_step_journey(void)
- {
- ret_code_t rc;
- fds_record_desc_t desc = {0};//用来操作记录的描述符结构清零
- fds_find_token_t tok = {0};//保存秘钥的令牌清零
- rc = fds_record_find(sportdatakey_file_id, sportdatakey_rec_key, &desc, &tok);//对应KEY记录查找数据
- APP_ERROR_CHECK(rc);
- if (rc == NRF_SUCCESS)//如果查找成功
- {
- // printf("load_step_journey rc == NRF_SUCCESS\n");
- /* A config file is in flash. Let's update it. */
- fds_flash_record_t config = {0};//把配置清零
- /* Open the record and read its contents. */
- rc = fds_record_open(&desc, &config);//打开记录读取数据
- APP_ERROR_CHECK(rc);
-
- step_journey_cun = config.p_header->length_words;
- memcpy(step_number,config.p_data,step_journey_cun*4);
-
- rc = fds_record_close(&desc);//关闭记录
- APP_ERROR_CHECK(rc);
- }
- else
- {
-
- }
- }
- void send_ble_data_A0_0(void)
- {
- uint8_t buf[100];
- uint8_t L=0;
-
- buf[L++] = 0;
- for(int i=0;i<8;i++)
- {
- buf[L++] = cf.systemtime[i];
- }
- send_to_ble_nus(DEX_NUM,0xa0,buf,L);
- }
- //发送数据到串口查询压力数据
- void send_uart_data_A1_1(void)
- {
- uint8_t buf[100];
- uint8_t L=0;
-
- buf[L++] = 0;
- for(int i=0;i<8;i++)
- {
- buf[L++] = cf.systemtime[i];
- }
- send_to_uart_AABBCC(DEX_NUM,0xa0,buf,L);
- }
- void send_ble_data_A1_0(void)
- {
- uint8_t buf[100];
- uint8_t L=0;
-
- buf[L++] = 0;
- for(int i=0;i<64;i++)
- {
- buf[L++] = cf.model_number[i];
- }
- buf[L++] = pm.software_ver[0];
- buf[L++] = pm.software_ver[1];
- buf[L++] = pm.hardware_ver[0];
- buf[L++] = pm.hardware_ver[1];
- send_to_ble_nus(DEX_NUM,0xa1,buf,L);
- }
- void send_ble_data_A1_1(void)
- {
- uint8_t buf[100];
- uint8_t L=0;
-
- buf[L++] = 1;
- buf[L++] = pm.power;
- buf[L++] = pm.time_of_end;
- buf[L++] = pm.inner_temperature;
- for(int i=0;i<4;i++)
- {
- buf[L++] = pm.pressure[i];
- }
-
- buf[L++] = pm_other.power;
- buf[L++] = pm_other.time_of_end;
- buf[L++] = pm_other.inner_temperature;
-
-
- for(int i=0;i<4;i++)
- {
- buf[L++] = pm_other.pressure[i];
- }
- send_to_ble_nus(DEX_NUM,0xa1,buf,L);
- }
- #define PAGE_E 50
- #define Pagesendtimes 20;//同一条命令最多发送的次数
- unsigned char PagesendtimesIndex=0;
- unsigned char pageindex=0;//标记这次要发的是第几包数据
- unsigned char allpagecun=0;//一共要发多少包数据
- void send_ble_data_A1_2(void)
- {
- uint8_t buf[255];
- uint8_t L=0;
- buf[L++] = 2;
- buf[L++] = pageindex;
- if(pageindex >= allpagecun)//表示这次发的是最后一包数据
- {
- for(int i=0;PAGE_E*(pageindex-1)+i <= step_journey_cun ;i++)
- {
- buf[L++] = step_number[PAGE_E*(pageindex-1)+i]>>24;
- buf[L++] = step_number[PAGE_E*(pageindex-1)+i]>>16;
- buf[L++] = step_number[PAGE_E*(pageindex-1)+i]>>8;
- buf[L++] = step_number[PAGE_E*(pageindex-1)+i]>>0;
- 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);
- NRF_LOG_INFO("send_ble_data_A1_2 -> for %d 0x%x",PAGE_E*(pageindex-1)+i,step_number[PAGE_E*(pageindex-1)+i]);
- }
- NRF_LOG_INFO("send_ble_data_A1_2 -> end");
- }
- else
- {
- for(int i=0;i<PAGE_E;i++)
- {
- buf[L++] = step_number[PAGE_E*(pageindex-1)+i]>>24;
- buf[L++] = step_number[PAGE_E*(pageindex-1)+i]>>16;
- buf[L++] = step_number[PAGE_E*(pageindex-1)+i]>>8;
- buf[L++] = step_number[PAGE_E*(pageindex-1)+i]>>0;
- }
- NRF_LOG_INFO("send_ble_data_A1_2 -> pageindex");
- }
-
- send_to_ble_nus(DEX_NUM,0xa1,buf,L);
- }
- //发送查询命令的反馈
- void send_ble_data_A1_2_0(unsigned char cun)
- {
- uint8_t buf[255];
- uint8_t L=0;
- buf[L++] = 2;
- buf[L++] = 0;
- for(int i=0;i<8;i++)
- {
- buf[L++] = cf.systemtime[i];
- }
- buf[L++] = cun;
- send_to_ble_nus(DEX_NUM,0xa1,buf,L);
- NRF_LOG_INFO("send_ble_data_A1_2_0 -> cun == %d",cun);
- }
- //处理手机发过来的命令
- //串口协议
- char anilicedata(const unsigned char *buff,unsigned char *buffout,int len)
- {
- int i=0,j=0;
- unsigned char crc=0;
- if(len<3)return 0;
- for(i=0;i<len;i++)
- {
- if(len-i>4)
- {
- if((buff[i]==0xaa)&&(buff[i+1]+buff[i+2]==0xff))//判断桢头和长度
- {
- if(len - i >= buff[i+1])//数据包长度符合
- {
- for(j=i;j < i+buff[i+1]-1;j++)//计算校验
- {
- crc += buff[j];
- }
- if( buff[i + buff[i+1] - 1] == crc)
- {
- for(j = 0;j < i+buff[i+1]; j++)
- {
- buffout[j] = buff[j+i];
- }
- return 1;
- }
- }
- }
- }
- }
- return 0;
- }
- unsigned char uart_command_buff[100];
- int uart_command_callback(const unsigned char *data,int length)
- {
- memcpy(uart_command_buff,data,length);
- switch(uart_command_buff[3])//处理命令号
- {
- case CMD_HEART:
- case 0x03:
- case 0x04:
- case CMD_MOTION:
- Send_bytes_to_Ble(uart_command_buff,length);
- NRF_LOG_INFO("uart_command_callback -> CMD_MOTION");
- break;
- case CMD_UPDATA:
- switch(uart_command_buff[4])
- {
- case UPDATE_NONE:
- break;
- case UPDATE_RUN://主机上报步数和路程 ,前1个字节是步数增量,路程两个字节(无符号16位,单位是厘米)
- {
- unsigned short temp=(((unsigned short)uart_command_buff[6])<<8) | ((unsigned short)uart_command_buff[7]);
- add_step_journey(uart_command_buff[5],temp);
- NRF_LOG_INFO("uart_command_callback -> UPDATE_RUN %d,%d",uart_command_buff[8],temp);
- }break;
- case UPDATE_BASEINFO:
- {
- int i=5;
- pm.power = uart_command_buff[i++];
- pm.inner_temperature = uart_command_buff[i++];
- pm.pressure[0]=uart_command_buff[i++];
- pm.pressure[1]=uart_command_buff[i++];
- pm.pressure[2]=uart_command_buff[i++];
- pm.pressure[3]=uart_command_buff[i++];
-
- pm_other.power = uart_command_buff[i++];
- pm_other.inner_temperature = uart_command_buff[i++];
- pm_other.pressure[0]=uart_command_buff[i++];
- pm_other.pressure[1]=uart_command_buff[i++];
- pm_other.pressure[2]=uart_command_buff[i++];
- pm_other.pressure[3]=uart_command_buff[i++];
-
- // NRF_LOG_INFO("uart_command_callback -> UPDATE_BASEINFO ");
- //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]);
- }break;
- default:NRF_LOG_INFO("uart_command_callback -> default %d",uart_command_buff[4]);
- break;
- }
- break;
- default:
- break;
- }
- return uart_command_buff[3];
- }
- void send_uart_data_UPDATE_BASEINFO(void)
- {
- uint8_t buf[100];
- uint8_t L=0;
-
- buf[L++]=UPDATE_BASEINFO;
-
- send_to_uart_AABBCC(DEX_NUM,UPDATE_BASEINFO,buf,L);
- }
- /**********************************************************
- * ????:Reply_GameCmd_Host
- * ????:??????
- * ????:?
- * ?????:?
- ***********************************************************/
- void Reply_GameCmd_Host(void)
- {
- uint8_t buf=0x01;
- uint8_t length =1;
- send_to_ble_nus(0x00,0xA2,&buf,length);
- }
- unsigned char buffrecdata[100];
- int ble_phone_command_callback(const unsigned char *data,int length)
- {
- if(anilicedata(data,buffrecdata,length)==0)
- {
- return -1;
- }
- switch(buffrecdata[3])//处理命令号
- {
- case 0xA2:
- Reply_GameCmd_Host();
- break;
- case 0xA1://查询
- switch(buffrecdata[4])
- {
- case 0: //查询设备基本信息
- NRF_LOG_INFO("ble_phone_command_callback -> 0xA1_0");
- send_ble_data_A1_0();
- break;
- case 1: //查询数据
- NRF_LOG_INFO("ble_phone_command_callback -> 0xA1_1");
- send_ble_data_A1_1();//转发给手机端
- break;
- case 2: //查询步数距离
- NRF_LOG_INFO("ble_phone_command_callback -> 0xA1_2_%d",buffrecdata[5]);
- if(buffrecdata[5]==0)//这是手机发过来的查询请求
- {
- for(int i=0;i<8;i++)
- {
- cf.systemtime[i] = buffrecdata[6+i];
- }
- pm.systemtime_minute=buffrecdata[14];//距离上一个整点的分钟数
- NRF_LOG_INFO("ble_phone_command_callback -> pm.systemtime_minute = %d",pm.systemtime_minute);
- if((step_journey_cun==0)&&(step_number[step_journey_cun]==0))//没有数据的时候回一个包的数量为0
- {
- send_ble_data_A1_2_0(0);
- }
- else if((step_journey_cun==0)&&(step_number[step_journey_cun]!=0))
- {
- allpagecun=1;
- send_ble_data_A1_2_0(1);
- pageindex = 1;//设个标志让定时器发送第一包数据
- PagesendtimesIndex = Pagesendtimes;
- }
- else
- {
- //计算总共要发的包数
- if(step_journey_cun%PAGE_E>0)
- allpagecun = step_journey_cun/PAGE_E + 1;
- else allpagecun = step_journey_cun/PAGE_E;
-
- send_ble_data_A1_2_0(allpagecun); //发送反馈告诉手机一共有多少包数据要发
- pageindex = 1;//设个标志让定时器发送第一包数据
- PagesendtimesIndex = Pagesendtimes;
- }
- }
- else
- {
- if(buffrecdata[5] == pageindex)//判断一下是不是当前发送包的反馈数据
- {
- if(pageindex >= allpagecun)
- {
- pageindex = 0;//表示发送已经结束了,这个时候可以清理保存的数据
- step_journey_cun = 0;//清理保存的数据
- step_number[step_journey_cun]=0;
- }
- else
- {
- pageindex++;//发送下一包,发送完成后
- PagesendtimesIndex = Pagesendtimes;
- }
- }
- }
- break;
- default :
- break;
- }
- break;
- default:
- break;
- }
- return 0;
- }
- void send_ble_data_temp(void)
- {
- uint8_t buf[255];
- uint8_t L=0;
- buf[L++] = 2;
- buf[L++] = 1;
- // for(int i=0;i<8;i++)
- // {
- // buf[L++] = 0xff;
- // }
- // buf[L++] = 30;
- send_to_ble_nus(DEX_NUM,0xa1,buf,L);
- }
- unsigned short iiiiiii=500;
- unsigned short timecun=0;
- void timer_callback_Chen(void)
- {
- if(timecun%100 == 0)
- {
- update_step_journey_work();//1s
-
- }
- 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);
- // send_ble_data_temp();
- if((pageindex > 0) && ( pageindex <= allpagecun) && ( timecun%50 == 0))
- {
- if(PagesendtimesIndex>0)
- {
- send_ble_data_A1_2();//发送数据
- PagesendtimesIndex--;
- }
- else
- {
- //在这里报错,发送超时错误
- }
- }
-
- if(timecun%6000==0)pm.systemtime_minute++;
- if(pm.systemtime_minute>=60)//到达一个小时
- {
- step_journey_cun++;
- step_number[step_journey_cun] = 0;//清0
- if(step_journey_cun >= step_number_length)step_journey_cun=0;
- pm.systemtime_minute=0;
- NRF_LOG_INFO("timer_callback_Chen -> pm.systemtime_minute>=60 , step_journey_cun = %d",step_journey_cun);
- }
-
-
-
-
- timecun++;
- }
|