Created
January 7, 2012 22:53
-
-
Save revmischa/1576363 to your computer and use it in GitHub Desktop.
TP-LINK WR703n LED Blinker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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