-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
557 lines (516 loc) · 19.3 KB
/
mainwindow.cpp
File metadata and controls
557 lines (516 loc) · 19.3 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#include "mainwindow.h"
#include "portmenu.h"
#include "ui_mainwindow.h"
#include "windows.h"
#include <algorithm>
#include <filesystem>
#include <chrono>
#include <thread>
#include <QLabel>
#include <QComboBox>
#include <QTimer>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QFile>
#include <QUrl>
#include <QProcess>
#include <string>
#include <QPushButton>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QLabel* labels[11] = {ui->label_1, ui->label_2, ui->label_3, ui->label_4, ui->label_5, ui->label_6, ui->label_7, ui->label_8, ui->label_9, ui->label_10, ui->label_11};
QComboBox* comboBoxes[11] = {ui->comboBox_1, ui->comboBox_2, ui->comboBox_3, ui->comboBox_4, ui->comboBox_5, ui->comboBox_6, ui->comboBox_7, ui->comboBox_8, ui->comboBox_9, ui->comboBox_10, ui->comboBox_11};
for(QLabel* label : labels)
{
label->setVisible(false);
}
for(QComboBox* comboBox : comboBoxes)
{
comboBox->setVisible(false);
}
ui->installButton->setVisible(false);
ui->updateLabel->setVisible(false);
ui->installButton_2->setVisible(false);
ui->installButton_3->setVisible(false);
std::ifstream portsFile(portMenu::portsFilePath);
portsFile.seekg(-1, std::ifstream::end);
bool isReading = true;
std::vector<char> portChars;
while(isReading)
{
char portChar = portsFile.get();
if(portChar == '.')
{
isReading = false;
std::string portcharstr(1, portChar);
}
else
{
portChars.push_back(portChar);
portsFile.seekg(-2, std::ifstream::cur);
}
}
std::reverse(portChars.begin(), portChars.end());
std::string portsString(portChars.begin(), portChars.end());
int ports = std::stoi(portsString);
portsFile.seekg(3);
for(int i = 0; i < ports; i++)
{
std::string portName;
labels[i]->setVisible(true);
std::getline(portsFile, portName);
labels[i]->setText(QString::fromStdString(portName));
comboBoxes[i]->setVisible(true);
currentComboBoxes.push_back(comboBoxes[i]);
comboBoxes[i]->addItems({
"A","B","C","D","E","F","G","H","I","J","K","L","M",
"N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
"1","2","3","4","5","6","7","8","9","0",
"`","-","=","[","]",";","'",",",".","/", R"(\)",
"~","!","@","#","$","%","^","&","*","(",")",
"_","+","{","}","|",":",R"(")","<",">","?"
"SPACE","TAB","CAPS LOCK","SHIFT","CTRL","ALT","BACKSPACE","ENTER",
"PrtSc","ScrLk","Pause","Ins","Home","PgUp","PgDown","Del","End",
"Up Arrow","Down Arrow","Left Arrow","Right Arrow"});
}
portsFile.close();
connect(ui->updateButton, &QPushButton::clicked, this, &MainWindow::writeCode);
connect(ui->installButton, &QPushButton::clicked, this, &MainWindow::installAll);
connect(ui->helpButton, &QPushButton::clicked, this, &MainWindow::toggleHelp);
connect(ui->installButton_2, &QPushButton::clicked, [this]() {
installKMK(false);
});
connect(ui->installButton_3, &QPushButton::clicked, this, &MainWindow::installKMK);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::resetCode()
{
std::string startCode = R"(
# You import all the IOs of your board
import board
# These are imports from the kmk library
from kmk.kmk_keyboard import KMKKeyboard
from kmk.scanners.keypad import KeysScanner
from kmk.keys import KC
from kmk.modules.macros import Press, Release, Tap, Macros
# This is the main instance of your keyboard
keyboard = KMKKeyboard()
# Add the macro extension
macros = Macros()
keyboard.modules.append(macros)
# Define your pins here!
PINS = [
]
# Tell kmk we are not using a key matrix
keyboard.matrix = KeysScanner(
pins=PINS,
value_when_pressed=False,
)
# Here you define the buttons corresponding to the pins
# Look here for keycodes: https://github.com/KMKfw/kmk_firmware/blob/main/docs/en/keycodes.md
# And here for macros: https://github.com/KMKfw/kmk_firmware/blob/main/docs/en/macros.md
keyboard.keymap = [
[#keymap
]
]
# Start kmk!
if __name__ == '__main__':
keyboard.go())";
std::ofstream codeW(R"(code.py)");
codeW << startCode;
codeW.flush();
std::ifstream portsFile(portMenu::portsFilePath);
std::ifstream codeR(R"(code.py)");
std::vector<std::string> currentPorts;
codeW.seekp(0);
codeR.seekg(0);
portsFile.seekg(0);
std::string line;
while(std::getline(portsFile, line))
{
if(line.find('D') != std::string::npos)
{
currentPorts.push_back(line);
}
}
while(std::getline(codeR, line))
{
codeW << line << '\n';
if(line.find("PINS = [") != std::string::npos)
{
for(std::string currentPort : currentPorts)
{
codeW << " board." << currentPort << ",\n";
}
}
}
}
std::map<QString, std::string> MainWindow::keyMap() const
{
return std::map<QString, std::string>{
{"A", "KC.A"}, {"B", "KC.B"}, {"C", "KC.C"}, {"D", "KC.D"}, {"E", "KC.E"},
{"F", "KC.F"}, {"G", "KC.G"}, {"H", "KC.H"}, {"I", "KC.I"}, {"J", "KC.J"},
{"K", "KC.K"}, {"L", "KC.L"}, {"N", "KC.N"}, {"O", "KC.O"}, {"P", "KC.P"},
{"Q", "KC.Q"}, {"S", "KC.S"}, {"T", "KC.T"}, {"U", "KC.U"}, {"V", "KC.V"},
{"W", "KC.W"}, {"X", "KC.X"}, {"Y", "KC.Y"}, {"Z", "KC.Z"}, {"1", "KC.N1"},
{"2", "KC.N2"}, {"3", "KC.N3"}, {"4", "KC.N4"}, {"5", "KC.N5"}, {"6", "KC.N6"},
{"7", "KC.N7"}, {"8", "KC.N8"}, {"9", "KC.N9"}, {"0", "KC.N0"}, {"`", "KC.GRAVE"},
{"-", "KC.MINUS"}, {"=", "KC.EQUAL"}, {"[", "KC.LBRACKET"}, {"]", "KC.RBRACKET"},
{";", "KC.SCOLON"}, {"'", "KC.QUOTE"}, {",", "KC.COMMA"}, {".", "KC.DOT"},
{"/", "KC.SLASH"}, {R"(\)", "KC.BSLASH"}, {"~", "KC.TILDE"}, {"!", "KC.EXCLAIM"},
{"@", "KC.AT"}, {"#", "KC.HASH"}, {"$", "KC.DOLLAR"}, {"%", "KC.PERCENT"},
{"^", "KC.CIRCUMFLEX"}, {"&", "KC.AMPERSAND"}, {"*", "KC.ASTERISK"},
{"(", "KC.LEFT_PAREN"}, {")", "KC.RIGHT_PAREN"}, {"SPACE", "KC.SPACE"},
{"TAB", "KC.TAB"}, {"_", "KC.UNDERSCORE"}, {"+", "KC.PLUS"}, {"{", "KC.LEFT_CURLY_BRACE"},
{"}", "KC.RIGHT_CURLY_BRACE"}, {"|", "KC.PIPE"}, {":", "KC.COLON"},
{R"(")", "KC.DOUBLE_QUOTE"}, {"<", "KC.LEFT_ANGLE_BRACKET"},
{">", "KC.RIGHT_ANGLE_BRACKET"}, {"?", "KC.QUESTION"}, {"CAPS LOCK", "KC.CAPSLOCK"},
{"SHIFT", "KC.LSHIFT"}, {"CTRL", "KC.LCTRL"}, {"ALT", "KC.LALT"},
{"BACKSPACE", "KC.BSPACE"}, {"ENTER", "KC.ENTER"}, {"PrtSc", "KC.PSCREEN"},
{"ScrLk", "KC.SCROLLLOCK"}, {"Pause", "KC.PAUSE"}, {"Ins", "KC.INSERT"},
{"Home", "KC.HOME"}, {"PgUp", "KC.PGUP"}, {"PgDown", "PGDOWN"}, {"Del", "KC.DELETE"},
{"End", "KC.END"}, {"Up Arrow", "KC.UP"}, {"Down Arrow", "KC.DOWN"},
{"Left Arrow", "KC.LEFT"}, {"Right Arrow", "KC.RIGHT"}};
}
void MainWindow::toggleHelp()
{
if(!isHelping)
{
ui->installButton_2->setVisible(true);
ui->installButton_3->setVisible(true);
isHelping = true;
}
else
{
ui->installButton_2->setVisible(false);
ui->installButton_3->setVisible(false);
isHelping = false;
}
}
void MainWindow::writeCode()
{
std::ifstream codeR(R"(code.py)");
std::fstream codeW(R"(code.py)");
codeW.seekp(0);
codeR.seekg(0);
std::string codeLine;
resetCode();
while(std::getline(codeR, codeLine))
{
codeW << codeLine << '\n';
if(codeLine.find(" [#keymap") != std::string::npos)
{
for(QComboBox* comboBox : currentComboBoxes)
{
codeW << " " << keyMap().at(comboBox->currentText()) << ",\n";
}
}
}
codeW.close();
codeR.close();
uploadCode();
}
void MainWindow::uploadCode()
{
bool isPlugged = false;
DWORD currentDrive = GetLogicalDrives();
for(char letter = 'A'; letter <= 'Z'; letter++)
{
if(currentDrive & (1 << (letter - 'A')))
{
std::wstring driveTargetLabel = L"CIRCUITPY";
std::wstring wrongDriveTargetLabel = L"RPI-RP2";
std::wstring root = std::wstring(1, letter) + L":\\";
wchar_t driveName[MAX_PATH] = {0};
if(GetVolumeInformationW(root.c_str(), driveName, MAX_PATH, nullptr, nullptr, nullptr, nullptr, 0))
{
if(driveTargetLabel == driveName)
{
isPlugged = true;
driveRoot = letter;
}
if(wrongDriveTargetLabel == driveName)
{
driveRoot = letter;
canMoveCP = true;
}
}
}
}
if(isPlugged)
{
std::filesystem::path currentPath = R"(code.py)";
std::filesystem::path newPath = driveRoot + R"(:\code.py)";
if(std::filesystem::exists(currentPath))
{
std::filesystem::copy(currentPath, newPath, std::filesystem::copy_options::overwrite_existing);
updateUser("Firmware updated!");
ui->installButton->setVisible(false);
}
}
else
{
updateUser(R"(Seems like your device
isn't plugged in or you may
not have CircuitPython
installed!)");
ui->installButton->setVisible(true);
}
}
void MainWindow::updateUser(QString update)
{
ui->updateLabel->setVisible(true);
ui->updateButton->setVisible(false);
ui->updateLabel->setText(update);
QTimer::singleShot(3000, [this]() {
ui->updateButton->setVisible(true);
});
QTimer::singleShot(3000, [this]() {
ui->updateLabel->setVisible(false);
});
}
void MainWindow::installAll()
{
if(!std::filesystem::exists(R"(adafruit-circuitpython-seeeduino_xiao_rp2040-en_US-9.2.9.uf2)"))
{
updateUser("installing CircuitPython");
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
QNetworkReply* reply = manager->get(QNetworkRequest(QUrl("https://downloads.circuitpython.org/bin/seeeduino_xiao_rp2040/en_US/adafruit-circuitpython-seeeduino_xiao_rp2040-en_US-9.2.9.uf2")));
connect(reply, &QNetworkReply::finished, [reply, this]()
{
if(reply->error() == QNetworkReply::NoError)
{
QByteArray data = reply->readAll();
QFile file(R"(adafruit-circuitpython-seeeduino_xiao_rp2040-en_US-9.2.9.uf2)");
if(file.open(QIODevice::WriteOnly))
{
file.write(data);
file.close();
}
}
reply->deleteLater();
moveCP();
installKMK(true);
ui->installButton->setVisible(false);
});
}
else
{
moveCP();
installKMK(true);
ui->installButton->setVisible(false);
}
}
void MainWindow::installCP()
{
if(!std::filesystem::exists(R"(adafruit-circuitpython-seeeduino_xiao_rp2040-en_US-9.2.9.uf2)"))
{
updateUser("installing CircuitPython");
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
QNetworkReply* reply = manager->get(QNetworkRequest(QUrl("https://downloads.circuitpython.org/bin/seeeduino_xiao_rp2040/en_US/adafruit-circuitpython-seeeduino_xiao_rp2040-en_US-9.2.9.uf2")));
connect(reply, &QNetworkReply::finished, [reply, this]()
{
if(reply->error() == QNetworkReply::NoError)
{
QByteArray data = reply->readAll();
QFile file(R"(adafruit-circuitpython-seeeduino_xiao_rp2040-en_US-9.2.9.uf2)");
if(file.open(QIODevice::WriteOnly))
{
file.write(data);
file.close();
}
}
reply->deleteLater();
moveCP();
});
}
else
{
moveCP();
}
}
void MainWindow::moveCP()
{
if(std::filesystem::exists(R"(adafruit-circuitpython-seeeduino_xiao_rp2040-en_US-9.2.9.uf2)"))
{
if(!canMoveCP)
{
DWORD currentDrive = GetLogicalDrives();
for(char letter = 'A'; letter <= 'Z'; letter++)
{
if(currentDrive & (1 << (letter - 'A')))
{
std::wstring wrongDriveTargetLabel = L"RPI-RP2";
std::wstring root = std::wstring(1, letter) + L":\\";
wchar_t driveName[MAX_PATH] = {0};
if(GetVolumeInformationW(root.c_str(), driveName, MAX_PATH, nullptr, nullptr, nullptr, nullptr, 0))
{
if(wrongDriveTargetLabel == driveName)
{
driveRoot = letter;
canMoveCP = true;
}
}
}
}
}
if(canMoveCP)
{
std::filesystem::path currentPath = R"(adafruit-circuitpython-seeeduino_xiao_rp2040-en_US-9.2.9.uf2)";
std::filesystem::path newPath = driveRoot + R"(:\adafruit-circuitpython-seeeduino_xiao_rp2040-en_US-9.2.9.uf2)";
std::filesystem::copy(currentPath, newPath);
std::filesystem::remove(currentPath);
QTimer::singleShot(3000, [this]() {
updateUser(R"(Success installing
CircuitPython! Try updating
your firmware again!)");
});
}
else
{
QTimer::singleShot(3000, [this]() {
updateUser(R"(If your device is plugged in,
then looks like it needs to
be put in bootloader mode!)");
});
std::filesystem::remove(R"(adafruit-circuitpython-seeeduino_xiao_rp2040-en_US-9.2.9.uf2)");
}
}
}
void MainWindow::installKMK(bool installingAll)
{
if(!std::filesystem::exists(R"(C:\temp\kmk_firmware-main.zip)"))
{
updateUser("Installing KMK");
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
QNetworkReply* reply = manager->get(QNetworkRequest(QUrl("https://github.com/KMKfw/kmk_firmware/archive/refs/heads/main.zip")));
connect(reply, &QNetworkReply::finished, [reply, installingAll, this]()
{
bool firstFailed = true;
QByteArray data = reply->readAll();
if(reply->error() == QNetworkReply::NoError)
{
QFile file(R"(C:\temp\kmk_firmware-main.zip)");
if(file.open(QIODevice::WriteOnly))
{
file.write(data);
file.close();
firstFailed = false;
}
}
reply->deleteLater();
std::filesystem::path newPath = R"(C:\temp\kmk_firmware-main)";
if(!std::filesystem::exists(newPath))
{
QString zipPath = R"(C:\temp\kmk_firmware-main.zip)";
QString extrFile = R"(C:\temp)";
QString command = QString("powershell -Command \"Expand-Archive -Path '%1' -DestinationPath '%2'\"")
.arg(zipPath, extrFile);
if(QProcess::execute(command) == 0)
{
updateUser("KMK installed successfully");
}
else
{
updateUser("KMK installed unsuccessfully");
}
}
if(reply->error() == QNetworkReply::NoError)
{
QFile file(R"(C:\temp\kmk_firmware-main.zip)");
if(file.open(QIODevice::WriteOnly))
{
file.write(data);
file.close();
}
std::filesystem::path newPath = R"(C:\temp\kmk_firmware-main)";
if(!std::filesystem::exists(newPath))
{
QString zipPath = R"(C:\temp\kmk_firmware-main.zip)";
QString extrFile = R"(C:\temp)";
QString command = QString("powershell -Command \"Expand-Archive -Path '%1' -DestinationPath '%2'\"")
.arg(zipPath, extrFile);
if(QProcess::execute(command) == 0)
{
updateUser("KMK installed successfully");
}
else
{
updateUser("KMK installed unsuccessfully");
}
}
}
moveKMK(installingAll);
});
}
else
{
moveKMK(installingAll);
}
}
void MainWindow::moveKMK(bool installingAll)
{
if(!canMoveCP)
{
DWORD currentDrive = GetLogicalDrives();
for(char letter = 'A'; letter <= 'Z'; letter++)
{
if(currentDrive & (1 << (letter - 'A')))
{
std::wstring DriveTargetLabel = L"CIRCUITPY";
std::wstring root = std::wstring(1, letter) + L":\\";
wchar_t driveName[MAX_PATH] = {0};
if(GetVolumeInformationW(root.c_str(), driveName, MAX_PATH, nullptr, nullptr, nullptr, nullptr, 0))
{
if(DriveTargetLabel == driveName)
{
driveRoot = letter;
canMoveCP = true;
}
}
}
}
}
if(canMoveCP)
{
std::filesystem::path newPath = driveRoot + R"(:\boot.py)";
std::filesystem::path currentPath = R"(C:\temp\kmk_firmware-main\boot.py)";
std::filesystem::copy(currentPath, newPath, std::filesystem::copy_options::skip_existing);
newPath = driveRoot + R"(:\kmk)";
currentPath = R"(C:\temp\kmk_firmware-main\kmk)";
std::filesystem::copy(currentPath, newPath, std::filesystem::copy_options::recursive | std::filesystem::copy_options::skip_existing);
std::filesystem::remove_all(R"(C:\temp)");
QTimer::singleShot(3000, [this]() {
updateUser(R"(Success installing
Try updating
your firmware again!)");
});
}
else
{
std::filesystem::remove_all(R"(C:\temp)");
if(installingAll)
{
QTimer::singleShot(100, [this]() {
updateUser(R"(If your device is plugged in,
then looks like it needs to
be put in bootloader mode!)");
});
}
else
{
QTimer::singleShot(100, [this]() {
updateUser(R"(If your device is plugged in,
then looks like it needs to
have CircuitPython installed!)");
});
}
}
}