-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhotoPrint.cpp
More file actions
321 lines (256 loc) · 9.71 KB
/
Copy pathPhotoPrint.cpp
File metadata and controls
321 lines (256 loc) · 9.71 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
#include <SDKDDKVer.h>
#include <BX3SSU.hpp>
#include <Arduino.hpp>
#include <Serial.hpp>
// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
class MyFrame : public wxFrame
{
public:
MyFrame(const wxString& title);
private:
asio::io_service io;
asio::serial_port serial;
BX3SSU *stage=nullptr;
wxChoice* PortChoice;
wxArrayString PortList;
void OnConnect(wxCommandEvent& event);
void OnSelectPort(wxCommandEvent& event);
void RefreshCOMs(wxCommandEvent& event);
wxButton* ConnectButton;
void OnMoveStage(wxCommandEvent& event);
void OnSetStageSpeed(wxCommandEvent& event);
void OnExit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void OnStartPasses(wxCommandEvent& event);
wxPanel *StageXYMPanel;
wxPanel *MultiPassPanel;
wxTextCtrl *StageSPDCtrl;
wxTextCtrl *StageXMCtrl;
wxTextCtrl *StageYMCtrl;
wxTextCtrl *PassesCtrl;
};
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame("PhotoPrint");
frame->Show(true);
return true;
}
MyFrame::MyFrame(const wxString& title):
wxFrame(NULL, wxID_ANY, title), serial(io)
{
// Set the frame icon
SetIcon(wxICON(AppIcon));
this->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW ) );
// Menu bar
wxMenu *menuFile = new wxMenu;
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT,"&Exit\tCtrl-Q", "Exit the application");
wxMenu *menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append(menuFile, "&File");
menuBar->Append(menuHelp, "&Help");
SetMenuBar( menuBar );
Bind(wxEVT_MENU, &MyFrame::OnAbout, this, wxID_ABOUT);
Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT);
// Main window panel
wxBoxSizer* FrameBoxSizer = new wxBoxSizer(wxVERTICAL);
// Port selection panel
wxPanel *PortSelectPanel = new wxPanel(this,wxID_ANY);
wxStaticBoxSizer *PortSelectSizer = new wxStaticBoxSizer(wxHORIZONTAL,PortSelectPanel,wxT("Port Selection"));
PortSelectPanel -> SetSizer(PortSelectSizer);
// Port selection
PortChoice = new wxChoice(PortSelectPanel,wxID_ANY,wxDefaultPosition,wxDefaultSize,PortList);
PortChoice -> Bind(wxEVT_CHOICE,&MyFrame::OnSelectPort,this);
// Connection button
wxButton * ReloadCOMsButton = new wxButton(PortSelectPanel,wxID_ANY,wxT("Search"),wxDefaultPosition);
ReloadCOMsButton -> Bind(wxEVT_BUTTON,&MyFrame::RefreshCOMs,this);
ConnectButton = new wxButton(PortSelectPanel,wxID_ANY,wxT("Connect"),wxDefaultPosition);
ConnectButton -> Bind(wxEVT_BUTTON,&MyFrame::OnConnect,this);
ConnectButton -> Enable(false);
PortSelectSizer -> Add(PortChoice, 1, wxALL, 5);
PortSelectSizer -> Add(ReloadCOMsButton, 0, wxALL, 5);
PortSelectSizer -> Add(ConnectButton, 0, wxALL, 5);
// Stage movement
StageXYMPanel = new wxPanel(this,wxID_ANY);
wxStaticBoxSizer *StageXYMSizer = new wxStaticBoxSizer(wxHORIZONTAL,StageXYMPanel,wxT("Stage Movement"));
StageXYMPanel -> SetSizer(StageXYMSizer);
// Stage motion controls
wxBoxSizer* StageCtrlSizer = new wxBoxSizer(wxVERTICAL);
wxBoxSizer* StageSPDSizer = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer* StageXCtrlSizer = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer* StageYCtrlSizer = new wxBoxSizer(wxHORIZONTAL);
wxStaticText *StageSPDLabel = new wxStaticText(StageXYMPanel,wxID_ANY,wxT("Speed [um/s]"));
StageSPDCtrl = new wxTextCtrl(StageXYMPanel,wxID_ANY,wxT("0"));
wxButton * SetStageSpeedButton = new wxButton(StageXYMPanel,wxID_ANY,wxT("Set"),wxDefaultPosition);
SetStageSpeedButton -> Bind(wxEVT_BUTTON,&MyFrame::OnSetStageSpeed,this);
StageSPDSizer -> Add(StageSPDLabel, 0, wxALIGN_CENTER|wxALL, 5);
StageSPDSizer -> Add(StageSPDCtrl, 1, wxALL, 5);
StageSPDSizer -> Add(SetStageSpeedButton, 0, wxEXPAND|wxALL, 5);
StageCtrlSizer -> Add(StageSPDSizer, 1, wxEXPAND);
wxStaticText *StageXMLabel = new wxStaticText(StageXYMPanel,wxID_ANY,wxT("X [um]"));
StageXMCtrl = new wxTextCtrl(StageXYMPanel,wxID_ANY,wxT("0"));
wxStaticText *StageYMLabel = new wxStaticText(StageXYMPanel,wxID_ANY,wxT("Y [um]"));
StageYMCtrl = new wxTextCtrl(StageXYMPanel,wxID_ANY,wxT("0"));
wxButton * MoveStageButton = new wxButton(StageXYMPanel,wxID_ANY,wxT("Move"),wxDefaultPosition);
MoveStageButton -> Bind(wxEVT_BUTTON,&MyFrame::OnMoveStage,this);
StageXCtrlSizer -> Add(StageXMLabel, 0, wxALIGN_CENTER|wxALL, 5);
StageXCtrlSizer -> Add(StageXMCtrl, 1, wxALL, 5);
StageYCtrlSizer -> Add(StageYMLabel, 0, wxALIGN_CENTER|wxALL, 5);
StageYCtrlSizer -> Add(StageYMCtrl, 1, wxALL, 5);
StageCtrlSizer -> Add(StageXCtrlSizer, 1, wxEXPAND);
StageCtrlSizer -> Add(StageYCtrlSizer, 1, wxEXPAND);
StageXYMSizer -> Add(StageCtrlSizer, 1, wxALL, 5);
StageXYMSizer -> Add(MoveStageButton, 0, wxEXPAND|wxALL, 5);
// Multi-pass panel
MultiPassPanel = new wxPanel(this, wxID_ANY);
wxStaticBoxSizer *MultiPassSizer = new wxStaticBoxSizer(wxHORIZONTAL, MultiPassPanel, wxT("Multi-Pass"));
MultiPassPanel->SetSizer(MultiPassSizer);
// Multi-pass controls
wxStaticText *PassesLabel = new wxStaticText(MultiPassPanel, wxID_ANY, wxT("Number of Passes"));
PassesCtrl = new wxTextCtrl(MultiPassPanel, wxID_ANY, wxT("1"));
wxButton *StartPassesButton = new wxButton(MultiPassPanel, wxID_ANY, wxT("Start"), wxDefaultPosition);
StartPassesButton->Bind(wxEVT_BUTTON, &MyFrame::OnStartPasses, this);
// Add controls to sizer
MultiPassSizer->Add(PassesLabel, 0, wxALIGN_CENTER | wxALL, 5);
MultiPassSizer->Add(PassesCtrl, 1, wxALL, 5);
MultiPassSizer->Add(StartPassesButton, 0, wxEXPAND | wxALL, 5);
// Disable stage movement controls
StageXYMPanel -> Enable(false);
MultiPassPanel -> Enable(false);
// Status bar
CreateStatusBar();
SetStatusText("Search serial devices to start");
// Add content to main sizer
FrameBoxSizer->Add(PortSelectPanel,0,wxEXPAND|wxALL,5);
FrameBoxSizer->Add(StageXYMPanel,0,wxEXPAND|wxALL,5);
FrameBoxSizer->Add(MultiPassPanel, 0, wxEXPAND | wxALL, 5);
// Set size hints
FrameBoxSizer -> SetSizeHints(this);
// Set sizer
this -> SetSizer(FrameBoxSizer);
this -> Layout();
}
void MyFrame::OnExit(wxCommandEvent& event)
{
Close(true);
}
void MyFrame::OnAbout(wxCommandEvent& event)
{
wxMessageBox("PhotoPrint was developed by Ariel S. Boiardi at SISSA BIOMAT Lab",
"About PhotoPrint", wxOK);
}
void MyFrame::OnMoveStage(wxCommandEvent& event)
{
int x = wxAtoi(StageXMCtrl->GetValue());
int y = wxAtoi(StageYMCtrl->GetValue());
wxLogStatus(wxString("Moving stage by (") + wxString::Format("%d", x) + wxString(", ") + wxString::Format("%d", y) + wxString(") um"));
if(stage -> isConnected())
{
stage->move(1000*x,1000*y);
}
else
{
wxLogStatus(wxString("Stage not connected"));
}
wxLogStatus(wxString("Finished"));
}
void MyFrame::OnSetStageSpeed(wxCommandEvent &event)
{
int speed = wxAtoi(StageSPDCtrl->GetValue());
wxLogStatus(wxString("Setting stage speed to ") + wxString::Format("%d", speed) + wxString(" um/s"));
if(stage -> isConnected())
{
stage->setSpeed(speed/10);
}
else
{
wxLogStatus(wxString("Stage not connected"));
}
}
void MyFrame::OnStartPasses(wxCommandEvent& event)
{
int passes = wxAtoi(PassesCtrl->GetValue());
wxLogStatus(wxString("Starting ") + wxString::Format("%d", passes) + wxString(" passes"));
int x = wxAtoi(StageXMCtrl->GetValue());
int y = wxAtoi(StageYMCtrl->GetValue());
int sign = 1;
if(stage->isConnected())
{
for(int i = 0; i < passes; ++i)
{
wxLogStatus(wxString("Pass ") + wxString::Format("%d", i + 1));
// Move stage
stage->move(sign*1000*x,sign*1000*y);
// Change direction
sign *= -1;
}
wxLogStatus("All passes completed");
}
else
{
wxLogStatus("Stage not connected");
}
}
void MyFrame::OnConnect(wxCommandEvent &event)
{
if(stage==nullptr)
{
// Get selected port
auto choice = PortChoice -> GetSelection();
std::string SelectedPort = PortList[choice];
// Create BX3SSU object
stage = new BX3SSU(io,SelectedPort);
// Enable stage movement controls
StageXYMPanel -> Enable(true);
MultiPassPanel -> Enable(true);
// Update speed
StageSPDCtrl -> SetValue(wxString::Format("%d",stage->speed*10));
// Update button label
ConnectButton->SetLabel("Disconnect");
SetStatusText("Connected to "+PortList[PortChoice->GetSelection()]);
}
else
{
// Delete BX3SSU object
stage->~BX3SSU();
stage = nullptr;
// Disable stage movement controls
StageXYMPanel -> Enable(false);
// Update button label
ConnectButton->SetLabel("Connect");
SetStatusText("Connect to a port");
}
}
void MyFrame::RefreshCOMs(wxCommandEvent &event)
{
SetStatusText("Searching for serial devices");
serial.close();
Serial temp_serial(io);
auto ports_avail = temp_serial.enumerate_ports();
PortList.Clear();
for (const std::string& port : ports_avail)
{
PortList.Add(port);
}
PortChoice->Clear();
PortChoice->Append(PortList);
if(PortList.GetCount()>0)
SetStatusText("Select a port to connect");
else
SetStatusText("No serial devices found");
}
void MyFrame::OnSelectPort(wxCommandEvent &event)
{
ConnectButton->Enable(true);
}