| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <errno.h>
- #include <getopt.h>
- #if defined(PROJECT) && (PROJECT == POS)
- #define DEVICE_PATH "/dev/leds"
- #else
- #define DEVICE_PATH "/dev/ssegment"
- #endif
- int write_data_to_device(unsigned int data)
- {
- int fd;
- ssize_t ret;
- // 打开设备
- fd = open(DEVICE_PATH, O_WRONLY);
- if (fd < 0)
- {
- return 1;
- }
- // 写入1字节
- ret = write(fd, &data, sizeof(data));
- if (ret < 0)
- {
- close(fd);
- return 1;
- }
- close(fd);
- return 0;
- }
- int read_from_device(unsigned int *data)
- {
- int fd;
- unsigned char buffer[256];
- ssize_t ret;
- int i;
- fd = open(DEVICE_PATH, O_RDWR);
- if (fd < 0)
- {
- return 1;
- }
- ret = read(fd, data, sizeof(unsigned int));
- if (ret < 0)
- {
- return 1;
- }
- close(fd);
- return 0;
- }
- int main(int argc, char *argv[])
- {
- int ret = 0;
- int opt;
- int flash_option = 0;
- int flash_value = 0;
- int decimal_point = 0;
- int read_option = 0;
- char *hex_arg = NULL;
- unsigned char data;
- unsigned int rw_data = 0;
- // 如果没有参数,显示帮助
- if (argc == 1)
- {
- return 1;
- }
- // 解析命令行参数
- optind = 1; // 重置 getopt
- while ((opt = getopt(argc, argv, "f:dr")) != -1)
- {
- switch (opt)
- {
- case 'f':
- flash_option = 1;
- flash_value = atoi(optarg);
- if (flash_value < 0)
- {
- fprintf(stderr, "错误: -f 参数必须是大于0的数字\n");
- return 1;
- }
- break;
- case 'd':
- decimal_point = 1;
- break;
- case 'r':
- read_option = 1;
- break;
- default:
- printf("error\n");
- return 1;
- }
- }
- // 检查 -r 选项的特殊规则
- if (read_option)
- {
- rw_data = 0;
- ret = read_from_device(&rw_data);
- data = rw_data & 0xFF;
- printf("The number displayed is %02x\n", data);
- if(rw_data & (1<<9))
- {
- printf("The msb is ON\n");
- }
- else
- {
- printf("The msb is OFF\n");
- }
- if(rw_data & (1<<15))
- {
- printf("The flashing is ON\n");
- }
- else
- {
- printf("The flashing is OFF\n");
- }
- return 0;
- }
- // 非 -r 选项,必须有最后的十六进制参数
- if (optind >= argc)
- {
- fprintf(stderr, "error\n");
- return 1;
- }
- hex_arg = argv[optind];
- // 解析十六进制参数 (支持 aa, bb, 12 等格式)
- char *endptr;
- long hex_value = strtol(hex_arg, &endptr, 16);
- // 检查转换是否成功
- if (*endptr != '\0' || endptr == hex_arg)
- {
- fprintf(stderr, "错误: 无效的十六进制参数 '%s'\n", hex_arg);
- fprintf(stderr, "请使用类似 aa, bb, 12, 0x12 的格式\n");
- return 1;
- }
- // 检查范围 (0-255)
- if (hex_value < 0 || hex_value > 255)
- {
- fprintf(stderr, "错误: 十六进制值必须在 0x00-0xFF 之间 (当前: 0x%lX)\n", hex_value);
- return 1;
- }
- data = (unsigned char)hex_value;
- if (flash_value == 1)
- {
- rw_data |= 1 << 15;
- }
- if (decimal_point == 1)
- {
- rw_data |= 1 << 9;
- }
- rw_data |= data;
- ret = write_data_to_device(rw_data);
- return 0;
- }
|