-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path3.cpp
More file actions
106 lines (84 loc) · 2.58 KB
/
3.cpp
File metadata and controls
106 lines (84 loc) · 2.58 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
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <iostream>
#include <fstream>
#include <fcntl.h>
#include <sys/ioctl.h>
using namespace std ;
class GPIO
{
private :
string default_path = "/sys/class/gpio/" ;
string path, export_path, unexport_path ;
int num ;
string num_str ;
int f_temp, status ;
int gpio_export() ;
int gpio_unexport() ;
public :
GPIO(int init_num) ;
int write_value(string value) ;
int set_dir(string dir) ;
~GPIO() ;
} ;
GPIO::GPIO(int init_num) {
path = default_path + "gpio" + to_string(init_num) ;
num = init_num ;
num_str = to_string(num) ;
gpio_export() ;
}
GPIO::~GPIO() {
gpio_unexport() ;
}
int GPIO::write_value(string value) {
string value_path = path + "/value" ;
f_temp = open(value_path.c_str(), O_WRONLY | O_CLOEXEC) ;
if (f_temp < 0) return f_temp ; // 파일 열기
status = write(f_temp, value.c_str(), value.length()) ;
if (status < 0) return status ; // 파일에 값 쓰기
close(f_temp) ; // 파일 닫기
return 0 ;
}
int GPIO::set_dir(string dir) {
string dir_path = path + "/direction" ;
f_temp = open(dir_path.c_str(), O_WRONLY | O_CLOEXEC) ;
cout << "f " << f_temp << endl ;
if (f_temp < 0) return f_temp ; // 파일 열기
status = write(f_temp, dir.c_str(), dir.length()) ;
cout << "fs " << status << endl ;
if (status < 0) return status ; // 파일에 값 쓰기
close(f_temp) ; // 파일 닫기
return 0 ;
}
int GPIO::gpio_export() {
export_path = default_path + "export" ;
f_temp = open(export_path.c_str(), O_WRONLY | O_CLOEXEC) ;
if (f_temp < 0) return f_temp ; // 파일 열기
status = write(f_temp, num_str.c_str(), num_str.length()) ;
if (status < 0) return status ; // 파일에 값 쓰기
close(f_temp) ; // 파일 닫기
return 0 ;
}
int GPIO::gpio_unexport() {
unexport_path = default_path + "unexport" ;
f_temp = open(unexport_path.c_str(), O_WRONLY | O_CLOEXEC) ;
if (f_temp < 0) return f_temp ; // 파일 열기
status = write(f_temp, num_str.c_str(), num_str.length()) ;
if (status < 0) return status ; // 파일에 값 쓰기
close(f_temp) ; // 파일 닫기
return 0 ;
}
int main(int argc, char* [])
{
int led_num = 76 ;
GPIO led = GPIO(led_num) ;
led.set_dir("out") ;
for (int i = 0; i < 10; i++) {
led.write_value("1") ;
usleep(500 * 1000) ;
led.write_value("0") ;
usleep(500 * 1000) ;
}
return 0 ;
}