-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathblink.c
More file actions
102 lines (85 loc) · 3.04 KB
/
Copy pathblink.c
File metadata and controls
102 lines (85 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* blink.c - CHIP program to blink status light and shut down when XIO-P7 is grounded. */
/* THIS PROGRAM IS NOT THE OFFICIAL BLINK PROGRAM! It is a very old verison
* and is only retained as an example of a C program. See blink.sh
*/
/* This work is dedicated to the public domain under CC0 1.0 Universal:
* http://creativecommons.org/publicdomain/zero/1.0/
*
* To the extent possible under law, Steven Ford has waived all copyright
* and related or neighboring rights to this work. In other words, you can
* use this code for any purpose without any restrictions.
* This work is published from: United States.
* Project home: https://github.com/fordsfords/blink
*/
#include <fcntl.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
/* See http://wiki.geeky-boy.com/w/index.php?title=Internal_error_handling */
#define WCHK(cond_expr) do { \
if (!(cond_expr)) { \
fprintf(stderr, "%s:%d, Warning, expected '%s' to be true (%s)\n", \
__FILE__, __LINE__, #cond_expr, strerror(errno)); \
} \
} while (0)
#define ECHK(cond_expr) do { \
if (!(cond_expr)) { \
fprintf(stderr, "%s:%d, Error, expected '%s' to be true (%s)\n", \
__FILE__, __LINE__, #cond_expr, strerror(errno)); \
abort(); \
} \
} while (0)
int main ( int argc, char *argv[] )
{
/* Store this process's PID to allow easy killing. */
FILE *pid_file = fopen("/tmp/blink.pid", "w"); ECHK(pid_file != NULL);
fprintf(pid_file, "%ld\n", (long)getpid());
fclose(pid_file);
/* initialize */
int ton;
int toff;
/* Check for parameters */
if(argc > 2) {
ton = strtol(argv[1], NULL, 10);
toff = strtol(argv[2], NULL, 10);
} else {
ton = 1000000; /* 1 million microseconds - 1 second of sleep */
toff = ton;
}
/* Enable and configure XIO-P7 as input. */
int exp_fd = open("/sys/class/gpio/export", O_WRONLY); ECHK(exp_fd != -1);
/* If port 7 is already exported, the following will fail. That's OK, but warn. */
int wlen = write(exp_fd, "415", 4); WCHK(wlen == 4);
close(exp_fd);
int dir_fd = open("/sys/class/gpio/gpio415/direction", O_RDWR); ECHK(dir_fd != -1);
wlen = write(dir_fd, "in", 3); ECHK(wlen == 3);
close(dir_fd);
int led = 0;
char readbuf[99] = "";
do { /* while readbuf */
if (led) {
int status = system("/usr/sbin/i2cset -f -y 0 0x34 0x93 0x1"); ECHK(status == 0);
} else {
int status = system("/usr/sbin/i2cset -f -y 0 0x34 0x93 0x0"); ECHK(status == 0);
}
led = ! led;
int val_fd = open("/sys/class/gpio/gpio415/value", O_RDWR); ECHK(val_fd != -1);
int rlen = read(val_fd, readbuf, sizeof(readbuf)); ECHK(rlen > 0);
close(val_fd);
if(led) {
usleep(toff);
} else {
usleep(ton);
}
} while (readbuf[0] == '1');
exp_fd = open("/sys/class/gpio/unexport", O_WRONLY); ECHK(exp_fd != -1);
wlen = write(exp_fd, "415", 4); ECHK(wlen == 4);
close(exp_fd);
int status = system("/sbin/shutdown now"); ECHK(status == 0);
sleep(5);
return 0;
} /* main */