c++测试程序

Signed-off-by: 45coll <674148718@qq.com>
master
45coll 2021-09-30 16:20:08 +08:00
parent 56bbd5b000
commit 3bca145d5f
5 changed files with 90 additions and 4 deletions

View File

@ -70,10 +70,12 @@ double target_angle = 91;
double target_voltage = 0;
void onPacketCallBack(AsyncUDPPacket packet)
{
target_velocity = atoi((char*)(packet.data()));
Serial.print("数据内容: ");
Serial.println(target_velocity);
char* da;
da= (char*)(packet.data());
Serial.println(da);
// target_velocity = atoi();
// Serial.print("数据内容: ");
// Serial.println(target_velocity);
wifi_flag = 1;
// packet.print("reply data");
}
@ -357,4 +359,5 @@ void wifi_print(char * s,double num)
strcat(str, n);
strcat(buf+strlen(buf), str);
strcat(buf, ",");
}

26
ctest/com.cpp Normal file
View File

@ -0,0 +1,26 @@
#include "com.h"
void Command::run(char* str){
for(int i=0; i < call_count; i++){
if(call_ids[i]){
call_list[i](&str);
break;
}
}
}
void Command::add(char* id, CommandCallback onCommand){
call_list[call_count] = onCommand;
call_ids[call_count] = id;
call_count++;
}
void Command::scalar(float* value, char* user_cmd){
*value = atof(user_cmd);
}
bool Commander::isSentinel(char* ch,char* str)
{
char s[strlen(ch)];
strncpy(s,str,strlen(ch));
if(strcmp(ch, s) == 0)
return true;
else
return false;
}

16
ctest/com.h Normal file
View File

@ -0,0 +1,16 @@
// callback function pointer definiton
typedef void (* CommandCallback)(char*); //!< command callback function pointer
class Command
{
public:
void add(char* id , CommandCallback onCommand);
void run(char* str);
void Command::scalar(float* value, char* user_cmd);
bool Commander::isSentinel(char* ch);
private:
// Subscribed command callback variables
CommandCallback call_list[20];//!< array of command callback pointers - 20 is an arbitrary number
char* call_ids[20]; //!< added callback commands
int call_count = 0;//!< number callbacks that are subscribed
}

41
ctest/ctest1.cpp Normal file
View File

@ -0,0 +1,41 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "com.h"
char buf[255];
void wifi_print(char * s,double num)
{
char str[255];
char n[255];
sprintf(n, "%.2f",num);
strcpy(str,s);
strcat(str, n);
strcat(buf+strlen(buf), str);
strcat(buf, ",");
}
float v = 0.1;
void re_command(float *num)
{
*num = *num + 1;
}
char* str = "abc23";
char* cmd_id = "abc";
main()
{
re_command(&v);
char s[strlen(cmd_id)];
strncpy(s,str,strlen(cmd_id));
printf("%s", s);
if(strcmp(cmd_id, s) == 0)
{
printf("%f", v);
v = atof(str+strlen(cmd_id));
printf("%f", v);
}
// char* da;
// da = "123456";
// if (da[0] == '2')
}

BIN
ctest/ctest1.exe Normal file

Binary file not shown.