-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGPIO.cpp
More file actions
84 lines (62 loc) · 2.16 KB
/
Copy pathGPIO.cpp
File metadata and controls
84 lines (62 loc) · 2.16 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
#include "GPIO.h"
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <iostream>
#include <fstream>
#include <fcntl.h>
#include <sys/ioctl.h>
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::read_value(char* buffer, int len) {
string value_path = path + "/value" ;
f_temp = open(value_path.c_str(), O_RDONLY | O_CLOEXEC) ;
if (f_temp < 0) return f_temp ; // 파일 열기
status = read(f_temp, buffer, len) ;
if (status < 0) return status ;
close(f_temp) ;
return 0 ;
}
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) ;
if (f_temp < 0) return f_temp ; // 파일 열기
status = write(f_temp, dir.c_str(), dir.length()) ;
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 ;
}