setss.c 3.6 KB

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