-
-
Notifications
You must be signed in to change notification settings - Fork 339
/
SerialWindow.cpp
349 lines (274 loc) · 9.29 KB
/
SerialWindow.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
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
/*
* Copyright 2012-2014, Adrien Destugues, pulkomandy@pulkomandy.tk
* Distributed under the terms of the MIT licence.
*/
#include "SerialWindow.h"
#include <stdio.h>
#include <FilePanel.h>
#include <GroupLayout.h>
#include <Menu.h>
#include <MenuBar.h>
#include <MenuItem.h>
#include <ScrollView.h>
#include <SerialPort.h>
#include "SerialApp.h"
#include "TermView.h"
const int SerialWindow::kBaudrates[] = { 75, 110, 300, 1200, 2400, 4800, 9600,
19200, 31250, 38400, 57600, 115200, 230400
};
// The values for these constants are not in the expected order, so we have to
// rely on this lookup table if we want to keep the menu items sorted.
const int SerialWindow::kBaudrateConstants[] = { B_50_BPS, B_75_BPS, B_110_BPS,
B_134_BPS, B_150_BPS, B_200_BPS, B_300_BPS, B_600_BPS, B_1200_BPS,
B_1800_BPS, B_2400_BPS, B_4800_BPS, B_9600_BPS, B_19200_BPS, B_31250_BPS,
B_38400_BPS, B_57600_BPS, B_115200_BPS, B_230400_BPS
};
const char* SerialWindow::kWindowTitle = "SerialConnect";
SerialWindow::SerialWindow()
: BWindow(BRect(100, 100, 400, 400), SerialWindow::kWindowTitle,
B_DOCUMENT_WINDOW, B_QUIT_ON_WINDOW_CLOSE | B_AUTO_UPDATE_SIZE_LIMITS)
, fLogFilePanel(NULL)
{
BMenuBar* menuBar = new BMenuBar(Bounds(), "menuBar");
menuBar->ResizeToPreferred();
BRect r = Bounds();
r.top = menuBar->Bounds().bottom + 1;
r.right -= B_V_SCROLL_BAR_WIDTH;
fTermView = new TermView(r);
fTermView->ResizeToPreferred();
r = fTermView->Frame();
r.left = r.right + 1;
r.right = r.left + B_V_SCROLL_BAR_WIDTH;
r.top -= 1;
r.bottom -= B_H_SCROLL_BAR_HEIGHT - 1;
BScrollBar* scrollBar = new BScrollBar(r, "scrollbar", NULL, 0, 0,
B_VERTICAL);
scrollBar->SetTarget(fTermView);
ResizeTo(r.right - 1, r.bottom + B_H_SCROLL_BAR_HEIGHT - 1);
AddChild(menuBar);
AddChild(fTermView);
AddChild(scrollBar);
fConnectionMenu = new BMenu("Connection");
BMenu* fileMenu = new BMenu("File");
BMenu* settingsMenu = new BMenu("Settings");
fConnectionMenu->SetRadioMode(true);
menuBar->AddItem(fConnectionMenu);
menuBar->AddItem(fileMenu);
menuBar->AddItem(settingsMenu);
// TODO edit menu - what's in it ?
//BMenu* editMenu = new BMenu("Edit");
//menuBar->AddItem(editMenu);
BMenuItem* logFile = new BMenuItem("Log to file" B_UTF8_ELLIPSIS,
new BMessage(kMsgLogfile));
fileMenu->AddItem(logFile);
#if 0
// TODO implement these
BMenuItem* xmodemSend = new BMenuItem("X/Y/ZModem send" B_UTF8_ELLIPSIS,
NULL);
fileMenu->AddItem(xmodemSend);
BMenuItem* xmodemReceive = new BMenuItem(
"X/Y/Zmodem receive" B_UTF8_ELLIPSIS, NULL);
fileMenu->AddItem(xmodemReceive);
#endif
// Configuring all this by menus may be a bit unhandy. Make a setting
// window instead ?
fBaudrateMenu = new BMenu("Baud rate");
fBaudrateMenu->SetRadioMode(true);
settingsMenu->AddItem(fBaudrateMenu);
fParityMenu = new BMenu("Parity");
fParityMenu->SetRadioMode(true);
settingsMenu->AddItem(fParityMenu);
fStopbitsMenu = new BMenu("Stop bits");
fStopbitsMenu->SetRadioMode(true);
settingsMenu->AddItem(fStopbitsMenu);
fFlowcontrolMenu = new BMenu("Flow control");
fFlowcontrolMenu->SetRadioMode(true);
settingsMenu->AddItem(fFlowcontrolMenu);
fDatabitsMenu = new BMenu("Data bits");
fDatabitsMenu->SetRadioMode(true);
settingsMenu->AddItem(fDatabitsMenu);
BMessage* message = new BMessage(kMsgSettings);
message->AddInt32("parity", B_NO_PARITY);
BMenuItem* parityNone = new BMenuItem("None", message);
message = new BMessage(kMsgSettings);
message->AddInt32("parity", B_ODD_PARITY);
BMenuItem* parityOdd = new BMenuItem("Odd", message);
message = new BMessage(kMsgSettings);
message->AddInt32("parity", B_EVEN_PARITY);
BMenuItem* parityEven = new BMenuItem("Even", message);
fParityMenu->AddItem(parityNone);
fParityMenu->AddItem(parityOdd);
fParityMenu->AddItem(parityEven);
fParityMenu->SetTargetForItems(be_app);
message = new BMessage(kMsgSettings);
message->AddInt32("databits", B_DATA_BITS_7);
BMenuItem* data7 = new BMenuItem("7", message);
message = new BMessage(kMsgSettings);
message->AddInt32("databits", B_DATA_BITS_8);
BMenuItem* data8 = new BMenuItem("8", message);
fDatabitsMenu->AddItem(data7);
fDatabitsMenu->AddItem(data8);
fDatabitsMenu->SetTargetForItems(be_app);
message = new BMessage(kMsgSettings);
message->AddInt32("stopbits", B_STOP_BITS_1);
BMenuItem* stop1 = new BMenuItem("1", message);
message = new BMessage(kMsgSettings);
message->AddInt32("stopbits", B_STOP_BITS_2);
BMenuItem* stop2 = new BMenuItem("2", message);
fStopbitsMenu->AddItem(stop1);
fStopbitsMenu->AddItem(stop2);
fStopbitsMenu->SetTargetForItems(be_app);
// Loop backwards to add fastest rates at top of menu
for (int i = sizeof(kBaudrates) / sizeof(kBaudrates[0]); --i >= 0;)
{
message = new BMessage(kMsgSettings);
message->AddInt32("baudrate", kBaudrateConstants[i]);
char buffer[7];
sprintf(buffer, "%d", kBaudrates[i]);
BMenuItem* item = new BMenuItem(buffer, message);
fBaudrateMenu->AddItem(item);
}
fBaudrateMenu->SetTargetForItems(be_app);
message = new BMessage(kMsgSettings);
message->AddInt32("flowcontrol", B_HARDWARE_CONTROL);
BMenuItem* hardware = new BMenuItem("Hardware", message);
message = new BMessage(kMsgSettings);
message->AddInt32("flowcontrol", B_SOFTWARE_CONTROL);
BMenuItem* software = new BMenuItem("Software", message);
message = new BMessage(kMsgSettings);
message->AddInt32("flowcontrol", B_HARDWARE_CONTROL | B_SOFTWARE_CONTROL);
BMenuItem* both = new BMenuItem("Both", message);
message = new BMessage(kMsgSettings);
message->AddInt32("flowcontrol", 0);
BMenuItem* noFlow = new BMenuItem("None", message);
fFlowcontrolMenu->AddItem(hardware);
fFlowcontrolMenu->AddItem(software);
fFlowcontrolMenu->AddItem(both);
fFlowcontrolMenu->AddItem(noFlow);
fFlowcontrolMenu->SetTargetForItems(be_app);
CenterOnScreen();
}
SerialWindow::~SerialWindow()
{
delete fLogFilePanel;
}
void SerialWindow::MenusBeginning()
{
// remove all items from the menu
fConnectionMenu->RemoveItems(0, fConnectionMenu->CountItems(), true);
// fill it with the (updated) serial port list
BSerialPort serialPort;
int deviceCount = serialPort.CountDevices();
bool connected = false;
for (int i = 0; i < deviceCount; i++)
{
char buffer[256];
serialPort.GetDeviceName(i, buffer, 256);
BMessage* message = new BMessage(kMsgOpenPort);
message->AddString("port name", buffer);
BMenuItem* portItem = new BMenuItem(buffer, message);
portItem->SetTarget(be_app);
const BString& connectedPort = ((SerialApp*)be_app)->GetPort();
if (connectedPort == buffer) {
connected = true;
portItem->SetMarked(true);
}
fConnectionMenu->AddItem(portItem);
}
if (deviceCount > 0) {
fConnectionMenu->AddSeparatorItem();
BMenuItem* disconnect = new BMenuItem("Disconnect",
new BMessage(kMsgOpenPort), 'Z', B_OPTION_KEY);
if (!connected)
disconnect->SetEnabled(false);
disconnect->SetTarget(be_app);
fConnectionMenu->AddItem(disconnect);
} else {
BMenuItem* noDevices = new BMenuItem("<no serial port available>", NULL);
noDevices->SetEnabled(false);
fConnectionMenu->AddItem(noDevices);
}
}
void SerialWindow::MessageReceived(BMessage* message)
{
switch (message->what)
{
case kMsgDataRead:
{
const char* bytes;
ssize_t length;
if (message->FindData("data", B_RAW_TYPE, (const void**)&bytes,
&length) == B_OK)
fTermView->PushBytes(bytes, length);
return;
}
case kMsgLogfile:
{
// Let's lazy init the file panel
if (fLogFilePanel == NULL) {
fLogFilePanel = new BFilePanel(B_SAVE_PANEL, &be_app_messenger,
NULL, B_FILE_NODE, false);
fLogFilePanel->SetMessage(message);
}
fLogFilePanel->Show();
return;
}
case kMsgSettings:
{
int32 baudrate;
stop_bits stopBits;
data_bits dataBits;
parity_mode parity;
uint32 flowcontrol;
if (message->FindInt32("databits", (int32*)&dataBits) == B_OK) {
for (int i = 0; i < fDatabitsMenu->CountItems(); i++) {
BMenuItem* item = fDatabitsMenu->ItemAt(i);
int32 code;
item->Message()->FindInt32("databits", &code);
if (code == dataBits)
item->SetMarked(true);
}
}
if (message->FindInt32("stopbits", (int32*)&stopBits) == B_OK) {
for (int i = 0; i < fStopbitsMenu->CountItems(); i++) {
BMenuItem* item = fStopbitsMenu->ItemAt(i);
int32 code;
item->Message()->FindInt32("stopbits", &code);
if (code == stopBits)
item->SetMarked(true);
}
}
if (message->FindInt32("parity", (int32*)&parity) == B_OK)
{
for (int i = 0; i < fParityMenu->CountItems(); i++) {
BMenuItem* item = fParityMenu->ItemAt(i);
int32 code;
item->Message()->FindInt32("parity", &code);
if (code == parity)
item->SetMarked(true);
}
}
if (message->FindInt32("flowcontrol", (int32*)&flowcontrol)
== B_OK) {
for (int i = 0; i < fFlowcontrolMenu->CountItems(); i++) {
BMenuItem* item = fFlowcontrolMenu->ItemAt(i);
int32 code;
item->Message()->FindInt32("flowcontrol", &code);
if (code == (int32)flowcontrol)
item->SetMarked(true);
}
}
if (message->FindInt32("baudrate", &baudrate) == B_OK) {
for (int i = 0; i < fBaudrateMenu->CountItems(); i++) {
BMenuItem* item = fBaudrateMenu->ItemAt(i);
int32 code;
item->Message()->FindInt32("baudrate", &code);
if (baudrate == code)
item->SetMarked(true);
}
}
return;
}
}
BWindow::MessageReceived(message);
}