USER_FUNC void test_uflash_start(void) { uint32_t i; uint32_t value; int pages; hfuflash_erase_page(0,1); for(i=0;i<HFFLASH_PAGE_SIZE;) { if(hfuflash_write(i,(char*)&i,sizeof(i))<sizeof(i)) { u_printf("uflash eof\n"); break; } i+=sizeof(i); } for(i=0;i<HFFLASH_PAGE_SIZE;) { if(hfuflash_read(i,(char*)&value,4)<4) { u_printf("uflash eof\n"); break; } u_printf("%d\n",value); i+=4; } pages = (HFUFLASH_SIZE+HFUFLASH1_SIZE)/HFFLASH_PAGE_SIZE; for(i=0;i<pages;i++) { u_printf("erase test %d\n",i); msleep(1000); if(hfuflash_erase_page(i*HFFLASH_PAGE_SIZE,pages-i)!=HF_SUCCESS) { u_printf("test erase fail!\n"); return ; } } for(i=0;i<HFUFLASH_SIZE+HFUFLASH1_SIZE;) { if(hfuflash_write(i,(char*)&i,sizeof(i))<sizeof(i)) { u_printf("uflash eof\n"); break; } if(hfuflash_read(i,(char*)&value,4)<4) { u_printf("uflash eof\n"); break; } if(value!=i) { u_printf("test fail %d %d\n",i,value); } i+=sizeof(i); } }
static int hf_atcmd_flash(pat_session_t s,int argc,char *argv[],char *rsp,int len) { char flash_data[33]; if(argc == 0) { memset(flash_data, 0, 33); hfuflash_read(0, (uint8_t *)flash_data, 32); sprintf(rsp, "=%s", flash_data); return 0; } else if(argc == 1) { hfuflash_erase_page(0, 1); hfuflash_write(0, (uint8_t *)argv[0], 32); } return 0; }
///定时线程,需要连接跳线 USER_FUNC void time_fdthread(void *arg) { while(1) { ///第一步:读时钟模块上的时间值 hfuart_send(huart, query_time_cmd, sizeof(query_time_cmd), 0); msleep(500); if (hfuart_recv(huart, recv_timebuf, sizeof(recv_timebuf), 0) > 0) { memset(time_str, 0, sizeof(time_str)); hour = BCDToInt(recv_timebuf[0]); ///将BCD码转成十进制的时 min = BCDToInt(recv_timebuf[1]); time_str[0] = (hour / 10) + 48; ///数字到字符 time_str[1] = (hour % 10) + 48; time_str[2] = ':'; time_str[3] = (min / 10) + 48; time_str[4] = (min % 10) + 48; // sendto(udp_fd, time_str, sizeof(time_str), 0, (struct sockaddr *)&address, sizeof(address)); } ///第二步:取内存中设定的时间值与当前获取的时间值进行匹配 /// 备注:每次都轮询完flash中所有的时间设置,标识1~4为喂食,标识5~8为喂水,这样set和cancel不用作修改 memset(read_timed_flashbuf, 0, sizeof(read_timed_flashbuf)); hfuflash_read(0, read_timed_flashbuf, sizeof(read_timed_flashbuf)); if (read_timed_flashbuf[1] == ',') ///如果不空 { ///2.1 分割整个竖线连接的字符串read_timed_flashbuf数组 p_timed_str = strtok(read_timed_flashbuf, "|"); n_timed = 0; while(p_timed_str) { timed_flash_set_str[n_timed] = p_timed_str; n_timed++; p_timed_str = strtok(NULL, "|"); } ///2.2 匹配时间并喂食,将需要连接的字符串标识存入存储数组中 k_timed = -1; memset(store_timed_flashbuf, 0, sizeof(store_timed_flashbuf)); while(k_timed < (n_timed - 1)) { k_timed++; memset(timed_cmpbuf, 0, sizeof(timed_cmpbuf)); strcpy(timed_cmpbuf, timed_flash_set_str[k_timed]); ///timed_flash_set_str[0 - (n_timed-1)]的格式为:1,08:30,1,3 // u_printf("timed_cmpbuf is: %s\n", timed_cmpbuf); ///以","分割timed_cmpbuf k_cmd = 0; p_cmd = strtok(timed_cmpbuf, ","); while(p_cmd) { everyTimeCmd[k_cmd] = p_cmd; k_cmd++; p_cmd = strtok(NULL, ","); } ///取everyTimeCmd[1]中时间值与时钟模块time_str比较 if (strcmp(time_str, everyTimeCmd[1]) == 0) { sendto(udp_fd, "Got it", sizeof("Got it"), 0, (struct sockaddr *)&address, sizeof(address)); //注:此处添加定时喂食、喂水的判断 control_feed[3]=(char)(atoi(everyTimeCmd[3]) * 5); hfuart_send(huart, control_feed, sizeof(control_feed), 0); if (strcmp(everyTimeCmd[2], "1") == 0) { continue; } } ///连接字符串 strcat(store_timed_flashbuf, timed_flash_set_str[k_timed]); strcat(store_timed_flashbuf, vertLine); } ///写进flash中 hfuflash_erase_page(0,1); hfuflash_write(0, store_timed_flashbuf, sizeof(store_timed_flashbuf)); sendto(udp_fd, "Only pet(A496): ", sizeof("Only pet(A496): "), 0, (struct sockaddr *)&address, sizeof(address)); sendto(udp_fd, store_timed_flashbuf, sizeof(store_timed_flashbuf), 0, (struct sockaddr *)&address, sizeof(address)); } sleep(75); ///保证每分钟轮询一次 } }
//外网模式接收测试 USER_FUNC void recvTest(void *arg) { if (hfthread_create(fdthread, "feed", 256, NULL, HFTHREAD_PRIORITIES_LOW, NULL, NULL) != HF_SUCCESS) //创建喂食线程 { u_printf("create feed thread fail\n"); } if (hfthread_create(wtthread, "water", 256, NULL, HFTHREAD_PRIORITIES_LOW, NULL, NULL) != HF_SUCCESS) //创建喂水线程 { u_printf("create water thread fail\n"); } if (hfthread_create(time_fdthread, "timed_fd", 256, NULL, HFTHREAD_PRIORITIES_LOW, NULL, NULL) != HF_SUCCESS) //创建定时线程 { u_printf("create timed_fd thread fail\n"); } while (getMessageThreadFlag()) { sleep(1); ///因为设备在连接服务器过程中此线程一直在循环,实际使用时需要删除这个时延 u_printf("Enter while!\n"); int msgType = recvMessage(); if (msgType == MESSAGE_FLAG) { //如果收到的是控制指令 // u_printf("recv-> %s",getCurrentMsg()); // sendMessage(getCurrentUser(),getCurrentMsg()); /** *第一步:调用getCurrentMsg()函数,获取收到的指令字符串,该函数返回字符数组(char*类型) *第二步:根据需要解析字符串得到有效指令码,并发到底层进行控制 *第三步:控制结束后,反馈的信息通过sendMessage(user,msg)函数发出,该函数需要传入两个参数 * user参数通过调用getCurrentUser()获得,msg参数即为底层要发给客户端的反馈信息(字符串类型) * *示例:char* codeStr = getCurrentMsg(); * char* code = 指令解析函数(); * 进行底层控制,并得到反馈字符串; * sendMessage(getCurrentUser(),"反馈字符串"); * */ u_printf("recv control cmd!\n"); memset(recvbuf, 0, sizeof(recvbuf)); char* str = getCurrentMsg(); u_printf("The client has received a message: %s\n", str); strcpy(recvbuf, str); // sendto(udp_fd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr *)&address, sizeof(address)); p = strtok(recvbuf, "#"); n = 0; while(p) { cmd[n] = p; n++; p = strtok(NULL, "#"); } u_printf("cmd[1] is :%s\n", cmd[1]); if (strncmp(cmd[0],"action",sizeof("action"))==0) //控制喂水、喂食,指令格式:action#1,10 { q = strtok(cmd[1], ","); j = 0; while(q) { actcmd[j] = q; j++; q = strtok(NULL, ","); } u_printf("actcmd[0] is :%s\n", actcmd[0]); if (strncmp(actcmd[0],"1",sizeof("1"))==0) { if (fdstate == 0) { fdstate = 1; //将喂食状态改为1,触发喂食线程 } else { sendMessage(getCurrentUser(), "fdbusy"); u_printf("Warning: fdbusy!\n"); } } else if (strncmp(actcmd[0],"0",sizeof("0"))==0) { if (wtstate == 0) { wtstate = 1; //将喂水状态改为1,触发喂水线程 } else { sendMessage(getCurrentUser(), "wtbusy"); u_printf("Warning: wtbusy!\n"); } } } else if(strncmp(cmd[0],"set",sizeof("set"))==0) //定时设置(添加和修改) { memset(read_flashbuf, 0, sizeof(read_flashbuf)); hfuflash_read(0, read_flashbuf, sizeof(read_flashbuf)); if (read_flashbuf[1] == ',') ///用户flash中有定时设置 { ///第一步:竖线分割flash中时间值,存于指针数组flash_set_str中 p_str = strtok(read_flashbuf, "|"); n_str = 0; while(p_str) { flash_set_str[n_str] = p_str; n_str++; p_str = strtok(NULL, "|"); } ///第二步:取flash_set_str值中标识与cmd[1]进行比较 k_str = n_str - 1; while(k_str >= 0) { memset(tmp_flash_strcmp, 0, sizeof(tmp_flash_strcmp)); strcpy(tmp_flash_strcmp, flash_set_str[k_str]); ///strcpy慎用 u_printf("The tmp_flash_strcmp is: %s\n", tmp_flash_strcmp); memset(tmp_cmd_strcmp, 0, sizeof(tmp_cmd_strcmp)); strcpy(tmp_cmd_strcmp, cmd[1]); u_printf("The tmp_cmd_strcmp is: %s\n", tmp_cmd_strcmp); if (strncmp(strtok(tmp_cmd_strcmp, ","), strtok(tmp_flash_strcmp, ","), 1) == 0) ///比较时间的标识 { // strcpy(flash_set_str[k_str], cmd[1]); flash_set_str[k_str] = cmd[1]; break; } k_str--; } ///第三步:连接字符串后放到store_flashbuf中 memset(store_flashbuf, 0, sizeof(store_flashbuf)); if (k_str < 0) //flash中没有该标识,即表示添加 { c_str = 0; while(c_str < n_str) { strcat(store_flashbuf, flash_set_str[c_str]); strcat(store_flashbuf, vertLine); c_str++; } strcat(store_flashbuf, cmd[1]); } else //修改某个标识的设置 { c_str = 0; while(c_str < n_str) { strcat(store_flashbuf, flash_set_str[c_str]); if (c_str < (n_str - 1)) strcat(store_flashbuf, vertLine); c_str++; } } } else ///flash中为空 { memset(store_flashbuf, 0, sizeof(store_flashbuf)); strcat(store_flashbuf, cmd[1]); } ///第四步:存储到用户flash中 hfuflash_erase_page(0, 1); hfuflash_write(0, store_flashbuf, sizeof(store_flashbuf)); u_printf("We have stored the store_flashbuf: %s\n", store_flashbuf); sendto(udp_fd, store_flashbuf, sizeof(store_flashbuf), 0, (struct sockaddr *)&address, sizeof(address)); sendMessage(getCurrentUser(),"control success"); } else if(strncmp(cmd[0], "cancel", sizeof("cancel")) == 0) //取消定时设置 { memset(read_flashbuf, 0, sizeof(read_flashbuf)); hfuflash_read(0, read_flashbuf, sizeof(read_flashbuf)); if (read_flashbuf[1] == ',') ///用户flash中有定时设置 { ///第一步:竖线分割flash中时间值,存于指针数组flash_set_str中 p_str = strtok(read_flashbuf, "|"); n_str = 0; while(p_str) { flash_set_str[n_str] = p_str; n_str++; p_str = strtok(NULL, "|"); } ///第二步:取flash_set_str值中标识与cmd[1]进行比较 k_str = 0; while(k_str < n_str) { memset(tmp_flash_strcmp, 0, sizeof(tmp_flash_strcmp)); strcpy(tmp_flash_strcmp, flash_set_str[k_str]); if (strncmp(strtok(tmp_flash_strcmp, ","), cmd[1], 1) == 0) { break; } k_str++; } ///第三步:flash_set_str[k_str]即为要删除的定时字符串,连接其余的放到store_flashbuf中 c_str = 0; memset(store_flashbuf, 0, sizeof(store_flashbuf)); while(c_str < n_str) { if(c_str == k_str) { c_str++; continue; } if (k_str < (n_str -1)) { strcat(store_flashbuf, flash_set_str[c_str]); if (c_str < (n_str - 1)) strcat(store_flashbuf, vertLine); c_str++; } else { strcat(store_flashbuf, flash_set_str[c_str]); if (c_str < (n_str - 2)) strcat(store_flashbuf, vertLine); c_str++; } } ///第四步:存储到用户flash中 hfuflash_erase_page(0,1); hfuflash_write(0, store_flashbuf, sizeof(store_flashbuf)); u_printf("We have stored the store_flashbuf: %s\n", store_flashbuf); sendto(udp_fd, store_flashbuf, sizeof(store_flashbuf), 0, (struct sockaddr *)&address, sizeof(address)); sendMessage(getCurrentUser(),"control success"); } } else if (strncmp(cmd[0],"flashMemory",sizeof("flashMemory"))==0) //读取内存中设定的时间值 { memset(read_flashbuf, 0, sizeof(read_flashbuf)); hfuflash_read(0, read_flashbuf, sizeof(read_flashbuf)); u_printf("The time setting is: %s\n", read_flashbuf); sendto(udp_fd, read_flashbuf, sizeof(read_flashbuf), 0, (struct sockaddr *)&address, sizeof(address)); sendMessage(getCurrentUser(), read_flashbuf); } // else if (strncmp(cmd[0],"clearAll",sizeof("clearAll"))==0) //清空内存中所有设定的时间值 // { // hfuflash_erase_page(0,1); // sendMessage(getCurrentUser(),"control success"); // } } } }