setss.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <getopt.h>
  8. #define DEVICE_PATH "/dev/ssegment"
  9. int write_data_to_device(unsigned int data)
  10. {
  11. int fd;
  12. ssize_t ret;
  13. // 打开设备
  14. fd = open(DEVICE_PATH, O_WRONLY);
  15. if (fd < 0)
  16. {
  17. return 1;
  18. }
  19. // 写入1字节
  20. ret = write(fd, &data, sizeof(data));
  21. if (ret < 0)
  22. {
  23. close(fd);
  24. return 1;
  25. }
  26. close(fd);
  27. return 0;
  28. }
  29. int read_from_device(unsigned int *data)
  30. {
  31. int fd;
  32. unsigned char buffer[256];
  33. ssize_t ret;
  34. int i;
  35. fd = open(DEVICE_PATH, O_RDWR);
  36. if (fd < 0)
  37. {
  38. return 1;
  39. }
  40. ret = read(fd, data, sizeof(unsigned int));
  41. if (ret < 0)
  42. {
  43. return 1;
  44. }
  45. close(fd);
  46. return 0;
  47. }
  48. int main(int argc, char *argv[])
  49. {
  50. int ret = 0;
  51. int opt;
  52. int flash_option = 0;
  53. int flash_value = 0;
  54. int decimal_point = 0;
  55. int read_option = 0;
  56. char *hex_arg = NULL;
  57. unsigned char data;
  58. unsigned int rw_data = 0;
  59. // 如果没有参数,显示帮助
  60. if (argc == 1)
  61. {
  62. return 1;
  63. }
  64. // 解析命令行参数
  65. optind = 1; // 重置 getopt
  66. while ((opt = getopt(argc, argv, "f:dr")) != -1)
  67. {
  68. switch (opt)
  69. {
  70. case 'f':
  71. flash_option = 1;
  72. flash_value = atoi(optarg);
  73. if (flash_value < 0)
  74. {
  75. fprintf(stderr, "错误: -f 参数必须是大于0的数字\n");
  76. return 1;
  77. }
  78. break;
  79. case 'd':
  80. decimal_point = 1;
  81. break;
  82. case 'r':
  83. read_option = 1;
  84. break;
  85. default:
  86. printf("error\n");
  87. return 1;
  88. }
  89. }
  90. // 检查 -r 选项的特殊规则
  91. if (read_option)
  92. {
  93. rw_data = 0;
  94. ret = read_from_device(&rw_data);
  95. data = rw_data & 0xFF;
  96. printf("The number displayed is %02x\n", data);
  97. if(rw_data & (1<<9))
  98. {
  99. printf("The msb is ON\n");
  100. }
  101. else
  102. {
  103. printf("The msb is OFF\n");
  104. }
  105. if(rw_data & (1<<15))
  106. {
  107. printf("The flashing is ON\n");
  108. }
  109. else
  110. {
  111. printf("The flashing is OFF\n");
  112. }
  113. return 0;
  114. }
  115. // 非 -r 选项,必须有最后的十六进制参数
  116. if (optind >= argc)
  117. {
  118. fprintf(stderr, "error\n");
  119. return 1;
  120. }
  121. hex_arg = argv[optind];
  122. // 解析十六进制参数 (支持 aa, bb, 12 等格式)
  123. char *endptr;
  124. long hex_value = strtol(hex_arg, &endptr, 16);
  125. // 检查转换是否成功
  126. if (*endptr != '\0' || endptr == hex_arg)
  127. {
  128. fprintf(stderr, "错误: 无效的十六进制参数 '%s'\n", hex_arg);
  129. fprintf(stderr, "请使用类似 aa, bb, 12, 0x12 的格式\n");
  130. return 1;
  131. }
  132. // 检查范围 (0-255)
  133. if (hex_value < 0 || hex_value > 255)
  134. {
  135. fprintf(stderr, "错误: 十六进制值必须在 0x00-0xFF 之间 (当前: 0x%lX)\n", hex_value);
  136. return 1;
  137. }
  138. data = (unsigned char)hex_value;
  139. if (flash_value == 1)
  140. {
  141. rw_data |= 1 << 15;
  142. }
  143. if (decimal_point == 1)
  144. {
  145. rw_data |= 1 << 9;
  146. }
  147. rw_data |= data;
  148. ret = write_data_to_device(rw_data);
  149. return 0;
  150. }