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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. }
  50. void MainWindow::log(QString logtext)
  51. {
  52. QString output = "[" + getCurrentTime() + "] " + logtext;
  53. ui->plain_Log->appendPlainText(output);
  54. }
  55. QString MainWindow::getCurrentTime()
  56. {
  57. QString currenttime = QTime::currentTime().toString("HH:mm:ss");
  58. return currenttime;
  59. }
  60. void MainWindow::applyButton()
  61. {
  62. if(ui->check_ClearLog->isChecked())
  63. {
  64. ui->plain_Log->clear();
  65. }
  66. bindPC();
  67. setupFeather();
  68. }
  69. void MainWindow::bindPC()
  70. {
  71. if(udpSocket_PC->state() == QAbstractSocket::BoundState)
  72. {
  73. udpSocket_PC->abort();
  74. }
  75. QString tmp_PCIP_full = buildPCIP();
  76. if(tmp_PCIP_full == "0") {
  77. log("Please try another IP address");
  78. } else {
  79. PCIP_full = QHostAddress(tmp_PCIP_full);
  80. PCPort = ui->line_PCPort->text().toUShort(nullptr,10);
  81. if(checkPort(PCPort))
  82. {
  83. log("Receive socket BCI-Stream set to:");
  84. log("-- PC-IP: " + PCIP_full.toString());
  85. log("-- PC-Port: " + QString::number(PCPort));
  86. if(udpSocket_PC->bind(PCIP_full, PCPort))
  87. {
  88. log("UDP PC socket: successfully bound.");
  89. } else {
  90. log("!! UDP PC socket: binding error !!");
  91. }
  92. } else {
  93. PCPort = 00000;
  94. ui->line_PCPort->setText("00000");
  95. log("!! PC Port not valid !!");
  96. QMessageBox::critical(this,
  97. "PC Port not valid",
  98. "PC Port is not valid. Please make sure that the port is a number between 0 and 65535.");
  99. }
  100. }
  101. }
  102. void MainWindow::setupFeather()
  103. {
  104. QString tmp_FeatherIP_full = buildFeatherIP();
  105. if(tmp_FeatherIP_full == "0") {
  106. log("Please try another IP address");
  107. } else {
  108. FeatherIP_full = QHostAddress(tmp_FeatherIP_full);
  109. FeatherPort = ui->line_FeatherPort->text().toUShort(nullptr,10);
  110. if(checkPort(FeatherPort))
  111. {
  112. log("Receive socket Adafruit Feather set to:");
  113. log("-- Feather-IP: " + FeatherIP_full.toString());
  114. log("-- Feather-Port: " + QString::number(FeatherPort));
  115. } else {
  116. FeatherPort = 00000;
  117. ui->line_FeatherPort->setText("00000");
  118. log("!! Port not valid !!");
  119. QMessageBox::critical(this,
  120. "Feather Port not valid",
  121. "Feather Port is not valid. Please make sure that the port is a number between 0 and 65535.");
  122. }
  123. }
  124. }
  125. bool MainWindow::checkPort(const quint16 port)
  126. {
  127. bool valid = false;
  128. if(port)
  129. {
  130. valid = true;
  131. }
  132. return valid;
  133. }
  134. QString MainWindow::buildPCIP()
  135. {
  136. int PCIP1 = ui->line_PCIP1->text().toInt();
  137. int PCIP2 = ui->line_PCIP2->text().toInt();
  138. int PCIP3 = ui->line_PCIP3->text().toInt();
  139. int PCIP4 = ui->line_PCIP4->text().toInt();
  140. if(
  141. ((PCIP1 < 0) || (PCIP1 > 255))
  142. || ((PCIP2 < 0) || (PCIP2 > 255))
  143. || ((PCIP3 < 0) || (PCIP3 > 255))
  144. || ((PCIP4 < 0) || (PCIP4 > 255))
  145. )
  146. {
  147. QMessageBox::critical(this,
  148. "PC IP address not valid",
  149. "PC IP address is not valid. Please make sure that the IP components only consist of numbers between 0 and 255.");
  150. log("!! Error while setting PC IP !!");
  151. return "0";
  152. } else {
  153. QString IP = QString::number(PCIP1) + "."
  154. + QString::number(PCIP2) + "."
  155. + QString::number(PCIP3) + "."
  156. + QString::number(PCIP4);
  157. return IP;
  158. }
  159. }
  160. QString MainWindow::buildFeatherIP()
  161. {
  162. int FeatherIP1 = ui->line_FeatherIP1->text().toInt();
  163. int FeatherIP2 = ui->line_FeatherIP2->text().toInt();
  164. int FeatherIP3 = ui->line_FeatherIP3->text().toInt();
  165. int FeatherIP4 = ui->line_FeatherIP4->text().toInt();
  166. if(
  167. ((FeatherIP1 < 0) || (FeatherIP1 > 255))
  168. || ((FeatherIP2 < 0) || (FeatherIP2 > 255))
  169. || ((FeatherIP3 < 0) || (FeatherIP3 > 255))
  170. || ((FeatherIP4 < 0) || (FeatherIP4 > 255))
  171. )
  172. {
  173. QMessageBox::critical(this,
  174. "Feather IP address not valid",
  175. "Feather IP address is not valid. Please make sure that the IP components only consist of numbers between 0 and 255.");
  176. log("!! Error while setting Feather IP !!");
  177. return "0";
  178. } else {
  179. QString IP = QString::number(FeatherIP1) + "."
  180. + QString::number(FeatherIP2) + "."
  181. + QString::number(FeatherIP3) + "."
  182. + QString::number(FeatherIP4);
  183. return IP;
  184. }
  185. }
  186. void MainWindow::readPCSocketData()
  187. {
  188. QString bufferStr;
  189. while (udpSocket_PC->hasPendingDatagrams())
  190. {
  191. QByteArray buffer;
  192. buffer.resize(static_cast<int>(udpSocket_PC->pendingDatagramSize()));
  193. udpSocket_PC->readDatagram(buffer.data(), buffer.size());
  194. bufferStr = QString::fromStdString(buffer.toStdString());
  195. if(bufferStr.contains(SEARCH_STR_STIMULUS,Qt::CaseSensitive))
  196. {
  197. bufferStr.replace(SEARCH_STR_STIMULUS, "");
  198. if(!bufferStr.contains("0"))
  199. {
  200. ui->lineLastSymbol->setText(bufferStr);
  201. }
  202. if(logBCIData)
  203. {
  204. log(bufferStr);
  205. }
  206. if(ui->check_Tactile->isChecked())
  207. {
  208. markDirectionTactile(bufferStr);
  209. }
  210. if(bufferStr != lastBufferStr){
  211. QByteArray data = bufferStr.toUtf8();
  212. udpSocket_Feather->writeDatagram(data, FeatherIP_full, FeatherPort);
  213. lastBufferStr = bufferStr;
  214. }
  215. }
  216. }
  217. }
  218. void MainWindow::markDirectionTactile(const QString direction)
  219. {
  220. unsigned int i_direction = direction.toUInt(nullptr,10);
  221. //qDebug() << i_direction;
  222. switch (i_direction) {
  223. case 1: //Tac1
  224. ui->label_Tac1->setStyleSheet(stylesheet_tac_marked);
  225. ui->label_Tac2->setStyleSheet(stylesheet_tac_unmarked);
  226. ui->label_Tac3->setStyleSheet(stylesheet_tac_unmarked);
  227. ui->label_Tac4->setStyleSheet(stylesheet_tac_unmarked);
  228. ui->label_Tac5->setStyleSheet(stylesheet_tac_unmarked);
  229. ui->label_Tac6->setStyleSheet(stylesheet_tac_unmarked);
  230. break;
  231. case 2: //Tac2
  232. ui->label_Tac1->setStyleSheet(stylesheet_tac_unmarked);
  233. ui->label_Tac2->setStyleSheet(stylesheet_tac_marked);
  234. ui->label_Tac3->setStyleSheet(stylesheet_tac_unmarked);
  235. ui->label_Tac4->setStyleSheet(stylesheet_tac_unmarked);
  236. ui->label_Tac5->setStyleSheet(stylesheet_tac_unmarked);
  237. ui->label_Tac6->setStyleSheet(stylesheet_tac_unmarked);
  238. break;
  239. case 3: //Tac3
  240. ui->label_Tac1->setStyleSheet(stylesheet_tac_unmarked);
  241. ui->label_Tac2->setStyleSheet(stylesheet_tac_unmarked);
  242. ui->label_Tac3->setStyleSheet(stylesheet_tac_marked);
  243. ui->label_Tac4->setStyleSheet(stylesheet_tac_unmarked);
  244. ui->label_Tac5->setStyleSheet(stylesheet_tac_unmarked);
  245. ui->label_Tac6->setStyleSheet(stylesheet_tac_unmarked);
  246. break;
  247. case 4: //Tac4
  248. ui->label_Tac1->setStyleSheet(stylesheet_tac_unmarked);
  249. ui->label_Tac2->setStyleSheet(stylesheet_tac_unmarked);
  250. ui->label_Tac3->setStyleSheet(stylesheet_tac_unmarked);
  251. ui->label_Tac4->setStyleSheet(stylesheet_tac_marked);
  252. ui->label_Tac5->setStyleSheet(stylesheet_tac_unmarked);
  253. ui->label_Tac6->setStyleSheet(stylesheet_tac_unmarked);
  254. break;
  255. case 5: //Tac5
  256. ui->label_Tac1->setStyleSheet(stylesheet_tac_unmarked);
  257. ui->label_Tac2->setStyleSheet(stylesheet_tac_unmarked);
  258. ui->label_Tac3->setStyleSheet(stylesheet_tac_unmarked);
  259. ui->label_Tac4->setStyleSheet(stylesheet_tac_unmarked);
  260. ui->label_Tac5->setStyleSheet(stylesheet_tac_marked);
  261. ui->label_Tac6->setStyleSheet(stylesheet_tac_unmarked);
  262. break;
  263. case 6: //Tac6
  264. ui->label_Tac1->setStyleSheet(stylesheet_tac_unmarked);
  265. ui->label_Tac2->setStyleSheet(stylesheet_tac_unmarked);
  266. ui->label_Tac3->setStyleSheet(stylesheet_tac_unmarked);
  267. ui->label_Tac4->setStyleSheet(stylesheet_tac_unmarked);
  268. ui->label_Tac5->setStyleSheet(stylesheet_tac_unmarked);
  269. ui->label_Tac6->setStyleSheet(stylesheet_tac_marked);
  270. break;
  271. }
  272. }
  273. void MainWindow::setLogBCIData(const bool log)
  274. {
  275. logBCIData = log;
  276. }
  277. void MainWindow::clearLog()
  278. {
  279. ui->plain_Log->clear();
  280. }
  281. void MainWindow::pushTac1(const bool state)
  282. {
  283. tac1ON = state;
  284. pushDirection();
  285. }
  286. void MainWindow::pushTac2(const bool state)
  287. {
  288. tac2ON = state;
  289. pushDirection();
  290. }
  291. void MainWindow::pushTac3(const bool state)
  292. {
  293. tac3ON = state;
  294. pushDirection();
  295. }
  296. void MainWindow::pushTac4(const bool state)
  297. {
  298. tac4ON = state;
  299. pushDirection();
  300. }
  301. void MainWindow::pushTac5(const bool state)
  302. {
  303. tac5ON = state;
  304. pushDirection();
  305. }
  306. void MainWindow::pushTac6(const bool state)
  307. {
  308. tac6ON = state;
  309. pushDirection();
  310. }
  311. void MainWindow::pushDirection()
  312. {
  313. if(tac1ON)
  314. udpSocket_Feather->writeDatagram(QByteArray::number(1), FeatherIP_full, FeatherPort);
  315. if(tac2ON)
  316. udpSocket_Feather->writeDatagram(QByteArray::number(2), FeatherIP_full, FeatherPort);
  317. if(tac3ON)
  318. udpSocket_Feather->writeDatagram(QByteArray::number(3), FeatherIP_full, FeatherPort);
  319. if(tac4ON)
  320. udpSocket_Feather->writeDatagram(QByteArray::number(4), FeatherIP_full, FeatherPort);
  321. if(tac5ON)
  322. udpSocket_Feather->writeDatagram(QByteArray::number(5), FeatherIP_full, FeatherPort);
  323. if(tac6ON)
  324. udpSocket_Feather->writeDatagram(QByteArray::number(6), FeatherIP_full, FeatherPort);
  325. }