-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCWSerial.cpp
More file actions
317 lines (206 loc) · 7.63 KB
/
CWSerial.cpp
File metadata and controls
317 lines (206 loc) · 7.63 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
/*
CWSerial - plugin for serial data flow from Xplane
Created by Michael Gerlicher, December 2022.
To report problems, download updates and examples, suggest enhancements or get technical support, please visit my patreon page:
www.patreon.com/curiosityworkshop
*/
/*
Todo:
*/
#include <stdio.h>
#define XPLM200
#include "XPLMPlugin.h"
#include "XPLMDataAccess.h"
#include "XPLMDisplay.h"
#include "XPLMGraphics.h"
#include "XPLMMenus.h"
#include "XPWidgets.h"
#include "XPStandardWidgets.h"
#include "XPLMUtilities.h"
#include "XPLMProcessing.h"
#include "XPLMCamera.h"
#include "XPUIGraphics.h"
#include "XPWidgetUtils.h"
//#include "serialclass.h"
#include "DataHandler.h"
#include "Config.h"
#include "StatusWindow.h"
#include "CWSerial.h"
FILE* serialLogFile; // for serial data log
FILE* errlog; // Used for logging problems
Config *CWConfig;
DataHandler *CWDataHandler;
long cycleCount = 0l;
float elapsedTime = 0;
int logSerial = false;
int refreshRate = 10; // how often in MS to send data
int gClicked = 0;
XPLMMenuID myMenu;
int disengageMenuItemIndex;
int logSerialMenuItemIndex;
PLUGIN_API int XPluginStart(
char * outName,
char * outSig,
char * outDesc)
{
int mySubMenuItem;
char outFilePath[256];
XPLMDebugString("CWSerialNMEA: Initializing Plug-in\n");
fopen_s(&errlog, "CWSerialNMEAError.log", "w");
if (!errlog)
{
XPLMDebugString("CWSerialNMEA: Unable to open error log file CWSerialNMEAError.log\n");
return 0;
}
setbuf(errlog, NULL);
// Provide our plugin's profile to the plugin system.
strcpy(outName, "CWSerialNMEA");
strcpy(outSig, "cwserial.rs232.cw");
strcpy(outDesc, "NMEA Serial Data Output Plugin");
fprintf(errlog, "Curiosity Workshop CWSerialNMEA version %u copyright 2023. \n", CWSERIAL_VERSION );
fprintf(errlog, "To report problems, download updates and examples, suggest enhancements or get technical support:\r\n");
fprintf(errlog, " patreon: www.patreon.com/curiosityworkshop\n");
fprintf(errlog, " discord: https://discord.gg/RacvaRFsMW\n");
fprintf(errlog, " YouTube: youtube.com/channel/UCISdHdJIundC-OSVAEPzQIQ \n\n");
fprintf(errlog, "CWSerialNMEA plugin Error log file begins now.\r\n");
XPLMGetPluginInfo( XPLMGetMyID(), NULL, outFilePath, NULL, NULL);
fprintf(errlog, "Plugin Path: %s\r\n", outFilePath);
// first load configuration stuff
CWConfig = new Config(CFG_FILE);
logSerial = CWConfig->getSerialLogFlag();
CWConfig->getRefreshRate(&refreshRate);
CWDataHandler = new DataHandler();
CWDataHandler->begin();
if (logSerial) fprintf(errlog, "Serial logging enabled.\r\n"); else fprintf(errlog, "Serial logging disabled.\r\n");
/* First we put a new menu item into the plugin menu.
* This menu item will contain a submenu for us. */
mySubMenuItem = XPLMAppendMenuItem(
XPLMFindPluginsMenu(), /* Put in plugins menu */
"CWSerialNMEA", /* Item Title */
//"AgPilotX Connection",
0, /* Item Ref */
1); /* Force English */
/* Now create a submenu attached to our menu item. */
myMenu = XPLMCreateMenu(
"XPLDirectB",
XPLMFindPluginsMenu(),
mySubMenuItem, /* Menu Item to attach to. */
MyMenuHandlerCallback, /* The handler */
0); /* Handler Ref */
/* Append a few menu items to our submenu. */
XPLMAppendMenuItem(myMenu, "Status",(void *) "Status", 1);
logSerialMenuItemIndex = XPLMAppendMenuItem(myMenu, "Log Serial Data", (void*) "Log Serial Data", 1);
XPLMAppendMenuSeparator(myMenu);
if (logSerial) XPLMCheckMenuItem(myMenu, logSerialMenuItemIndex, xplm_Menu_Checked);
else XPLMCheckMenuItem(myMenu, logSerialMenuItemIndex, xplm_Menu_Unchecked);
XPLMRegisterFlightLoopCallback( // Setup timed processing
MyFlightLoopCallback, /* Callback */
-2.0, /* Interval */
NULL); /* refcon not used. */
if (logSerial)
{
fopen_s(&serialLogFile, "CWSerialNMEA.log", "w");
if (serialLogFile) fprintf(serialLogFile, "Serial logger. Unprintable characters are represented by '~'\n");
}
XPLMDebugString("CWSerialNMEA: Plugin initialization complete.\n");
return 1;
}
// XPluginStop
PLUGIN_API void XPluginStop(void)
{
// cmpSelect.saveConfiguration();
// cmpLEDEdit.saveConfiguration();
// if (errlog) fprintf(errlog, "Ending plugin, cycle count: %u Packets transmitted: %u\n", cycleCount, packetsSent);
if (errlog) fclose(errlog);
if (serialLogFile) fclose(serialLogFile);
}
// XPluginDisable Handler. We do nothing in this plugin
PLUGIN_API void XPluginDisable(void)
{
}
//XpluginEnable handler. We do nothing in this plugin.
PLUGIN_API int XPluginEnable(void)
{
return 1;
}
// XPluginRecieveMessage Handler
PLUGIN_API void XPluginReceiveMessage(
XPLMPluginID inFromWho,
int inMessage,
void* inParam)
{
char pluginName[256];
XPLMGetPluginInfo(inFromWho, pluginName, NULL, NULL, NULL);
if (inMessage == XPLM_MSG_PLANE_UNLOADED)
{
}
if (inMessage == 108) // 108 is supposed to = XPLM_MSG_LIVERY_LOADED
//if (inMessage == XPLM_MSG_PLANE_LOADED)
{
}
// fprintf(errlog, "Xplane sent me a message from: %s, message: %i\n", pluginName, inMessage);
}
/**************************************************************************************/
/* MyMenuHandlerCallback-- Handle users request for calibration */
/**************************************************************************************/
void MyMenuHandlerCallback(
void* inMenuRef,
void* inItemRef)
{
// fprintf(errlog, "User selected inMenuRef: %i, inItemRef: %i\n", (int)(int *)inMenuRef, (int)(int *)inItemRef);
// Handle request for status window
if (!strcmp((const char*)inItemRef, "Status")) // menu 0, item 1, "Status"
{
if (!statusWindowActive() ) statusWindowCreate();
}
// Handle request toggle for serial logging
if (!strcmp((const char*)inItemRef, "Log Serial Data")) // menu 0, item 1, "Log Serial Data"
{
if (logSerial)
{
fclose(serialLogFile);
logSerial = false;
XPLMCheckMenuItem(myMenu, logSerialMenuItemIndex, xplm_Menu_Unchecked);
//CWConfig->setSerialLogFlag(0);
}
else
{
fopen_s(&serialLogFile, "CWSerialNMEA.log", "w");
if (serialLogFile)
{
fprintf(serialLogFile, "Serial logger. Unprintable characters are represented by '~'\n");
logSerial = true;
XPLMCheckMenuItem(myMenu, logSerialMenuItemIndex, xplm_Menu_Checked);
//CWConfig->setSerialLogFlag(1);
}
}
}
}
/**************************************************************************************/
/* MyFlightLoopCallback -- Called by xplane every few flight loops */
/**************************************************************************************/
float MyFlightLoopCallback(
float inElapsedSinceLastCall,
float inElapsedTimeSinceLastFlightLoop,
int inCounter,
void* inRefcon)
{
//return XPLDIRECT_RETURN_TIME;
//fprintf(errlog, "Time: %f \n", inElapsedSinceLastCall);
elapsedTime += inElapsedSinceLastCall;
//if (cycleCount == 1)
// {
// engageDevices();
//sendRefreshRequest();
// }
CWDataHandler->processSerial();
cycleCount++;
return (float)refreshRate / 1000;
}
int widgetMessageDispatcher(XPWidgetMessage inMessage, XPWidgetID inWidget, intptr_t inParam1, intptr_t inParam2)
{
// need to add for each dialog for now
//if (cmpSelect.isYourWidget(inWidget)) return cmpSelect.widgetMessageHandler(inMessage, inWidget, inParam1, inParam2);
//if (cmpLEDEdit.isYourWidget(inWidget)) return cmpLEDEdit.widgetMessageHandler(inMessage, inWidget, inParam1, inParam2);
return 0; // always return 0 if we don't know what to do with the message
}