-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
183 lines (141 loc) · 4.7 KB
/
main.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
/*This c++ program should do the following steps:
* 1. delete any pre-exisiting 'outputFile' and declare a 'new' outputFile,
* 2. read pin state from 'readPinFile'.
* 3. write '1' to 'outputFile' if pin state is 'HIGH' and write '0' to 'outputFile' if pin state is 'LOW',
* 4. continue running until user says otherwise.
*
Each step is divided into its own code block, containing logic and exception handling.
As a testing environment, step 2 is replaced by a code block which emulates pin state by changing between 'LOW' and 'HIGH' every 5 seconds
*/
//#include <pigpio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <filesystem>
#include <ctime>
#include <cstdlib>
using namespace std;
using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::fstream;
//global declarations
const char* outputFile ="triggerOutStatus.txt";
const char* readPinFile = "dummyGPIO.txt";
char currentStatus; //A store for the single integers read from 'readPinFile'
char previousStatus = 0; //A store for the last/previous value of 'currentStatus'
fstream ifs;
fstream ofs;
int interval = 5; //seconds between permutations/loops
int outputStatus; //declaring variable 'outputStatus' for storing integers
string runCommand;
const char* outputRunCommand;
char recordingTriggered = false;
// The function that runs once if functionRead reads '1', is called functionTrigger
int main () {
//code block 1: user input, pre-run tests and initializations
//initialising RPi GPIO pins
/* if (gpioInitialise() < 0)
{
return 1;
}
*/
cout << "which command would you like to run?"<< endl;
getline(cin, runCommand);
cout << "Running " << runCommand << endl;
/*Testing whether the readPinFile can be read from,
*and printing corresponding message.
*If no file exists, it is created.*/
ifs.open(readPinFile, ios::out);
if (ifs.is_open())
{
cout << "inputFile is opened!" << endl;
ifs.put('0');
}
else
{
cerr << "inputFile could not be opened" << endl;
}
ifs.close();
/*Testing whether the outputFile can be read from,
*and printing corresponding message.
*If no file exists, it is created.*/
ofs.open(outputFile, ios::out);
if (ofs.is_open())
{
cout << "outputFile is opened!" << endl;
ofs.put('0');
}
else
{
cerr << "outputFile could not be opened" << endl;
}
ofs.close();
// End of code block 1
//--------------------------------------------------------------//
// Code block 2: Setting up a loop which runs function() every time 'interval' is reached.
while (1) //infinite loop
{
//opening 'readPinFile' as read-only filestream, reading the first character and storing it in 'currentStatus'
previousStatus = currentStatus;
ifs.open(readPinFile, ios::in);
ifs.get(currentStatus);
//printing 'currentStatus' for testing purposes
if (previousStatus != currentStatus)
{
cout << "input is currently " << currentStatus << endl;
}
//closing read-only filestream of 'readPinFile'
ifs.close();
// End of code block 2
//------------------------------------------------------------//
//Start of code block 3, reading status of pin into a variable
/*
gpioSetMode(18, PI_INPUT);
gpioSetPullUpDown(18, PI_PUD_DOWN);
if (gpioRead(18) == PI_LOW)
{*/
//End of actual code block 3
//--------------------------------------------------------------------//
//Start of code block 4, writing currentStatus to outputFile
if (currentStatus != previousStatus)
{
//opening 'outputFile' as a writable filestream
ofs.open(outputFile, ios::out);
// A switch case which reads the value stored in 'currentStatus' and writes the same value to 'outputFile'
switch(currentStatus)
{
// if currentStatus equals 1, it means the extruder is active. Possible to trigger a recording
case '1' :
cout << "<Extruder active>" << endl;
outputStatus = '1';
ofs.put(outputStatus);
ofs.close();
//run command only once upon trigger condition
if (recordingTriggered != true)
{
system(runCommand.c_str());
recordingTriggered = true;
}
break;
//if currentStatus equals 0, it means the extruder is inactive. Don't trigger a recording
case '0' :
cout << "<Not extruding>" << endl;
outputStatus = '0';
ofs.put(outputStatus);
ofs.close();
break;
// Error handling in case something goes wrong
default :
cout << "Switch statement failed" << endl;
}
}
if (recordingTriggered == true)
{
break;
}
}
// gpioTerminate(); // Compulsory. If gpio isn't terminated, pins aren't uninitialized and can cause trouble for later assignments.
return 0;
}