You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mainwindow.cpp 13KB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QTime>
  4. #include <QMessageBox>
  5. #include <QThread>
  6. #define SEARCH_STR_STIMULUS "StimulusCode "
  7. MainWindow::MainWindow(QWidget *parent) :
  8. QMainWindow(parent),
  9. ui(new Ui::MainWindow)
  10. {
  11. ui->setupUi(this);
  12. PCIP_1 = 0; PCIP_2 = 0; PCIP_3 = 0; PCIP_4 = 0;
  13. FeatherIP_1 = 0; FeatherIP_2 = 0; FeatherIP_3 = 0; FeatherIP_4 = 0;
  14. //default
  15. PCIP_full = QHostAddress("127.0.0.1");
  16. PCPort = 5001;
  17. FeatherIP_full = QHostAddress("192.168.4.1");
  18. FeatherPort = 8888;
  19. udpSocket_PC = new QUdpSocket(this);
  20. udpSocket_Feather = new QUdpSocket(this); //no binding to socket needed. Just send data!
  21. stylesheet_tac_marked = "QLabel {background-color: rgb(0,170,0);}";
  22. stylesheet_tac_unmarked = "QLabel {background-color: white;}";
  23. logBCIData = true;
  24. tac1ON = false;
  25. tac2ON = false;
  26. tac3ON = false;
  27. tac4ON = false;
  28. tac5ON = false;
  29. tac6ON = false;
  30. lastBufferStr = "0";
  31. setupSignalsAndSlots();
  32. }
  33. MainWindow::~MainWindow()
  34. {
  35. delete ui;
  36. }
  37. void MainWindow::setupSignalsAndSlots()
  38. {
  39. connect(ui->push_Apply, SIGNAL(clicked()), this, SLOT(applyButton()));
  40. connect(udpSocket_PC, SIGNAL(readyRead()), this, SLOT(readPCSocketData()));
  41. connect(ui->check_LogBCI, SIGNAL(toggled(bool)), this, SLOT(setLogBCIData(bool)));
  42. connect(ui->push_ClearLog, SIGNAL(clicked()), this, SLOT(clearLog()));
  43. connect(ui->push_Tac1, SIGNAL(clicked(bool)), this, SLOT(pushTac1(bool)));
  44. connect(ui->push_Tac2, SIGNAL(clicked(bool)), this, SLOT(pushTac2(bool)));
  45. connect(ui->push_Tac3, SIGNAL(clicked(bool)), this, SLOT(pushTac3(bool)));
  46. connect(ui->push_Tac4, SIGNAL(clicked(bool)), this, SLOT(pushTac4(bool)));
  47. connect(ui->push_Tac5, SIGNAL(clicked(bool)), this, SLOT(pushTac5(bool)));
  48. connect(ui->push_Tac6, SIGNAL(clicked(bool)), this, SLOT(pushTac6(bool)));
  49. connect(ui->push_allLOW, SIGNAL(clicked()), this, SLOT(allLOW()));
  50. connect(ui->push_allHIGH, SIGNAL(clicked()), this, SLOT(allHIGH()));
  51. }
  52. void MainWindow::log(QString logtext)
  53. {
  54. QString output = "[" + getCurrentTime() + "] " + logtext;
  55. ui->plain_Log->appendPlainText(output);
  56. }
  57. QString MainWindow::getCurrentTime()
  58. {
  59. QString currenttime = QTime::currentTime().toString("HH:mm:ss");
  60. return currenttime;
  61. }
  62. void MainWindow::applyButton()
  63. {
  64. if(ui->check_ClearLog->isChecked())
  65. {
  66. ui->plain_Log->clear();
  67. }
  68. bindPC();
  69. setupFeather();
  70. }
  71. void MainWindow::bindPC()
  72. {
  73. if(udpSocket_PC->state() == QAbstractSocket::BoundState)
  74. {
  75. udpSocket_PC->abort();
  76. }
  77. QString tmp_PCIP_full = buildPCIP();
  78. if(tmp_PCIP_full == "0") {
  79. log("Please try another IP address");
  80. } else {
  81. PCIP_full = QHostAddress(tmp_PCIP_full);
  82. PCPort = ui->line_PCPort->text().toUShort(nullptr,10);
  83. if(checkPort(PCPort))
  84. {
  85. log("Receive socket BCI-Stream set to:");
  86. log("-- PC-IP: " + PCIP_full.toString());
  87. log("-- PC-Port: " + QString::number(PCPort));
  88. if(udpSocket_PC->bind(PCIP_full, PCPort))
  89. {
  90. log("UDP PC socket: successfully bound.");
  91. } else {
  92. log("!! UDP PC socket: binding error !!");
  93. }
  94. } else {
  95. PCPort = 00000;
  96. ui->line_PCPort->setText("00000");
  97. log("!! PC Port not valid !!");
  98. QMessageBox::critical(this,
  99. "PC Port not valid",
  100. "PC Port is not valid. Please make sure that the port is a number between 0 and 65535.");
  101. }
  102. }
  103. }
  104. void MainWindow::setupFeather()
  105. {
  106. QString tmp_FeatherIP_full = buildFeatherIP();
  107. if(tmp_FeatherIP_full == "0") {
  108. log("Please try another IP address");
  109. } else {
  110. FeatherIP_full = QHostAddress(tmp_FeatherIP_full);
  111. FeatherPort = ui->line_FeatherPort->text().toUShort(nullptr,10);
  112. if(checkPort(FeatherPort))
  113. {
  114. log("Receive socket Adafruit Feather set to:");
  115. log("-- Feather-IP: " + FeatherIP_full.toString());
  116. log("-- Feather-Port: " + QString::number(FeatherPort));
  117. } else {
  118. FeatherPort = 00000;
  119. ui->line_FeatherPort->setText("00000");
  120. log("!! Port not valid !!");
  121. QMessageBox::critical(this,
  122. "Feather Port not valid",
  123. "Feather Port is not valid. Please make sure that the port is a number between 0 and 65535.");
  124. }
  125. }
  126. }
  127. bool MainWindow::checkPort(const quint16 port)
  128. {
  129. bool valid = false;
  130. if(port)
  131. {
  132. valid = true;
  133. }
  134. return valid;
  135. }
  136. QString MainWindow::buildPCIP()
  137. {
  138. int PCIP1 = ui->line_PCIP1->text().toInt();
  139. int PCIP2 = ui->line_PCIP2->text().toInt();
  140. int PCIP3 = ui->line_PCIP3->text().toInt();
  141. int PCIP4 = ui->line_PCIP4->text().toInt();
  142. if(
  143. ((PCIP1 < 0) || (PCIP1 > 255))
  144. || ((PCIP2 < 0) || (PCIP2 > 255))
  145. || ((PCIP3 < 0) || (PCIP3 > 255))
  146. || ((PCIP4 < 0) || (PCIP4 > 255))
  147. )
  148. {
  149. QMessageBox::critical(this,
  150. "PC IP address not valid",
  151. "PC IP address is not valid. Please make sure that the IP components only consist of numbers between 0 and 255.");
  152. log("!! Error while setting PC IP !!");
  153. return "0";
  154. } else {
  155. QString IP = QString::number(PCIP1) + "."
  156. + QString::number(PCIP2) + "."
  157. + QString::number(PCIP3) + "."
  158. + QString::number(PCIP4);
  159. return IP;
  160. }
  161. }
  162. QString MainWindow::buildFeatherIP()
  163. {
  164. int FeatherIP1 = ui->line_FeatherIP1->text().toInt();
  165. int FeatherIP2 = ui->line_FeatherIP2->text().toInt();
  166. int FeatherIP3 = ui->line_FeatherIP3->text().toInt();
  167. int FeatherIP4 = ui->line_FeatherIP4->text().toInt();
  168. if(
  169. ((FeatherIP1 < 0) || (FeatherIP1 > 255))
  170. || ((FeatherIP2 < 0) || (FeatherIP2 > 255))
  171. || ((FeatherIP3 < 0) || (FeatherIP3 > 255))
  172. || ((FeatherIP4 < 0) || (FeatherIP4 > 255))
  173. )
  174. {
  175. QMessageBox::critical(this,
  176. "Feather IP address not valid",
  177. "Feather IP address is not valid. Please make sure that the IP components only consist of numbers between 0 and 255.");
  178. log("!! Error while setting Feather IP !!");
  179. return "0";
  180. } else {
  181. QString IP = QString::number(FeatherIP1) + "."
  182. + QString::number(FeatherIP2) + "."
  183. + QString::number(FeatherIP3) + "."
  184. + QString::number(FeatherIP4);
  185. return IP;
  186. }
  187. }
  188. void MainWindow::readPCSocketData()
  189. {
  190. QString bufferStr;
  191. while (udpSocket_PC->hasPendingDatagrams())
  192. {
  193. QByteArray buffer;
  194. buffer.resize(static_cast<int>(udpSocket_PC->pendingDatagramSize()));
  195. udpSocket_PC->readDatagram(buffer.data(), buffer.size());
  196. bufferStr = QString::fromStdString(buffer.toStdString());
  197. if(bufferStr.contains(SEARCH_STR_STIMULUS,Qt::CaseSensitive))
  198. {
  199. bufferStr.replace(SEARCH_STR_STIMULUS, "");
  200. if(!bufferStr.contains("0"))
  201. {
  202. ui->lineLastSymbol->setText(bufferStr);
  203. }
  204. if(logBCIData)
  205. {
  206. log(bufferStr);
  207. }
  208. if(ui->check_Tactile->isChecked())
  209. {
  210. markDirectionTactile(bufferStr);
  211. }
  212. if(bufferStr != lastBufferStr){
  213. QByteArray data = bufferStr.toUtf8();
  214. udpSocket_Feather->writeDatagram(data, FeatherIP_full, FeatherPort);
  215. lastBufferStr = bufferStr;
  216. }
  217. }
  218. }
  219. }
  220. void MainWindow::markDirectionTactile(const QString direction)
  221. {
  222. unsigned int i_direction = direction.toUInt(nullptr,10);
  223. //qDebug() << i_direction;
  224. switch (i_direction) {
  225. case 0: //nothing
  226. ui->label_Tac1->setStyleSheet(stylesheet_tac_unmarked);
  227. ui->label_Tac2->setStyleSheet(stylesheet_tac_unmarked);
  228. ui->label_Tac3->setStyleSheet(stylesheet_tac_unmarked);
  229. ui->label_Tac4->setStyleSheet(stylesheet_tac_unmarked);
  230. ui->label_Tac5->setStyleSheet(stylesheet_tac_unmarked);
  231. ui->label_Tac6->setStyleSheet(stylesheet_tac_unmarked);
  232. break;
  233. case 1: //Tac1
  234. ui->label_Tac1->setStyleSheet(stylesheet_tac_marked);
  235. ui->label_Tac2->setStyleSheet(stylesheet_tac_unmarked);
  236. ui->label_Tac3->setStyleSheet(stylesheet_tac_unmarked);
  237. ui->label_Tac4->setStyleSheet(stylesheet_tac_unmarked);
  238. ui->label_Tac5->setStyleSheet(stylesheet_tac_unmarked);
  239. ui->label_Tac6->setStyleSheet(stylesheet_tac_unmarked);
  240. break;
  241. case 2: //Tac2
  242. ui->label_Tac1->setStyleSheet(stylesheet_tac_unmarked);
  243. ui->label_Tac2->setStyleSheet(stylesheet_tac_marked);
  244. ui->label_Tac3->setStyleSheet(stylesheet_tac_unmarked);
  245. ui->label_Tac4->setStyleSheet(stylesheet_tac_unmarked);
  246. ui->label_Tac5->setStyleSheet(stylesheet_tac_unmarked);
  247. ui->label_Tac6->setStyleSheet(stylesheet_tac_unmarked);
  248. break;
  249. case 3: //Tac3
  250. ui->label_Tac1->setStyleSheet(stylesheet_tac_unmarked);
  251. ui->label_Tac2->setStyleSheet(stylesheet_tac_unmarked);
  252. ui->label_Tac3->setStyleSheet(stylesheet_tac_marked);
  253. ui->label_Tac4->setStyleSheet(stylesheet_tac_unmarked);
  254. ui->label_Tac5->setStyleSheet(stylesheet_tac_unmarked);
  255. ui->label_Tac6->setStyleSheet(stylesheet_tac_unmarked);
  256. break;
  257. case 4: //Tac4
  258. ui->label_Tac1->setStyleSheet(stylesheet_tac_unmarked);
  259. ui->label_Tac2->setStyleSheet(stylesheet_tac_unmarked);
  260. ui->label_Tac3->setStyleSheet(stylesheet_tac_unmarked);
  261. ui->label_Tac4->setStyleSheet(stylesheet_tac_marked);
  262. ui->label_Tac5->setStyleSheet(stylesheet_tac_unmarked);
  263. ui->label_Tac6->setStyleSheet(stylesheet_tac_unmarked);
  264. break;
  265. case 5: //Tac5
  266. ui->label_Tac1->setStyleSheet(stylesheet_tac_unmarked);
  267. ui->label_Tac2->setStyleSheet(stylesheet_tac_unmarked);
  268. ui->label_Tac3->setStyleSheet(stylesheet_tac_unmarked);
  269. ui->label_Tac4->setStyleSheet(stylesheet_tac_unmarked);
  270. ui->label_Tac5->setStyleSheet(stylesheet_tac_marked);
  271. ui->label_Tac6->setStyleSheet(stylesheet_tac_unmarked);
  272. break;
  273. case 6: //Tac6
  274. ui->label_Tac1->setStyleSheet(stylesheet_tac_unmarked);
  275. ui->label_Tac2->setStyleSheet(stylesheet_tac_unmarked);
  276. ui->label_Tac3->setStyleSheet(stylesheet_tac_unmarked);
  277. ui->label_Tac4->setStyleSheet(stylesheet_tac_unmarked);
  278. ui->label_Tac5->setStyleSheet(stylesheet_tac_unmarked);
  279. ui->label_Tac6->setStyleSheet(stylesheet_tac_marked);
  280. break;
  281. }
  282. }
  283. void MainWindow::setLogBCIData(const bool log)
  284. {
  285. logBCIData = log;
  286. }
  287. void MainWindow::clearLog()
  288. {
  289. ui->plain_Log->clear();
  290. }
  291. void MainWindow::pushTac1(const bool state)
  292. {
  293. tac1ON = state;
  294. pushDirection();
  295. }
  296. void MainWindow::pushTac2(const bool state)
  297. {
  298. tac2ON = state;
  299. pushDirection();
  300. }
  301. void MainWindow::pushTac3(const bool state)
  302. {
  303. tac3ON = state;
  304. pushDirection();
  305. }
  306. void MainWindow::pushTac4(const bool state)
  307. {
  308. tac4ON = state;
  309. pushDirection();
  310. }
  311. void MainWindow::pushTac5(const bool state)
  312. {
  313. tac5ON = state;
  314. pushDirection();
  315. }
  316. void MainWindow::pushTac6(const bool state)
  317. {
  318. tac6ON = state;
  319. pushDirection();
  320. }
  321. void MainWindow::pushDirection()
  322. {
  323. //first switch off all
  324. udpSocket_Feather->writeDatagram(QByteArray::number(0), FeatherIP_full, FeatherPort);
  325. if(tac1ON)
  326. udpSocket_Feather->writeDatagram(QByteArray::number(1), FeatherIP_full, FeatherPort);
  327. if(tac2ON)
  328. udpSocket_Feather->writeDatagram(QByteArray::number(2), FeatherIP_full, FeatherPort);
  329. if(tac3ON)
  330. udpSocket_Feather->writeDatagram(QByteArray::number(3), FeatherIP_full, FeatherPort);
  331. if(tac4ON)
  332. udpSocket_Feather->writeDatagram(QByteArray::number(4), FeatherIP_full, FeatherPort);
  333. if(tac5ON)
  334. udpSocket_Feather->writeDatagram(QByteArray::number(5), FeatherIP_full, FeatherPort);
  335. if(tac6ON)
  336. udpSocket_Feather->writeDatagram(QByteArray::number(6), FeatherIP_full, FeatherPort);
  337. }
  338. void MainWindow::allLOW()
  339. {
  340. udpSocket_Feather->writeDatagram(QByteArray::number(0), FeatherIP_full, FeatherPort);
  341. ui->push_Tac1->setChecked(false);
  342. ui->push_Tac2->setChecked(false);
  343. ui->push_Tac3->setChecked(false);
  344. ui->push_Tac4->setChecked(false);
  345. ui->push_Tac5->setChecked(false);
  346. ui->push_Tac6->setChecked(false);
  347. }
  348. void MainWindow::allHIGH()
  349. {
  350. udpSocket_Feather->writeDatagram(QByteArray::number(1), FeatherIP_full, FeatherPort);
  351. udpSocket_Feather->writeDatagram(QByteArray::number(2), FeatherIP_full, FeatherPort);
  352. udpSocket_Feather->writeDatagram(QByteArray::number(3), FeatherIP_full, FeatherPort);
  353. udpSocket_Feather->writeDatagram(QByteArray::number(4), FeatherIP_full, FeatherPort);
  354. udpSocket_Feather->writeDatagram(QByteArray::number(5), FeatherIP_full, FeatherPort);
  355. udpSocket_Feather->writeDatagram(QByteArray::number(6), FeatherIP_full, FeatherPort);
  356. ui->push_Tac1->setChecked(true);
  357. ui->push_Tac2->setChecked(true);
  358. ui->push_Tac3->setChecked(true);
  359. ui->push_Tac4->setChecked(true);
  360. ui->push_Tac5->setChecked(true);
  361. ui->push_Tac6->setChecked(true);
  362. }