forked from troublemake/qt_led
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio.cpp
executable file
·192 lines (154 loc) · 5.54 KB
/
gpio.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Copyright 2009 Cliff Brake <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <QDir>
#include <QtDebug>
#include <unistd.h>
#include <stdlib.h>
#include "gpio.h"
QString base_dir = "/sys/class/gpio";
// used to set the direction text
static QString direction_text[] = {
"in",
"out"
};
Gpio::Gpio(int gpio_number, GpioDir direction):
_dir(direction),
_number(gpio_number),
_value_file(NULL),
_value_stream(NULL)
{
_ready = false;
_sys_gpio = new QString(QString("%1/gpio%2").arg(base_dir).arg(_number));
_sys_gpio_value = new QString(QString("%1/value").arg(*_sys_gpio));
qDebug() << "_sys_gpio = " << *_sys_gpio << "\n";
qDebug() << "_sys_gpio_value = " << *_sys_gpio_value << "\n";
}
Gpio::~Gpio()
{
delete _sys_gpio;
delete _sys_gpio_value;
if (_value_file)
delete _value_file;
if (_value_stream)
delete _value_stream;
}
// export Gpio and return false if failed
bool Gpio::_export_gpio()
{
if (! QFile::exists(*_sys_gpio)) {
// try to export the GPIO
QString sys_gpio_export = QString("%1/export").arg(base_dir);
QFile export_file(sys_gpio_export);
if (! export_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qWarning() << "Error opening gpio export file for gpio" << _number << "\n";
return false;
}
QTextStream out(&export_file);
out << _number << "\n";
out.flush();
// Now try again to see if the gpio has been exported
if (! QFile::exists(*_sys_gpio)) {
qWarning() << "Error exporting GPIO" << _number << "\n";
return false;
}
}
return true;
}
bool Gpio::set_direction(GpioDir direction)
{
QString sys_gpio_direction = QString("%1/direction").arg(*_sys_gpio);
if (! QFile::exists(sys_gpio_direction)) {
qWarning() << "Error, GPIO direction file does not exist" << "\n";
return false;
}
QFile sys_gpio_direction_file(sys_gpio_direction);
if (! sys_gpio_direction_file.open(QIODevice::ReadWrite | QIODevice::Text)) {
qWarning() << "Error openning gpio direction for gpio " << _number << "\n";
return false;
}
QTextStream out(&sys_gpio_direction_file);
if (direction == GPIO_DIR_INPUT) {
out << "in" << "\n";
} else {
out << "out" << "\n";
}
_dir = direction;
return true;
}
GpioDir Gpio::get_direction()
{
QString sys_gpio_direction = QString("%1/direction").arg(*_sys_gpio);
if (! QFile::exists(sys_gpio_direction)) {
qWarning() << "Error, GPIO direction file does not exist" << "\n";
}
QFile sys_gpio_direction_file(sys_gpio_direction);
if (! sys_gpio_direction_file.open(QIODevice::ReadWrite | QIODevice::Text)) {
qWarning() << "Error openning gpio direction for gpio " << _number << "\n";
return GPIO_DIR_ERROR;
}
QTextStream in(&sys_gpio_direction_file);
QString value;
in >> value;
if (value.contains("out")) {
return GPIO_DIR_OUTPUT;
} else {
return GPIO_DIR_INPUT;
}
}
bool Gpio::init()
{
// first export the GPIO if required
if (! _export_gpio()) return false;
if (! set_direction(_dir)) return false;
// set up file handles for reading/writing value
_value_file = new QFile(*_sys_gpio_value);
if (! _value_file->open(QIODevice::ReadWrite | QIODevice::Text)) {
qWarning() << "Error opening value file for gpio" << _number << "\n";
return false;
}
_value_stream = new QTextStream(_value_file);
_ready = true;
return true;
}
GpioState Gpio::get_state()
{
if (! _ready)
return GPIO_STATE_ERROR;
_value_stream->seek(0);
int value;
*_value_stream >> value;
if (value == 0) {
return GPIO_STATE_LOW;
} else {
return GPIO_STATE_HIGH;
}
}
bool Gpio::set_state(GpioState state)
{
if (! _ready)
return false;
if (_dir != GPIO_DIR_OUTPUT)
return false;
_value_stream->seek(0);
if (state == GPIO_STATE_LOW) {
*_value_stream << 0;
} else {
*_value_stream << 1;
}
return true;
}