-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path9-2.cpp
More file actions
75 lines (62 loc) · 1.93 KB
/
Copy path9-2.cpp
File metadata and controls
75 lines (62 loc) · 1.93 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
#include "GPIO.h"
#include <unistd.h>
#include <iostream>
#include <string>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <chrono>
using namespace std ;
using namespace std::chrono ;
int main(int argc, char* [])
{
int led_num = 76 ;
int btn_num = 74 ;
char buffer[10] ;
int status ;
system_clock::time_point start, end, timeout ;
microseconds micro ;
GPIO led = GPIO(led_num) ;
GPIO echo = GPIO(74) ;
GPIO trig = GPIO(75) ;
status = trig.set_dir("out") ;
cout << "trig out " << status << endl ;
if (status < 0) return -1 ;
status = echo.set_dir("in") ;
cout << "echo in " << status << endl ;
if (status < 0) return -1 ;
trig.write_value("1") ;
usleep(10) ;
trig.write_value("0") ;
timeout = system_clock::now() ; // 최대 대기 시간을 위한 값
while (1) { // HIGH까지 대기
echo.read_value(buffer, 1) ;
if (buffer[0] == '1') {
start = system_clock::now() ;
break ;
} else {
micro = duration_cast<microseconds>(system_clock::now() - timeout) ;
if (micro.count() > 1000 * 1000) {
cout << "HIGH timeout" << micro.count() << endl ;
return -1 ;
}
}
}
cout << "LOW " << endl ;
timeout = system_clock::now() ; // 최대 대기 시간을 위한 값
while (1) { // LOW까지 대기
echo.read_value(buffer, 1) ;
if (buffer[0] == '0') {
end = system_clock::now() ;
break ;
} else {
micro = duration_cast<microseconds>(system_clock::now() - timeout) ;
if (micro.count() > 1000 * 1000) {
cout << "LOW timeout" << endl ;
return -1 ;
}
}
}
micro = duration_cast<microseconds>(end - start) ;
cout << "length " << micro.count() / 58 << " cm" << endl ;
return 0 ;
}