-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
269 lines (212 loc) · 7.82 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <iostream>
#include <unistd.h>
#include <thread>
#include <atomic>
#include <mutex>
#include <pthread.h>
#include <QFileInfo>
#include <QApplication>
#include <QElapsedTimer>
#include <QDebug>
#include <QDir>
#include <QTimer>
#include <QTime>
#include <scratchy/constantvelocityquery.h>
#include "model/tactiledisplay.h"
#include "model/actuator.h"
#include "model/scene.h"
#include "model/model.h"
#include "model/frequencymodel.h"
#include "model/nullmodel.h"
#include "view/hdmiscreen.h"
#include "util/hardwareabstraction.h"
#include "util/filelogger.h"
#include "config.h"
#define VERSION_STRING "v1.0"
//#define REALTIME_PRIORITY
int main(int argc, char* argv[])
{
#ifdef REALTIME_PRIORITY
// Realtime priority (Dangerous, may freeze system!)
__pid_t pid = getpid();
struct sched_param param;
param.__sched_priority = 10;
qDebug() << sched_setscheduler(pid, SCHED_FIFO, ¶m);
#endif
QApplication app(argc, argv);
Log::setWorkingDirectory(QString(MAP_DIRECTORY) + "../logs/");
View* view = new HDMIScreen();
view->initialize();
HardwareAbstraction HAL(view, true);
view->showInfo("ScratchyShow", QString("Version: ") + VERSION_STRING);
// Initialize mouse
PositionQuery* currentPosition = HAL.connectInputdevice(new ConstantVelocityQuery(0.05, 0.305));
// Load tactile display definition
TactileDisplay tactileDisplay = tactileDisplayConfig();
HAL.initializeSignalBoards();
if(!HAL.checkAdressLayout(&tactileDisplay))
{
view->showInfo("Error", "Physical configuration does not match software model");
sleep(4);
HAL.shutdown();
exit(0);
}
// Search for tactile scene files (*.tsc) within the map directory
QVector<QPair<QString, QString>> tactileScenes;
QDir tactileDir(MAP_DIRECTORY);
QStringList filters; filters << "*.tsc";
for(const QString& file : tactileDir.entryList(filters))
{
QFile fl(QString(MAP_DIRECTORY) + file);
if(!fl.open(QIODevice::ReadOnly | QIODevice::Text))
continue;
QString title;
while(!fl.atEnd())
{
QString line = QString(fl.readLine()).trimmed();
if(line.startsWith('#'))
continue;
title = line;
break;
}
fl.close();
tactileScenes << qMakePair(QString(MAP_DIRECTORY) + file, title);
view->addMapEntry(title, QString(MAP_DIRECTORY) + file);
qDebug() << file << "->" << title;
}
if(tactileScenes.size() == 0)
{
qDebug() << "No tactile scenes found. Aborting.";
exit(-1);
}
int currentMap = 0;
int nextMap = 0;
view->showSelection(tactileScenes[currentMap].second, tactileScenes[nextMap].second);
Scene scene(&tactileDisplay, currentPosition);
scene.registerModel("Frequency", Factory<FrequencyModel>());
scene.registerModel("Null", Factory<NullModel>());
// TODO: Add python model
scene.loadScenario(tactileScenes[currentMap].first);
view->changeScene(&scene);
view->setTactileDisplay(&tactileDisplay);
view->setInputDevice(currentPosition);
if(HDMIScreen* vw = dynamic_cast<HDMIScreen*>(view))
{
QObject::connect(vw, &HDMIScreen::mapRepositioned, [&](const Entity& e, QVector2D pos, bool silent){
scene.updatePosition(e, pos);
if(!silent)
{
vw->updateScene();
scene.enable();
Log::timestamp();
Log::push(QString("User repositioned map ID") + QString::number(e.id) + " - New position: (" + QString::number(pos.x()) + ", " + QString::number(pos.x()) + ")");
}
});
QObject::connect(vw, &HDMIScreen::mapSelected, [&](){
scene.disable();
});
QObject::connect(vw, &HDMIScreen::onSelect, [&](const QString& title){
int index = 0;
for(int n = 0; n < tactileScenes.size(); n++)
{
if(tactileScenes[n].second == title)
{
index = n;
}
}
// Scene statistics
Log::push("---- Map usage statistics ----");
for(const auto& e : scene.getEntities())
{
Log::push(QString::number(e.statistics.index) + ": Average velocity = "
+ QString::number(e.statistics.averageVelocity, 'f', 4)
+ ", Hover Time = "+ QString::number(e.statistics.hoverTime*0.001, 'f', 4));
}
currentMap = index; nextMap = index;
view->showSelection(tactileScenes[currentMap].second, tactileScenes[nextMap].second);
scene.loadScenario(tactileScenes[currentMap].first);
view->changeScene(&scene);
Log::timestamp();
Log::push(QString("Scene changed to '") + title + "'");
});
}
std::atomic<bool> running(true);
std::thread modelThread([&]()
{
QVector<FrequencyTable> query;
query.resize(tactileDisplay.actuators().size());
QElapsedTimer perfcounter;
int cnt = 0; qint64 worst = 0;
perfcounter.start();
while(running.load())
{
cnt++; perfcounter.restart();
currentPosition->update();
// Move virtual display to current coordinates and update velocities:
tactileDisplay.transform(currentPosition);
scene.queryMap(currentPosition, query);
HAL.sendTables(&tactileDisplay, query);
// ----
qint64 elapsed = perfcounter.nsecsElapsed();
const int wait = 1e6/updateLimit() - elapsed/1000;
if(wait > 0)
usleep(wait);
worst = qMax(worst, perfcounter.nsecsElapsed());
if(cnt % 1000 == 0)
{
qDebug() << "FPS:" << 1/(worst*1.0e-09);
worst = 0;
}
}
qDebug() << "Model thread ended normally";
});
// Main Loop
view->clearInfo();
// Adjust scheduling
sched_param sch_params;
sch_params.sched_priority = 99;
pthread_setschedparam(modelThread.native_handle(), SCHED_RR, &sch_params);
auto ButtonHandler = [&]()
{
switch(HAL.currentButton())
{
case HardwareAbstraction::DisplayButton::Up:
nextMap--;
if(nextMap < 0)
nextMap = tactileScenes.size() - 1;
view->showSelection(tactileScenes[currentMap].second, tactileScenes[nextMap].second);
break;
case HardwareAbstraction::DisplayButton::Down:
nextMap++;
if(nextMap >= tactileScenes.size())
nextMap = 0;
view->showSelection(tactileScenes[currentMap].second, tactileScenes[nextMap].second);
break;
case HardwareAbstraction::DisplayButton::Select:
currentMap = nextMap;
view->showSelection(tactileScenes[currentMap].second, tactileScenes[nextMap].second);
scene.loadScenario(tactileScenes[currentMap].first);
view->changeScene(&scene);
break;
case HardwareAbstraction::DisplayButton::Back:
HAL.initializeSignalBoards(); // Reset Signalboards
break;
case HardwareAbstraction::DisplayButton::Reset: // Shutdown
running = false;
sleep(1);
HAL.shutdown();
sleep(1);
view->shutdown();
app.exit();
break;
case HardwareAbstraction::DisplayButton::None:
break;
}
};
QTimer guiClock;
guiClock.setInterval(5);
QObject::connect(&guiClock, &QTimer::timeout, ButtonHandler);
QObject::connect(&guiClock, SIGNAL(timeout()), view, SLOT(updatePosition()));
guiClock.start();
return app.exec();
}