-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpiopin.cpp
97 lines (74 loc) · 1.54 KB
/
gpiopin.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
#include <QDebug>
#include "gpiopin.h"
#include "gpiocontroller.h"
GpioPin::GpioPin(QObject *parent) : QObject(parent)
, m_num(-1), m_dir("in"), m_val(-1), m_pinController(GpioController::instance())
{
connect(&m_poll, &QTimer::timeout, this, &GpioPin::onPinPoll );
}
GpioPin::~GpioPin()
{
}
QString GpioPin::direction()
{
m_dir = m_pinController.direction(m_num);
return m_dir;
}
void GpioPin::setDirection(const QString &dir)
{
m_dir = dir;
m_pinController.setDirection(m_num, dir);
emit pinDirectionChanged(dir);
}
int GpioPin::value()
{
m_val = m_pinController.pin(m_num);
return m_val;
}
void GpioPin::setValue(int val)
{
if (m_dir.compare("in") == 0) {
qDebug() << "cannot set input pin" << m_num << " value";
return;
}
m_val = val;
m_pinController.setPin(m_num, val);
emit pinValueChanged(val);
}
void GpioPin::onPinPoll()
{
auto val = m_pinController.pin(m_num);
if (val == m_val) {
/* no change */
return;
}
m_val = val;
emit pinValueChanged(val);
}
int GpioPin::number()
{
return m_num;
}
void GpioPin::setNumber(int val)
{
if (m_num >= 0) {
qDebug() << "Pin number " << val << "is invalid";
return;
}
m_num = val;
}
bool GpioPin::poll()
{
return m_poll.isActive();
}
void GpioPin::setPoll(bool poll)
{
if (m_pinController.direction(m_num) == "out") {
qDebug() << "Cannot poll output pin";
return;
}
m_poll.stop();
if (poll) {
m_poll.start(1000);
}
}