parent
5f0dbeb36f
commit
ee17861081
@ -0,0 +1,225 @@
|
||||
#include "i2c_keyboard_detection.h"
|
||||
#include "ui_i2c_keyboard_detection.h"
|
||||
|
||||
#include <qfile.h>
|
||||
#include <qevent.h>
|
||||
|
||||
#include <qmessagebox.h>
|
||||
#include <qfiledialog.h>
|
||||
|
||||
#include <qdatetime.h>
|
||||
|
||||
i2c_keyboard_detection::i2c_keyboard_detection(QWidget *parent) :
|
||||
QMainWindow(parent),
|
||||
ui(new Ui::i2c_keyboard_detection)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
QObject::connect(&m_com, SIGNAL(readyRead()), this, SLOT(comDataReceived()));
|
||||
QFile fileKbdCodes("hid_keyboard_codes.h");
|
||||
if (!fileKbdCodes.open(QFile::ReadOnly))
|
||||
return;
|
||||
m_keyComments.clear();
|
||||
ui->keyList->clear();
|
||||
while(1) {
|
||||
QString currentStr = QString(fileKbdCodes.readLine());
|
||||
QString defineStr = "#define ";
|
||||
if (currentStr.length() == 0)
|
||||
break;
|
||||
QString strComment = currentStr.right(currentStr.length() - currentStr.indexOf("//"));
|
||||
currentStr = currentStr.left(currentStr.indexOf("//"));
|
||||
QStringList strWords = currentStr.split(QRegExp("\\s"));
|
||||
for (int i = (strWords.length() - 1); i >= 0; i--)
|
||||
{
|
||||
if (strWords[i].length() == 0)
|
||||
strWords.removeAt(i);
|
||||
}
|
||||
if (strWords.length() != 3)
|
||||
continue;
|
||||
if (strWords[0] != "#define")
|
||||
continue;
|
||||
if (!strWords[2].startsWith("0x"))
|
||||
continue;
|
||||
strWords[2] = strWords[2].right(2);
|
||||
bool isKbdCode;
|
||||
int kbdCode = strWords[2].toInt(&isKbdCode, 16);
|
||||
if (!isKbdCode)
|
||||
continue;
|
||||
QString addedElement = strWords[1] + " (" + QString::number(kbdCode,16) + ")";
|
||||
ui->keyList->addItem(addedElement);
|
||||
m_keyComments.push_back(strComment);
|
||||
}
|
||||
fileKbdCodes.close();
|
||||
//int res = system("iceprog hardware.bin");
|
||||
//qDebug ("Result: %u", res);
|
||||
//ui->keyList->event();
|
||||
}
|
||||
|
||||
i2c_keyboard_detection::~i2c_keyboard_detection()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void i2c_keyboard_detection::keyPressEvent(QKeyEvent *e)
|
||||
{
|
||||
qDebug("Key pressed: %u", e->nativeScanCode());
|
||||
// Qt::Key_S;
|
||||
}
|
||||
|
||||
void i2c_keyboard_detection::on_keyList_itemSelectionChanged()
|
||||
{
|
||||
qDebug ("Current key item changed! Now %u", ui->keyList->currentRow());
|
||||
/*ui->keyList->setToolTip*/ui->label_keyComment->setText(m_keyComments.at(ui->keyList->currentRow()));
|
||||
QString currentKey = ui->keyList->currentItem()->text();
|
||||
for (int i = 0; i < m_kbdDetectReport.length(); i++)
|
||||
{
|
||||
if ((m_kbdDetectReport[i].split("\t").at(0) == currentKey) && (ui->enSendKeyToReport->isChecked()))
|
||||
QMessageBox::warning(this, "KEY IS ALREADY ADDED", "This key was already added to report. When you press some key, old value will be lost");
|
||||
}
|
||||
}
|
||||
|
||||
void i2c_keyboard_detection::on_comRefleshList_clicked()
|
||||
{
|
||||
QList<QSerialPortInfo> comList = QSerialPortInfo::availablePorts();
|
||||
ui->comList->clear();
|
||||
for (int i = 0; i < comList.length(); i++)
|
||||
ui->comList->addItem(comList.at(i).portName());
|
||||
}
|
||||
|
||||
void i2c_keyboard_detection::on_comConnect_clicked()
|
||||
{
|
||||
QWidget* widgets_locked[] = { ui->comList, ui->comRefleshList };
|
||||
QWidget* widgets_unlocked[] = { };
|
||||
if (ui->comConnect->text() == "Connect") {
|
||||
m_com.setPortName(ui->comList->currentText());
|
||||
if (m_com.open(QSerialPort::ReadWrite)) {
|
||||
m_com.setParity(QSerialPort::NoParity);
|
||||
m_com.setDataBits(QSerialPort::Data8);
|
||||
m_com.setStopBits(QSerialPort::OneStop);
|
||||
m_com.setFlowControl(QSerialPort::NoFlowControl);
|
||||
m_com.setBaudRate(115200);
|
||||
ui->comConnect->setText("Disconnect");
|
||||
m_kbdDetectBytes.clear();
|
||||
m_kbdDetectData.clear();
|
||||
}
|
||||
else
|
||||
QMessageBox::warning(this, "COM OPEN ERROR", "Com " + ui->comList->currentText() + " cannot open!");
|
||||
}
|
||||
else {
|
||||
m_com.close();
|
||||
ui->comConnect->setText("Connect");
|
||||
}
|
||||
for (int i = 0; i < sizeof(widgets_locked)/sizeof(QWidget*); i++)
|
||||
widgets_locked[i]->setEnabled(!m_com.isOpen());
|
||||
for (int i = 0; i < sizeof(widgets_unlocked)/sizeof(QWidget*); i++)
|
||||
widgets_unlocked[i]->setEnabled(m_com.isOpen());
|
||||
}
|
||||
|
||||
void i2c_keyboard_detection::comDataReceived()
|
||||
{
|
||||
QByteArray dataFromCom = m_com.readAll();
|
||||
for (int i = 0; i < dataFromCom.length(); i++)
|
||||
{
|
||||
if (dataFromCom[i] == 0xFF)
|
||||
{
|
||||
if (m_kbdDetectBytes.length() != 5)
|
||||
{
|
||||
//on_comConnect_clicked();
|
||||
QMessageBox::warning(this, "COM PROTOCOL ERROR", "Received start, but last frame is not complete");
|
||||
}
|
||||
m_kbdDetectBytes.clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
m_kbdDetectBytes.push_back(dataFromCom[i]);
|
||||
if (m_kbdDetectBytes.length() > 5)
|
||||
{
|
||||
if (m_com.isOpen())
|
||||
on_comConnect_clicked();
|
||||
QMessageBox::warning(this, "COM PROTOCOL ERROR", "Received extra byte of frame");
|
||||
}
|
||||
else if (m_kbdDetectBytes.length() == 5)
|
||||
{
|
||||
int64_t result = ((((int64_t)m_kbdDetectBytes[4]) & 0xFF) << 0) |
|
||||
((((int64_t)m_kbdDetectBytes[3]) & 0xFF) << 8) | ((((int64_t)m_kbdDetectBytes[2]) & 0xFF) << 16) |
|
||||
((((int64_t)m_kbdDetectBytes[1]) & 0xFF) << 24) | ((((int64_t)m_kbdDetectBytes[0]) & 0xFF) << 32);
|
||||
//m_kbdDetectBytes.clear();
|
||||
if (result != 0)
|
||||
{
|
||||
for (int k = 0; k < m_kbdDetectData.length(); k++) {
|
||||
if (m_kbdDetectData[k] == result)
|
||||
return; // RECEIVING IN PROCESS, WAIT TERMINATION FRAME, THIS FRAME WAS ALREADY ADDED
|
||||
}
|
||||
m_kbdDetectData.push_back(result);
|
||||
return; // RECEIVING IN PROCESS, WAIT TERMINATION FRAME
|
||||
}
|
||||
// TERMINATION FRAME PROCESSING START
|
||||
QString currentStatus;
|
||||
QVector<int> connectedLines;
|
||||
if (m_kbdDetectData.length() == 0)
|
||||
currentStatus = "No keys are pressed";
|
||||
else {
|
||||
currentStatus = "Detected connections: ";
|
||||
for (int k = 0; k < m_kbdDetectData.length(); k++) {
|
||||
//currentKbdReport = currentKbdReport + QString::number(last_kbd_data[k]) + ", ";
|
||||
for (quint64 n = 0; n < 34; n++) {
|
||||
quint64 mask = 1;
|
||||
mask = mask << n;
|
||||
if ((m_kbdDetectData[k] & mask) != 0)
|
||||
{
|
||||
currentStatus = currentStatus + QString::number(n) + " ";
|
||||
connectedLines.push_back(n);
|
||||
}
|
||||
}
|
||||
currentStatus = currentStatus + ", ";
|
||||
}
|
||||
currentStatus = currentStatus.left(currentStatus.length() - 2);
|
||||
m_kbdDetectData.clear();
|
||||
}
|
||||
currentStatus = currentStatus + " (" + QTime::currentTime().toString("HH:mm:ss.zzz") + ")";
|
||||
ui->labelKeyboardStatus->setText(currentStatus);
|
||||
if ((connectedLines.length() != 0) && (ui->enSendKeyToReport->isChecked()))
|
||||
{
|
||||
QString currentKey = ui->keyList->currentItem()->text();
|
||||
for (int k = (m_kbdDetectReport.length() - 1); k >=0; k--)
|
||||
{
|
||||
if (m_kbdDetectReport[k].split("\t").at(0) == currentKey)
|
||||
m_kbdDetectReport.remove(k);
|
||||
}
|
||||
for (int k = 0; k < connectedLines.length(); k++)
|
||||
currentKey = currentKey + "\t" + QString::number(connectedLines[k]);
|
||||
m_kbdDetectReport.push_front(currentKey);
|
||||
QString tarReport;
|
||||
for (int k = 0; k < m_kbdDetectReport.length(); k++)
|
||||
tarReport = tarReport + m_kbdDetectReport[k] + "\n";
|
||||
ui->kbdReport->setText(tarReport);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void i2c_keyboard_detection::on_enSendKeyToReport_clicked(bool checked)
|
||||
{
|
||||
if (checked)
|
||||
on_keyList_itemSelectionChanged();
|
||||
}
|
||||
|
||||
void i2c_keyboard_detection::on_saveKbdReport_clicked()
|
||||
{
|
||||
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "kbd_connections.txt", tr("*.txt"));
|
||||
if (fileName == "")
|
||||
return;
|
||||
QFile savedFile(fileName);
|
||||
if (!savedFile.open(QFile::WriteOnly))
|
||||
{
|
||||
QMessageBox::warning(this, "CANNOT SAVE FILE", "Can't open file for save report");
|
||||
return;
|
||||
}
|
||||
QString tarReport;
|
||||
for (int k = 0; k < m_kbdDetectReport.length(); k++)
|
||||
tarReport = tarReport + m_kbdDetectReport[k] + "\n";
|
||||
if (savedFile.exists())
|
||||
savedFile.resize(0);
|
||||
savedFile.write(tarReport.toUtf8());
|
||||
savedFile.close();
|
||||
}
|
Loading…
Reference in new issue