-
Notifications
You must be signed in to change notification settings - Fork 0
/
8-2.cpp
105 lines (79 loc) · 2.45 KB
/
8-2.cpp
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
#include <iostream>
#include <fstream>
#include <string>
#include <unistd.h>
#include <fcntl.h>
using namespace std ;
#define GPIO_PATH "/sys/class/gpio/gpio"
#define GPIO_EXPORT "/sys/class/gpio/export"
#define GPIO_UNEXPORT "/sys/class/gpio/unexport"
int write_to_path(const char* path, string value)
{
int f ;
cout << path << endl ;
f = open(path, O_WRONLY | O_CLOEXEC) ;
if (f < 0) {
return -1 ;
}
write(f, value.c_str(), value.length()) ;
close(f) ;
return 0 ;
}
int main(int argc, char* [])
{
int f_export, f_unexport, status ;
string echo = "113" ;
string trig = "116" ;
f_export = open(GPIO_EXPORT, O_WRONLY | O_CLOEXEC) ;
cout << f_export << endl ;
if (f_export < 0) {
return -1 ;
}
status = write(f_export, echo.c_str(), echo.length()) ;
cout << "echo set " << status << endl ;
status = write(f_export, trig.c_str(), trig.length()) ;
cout << "trig set " << status << endl ;
close(f_export) ;
status = write_to_path((GPIO_PATH + trig + "/direction").c_str(), "out") ;
cout << "function " << status << endl ;
status = write_to_path((GPIO_PATH + trig + "/value").c_str(), "1") ;
cout << "trig " << status << endl ;
usleep(10) ;
status = write_to_path((GPIO_PATH + trig + "/value").c_str(), "0") ;
cout << "trig " << status << endl ;
// read_from_path
int f_trig ;
char buf[5] = "" ;
f_trig = open((GPIO_PATH + echo + "/value").c_str(), O_RDONLY | O_CLOEXEC) ;
if (f_trig < 0) {
return f_trig ;
}
while (1) {
read(f_trig, buf, 1) ;
cout << buf << endl ;
if (buf[0] == '1') {
break ;
}
}
// status = read(f_trig, buf, 1) ;
// cout << "read " << status << " " << buf << endl ;
// High까지 대기, 타이머시작
// LOW 까지 대기, 타이머 종료
// f_unexport = open(GPIO_UNEXPORT, O_WRONLY | O_CLOEXEC) ;
// cout << f_unexport << endl ;
// if (f_export < 0) {
// return -1 ;
// }
// status = write(f_unexport, echo.c_str(), echo.length()) ;
// cout << status << endl ;
// status = write(f_unexportf_export, trig.c_str(), trig.length()) ;
// cout << status << endl ;
// close(f_unexport) ;
return 0 ;
}
// 113 - echo
// 116 - trig
// export로 경로 추가
// trig direction 바꾸기
// trig에 10us high
// echo 시간값으로 읽기