-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvlcd.cpp
126 lines (103 loc) · 3.63 KB
/
vlcd.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
#include "vlcd.h"
#include "ui_vlcd.h"
#include <QSettings>
#include <QDebug>
#include "hid.h"
VLCD::VLCD(QWidget *parent) :
QWidget(parent),
ui(new Ui::VLCD)
{
ui->setupUi(this);
QObject::connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(appIsClosing()));
QSettings settings(":/config.ini", QSettings::IniFormat);
settings.beginGroup("application");
bool ok;
setWindowTitle(settings.value("name").toString());
_hid.setVidPid(settings.value("vid").toString().toUShort(&ok, 16), settings.value("pid").toString().toUShort(&ok, 16));
using namespace std::chrono;
startTimer(seconds(1));
settings.endGroup();
QStringList groups = settings.childGroups();
QStringListIterator iter(groups);
while (iter.hasNext()) {
QString group = iter.next();
if (group.startsWith("color.")) {
settings.beginGroup(group);
QMap<QString, QVariant> colorSet;
colorSet.insert("foreground", parseColor(settings.value("foreground").toString()));
colorSet.insert("background", parseColor(settings.value("background").toString()));
colorSet.insert("shadow", settings.value("shadow").toBool());
ui->colorList->addItem(settings.value("name").toString(), colorSet);
settings.endGroup();
}
}
QObject::connect(&_hid, SIGNAL(packetReceived(uint8_t *, int )), this, SLOT(gotPacket(uint8_t *, int )));
}
unsigned int VLCD::parseColor(const QString &str) {
if (str.startsWith("#")) {
bool ok;
return str.right(str.length() - 1).toUInt(&ok, 16);
}
return 0;
}
VLCD::~VLCD()
{
delete ui;
}
void VLCD::timerEvent(QTimerEvent __attribute__((unused)) *event) {
QMap<QString, QString> list = _hid.getCompatibleDevices();
// First we want to add any new devices to the list
QMapIterator<QString, QString> iter(list);
while (iter.hasNext()) {
iter.next();
if ((ui->deviceList->findData(iter.key()) >= 0) && (ui->deviceList->findText(iter.value()) >= 0)) {
continue;
}
qDebug() << "Adding item " << iter.key();
ui->deviceList->addItem(iter.value(), iter.key());
}
// Now we can remove any devices that no longer exist
bool found = false;
do {
found = false;
for (int i = 0; i < ui->deviceList->count(); i++) {
QString path = ui->deviceList->itemData(i).toString();
QString text = ui->deviceList->itemText(i);
if (list.contains(path)) {
if (list[path] == text) {
continue;
}
}
qDebug() << "Removing item " << path;
ui->deviceList->removeItem(i);
found = true;
break;
}
} while (found);
}
void VLCD::on_deviceList_currentIndexChanged(int __attribute__((unused)) index)
{
QString path = ui->deviceList->currentData().toString();
_hid.connectToPath(path);
}
void VLCD::appIsClosing() {
_hid.cleanup();
}
void VLCD::gotPacket(uint8_t *data, int len) {
if (len <= 0) return;
switch (data[0]) {
case 1: // Control packet
break;
case 2: { // Page packet
int page = data[1];
ui->LCDDisplay->setPageData(page, &data[2], 256);
} break;
}
}
void VLCD::on_colorList_currentIndexChanged(int __attribute__((unused)) index)
{
QMap<QString, QVariant> colorSet = ui->colorList->currentData().toMap();
ui->LCDDisplay->setColor(QColor(colorSet["foreground"].toUInt()), QColor(colorSet["background"].toUInt()));
ui->LCDDisplay->setShadow(colorSet["shadow"].toBool());
ui->LCDDisplay->repaint();
}