OpenCPN Partial API docs
Loading...
Searching...
No Matches
send_to_gps_dlg.cpp
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2010 by David S. Register *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
24#include <wx/arrstr.h>
25#include <wx/button.h>
26#include <wx/combobox.h>
27#include <wx/dialog.h>
28#include <wx/dynarray.h>
29#include <wx/event.h>
30#include <wx/gdicmn.h>
31#include <wx/sizer.h>
32#include <wx/stattext.h>
33#include <wx/string.h>
34#include <wx/window.h>
35
36#include "model/config_vars.h"
37#include "model/conn_params.h"
38#include "model/rest_server.h"
39#include "model/route.h"
40#include "model/route_point.h"
41#include "model/ser_ports.h"
42
43#include "ocpn_platform.h"
44#include "route_gui.h"
45#include "route_point_gui.h"
46#include "send_to_gps_dlg.h"
47
48BEGIN_EVENT_TABLE(SendToGpsDlg, wxDialog)
49EVT_BUTTON(ID_STG_CANCEL, SendToGpsDlg::OnCancelClick)
50EVT_BUTTON(ID_STG_OK, SendToGpsDlg::OnSendClick)
51END_EVENT_TABLE()
52
54 m_itemCommListBox = NULL;
55 m_pgauge = NULL;
56 m_SendButton = NULL;
57 m_CancelButton = NULL;
58 m_pRoute = NULL;
59 m_pRoutePoint = NULL;
60 premtext = NULL;
61}
62
63SendToGpsDlg::SendToGpsDlg(wxWindow* parent, wxWindowID id,
64 const wxString& caption, const wxString& hint,
65 const wxPoint& pos, const wxSize& size, long style) {
66 Create(parent, id, caption, hint, pos, size, style);
67}
68
69SendToGpsDlg::~SendToGpsDlg() {
70 delete m_itemCommListBox;
71 delete m_pgauge;
72 delete m_SendButton;
73 delete m_CancelButton;
74}
75
76bool SendToGpsDlg::Create(wxWindow* parent, wxWindowID id,
77 const wxString& caption, const wxString& hint,
78 const wxPoint& pos, const wxSize& size, long style) {
79 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS);
80 wxDialog::Create(parent, id, caption, pos, size, style);
81
82 CreateControls(hint);
83 GetSizer()->Fit(this);
84 GetSizer()->SetSizeHints(this);
85 Centre();
86
87 return TRUE;
88}
89
90void SendToGpsDlg::CreateControls(const wxString& hint) {
91 SendToGpsDlg* itemDialog1 = this;
92
93 wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
94 itemDialog1->SetSizer(itemBoxSizer2);
95
96 // Create the ScrollBox list of available com ports in a labeled static
97 // box
98 wxStaticBox* comm_box =
99 new wxStaticBox(this, wxID_ANY, _("GPS/Plotter Port"));
100
101 wxStaticBoxSizer* comm_box_sizer = new wxStaticBoxSizer(comm_box, wxVERTICAL);
102 itemBoxSizer2->Add(comm_box_sizer, 0, wxEXPAND | wxALL, 5);
103
104 wxArrayString* pSerialArray = EnumerateSerialPorts();
105
106 m_itemCommListBox = new wxComboBox(this, ID_STG_CHOICE_COMM);
107
108 // Fill in the listbox with all detected serial ports
109 for (unsigned int iPortIndex = 0; iPortIndex < pSerialArray->GetCount();
110 iPortIndex++) {
111 wxString full_port = pSerialArray->Item(iPortIndex);
112 full_port.Prepend("Serial:");
113 m_itemCommListBox->Append(full_port);
114 }
115
116 delete pSerialArray;
117
118 // Add any defined Network connections supporting "output"
119 wxArrayString netconns;
120 for (auto* cp : TheConnectionParams()) {
121 wxString netident;
122
123 if ((cp->IOSelect != DS_TYPE_INPUT) && cp->Type == NETWORK &&
124 (cp->NetProtocol == TCP)) {
125 netident << "TCP:" << cp->NetworkAddress << ":" << cp->NetworkPort;
126 m_itemCommListBox->Append(netident);
127 netconns.Add(netident);
128 }
129 if ((cp->IOSelect != DS_TYPE_INPUT) && cp->Type == NETWORK &&
130 (cp->NetProtocol == UDP)) {
131 netident << "UDP:" << cp->NetworkAddress << ":" << cp->NetworkPort;
132 m_itemCommListBox->Append(netident);
133 netconns.Add(netident);
134 }
135 }
136
137 // Add Bluetooth, if the platform supports it natively
138 if (g_Platform) {
139 if (g_Platform->startBluetoothScan()) {
140 wxSleep(2);
141 wxArrayString btscanResults = g_Platform->getBluetoothScanResults();
142
143 unsigned int i = 1;
144 while ((i + 1) < btscanResults.GetCount()) {
145 wxString item1 = btscanResults[i] + ";";
146 wxString item2 = btscanResults.Item(i + 1);
147 wxString port = item1 + item2;
148 port.Prepend("Bluetooth:");
149 m_itemCommListBox->Append(port);
150
151 i += 2;
152 }
153
154 g_Platform->stopBluetoothScan();
155 }
156 }
157
158 // Make the proper initial selection
159 if (!g_uploadConnection.IsEmpty()) {
160 if (g_uploadConnection.Lower().StartsWith("tcp") ||
161 g_uploadConnection.Lower().StartsWith("udp")) {
162 bool b_connExists = false;
163 for (unsigned int i = 0; i < netconns.GetCount(); i++) {
164 if (g_uploadConnection.IsSameAs(netconns[i])) {
165 b_connExists = true;
166 break;
167 }
168 }
169 if (b_connExists) m_itemCommListBox->SetValue(g_uploadConnection);
170 } else
171 m_itemCommListBox->SetValue(g_uploadConnection);
172 } else
173 m_itemCommListBox->SetSelection(0);
174
175 comm_box_sizer->Add(m_itemCommListBox, 0, wxEXPAND | wxALL, 5);
176
177 // Add a reminder text box
178 itemBoxSizer2->AddSpacer(20);
179
180 premtext = new wxStaticText(
181 this, -1, _("Prepare GPS for Route/Waypoint upload and press Send..."));
182 itemBoxSizer2->Add(premtext, 0, wxEXPAND | wxALL, 10);
183
184 // Create a progress gauge
185 wxStaticBox* prog_box = new wxStaticBox(this, wxID_ANY, _("Progress..."));
186
187 wxStaticBoxSizer* prog_box_sizer = new wxStaticBoxSizer(prog_box, wxVERTICAL);
188 itemBoxSizer2->Add(prog_box_sizer, 0, wxEXPAND | wxALL, 5);
189
190 m_pgauge = new wxGauge(this, -1, 100);
191 prog_box_sizer->Add(m_pgauge, 0, wxEXPAND | wxALL, 5);
192
193 // OK/Cancel/etc.
194 wxBoxSizer* itemBoxSizer16 = new wxBoxSizer(wxHORIZONTAL);
195 itemBoxSizer2->Add(itemBoxSizer16, 0, wxALIGN_RIGHT | wxALL, 5);
196
197 m_CancelButton = new wxButton(itemDialog1, ID_STG_CANCEL, _("Cancel"),
198 wxDefaultPosition, wxDefaultSize, 0);
199 itemBoxSizer16->Add(m_CancelButton, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
200
201 m_SendButton = new wxButton(itemDialog1, ID_STG_OK, _("Send"),
202 wxDefaultPosition, wxDefaultSize, 0);
203 itemBoxSizer16->Add(m_SendButton, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
204 m_SendButton->SetDefault();
205}
206
207void SendToGpsDlg::SetMessage(wxString msg) {
208 if (premtext) {
209 premtext->SetLabel(msg);
210 premtext->Refresh(true);
211 }
212}
213
214void SendToGpsDlg::OnSendClick(wxCommandEvent& event) {
215 // Get the selected comm port
216 wxString src = m_itemCommListBox->GetValue();
217 int tail = src.Find(" - ");
218 if (tail != wxNOT_FOUND) {
219 src = src.SubString(0, tail);
220 }
221 if (!src.Lower().StartsWith("tcp") && !src.Lower().StartsWith("udp") &&
222 !src.Lower().StartsWith("serial") && !src.Lower().StartsWith("usb:") &&
223 !src.Lower().StartsWith("bluetooth")) {
224 src = src.Prepend("Serial:");
225 }
226 g_uploadConnection = src; // save for persistence
227
228 wxString destPort = src.BeforeFirst(' '); // Serial:
229
230 // For Bluetooth, we need the entire string
231 if (src.Lower().Find("bluetooth") != wxNOT_FOUND) destPort = src;
232
233 // And send it out
234 if (m_pRoute) RouteGui(*m_pRoute).SendToGPS(destPort, true, this);
235 if (m_pRoutePoint) RoutePointGui(*m_pRoutePoint).SendToGPS(destPort, this);
236
237 // Show( false );
238 // event.Skip();
239 Close();
240}
241
242void SendToGpsDlg::OnCancelClick(wxCommandEvent& event) {
243 // Show( false );
244 // event.Skip();
245 Close();
246}
Dialog for sending routes/waypoints to a GPS device.
Global variables stored in configuration file.
Connection parameters.
OpenCPN Platform specific support utilities.
REST API server.
Route abstraction.
Route drawing stuff.
Waypoint or mark abstraction.
Purpose: Track and Trackpoint drawing stuff.
Send route/waypoint to GPS dialog.
Serial ports support, notably enumeration.
wxArrayString * EnumerateSerialPorts(void)
Enumerate all serial ports.