-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainapplication.cpp
More file actions
185 lines (173 loc) · 5.52 KB
/
mainapplication.cpp
File metadata and controls
185 lines (173 loc) · 5.52 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
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
#include "mainapplication.h"
/**
* @brief MainApplication::MainApplication
* @author Louis P. Meadows
* @date October 4th 2019
* @version 1.0.0
* @param parent
*/
MainApplication::MainApplication(QObject* parent) : QObject(parent)
{
gpio = new GPIO();
gpioProcess = new QProcess();
connect(gpioProcess, SIGNAL(started()), this, SLOT(startedGPIO()));
connect(gpioProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this,
SLOT(onFinish(int, QProcess::ExitStatus)));
connect(gpioProcess, SIGNAL(readyReadStandardOutput()), this,
SLOT(readGPIO()));
connect(gpioProcess, SIGNAL(readyReadStandardError()), this,
SLOT(readGPIOerror()));
connect(gpioProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this,
SLOT(stateChanged(QProcess::ProcessState)));
qDebug() << __LINE__ << __FUNCTION__ << "Setting 0,0";
setGPIO(0, 0);
gpioProcess->waitForFinished(-1);
qDebug() << __LINE__ << __FUNCTION__ << getGPIO();
qDebug() << __LINE__ << __FUNCTION__ << "Setting 0,1";
setGPIO(0, 1);
gpioProcess->waitForFinished(-1);
qDebug() << __LINE__ << __FUNCTION__ << getGPIO();
qDebug() << __LINE__ << __FUNCTION__ << "Setting 1,0";
setGPIO(1, 0);
gpioProcess->waitForFinished(-1);
qDebug() << __LINE__ << __FUNCTION__ << getGPIO();
qDebug() << __LINE__ << __FUNCTION__ << "Setting 1,1";
setGPIO(1, 1);
gpioProcess->waitForFinished(-1);
qDebug() << __LINE__ << __FUNCTION__ << getGPIO();
}
/**
* @brief MainApplication::stateChanged
* @author Louis P. Meadows
* @param newstate
*/
void MainApplication::stateChanged(QProcess::ProcessState newstate)
{
switch (newstate)
{
case QProcess::NotRunning:
qDebug() << __LINE__ << __FUNCTION__ << "The GPIO process is not running.";
break;
case QProcess::Starting:
qDebug() << __LINE__ << __FUNCTION__
<< "The GPIO process is starting, but the program has not yet "
"been invoked.";
break;
case QProcess::Running:
qDebug()
<< __LINE__ << __FUNCTION__
<< "The GPIO process is running and is ready for reading and writing.";
break;
}
}
/**
* @brief MainApplication::readGPIOerror
* @author Louis P. Meadows
*/
void MainApplication::readGPIOerror()
{
gpioErrorResponse = gpioProcess->readAllStandardError();
qDebug() << __LINE__ << __FUNCTION__ << gpioErrorResponse;
}
/**
* @brief MainApplication::readGPIO
* @author Louis P. Meadows
*/
void MainApplication::readGPIO()
{
gpioResponse = gpioProcess->readAllStandardOutput();
qDebug() << __LINE__ << __FUNCTION__ << gpioResponse;
}
/**
* @brief MainApplication::onFinish
* @param exitCode
* @param exitStatus
* @author Louis P. Meadows
*/
void MainApplication::onFinish(int exitCode, QProcess::ExitStatus exitStatus)
{
qDebug() << __LINE__ << __FUNCTION__ << "GPIO finished:";
qDebug("Exit code: %i", exitCode);
qDebug("Exit status: %i", exitStatus);
switch (exitStatus)
{
case QProcess::NormalExit:
qDebug() << __LINE__ << __FUNCTION__ << "The GPIO process exited normally.";
break;
case QProcess::CrashExit:
qDebug() << __LINE__ << __FUNCTION__ << "The GPIO process crashed.";
break;
}
}
/**
* @brief MainApplication::startedGPIO
* @author Louis P. Meadows
*/
void MainApplication::startedGPIO()
{
qDebug() << __LINE__ << __FUNCTION__ << "Started GPIO process.";
}
/**
* @brief MainApplication::getGPIO
* @author Louis P. Meadows
* @return
*/
QString MainApplication::getGPIO()
{
QString pins;
QString command = "i2cget";
QStringList arguments;
arguments << "-y";
arguments << "0";
arguments << "0x3E";
arguments << "0";
gpioProcess->setProgram(command);
gpioProcess->setArguments(arguments);
gpioProcess->start();
gpioProcess->waitForReadyRead();
pins = gpioProcess->readAllStandardOutput();
gpioProcess->waitForFinished(-1);
return pins;
}
/**
* @brief MainApplication::setGPIO
* @param pin3
* @param pin4
*/
void MainApplication::setGPIO(bool pin3, bool pin4)
{
// pin 0 & 1 input from the controller
// pin 2 & 3 output to the controller
QString command = "i2cset";
QStringList arguments;
arguments << "-y -r 0 0x3E 1 ";
arguments << "-r";
arguments << "0";
arguments << "0x3E";
arguments << "1";
if ((pin3) && (pin4))
{
arguments << "12";
}
if ((!pin3) && (pin4))
{
arguments << "8";
}
if ((pin3) && (!pin4))
{
arguments << "4";
}
if ((!pin3) && (!pin4))
{
arguments << "0";
}
gpioProcess->setProgram(command);
gpioProcess->setArguments(arguments);
while (gpioProcess->state() == QProcess::NotRunning)
{
gpioProcess->start();
qDebug() << __LINE__ << __FUNCTION__ << "i2cset started " << pin3 << ","
<< pin4;
}
gpioProcess->waitForFinished(-1);
}