-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathled-test.c
More file actions
40 lines (31 loc) · 771 Bytes
/
led-test.c
File metadata and controls
40 lines (31 loc) · 771 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#define FILE_NAME "/dev/simple-led"
#define MSG_LEN 1
#define BUF_SIZE (MSG_LEN + 1)
int main()
{
int fd;
char out_buffer[BUF_SIZE] = "";
char in_buffer[BUF_SIZE] = "";
fd = open(FILE_NAME, O_RDWR | O_SYNC);
if (fd < 0) {
perror("Failed to open device");
exit(errno);
}
read(fd, in_buffer, MSG_LEN);
printf("LED initial state: %s\n", in_buffer);
if (!fgets(out_buffer, BUF_SIZE, stdin)) {
printf("Could not read value. Exiting.\n");
exit(-EIO);
}
// TODO: Check return values for read, write, close
write(fd, out_buffer, MSG_LEN);
read(fd, in_buffer, MSG_LEN);
printf("LED final state: %s\n", in_buffer);
close(fd);
return EXIT_SUCCESS;
}