Skip to content

Commit

Permalink
优化代码格式;添加hal内硬件驱动日志打印
Browse files Browse the repository at this point in the history
  • Loading branch information
sfxfs committed Mar 31, 2024
1 parent 5efdde5 commit d91cd9e
Show file tree
Hide file tree
Showing 19 changed files with 134 additions and 115 deletions.
1 change: 0 additions & 1 deletion cal/rpc_cjson.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "rpc_cjson.h"

#include "log.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Expand Down
1 change: 0 additions & 1 deletion cal/tcp_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "log.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
Expand Down
6 changes: 4 additions & 2 deletions cal/uvm_cal.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "uvm_cal.h"

#include "log.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Expand Down Expand Up @@ -59,7 +58,10 @@ static char *on_recv(const char *data, int len, void *arg)
/* build response */
char *resp = calloc(jresp_len + HTTP_HEADER_BUF_SIZE, sizeof(char));
sprintf(resp,
"HTTP/1.1 200 OK\r\nContent-Type: application/json; charset=UTF-8\r\nAccept: application/json\r\nContent-Length: %d\r\n\r\n%s",
"HTTP/1.1 200 OK\r\n"
"Content-Type: application/json; charset=UTF-8\r\n"
"Accept: application/json\r\n"
"Content-Length: %d\r\n\r\n%s",
jresp_len, jresp);

/* end up */
Expand Down
7 changes: 3 additions & 4 deletions cfg/cfg_dev_ctl.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include <stdio.h>
#include <stdlib.h>
#include "cfg_dev_ctl.h"

#include "cJSON.h"
#include "s2j.h"

#include "cfg_dev_ctl.h"
#include <stdio.h>
#include <stdlib.h>

