-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpioFileSystem.h
More file actions
68 lines (62 loc) · 1.68 KB
/
gpioFileSystem.h
File metadata and controls
68 lines (62 loc) · 1.68 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
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
void exportPin(int pin){
int fd;
char buf[100];
//Export the specific GPIO
//this will create a new folder with the gpio name
sprintf(buf, "%d", pin);
fd = open("/sys/class/gpio/export", O_WRONLY);
if (fd < 0){
perror("Unable to open /sys/class/gpio/export");
}
write(fd, buf, strlen(buf));
close(fd);
return;
}
void setDirectionOut(int pin){
// Set the pin to be an output by writing "out" to /sys/class/gpio/gpio24/direction
int fd;
char buf[100];
//Set the direction of the GPIO to output
sprintf(buf, "/sys/class/gpio/gpio%d/direction", pin);
fd = open(buf, O_WRONLY);
if (fd < 0){
perror("Unable to open /sys/class/gpio/export");
}
write(fd, "out", 3); // Set out direction
close(fd);
}
void setDirectionIn(int pin){
// Set the pin to be an output by writing "in" to /sys/class/gpio/gpio24/direction
int fd;
char buf[100];
//Set the direction of the GPIO to output
sprintf(buf, "/sys/class/gpio/gpio%d/direction", pin);
fd = open(buf, O_WRONLY);
if (fd < 0){
perror("Unable to open /sys/class/gpio/export");
}
write(fd, "in", 2); // Set in direction
close(fd);
}
void writePin(int fd, int pin, char val){
if (fd == -1) {
perror("Unable to open /sys/class/gpio/export");
return;
}
write(fd, &val, 1);
}
void readPin(int fd, int pin, char &val){
if (fd == -1) {
perror("Unable to open /sys/class/gpio/export");
return;
}
read(fd, &val, 1);
}