-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpio.cpp
More file actions
184 lines (160 loc) · 5.3 KB
/
gpio.cpp
File metadata and controls
184 lines (160 loc) · 5.3 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
#include "gpio.hpp"
/**
* @brief GPIO::GPIO
* @param parent
* @author Louis P. Meadows
* @date November 13th 2019
* @copyright Sciton
*/
GPIO::GPIO(QObject* parent) : QObject(parent)
{
trace = true;
// i2cget, i2cset command syntax:
// -y subpress interactive mode
// -r Read back the value right after writing it, and compare
// the result with the value written.
// 0: i2c-0
// 0x3E: slave address
// 0x__: command register bits
//
gpioProcess = new QProcess(this);
gpioProcess->start(configure_gpio_command);
timerLED = new QTimer(this); // create a timer
connect(timerLED, SIGNAL(timeout()), this, SLOT(turnOffLED()));
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)));
}
/**
* @brief GPIO::stateChanged
* @param newstate
* @author Louis P. Meadows
* @date November 13th 2019
* @copyright Sciton
*/
void GPIO::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 GPIO::readGPIOerror
* @date November 6th 2019
* @author Louis P. Meadows
* @copyright Sciton
*/
void GPIO::readGPIOerror()
{
gpioErrorResponse = gpioProcess->readAllStandardError();
qDebug() << __LINE__ << __FUNCTION__ << "Error:" << gpioErrorResponse;
}
/**
* @brief GPIO::readGPIO
* @date November 6th 2019
* @author Louis P. Meadows
* @copyright Sciton
*/
void GPIO::readGPIO()
{
gpioResponse = gpioProcess->readAllStandardOutput();
qDebug() << __LINE__ << __FUNCTION__ << gpioResponse;
}
/**
* @brief GPIO::onFinish
* @param exitCode
* @param exitStatus
* @author Louis P. Meadows
* @copyright Sciton
*/
void GPIO::onFinish(int exitCode, QProcess::ExitStatus 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;
}
qDebug() << __LINE__ << __FUNCTION__ << "Exit code:" << exitCode;
qDebug() << __LINE__ << __FUNCTION__ << "Exit status:" << exitStatus;
}
/**
* @brief GPIO::turnOffLED
* @author Louis P Meadows
* @date November 13th 2019
* @copyright Sciton
*/
void GPIO::turnOffLED()
{
gpioProcess->write("i2cset -y -r 0 0x3E 1 0");
timerLED->stop(); // only run when LED is on
}
/**
* @brief GPIO::startedGPIO
* @author Louis P. Meadows
* @date November 13th 2019
* @copyright Sciton
*/
void GPIO::startedGPIO()
{
qDebug() << __LINE__ << __FUNCTION__ << "Started GPIO process.";
}
/**
* @brief GPIO::setGPIO
* @param pin3
* @param pin4
* @author Louis P. Meadows
* @date November 13th 2019
* @copyright Sciton
*/
void GPIO::setGPIO(bool pin3, bool pin4)
{
// pin 0 & 1 input from the controller
// pin 2 & 3 output to the controller
if ((pin3) && (pin4))
{
laserFireCount++;
timerLED->start();
gpioProcess->write("i2cset -y -r 0 0x3E 1 12");
qDebug() << "=============================================="
"==========================================";
qDebug() << "====================================== Firing "
"LASER ====================================";
qDebug() << "=============================================="
"==========================================";
qDebug() << "LASER Has fired " << laserFireCount << " times";
}
if ((!pin3) && (pin4))
{
gpioProcess->write("i2cset -y -r 0 0x3E 1 8");
}
if ((pin3) && (!pin4))
{
gpioProcess->write("i2cset -y -r 0 0x3E 1 4");
}
if ((!pin3) && (!pin4))
{
gpioProcess->write("i2cset -y -r 0 0x3E 1 0");
timerLED->stop();
}
qDebug() << __LINE__ << __FUNCTION__ << "i2cset started " << pin3 << "," << pin4;
}