-
Notifications
You must be signed in to change notification settings - Fork 2
/
gpio.c
46 lines (35 loc) · 931 Bytes
/
gpio.c
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
#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include "api/kgpio.h"
#include "gpio.h"
static int gpio_dev = -1;
static void gpio_set_led (const led *l, bool on);
int init_gpio (void) {
if ((gpio_dev = open("/dev/gpio", O_RDONLY)) == -1) {
perror("Failed to open /dev/gpio");
return -1;
}
return 0;
}
void gpio_set_brightness (const led *l, int percent) {
if (percent >= 10)
gpio_set_led(l, true);
else
gpio_set_led(l, false);
}
static void gpio_set_led (const led *l, bool on) {
galois_gpio_data_t port_state = {0};
port_state.mode = GPIO_MODE_DATA_OUT;
port_state.port = l->gpio_pin;
port_state.data = on ? 1 : 0;
if (ioctl(gpio_dev, GPIO_IOCTL_SET, &port_state) == -1) {
perror("Failed to set GPIO direction");
exit(EXIT_FAILURE);
}
if (ioctl(gpio_dev, GPIO_IOCTL_WRITE, &port_state) == -1) {
perror("Failed to set GPIO state");
exit(EXIT_FAILURE);
}
}