static const dev_ctl_params default_params = {
.arm_attr = {
Expand Down
7 changes: 3 additions & 4 deletions cfg/cfg_others.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include <stdio.h>
#include <stdlib.h>
#include "cfg_others.h"

#include "cJSON.h"
#include "s2j.h"

#include "cfg_others.h"
#include <stdio.h>
#include <stdlib.h>

static others_params default_params = {
.server_config = {
Expand Down
7 changes: 3 additions & 4 deletions cfg/cfg_propeller.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include <stdio.h>
#include <stdlib.h>
#include "cfg_propeller.h"

#include "cJSON.h"
#include "s2j.h"

#include "cfg_propeller.h"
#include <stdio.h>
#include <stdlib.h>

static const propeller_params default_params = {
.pwm_freq_offset = 0,
Expand Down
7 changes: 3 additions & 4 deletions cfg/cfg_rocket_ratio.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include <stdio.h>
#include <stdlib.h>
#include "cfg_rocket_ratio.h"

#include "cJSON.h"
#include "s2j.h"

#include "cfg_rocket_ratio.h"
#include <stdio.h>
#include <stdlib.h>

static const rocket_ratio_params default_params = {
.x = {
Expand Down
79 changes: 47 additions & 32 deletions cfg/uvm_cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,41 @@
#include <unistd.h>
#include <string.h>

static int uvm_cfg_initialize_value(config_data data_s)
static int uvm_cfg_initialize_value(config_data *data_s)
{
uvm_cfg_delete(data_s);
data_s.dev_ctl = dev_ctl_create_with_init_val();
data_s.propeller = propeller_create_with_init_val();
data_s.rocket_ratio = rocket_ratio_create_with_init_val();
data_s.others = others_create_with_init_val();

if (data_s.dev_ctl && data_s.propeller && data_s.rocket_ratio && data_s.others)
data_s->dev_ctl = dev_ctl_create_with_init_val();
data_s->propeller = propeller_create_with_init_val();
data_s->rocket_ratio = rocket_ratio_create_with_init_val();
data_s->others = others_create_with_init_val();

if (data_s->dev_ctl && data_s->propeller && data_s->rocket_ratio && data_s->others)
return 0;
return -1;
}

void uvm_cfg_delete(config_data data_s)
void uvm_cfg_delete(config_data *data_s)
{
if (data_s.dev_ctl)
free(data_s.dev_ctl);
if (data_s.propeller)
free(data_s.propeller);
if (data_s.rocket_ratio)
free(data_s.rocket_ratio);
if (data_s.others)
free(data_s.others);
bzero(&data_s, sizeof(config_data));
if (!data_s)
return;

if (data_s->dev_ctl)
free(data_s->dev_ctl);
if (data_s->propeller)
free(data_s->propeller);
if (data_s->rocket_ratio)
free(data_s->rocket_ratio);
if (data_s->others)
free(data_s->others);
bzero(data_s, sizeof(config_data));
}

int uvm_cfg_read (config_data data_s)
int uvm_cfg_read (config_data *data_s)
{
if (!data_s)
return -1;

uvm_cfg_delete(data_s);

char *data = uvm_intf_read_from_file();
Expand All @@ -49,14 +56,14 @@ int uvm_cfg_read (config_data data_s)
return -2;
}

data_s.others = others_j2s(cJSON_GetObjectItem(json, "others_params"));
data_s.dev_ctl = dev_ctl_j2s(cJSON_GetObjectItem(json, "dev_ctl_params"));
data_s.propeller = propeller_j2s(cJSON_GetObjectItem(json, "propeller_params"));
data_s.rocket_ratio = rocket_ratio_j2s(cJSON_GetObjectItem(json, "rocket_ratio_params"));
data_s->others = others_j2s(cJSON_GetObjectItem(json, "others_params"));
data_s->dev_ctl = dev_ctl_j2s(cJSON_GetObjectItem(json, "dev_ctl_params"));
data_s->propeller = propeller_j2s(cJSON_GetObjectItem(json, "propeller_params"));
data_s->rocket_ratio = rocket_ratio_j2s(cJSON_GetObjectItem(json, "rocket_ratio_params"));

cJSON_Delete(json);

if (data_s.dev_ctl && data_s.propeller && data_s.rocket_ratio && data_s.others)
if (data_s->dev_ctl && data_s->propeller && data_s->rocket_ratio && data_s->others)
{
log_info("Config file read succeed.");
return 0;
Expand All @@ -66,40 +73,43 @@ int uvm_cfg_read (config_data data_s)



int uvm_cfg_write(config_data data_s)
int uvm_cfg_write(config_data *data_s)
{
if (!data_s)
return -1;

void *temp = NULL;
cJSON *json = cJSON_CreateObject();

if (data_s.others)
cJSON_AddItemToObject(json, "others_params", others_s2j(data_s.others));
if (data_s->others)
cJSON_AddItemToObject(json, "others_params", others_s2j(data_s->others));
else
{
temp = others_create_with_init_val();
cJSON_AddItemToObject(json, "others_params", others_s2j(temp));
free(temp);
}

if (data_s.dev_ctl)
cJSON_AddItemToObject(json, "dev_ctl_params", dev_ctl_s2j(data_s.dev_ctl));
if (data_s->dev_ctl)
cJSON_AddItemToObject(json, "dev_ctl_params", dev_ctl_s2j(data_s->dev_ctl));
else
{
temp = dev_ctl_create_with_init_val();
cJSON_AddItemToObject(json, "dev_ctl_params", others_s2j(temp));
free(temp);
}

if (data_s.propeller)
cJSON_AddItemToObject(json, "propeller_params", propeller_s2j(data_s.propeller));
if (data_s->propeller)
cJSON_AddItemToObject(json, "propeller_params", propeller_s2j(data_s->propeller));
else
{
temp = propeller_create_with_init_val();
cJSON_AddItemToObject(json, "propeller_params", others_s2j(temp));
free(temp);
}

if (data_s.rocket_ratio)
cJSON_AddItemToObject(json, "rocket_ratio_params", rocket_ratio_s2j(data_s.rocket_ratio));
if (data_s->rocket_ratio)
cJSON_AddItemToObject(json, "rocket_ratio_params", rocket_ratio_s2j(data_s->rocket_ratio));
else
{
temp = rocket_ratio_create_with_init_val();
Expand All @@ -116,8 +126,13 @@ int uvm_cfg_write(config_data data_s)
return ret;
}

int uvm_cfg_init(config_data data_s)
int uvm_cfg_init(config_data *data_s)
{
if (!data_s)
return -1;

bzero(data_s, sizeof(config_data));

if (0 == access(CONFIG_FILE_PATH, F_OK)) // 0:存在
{
return uvm_cfg_read(data_s);
Expand Down
8 changes: 4 additions & 4 deletions cfg/uvm_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ typedef struct
} config_data;

/* ess */
void uvm_cfg_delete(config_data data_s);
int uvm_cfg_init (config_data data_s);
int uvm_cfg_read (config_data data_s);
int uvm_cfg_write (config_data data_s);
void uvm_cfg_delete(config_data *data_s);
int uvm_cfg_init (config_data *data_s);
int uvm_cfg_read (config_data *data_s);
int uvm_cfg_write (config_data *data_s);

/* add */
// int uvm_cfg_read_cjson (void *cjson);
Expand Down
2 changes: 1 addition & 1 deletion hal/interface_pca9685.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,6 @@ void pca9685_interface_debug_print(const char *const fmt, ...)
{
va_list temp_va;
va_start(temp_va, fmt);
printf(fmt, temp_va);
log_error(fmt, temp_va);
va_end(temp_va);
}
3 changes: 2 additions & 1 deletion hal/utils/gpio.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "log.h"
#include <errno.h>
#include <stdio.h>
#include <string.h>
Expand All @@ -18,7 +19,7 @@ int uvm_gpio_export(uint32_t gpio)
if (ret < 0)
return ret;
if (!access(file, F_OK)) {
printf("warning: gpio %d, file (%s) has exist\n", gpio, file);
log_warn("gpio %d, file (%s) has exist", gpio, file);
return 0;
}
return write_sysfs_int("export", GPIO_SYSFS_PATH, gpio);
Expand Down
11 changes: 6 additions & 5 deletions hal/utils/iic.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "log.h"
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
Expand Down Expand Up @@ -28,13 +29,13 @@ int uvm_i2c_init(char *dev)
int fd = open(dev, O_RDWR);
if (fd < 0)
{
printf("IIC: Fail to open %s \r\n", dev);
log_error("IIC: Fail to open %s", dev);
return -1;
}

if (ioctl(fd, I2C_TENBIT, 0) < 0) // 7-bit addr
{
printf("IIC: Unable to set address type: %s\n", strerror(errno));
log_error("IIC: Unable to set address type: %s", strerror(errno));
close(fd);
return -2;
}
Expand All @@ -52,7 +53,7 @@ int uvm_i2c_write(int fd, uint8_t addr, uint8_t reg, uint8_t len, uint8_t *val)
{
if (ioctl(fd, I2C_SLAVE, (int)addr) < 0)
{
printf("IIC: Unable to select I2C device: %s\n", strerror(errno));
log_error("IIC: Unable to select I2C device: %s\n", strerror(errno));
close(fd);
return -1;
}
Expand All @@ -69,7 +70,7 @@ int uvm_i2c_read(int fd, uint8_t addr, uint8_t reg, uint8_t len, uint8_t *val)
{
if (ioctl(fd, I2C_SLAVE, addr) < 0)
{
printf("IIC: Unable to select I2C device: %s\n", strerror(errno));
log_error("IIC: Unable to select I2C device: %s", strerror(errno));
return -1;
}

Expand All @@ -87,7 +88,7 @@ int uvm_i2c_i2cdetect(int fd, uint8_t addr)
{
if (ioctl(fd, I2C_SLAVE, addr) < 0)
{
printf("IIC: Unable to select I2C device: %s\n", strerror(errno));
log_error("IIC: Unable to select I2C device: %s", strerror(errno));
return -1;
}
return 0;
Expand Down
5 changes: 3 additions & 2 deletions hal/utils/pwm.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "log.h"
#include <errno.h>
#include <stdio.h>
#include <string.h>
Expand All @@ -21,7 +22,7 @@ int uvm_pwm_export(uint32_t pwm)
return ret;

if (!access(file, F_OK)) {
printf("warning: pwm %d, file (%s) has exist\n", pwm, file);
log_warn("pwm %d, file (%s) has exist", pwm, file);
return 0;
}
return write_sysfs_int("export", path, 0);
Expand Down Expand Up @@ -183,7 +184,7 @@ int uvm_pwm_deinit(uint32_t pwm) {

ret = uvm_pwm_unexport(pwm);
if (ret < 0) {
printf("uvm_pwm_unexport fail\n");
log_warn("pwm unexport fail");
return ret;
}
return 0;
Expand Down
Loading

0 comments on commit d91cd9e

Please sign in to comment.