-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembedded_linux.c
85 lines (73 loc) · 1.54 KB
/
embedded_linux.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "embedded_linux.h"
void init_gpio(unsigned int gpio, unsigned int direction){
FILE* f = NULL;
f = fopen("/sys/class/gpio/export", "w");
fprintf(f, "%d", gpio);
fclose(f);
//DIRECTION
char path[50];
sprintf(path, "/sys/class/gpio/gpio%d/direction", gpio);
f = fopen(path, "w");
if(direction == OUTPUT){
fprintf(f, "out");
fclose(f);
}
else{
fprintf(f, "in");
fclose(f);
}
}
///INPUT
int get_value(unsigned int gpio){
FILE* f = NULL;
char leitura, path[50];
sprintf(path, "/sys/class/gpio/gpio%d/value", gpio);
f = fopen(path, "r");
leitura = fgetc(f);
fclose(f);
if(strncmp(&leitura, "1", 1) == 0){
return 1;
}else{
return 0;
}
}
//OUTPUT
void set_gpio_high(unsigned int gpio){
FILE* f = NULL;
char path[50];
sprintf(path, "/sys/class/gpio/gpio%d/value", gpio);
f = fopen(path, "w");
fprintf(f, "1");
fclose(f);
}
void set_gpio_low(unsigned int gpio){
FILE* f = NULL;
char path[50];
sprintf(path, "/sys/class/gpio/gpio%d/value", gpio);
f = fopen(path, "w");
fprintf(f, "0");
fclose(f);
}
void init_analog_pins(){
FILE* f = NULL;
char path[50];
f = fopen("/sys/devices/platform/bone_capemgr/slots", "w");
fprintf(f, "BB-ADC");
fclose(f);
usleep(500000);
}
int get_value_ain(unsigned int ain){
FILE* f = NULL;
char path[50], leitura[5];
sprintf(path, "/sys/bus/iio/devices/iio\:device0/in_voltage%d_raw", ain);
f = fopen(path, "r");
fgets(leitura, 4, f);
fclose(f);
int n = atoi(leitura);
printf("%d\n", n);
return n;
}