Improve readability of codes in farm ai demo

Adjust the indentation of codes.
This commit is contained in:
David Lin
2020-02-24 21:46:01 +08:00
committed by GitHub
parent a86a9c18f8
commit ddb8268cb7

View File

@@ -20,11 +20,13 @@ static int str_to_hex(const char *bufin, int len, char *bufout)
{ {
int i = 0; int i = 0;
unsigned char tmp2 = 0x0; unsigned char tmp2 = 0x0;
unsigned int tmp = 0; unsigned char tmp = 0x0;
if (NULL == bufin || len <= 0 || NULL == bufout) if (NULL == bufin || len <= 0 || NULL == bufout)
{ {
return -1; return -1;
} }
for(i = 0; i < len; i = i+2) for(i = 0; i < len; i = i+2)
{ {
tmp2 = bufin[i]; tmp2 = bufin[i];
@@ -34,13 +36,14 @@ static int str_to_hex(const char *bufin, int len, char *bufout)
bufout[i/2] =(tmp2<<4)|(tmp&0x0F); bufout[i/2] =(tmp2<<4)|(tmp&0x0F);
} }
return 0; return 0;
} }
int mq_dev_report(void) int mq_dev_report(void)
{ {
char *data; char *data;
cJSON *root = NULL; cJSON *root = NULL;
mqtt_pub_opt_t pub_opt; mqtt_pub_opt_t pub_opt;
pub_opt.dup = 0; pub_opt.dup = 0;
pub_opt.qos = 0; pub_opt.qos = 0;
@@ -48,83 +51,91 @@ int mq_dev_report(void)
pub_opt.id = 0; pub_opt.id = 0;
pub_opt.topic = MQTT_PUBLISH_TOPIC; pub_opt.topic = MQTT_PUBLISH_TOPIC;
// {"motor":0,"light":1,"temperature":22.22,"humidity":55.55,"light_intensity":100.00} //{"motor":0,"light":1,"temperature":22.22,"humidity":55.55,"light_intensity":100.00}
root = cJSON_CreateObject(); root = cJSON_CreateObject();
cJSON_AddNumberToObject(root, "motor",E53_IA1_Data.MotorMode); cJSON_AddNumberToObject(root, "motor",E53_IA1_Data.MotorMode);
cJSON_AddNumberToObject(root, "light",E53_IA1_Data.LightMode); cJSON_AddNumberToObject(root, "light",E53_IA1_Data.LightMode);
cJSON_AddNumberToObject(root, "temperature",math_keep_ndot((double)E53_IA1_Data.Temperature,2)); cJSON_AddNumberToObject(root, "temperature",math_keep_ndot((double)E53_IA1_Data.Temperature,2));
cJSON_AddNumberToObject(root, "humidity",math_keep_ndot((double)E53_IA1_Data.Humidity,2)); cJSON_AddNumberToObject(root, "humidity",math_keep_ndot((double)E53_IA1_Data.Humidity,2));
cJSON_AddNumberToObject(root, "light_intensity",math_keep_ndot((double)E53_IA1_Data.Lux,2)); cJSON_AddNumberToObject(root, "light_intensity",math_keep_ndot((double)E53_IA1_Data.Lux,2));
/* formatted print */ /* formatted print */
data=cJSON_PrintUnformatted(root); data=cJSON_PrintUnformatted(root);
pub_opt.topic = MQTT_PUBLISH_TOPIC; pub_opt.topic = MQTT_PUBLISH_TOPIC;
pub_opt.payload =(unsigned char *)data; pub_opt.payload =(unsigned char *)data;
pub_opt.payload_len = strlen(data); pub_opt.payload_len = strlen(data);
printf("\r\nreport--->topic:%s\r\ndata(%d):%s", pub_opt.topic,pub_opt.payload_len,pub_opt.payload); printf("\r\nreport--->topic:%s\r\ndata(%d):%s", pub_opt.topic,pub_opt.payload_len,pub_opt.payload);
if (tos_mqtt_publish(sock_id, &pub_opt) != 0) { if (tos_mqtt_publish(sock_id, &pub_opt) != 0) {
printf("publish failed!!!\n"); printf("publish failed!!!\n");
} }
tos_mmheap_free(data); tos_mmheap_free(data);
cJSON_Delete(root); cJSON_Delete(root);
return 0; return 0;
} }
int parse_dev_cmd(uint8_t *data) int parse_dev_cmd(uint8_t *data)
{ {
//{"motor":0,"light":1} //{"motor":0,"light":1}
//JSON字符串到cJSON格式 //JSON字符串到cJSON格式
cJSON* cjson = cJSON_Parse((char *)data); cJSON* cjson = cJSON_Parse((char *)data);
cJSON *cjsonret=NULL; cJSON *cjsonret=NULL;
int motor; int motor;
int light; int light;
char *str; char *str;
printf("parse_dev_cmd...\n");
if(cjson == NULL){ printf("parse_dev_cmd...\n");
printf("json pack into cjson error..."); if(cjson == NULL){
return -1; printf("json pack into cjson error...");
} return -1;
else{ }
cJSON_Print(cjson); else{
} cJSON_Print(cjson);
cjsonret = NULL; }
cjsonret = cJSON_GetObjectItem(cjson,"motor");
if(cjsonret!=NULL) cjsonret = NULL;
{ cjsonret = cJSON_GetObjectItem(cjson,"motor");
//打印输出 if(cjsonret!=NULL)
motor=cjsonret->valueint; {
printf("motor=%d\n",motor); //打印输出
if(motor==1) motor=cjsonret->valueint;
{//power on relay printf("motor=%d\n",motor);
if(motor==1)
{
//power on relay
motor_control(1); motor_control(1);
} }
else if(motor==0) else if(motor==0)
{//power off relay {
//power off relay
motor_control(0); motor_control(0);
} }
} }
cjsonret = NULL;
cjsonret = cJSON_GetObjectItem(cjson,"light"); cjsonret = NULL;
if(cjsonret!=NULL) cjsonret = cJSON_GetObjectItem(cjson,"light");
{ if(cjsonret!=NULL)
//打印输出 {
light=cjsonret->valueint; //打印输出
//打印输出 light=cjsonret->valueint;
printf("light=%d\n",light); //打印输出
if(light==1) printf("light=%d\n",light);
{//power on relay if(light==1)
{
//power on relay
light_control(1); light_control(1);
} }
else if(light==0) else if(light==0)
{//power off relay {
//power off relay
light_control(0); light_control(0);
} }
} }
cjsonret = cJSON_GetObjectItem(cjson,"speaker"); cjsonret = cJSON_GetObjectItem(cjson,"speaker");
if(cjsonret!=NULL) if(cjsonret!=NULL)
{ {
//打印输出 //打印输出
printf("parse speaker:%s\n",cjsonret->valuestring); printf("parse speaker:%s\n",cjsonret->valuestring);
str=tos_mmheap_alloc(strlen(cjsonret->valuestring)); str=tos_mmheap_alloc(strlen(cjsonret->valuestring));
if(str!=NULL) if(str!=NULL)
{ {
@@ -134,13 +145,15 @@ int parse_dev_cmd(uint8_t *data)
speaker(str); speaker(str);
tos_mmheap_free(str); tos_mmheap_free(str);
} }
} }
//delete cjson //delete cjson
cJSON_Delete(cjson); cJSON_Delete(cjson);
return 0;
return 0;
} }
int mq_dev_receiv(void){ int mq_dev_receiv(void)
{
static int count = 1; static int count = 1;
uint8_t read_data[100]; uint8_t read_data[100];
int read_len; int read_len;
@@ -156,27 +169,29 @@ int mq_dev_receiv(void){
printf("read buf1,\r\n"); printf("read buf1,\r\n");
read_len = tos_mqtt_receive(sub_opt.topic, strlen(sub_opt.topic), read_data, sizeof(read_data)); read_len = tos_mqtt_receive(sub_opt.topic, strlen(sub_opt.topic), read_data, sizeof(read_data));
if (read_len >= 0) { if (read_len >= 0) {
printf("---------->topic: %s, payload: %s, payload_len: %d\n", sub_opt.topic, read_data, read_len); printf("---------->topic: %s, payload: %s, payload_len: %d\n", sub_opt.topic, read_data, read_len);
parse_dev_cmd(read_data); parse_dev_cmd(read_data);
} }
count++; count++;
return 0;
return 0;
} }
int mq_dev_subscribe(void){ int mq_dev_subscribe(void)
{
mqtt_sub_opt_t sub_opt; mqtt_sub_opt_t sub_opt;
sub_opt.count = 1; sub_opt.count = 1;
sub_opt.dup = 0; sub_opt.dup = 0;
sub_opt.id = 1; sub_opt.id = 1;
sub_opt.req_qos = 0; sub_opt.req_qos = 0;
sub_opt.topic = MQTT_SUBSCRIBE_TOPIC; sub_opt.topic = MQTT_SUBSCRIBE_TOPIC;
printf("subscribe topic1:%s",sub_opt.topic); printf("subscribe topic1:%s",sub_opt.topic);
if (tos_mqtt_subscribe(sock_id, &sub_opt) != 0) { if (tos_mqtt_subscribe(sock_id, &sub_opt) != 0) {
printf("subscribe failed!!!\n"); printf("subscribe failed!!!\n");
} }
return 0;
return 0;
} }
void ai_demo(void) void ai_demo(void)
{ {
@@ -205,15 +220,18 @@ void ai_demo(void)
} }
} }
void sensor_read(void){ void sensor_read(void)
while(1){ {
E53_IA1_Read_Data(); while(1)
osDelay(100); {
E53_IA1_Read_Data();
osDelay(100);
} }
} }
#define welcomspeak "欢迎TOS" #define welcomspeak "欢迎TOS"
void speaker(char *str){ void speaker(char *str)
{
uint8_t data[100]; uint8_t data[100];
int16_t len=0; int16_t len=0;
@@ -244,4 +262,3 @@ void application_entry(void *arg)
tos_task_delay(1000); tos_task_delay(1000);
} }
} }