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

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