| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include "led_ctl.h"
- void usage(const char *prog)
- {
- printf("Usage: %s <command> [args]\n", prog);
- printf("Commands:\n");
- printf(" color <r|g> Set LED color\n");
- printf(" blink <on|off> [time] Set blink (time unit: 100ms, def: 5)\n");
- printf(" status Show current status\n");
- printf("\nExample:\n");
- printf(" sudo %s color r (Set color to RED)\n", prog);
- printf(" sudo %s blink on 5 (Blink every 500ms)\n", prog);
- }
- int main(int argc, char *argv[])
- {
- if (geteuid() != 0)
- {
- fprintf(stderr, "Error: Must run as root (sudo).\n");
- return 1;
- }
- if (led_driver_init() < 0)
- {
- return 1;
- }
- if (argc < 2)
- {
- usage(argv[0]);
- return 1;
- }
- const char *cmd = argv[1];
- if (strcmp(cmd, "color") == 0)
- {
- if (argc < 3)
- {
- fprintf(stderr, "Error: Missing color argument.\n");
- return 1;
- }
- if (strcmp(argv[2], "r") == 0)
- {
- if (led_set_color(LED_RED) == 0)
- printf("OK: Color set to RED\n");
- }
- else if (strcmp(argv[2], "g") == 0)
- {
- if (led_set_color(LED_GREEN) == 0)
- printf("OK: Color set to GREEN\n");
- }
- else
- {
- fprintf(stderr, "Error: Unknown color '%s'\n", argv[2]);
- return 1;
- }
- }
- else if (strcmp(cmd, "blink") == 0)
- {
- if (argc < 3)
- {
- fprintf(stderr, "Error: Missing state argument.\n");
- return 1;
- }
- if (strcmp(argv[2], "on") == 0)
- {
- int t = 5; // 500ms
- if (argc >= 4)
- t = atoi(argv[3]);
- if (t < 0 || t > 255)
- t = 5;
- if (led_set_blink(1, (uint8_t)t) == 0)
- printf("OK: Blink ON (Interval: %d00ms)\n", t);
- }
- else if (strcmp(argv[2], "off") == 0)
- {
- if (led_set_blink(0, 0) == 0)
- printf("OK: Blink OFF\n");
- }
- else
- {
- fprintf(stderr, "Error: Unknown state '%s'\n", argv[2]);
- return 1;
- }
- }
- else if (strcmp(cmd, "status") == 0)
- {
- led_print_status();
- }
- else
- {
- usage(argv[0]);
- return 1;
- }
- return 0;
- }
|