main.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include "led_ctl.h"
  6. void usage(const char *prog)
  7. {
  8. printf("Usage: %s <command> [args]\n", prog);
  9. printf("Commands:\n");
  10. printf(" color <r|g> Set LED color\n");
  11. printf(" blink <on|off> [time] Set blink (time unit: 100ms, def: 5)\n");
  12. printf(" status Show current status\n");
  13. printf("\nExample:\n");
  14. printf(" sudo %s color r (Set color to RED)\n", prog);
  15. printf(" sudo %s blink on 5 (Blink every 500ms)\n", prog);
  16. }
  17. int main(int argc, char *argv[])
  18. {
  19. if (geteuid() != 0)
  20. {
  21. fprintf(stderr, "Error: Must run as root (sudo).\n");
  22. return 1;
  23. }
  24. if (led_driver_init() < 0)
  25. {
  26. return 1;
  27. }
  28. if (argc < 2)
  29. {
  30. usage(argv[0]);
  31. return 1;
  32. }
  33. const char *cmd = argv[1];
  34. if (strcmp(cmd, "color") == 0)
  35. {
  36. if (argc < 3)
  37. {
  38. fprintf(stderr, "Error: Missing color argument.\n");
  39. return 1;
  40. }
  41. if (strcmp(argv[2], "r") == 0)
  42. {
  43. if (led_set_color(LED_RED) == 0)
  44. printf("OK: Color set to RED\n");
  45. }
  46. else if (strcmp(argv[2], "g") == 0)
  47. {
  48. if (led_set_color(LED_GREEN) == 0)
  49. printf("OK: Color set to GREEN\n");
  50. }
  51. else
  52. {
  53. fprintf(stderr, "Error: Unknown color '%s'\n", argv[2]);
  54. return 1;
  55. }
  56. }
  57. else if (strcmp(cmd, "blink") == 0)
  58. {
  59. if (argc < 3)
  60. {
  61. fprintf(stderr, "Error: Missing state argument.\n");
  62. return 1;
  63. }
  64. if (strcmp(argv[2], "on") == 0)
  65. {
  66. int t = 5; // 500ms
  67. if (argc >= 4)
  68. t = atoi(argv[3]);
  69. if (t < 0 || t > 255)
  70. t = 5;
  71. if (led_set_blink(1, (uint8_t)t) == 0)
  72. printf("OK: Blink ON (Interval: %d00ms)\n", t);
  73. }
  74. else if (strcmp(argv[2], "off") == 0)
  75. {
  76. if (led_set_blink(0, 0) == 0)
  77. printf("OK: Blink OFF\n");
  78. }
  79. else
  80. {
  81. fprintf(stderr, "Error: Unknown state '%s'\n", argv[2]);
  82. return 1;
  83. }
  84. }
  85. else if (strcmp(cmd, "status") == 0)
  86. {
  87. led_print_status();
  88. }
  89. else
  90. {
  91. usage(argv[0]);
  92. return 1;
  93. }
  94. return 0;
  95. }