我需要在运行时生成三种不同类型的路径:
- /sys/class/gpio/gpio%d
- /sys/class/gpio/gpio%d/值
- /sys/class/gpio/gpio%d/方向
目前我通过执行以下操作生成这些:
#define GPIO_PATH_BASE "/sys/class/gpio/gpio"
#define GPIO_PATH_DIRECTION "/direction"
#define GPIO_PATH_VALUE "/value"
int open_gpio(const char * port) {
char * base_path = (char *) malloc(sizeof(GPIO_PATH_BASE) + sizeof(port));
strcpy(base_path, GPIO_PATH_BASE);
strcat(base_path, port);
char * value_path = (char *) malloc(sizeof(base_path) + sizeof(GPIO_PATH_VALUE));
strcpy(value_path, (const char *) base_path);
strcat(value_path, GPIO_PATH_VALUE);
char * dir_path = (char *) malloc(sizeof(base_path) + sizeof(GPIO_PATH_DIRECTION));
strcpy(dir_path, (const char *) base_path);
strcat(dir_path, GPIO_PATH_DIRECTION);
}
我实际上对这种方法很不满意。是否有可能让宏为我提供这些东西,或者我应该创建一个辅助函数?
博多