-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui.cpp
292 lines (260 loc) · 8.36 KB
/
gui.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
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
#include <iostream>
#include <fstream>
#include <string>
#include <stdexcept>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic ignored "-Winconsistent-missing-override"
#pragma GCC diagnostic ignored "-Wpotentially-evaluated-expression"
#include <wx/wx.h>
#include <wx/aboutdlg.h>
#include <wx/gauge.h>
#include <wx/custombgwin.h>
#pragma GCC diagnostic pop
#include "ihex.hpp"
#include "usb.hpp"
#ifdef __linux
unsigned char background_png[] = {
#include "background.inc"
};
#endif
enum {
ID_UPLOAD_THREAD_FINISHED,
ID_PROGRESS_TIMER,
};
class MainFrame;
class UploadThread : public wxThread {
public:
UploadThread(MainFrame *handler) : wxThread(wxTHREAD_DETACHED) { handler_ = handler; }
~UploadThread();
protected:
ExitCode Entry() override;
MainFrame *handler_;
};
class MainFrame : public wxFrame {
public:
MainFrame() : wxFrame(
nullptr, wxID_ANY, "Truly Ergonomic Keyboard - Firmware Upgrade",
wxPoint(50, 50), wxSize(537, 227),
wxDEFAULT_FRAME_STYLE & ~(wxRESIZE_BORDER | wxMAXIMIZE_BOX)
) {
auto file = new wxMenu;
auto help = new wxMenu;
auto menu = new wxMenuBar;
load_menu_ = file->Append(wxID_OPEN, "&Load\tCtrl-O", "Load Firmware.");
upload_menu_ = file->Append(wxID_UP, "&Upgrade\tCtrl-U", "Upgrade Firmware.");
file->AppendSeparator();
file->Append(wxID_EXIT);
help->Append(wxID_ABOUT);
menu->Append(file, "&File");
menu->Append(help, "&Help");
SetMenuBar(menu);
CreateStatusBar();
auto panel = new wxCustomBackgroundWindow<wxPanel>();
panel->Create(this, -1, wxPoint(0, 0), wxSize(537, 227));
panel->SetBackgroundBitmap(wxBITMAP_PNG(background));
load_ = new wxButton(panel, wxID_OPEN, "1. Load", wxPoint(), wxSize(100, 28));
load_->SetPosition(wxPoint(30, 100 - load_->GetSize().GetHeight() / 2));
filename_ = new wxTextCtrl(panel, -1, wxEmptyString, wxPoint(), wxSize(357, 22), wxTE_READONLY);
filename_->SetPosition(wxPoint(150, 100 - filename_->GetSize().GetHeight() / 2));
upload_ = new wxButton(panel, wxID_UP, "2. Upgrade", wxPoint(), wxSize(100, 28));
upload_->SetPosition(wxPoint(30, 165 - upload_->GetSize().GetHeight() / 2));
progress_ = new wxGauge(panel, -1, 100, wxPoint(), wxSize(357, 22));
progress_->SetPosition(wxPoint(150, 165 - progress_->GetSize().GetHeight() / 2));
timer_ = new wxTimer(this, ID_PROGRESS_TIMER);
Fit();
enable(true, false);
SetStatusText("Ready.");
}
void enable(bool load, bool upload) {
load_->Enable(load);
load_menu_->Enable(load);
upload_->Enable(upload);
upload_menu_->Enable(upload);
if (!upload) progress_->SetValue(0);
if (load && !upload) load_->SetFocus();
else if (upload) upload_->SetFocus();
}
private:
wxTextCtrl *filename_ = nullptr;
wxButton *load_ = nullptr;
wxMenuItem *load_menu_ = nullptr;
wxButton *upload_ = nullptr;
wxMenuItem *upload_menu_ = nullptr;
wxGauge *progress_ = nullptr;
UploadThread *upload_thread_ = nullptr;
wxCriticalSection upload_thread_cs_;
wxTimer *timer_ = nullptr;
std::vector<std::uint8_t> firmware_;
friend class UploadThread;
void OnLoad(wxCommandEvent &e) {
auto fn = wxFileSelector(
"Select Firmware.",
wxEmptyString,
wxEmptyString,
wxEmptyString,
"HEX files (*.hex)|*.hex|All files|*",
0,
this
);
if (!fn) return;
enable(true, false);
progress_->SetValue(0);
SetStatusText("Loading file.");
try {
std::ifstream f(fn.ToStdString());
if (!f) throw std::runtime_error{"Unable to open file."};
firmware_ = load_ihex(f);
} catch (std::exception &e) {
SetStatusText("Error while loading file.");
wxMessageBox(e.what(), "Error while loading file", wxICON_ERROR | wxOK | wxCENTRE, this);
return;
}
filename_->SetValue(fn);
filename_->SetInsertionPoint(filename_->GetLastPosition());
SetStatusText("Ready to Upgrade.");
enable(true, true);
}
void OnUpload(wxCommandEvent &e) {
wxCriticalSectionLocker enter(upload_thread_cs_);
enable(false, false);
SetStatusText("Upgrading...");
timer_->Start(50);
upload_thread_ = new UploadThread(this);
upload_thread_->Create(1024*1024);
upload_thread_->Run();
}
void OnAbout(wxCommandEvent &e) {
wxAboutDialogInfo info;
info.SetName("Truly Ergonomic Keyboard - Firmware Upgrade");
info.SetVersion("1.3.0");
info.SetDescription("This tool upgrades the Firmware of your Truly Ergonomic keyboard over USB.\nThe source code is available on: http://github.com/m-ou-se/tek");
info.SetCopyright("2015 - Mara Bos");
wxAboutBox(info);
}
void OnExit(wxCommandEvent &e) {
wxCriticalSectionLocker enter(upload_thread_cs_);
if (upload_thread_) return;
Close(true);
}
void OnUpdateThreadFinished(wxThreadEvent &e) {
timer_->Stop();
auto err = e.GetPayload<std::string>();
if (err.empty()) {
progress_->SetValue(100);
SetStatusText("Keyboard Firmware upgraded.");
wxMessageBox("Succesfully upgraded Firmware.", "Done");
} else {
progress_->SetValue(0);
SetStatusText("Error while upgrading.");
wxMessageBox("Error while upgrading firmware:\n\n" + err, "Error", wxICON_ERROR | wxOK | wxCENTRE);
}
enable(true, true);
}
void OnProgressTick(wxTimerEvent &e) {
progress_->Pulse();
}
wxDECLARE_EVENT_TABLE();
};
wxBEGIN_EVENT_TABLE(MainFrame, wxFrame)
EVT_MENU(wxID_OPEN, MainFrame::OnLoad)
EVT_MENU(wxID_UP, MainFrame::OnUpload)
EVT_MENU(wxID_EXIT, MainFrame::OnExit)
EVT_MENU(wxID_ABOUT, MainFrame::OnAbout)
EVT_BUTTON(wxID_OPEN, MainFrame::OnLoad)
EVT_BUTTON(wxID_UP, MainFrame::OnUpload)
EVT_THREAD(ID_UPLOAD_THREAD_FINISHED, MainFrame::OnUpdateThreadFinished)
EVT_TIMER(ID_PROGRESS_TIMER, MainFrame::OnProgressTick)
wxEND_EVENT_TABLE()
UploadThread::~UploadThread() {
wxCriticalSectionLocker enter(handler_->upload_thread_cs_);
handler_->upload_thread_ = nullptr;
}
wxThread::ExitCode UploadThread::Entry() {
std::string err;
try {
std::pair<libusb_device_handle *, bool> c;
auto &dev = c.first;
auto &need_switch = c.second;
std::cerr << "Connecting to keyboard." << std::endl;
try {
c = usb::connect();
} catch (std::exception &e) {
std::cerr << "Error in connect: " << e.what() << std::endl;
wxSleep(1);
std::cerr << "Trying again to connect." << std::endl;
c = usb::connect();
}
if (!dev) throw std::runtime_error{"No Truly Ergonomic keyboard found."};
std::clog << "Found keyboard." << std::endl;
if (need_switch) {
std::clog << "It is in normal mode." << std::endl;
int retries = 0;
while (true) {
if (dev) {
std::clog << "Switching keyboard to upgrade mode." << std::endl;
try {
usb::switch_mode(dev);
} catch (std::exception &e) {
std::cerr << "Error in switch_mode: " << e.what() << std::endl;
}
usb::close(dev);
}
wxSleep(1);
std::clog << "Reconnecting." << std::endl;
try {
c = usb::connect();
} catch (std::exception &e) {
std::cerr << "Error in connect: " << e.what() << std::endl;
dev = nullptr;
}
if (!dev) {
std::clog << "No keyboard found." << std::endl;
} else if (need_switch) {
std::clog << "Reconnected, but keyboard is in normal mode." << std::endl;
} else {
std::clog << "Reconnected to keyboard in upgrade mode." << std::endl;
break;
}
if (++retries < 5) {
std::clog << "Retrying." << std::endl;
} else {
std::clog << "Giving up." << std::endl;
break;
}
}
if (!dev) {
throw std::runtime_error{"Keyboard did not reconnect after switching it to firmware upgrade mode.\nPlease reconnect it and try again."};
}
if (need_switch) {
usb::close(dev);
throw std::runtime_error{"Unable to switch keyboard to firmware upgrade mode.\nMake sure DIP #5 is set to OFF."};
}
} else {
std::clog << "It is already in upgrade mode." << std::endl;
}
std::clog << "Uploading firmware." << std::endl;
usb::program(dev, handler_->firmware_.data(), handler_->firmware_.size());
std::clog << "Switching back to normal mode." << std::endl;
usb::switch_mode(dev);
usb::close(dev);
std::clog << "Done." << std::endl;
} catch (std::exception &e) {
std::clog << "Error: " << e.what() << std::endl;
err = e.what();
}
auto ev = new wxThreadEvent(wxEVT_THREAD, ID_UPLOAD_THREAD_FINISHED);
ev->SetPayload<std::string>(std::move(err));
wxQueueEvent(handler_, ev);
return 0;
}
class TekApp : public wxApp {
public:
bool OnInit() override {
wxInitAllImageHandlers();
auto frame = new MainFrame();
frame->Show(true);
return true;
}
};
wxIMPLEMENT_APP(TekApp);