-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.c
More file actions
120 lines (98 loc) · 2.44 KB
/
Copy pathbutton.c
File metadata and controls
120 lines (98 loc) · 2.44 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include "button.h"
#define WRITE "w"
#define LED_OPEN_ERROR "ERROR OPENING LED0 BRIGHTNESS FILE!\n"
#define WRITE_ERROR "ERROR WRITENG DATA!\n"
#define DIRECTION "in"
#define DIRECTION_PATH "/sys/class/gpio/gpio31/direction"
#define VALUE_PATH "/sys/class/gpio/gpio31/value"
#define READ "r"
#define READ_ERROR "error, unable to read value.\n"
#define EXPORT "/sys/class/gpio/export"
#define WRITE_PATH "w"
#define EXPORT_ERROR "Error: UNable to open export file.\n"
static int button_status = 0;
//static struct timespec ts = {0, 100000000L };
static void export_gpio(int gpio_number);
static void get_button_enable();
static void export_gpio(int gpio_number){
FILE *pfile = fopen (EXPORT, WRITE_PATH);
if (pfile == NULL){
printf (EXPORT_ERROR);
exit(1);
}
fprintf(pfile, "%d", gpio_number);
fclose(pfile);
}
void change_button_direction(){
export_gpio(31);
FILE *fileLED = fopen(DIRECTION_PATH, WRITE);
if (fileLED == NULL){
printf(LED_OPEN_ERROR);
return;
}
int charWritten = fprintf(fileLED, DIRECTION);
if (charWritten <= 0){
printf(WRITE_ERROR);
}
fclose(fileLED);
}
static void get_button_enable(){
FILE *file = fopen(VALUE_PATH,READ);
if (file == NULL){
printf(READ_ERROR);
exit(-1);
}
const int max_length = 1024;
char buff[max_length];
fgets(buff, max_length, file);
fclose(file);
if (buff[0] == '0'){
//printf("buff 1\n");
_Bool button_back = 1;
while(button_back){
FILE *file = fopen(VALUE_PATH,READ);
if (file == NULL){
printf(READ_ERROR);
exit(-1);
}
const int max_length = 1024;
char buff[max_length];
fgets(buff, max_length, file);
fclose(file);
if (buff[0] == '1'){
button_status = 1;
return;
}
}
}
button_status = 0;
return;
}
int get_button_status(){
return button_status;
}
int button_thread(){
get_button_enable();
// for (int i = 0; i < 1; i++){
// nanosleep (&ts, NULL);
// }
// printf("%d\n",get_button_status() );
if (get_button_status()){
return 1;
}
return 0;
}
void button_init(){
change_button_direction();
}
// int main(){
// button_init();
// button_cleanup();
// return 0;
// }