#include <stdio.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <signal.h> | |
static const char* led_file = "/sys/devices/platform/leds-gpio/leds/tp-link:blue:system/brightness"; | |
FILE *led_fh; | |
void cleanup(int); | |
int main() { | |
uint8_t write_val[2]; | |
char err_msg[512]; | |
uint8_t toggle = 0; | |
// open LED brightness device file for writing | |
led_fh = fopen(led_file, "wb"); | |
if (led_fh == NULL) { | |
sprintf(err_msg, "Cannot open LED device file %s", led_file); | |
perror(err_msg); | |
exit(1); | |
} | |
// catch interrupt signal | |
struct sigaction int_handler = { | |
.sa_flags = 0, | |
.sa_handler = cleanup | |
}; | |
sigemptyset(&int_handler.sa_mask); | |
sigaction(SIGINT, &int_handler, NULL); | |
// blink forever | |
while (1) { | |
strcpy(write_val, toggle ? "1" : "0"); | |
printf("Writing %s\n", write_val); | |
rewind(led_fh); | |
fprintf(led_fh, "%s\n", write_val); | |
toggle = ~toggle; | |
usleep(1000 * 40); // sleep 40msec | |
} | |
} | |
// called at interrupt | |
void cleanup(int s) { | |
printf("Cleaning up...\n"); | |
fclose(led_fh); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment