/
x2focuser.cpp
446 lines (354 loc) · 10.9 KB
/
x2focuser.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include <stdio.h>
#include <string.h>
#include "x2focuser.h"
#include "../../licensedinterfaces/theskyxfacadefordriversinterface.h"
#include "../../licensedinterfaces/sleeperinterface.h"
#include "../../licensedinterfaces/loggerinterface.h"
#include "../../licensedinterfaces/basiciniutilinterface.h"
#include "../../licensedinterfaces/mutexinterface.h"
#include "../../licensedinterfaces/basicstringinterface.h"
#include "../../licensedinterfaces/tickcountinterface.h"
#include "../../licensedinterfaces/serxinterface.h"
#include "../../licensedinterfaces/sberrorx.h"
#include "../../licensedinterfaces/serialportparams2interface.h"
X2Focuser::X2Focuser(const char* pszDisplayName,
const int& nInstanceIndex,
SerXInterface * pSerXIn,
TheSkyXFacadeForDriversInterface * pTheSkyXIn,
SleeperInterface * pSleeperIn,
BasicIniUtilInterface * pIniUtilIn,
LoggerInterface * pLoggerIn,
MutexInterface * pIOMutexIn,
TickCountInterface * pTickCountIn)
{
m_pSerX = pSerXIn;
m_pTheSkyXForMounts = pTheSkyXIn;
m_pSleeper = pSleeperIn;
m_pIniUtil = pIniUtilIn;
m_pLogger = pLoggerIn;
m_pIOMutex = pIOMutexIn;
m_pTickCount = pTickCountIn;
m_bLinked = false;
m_nPosition = 0;
m_fLastTemp = -273.15f; // aboslute zero :)
// Read in settings
if (m_pIniUtil) {
m_Aaf2Controller.setPosLimit(m_pIniUtil->readInt(PARENT_KEY, POS_LIMIT, 0));
m_Aaf2Controller.enablePosLimit(m_pIniUtil->readInt(PARENT_KEY, POS_LIMIT_ENABLED, false));
}
m_Aaf2Controller.SetSerxPointer(m_pSerX);
m_Aaf2Controller.setLogger(m_pLogger);
m_Aaf2Controller.setSleeper(m_pSleeper);
}
X2Focuser::~X2Focuser()
{
//Delete objects used through composition
if (GetSerX())
delete GetSerX();
if (GetTheSkyXFacadeForDrivers())
delete GetTheSkyXFacadeForDrivers();
if (GetSleeper())
delete GetSleeper();
if (GetSimpleIniUtil())
delete GetSimpleIniUtil();
if (GetLogger())
delete GetLogger();
if (GetMutex())
delete GetMutex();
}
#pragma mark - DriverRootInterface
int X2Focuser::queryAbstraction(const char* pszName, void** ppVal)
{
*ppVal = NULL;
if (!strcmp(pszName, LinkInterface_Name))
*ppVal = (LinkInterface*)this;
else if (!strcmp(pszName, FocuserGotoInterface2_Name))
*ppVal = (FocuserGotoInterface2*)this;
else if (!strcmp(pszName, ModalSettingsDialogInterface_Name))
*ppVal = dynamic_cast<ModalSettingsDialogInterface*>(this);
else if (!strcmp(pszName, X2GUIEventInterface_Name))
*ppVal = dynamic_cast<X2GUIEventInterface*>(this);
else if (!strcmp(pszName, FocuserTemperatureInterface_Name))
*ppVal = dynamic_cast<FocuserTemperatureInterface*>(this);
else if (!strcmp(pszName, LoggerInterface_Name))
*ppVal = GetLogger();
else if (!strcmp(pszName, ModalSettingsDialogInterface_Name))
*ppVal = dynamic_cast<ModalSettingsDialogInterface*>(this);
else if (!strcmp(pszName, SerialPortParams2Interface_Name))
*ppVal = dynamic_cast<SerialPortParams2Interface*>(this);
return SB_OK;
}
#pragma mark - DriverInfoInterface
void X2Focuser::driverInfoDetailedInfo(BasicStringInterface& str) const
{
str = "Focuser X2 plugin by Rodolphe Pineau";
}
double X2Focuser::driverInfoVersion(void) const
{
return DRIVER_VERSION;
}
void X2Focuser::deviceInfoNameShort(BasicStringInterface& str) const
{
str="AAF2";
}
void X2Focuser::deviceInfoNameLong(BasicStringInterface& str) const
{
deviceInfoNameShort(str);
}
void X2Focuser::deviceInfoDetailedDescription(BasicStringInterface& str) const
{
str = "AAF2 Controller";
}
void X2Focuser::deviceInfoFirmwareVersion(BasicStringInterface& str)
{
if(!m_bLinked) {
str="NA";
}
else {
X2MutexLocker ml(GetMutex());
// get firmware version
char cFirmware[SERIAL_BUFFER_SIZE];
m_Aaf2Controller.getFirmwareVersion(cFirmware, SERIAL_BUFFER_SIZE);
str = cFirmware;
}
}
void X2Focuser::deviceInfoModel(BasicStringInterface& str)
{
str="AAF2";
}
#pragma mark - LinkInterface
int X2Focuser::establishLink(void)
{
char szPort[DRIVER_MAX_STRING];
int nErr;
X2MutexLocker ml(GetMutex());
// get serial port device name
portNameOnToCharPtr(szPort,DRIVER_MAX_STRING);
nErr = m_Aaf2Controller.Connect(szPort);
if(nErr)
m_bLinked = false;
else
m_bLinked = true;
return nErr;
}
int X2Focuser::terminateLink(void)
{
if(!m_bLinked)
return SB_OK;
X2MutexLocker ml(GetMutex());
m_Aaf2Controller.haltFocuser();
m_Aaf2Controller.Disconnect();
m_bLinked = false;
return SB_OK;
}
bool X2Focuser::isLinked(void) const
{
return m_bLinked;
}
#pragma mark - ModalSettingsDialogInterface
int X2Focuser::initModalSettingsDialog(void)
{
return SB_OK;
}
int X2Focuser::execModalSettingsDialog(void)
{
int nErr = SB_OK;
X2ModalUIUtil uiutil(this, GetTheSkyXFacadeForDrivers());
X2GUIInterface* ui = uiutil.X2UI();
X2GUIExchangeInterface* dx = NULL;//Comes after ui is loaded
bool bPressedOK = false;
bool bLimitEnabled = false;
int nPosition = 0;
int nPosLimit = 0;
mUiEnabled = false;
if (NULL == ui)
return ERR_POINTER;
if ((nErr = ui->loadUserInterface("aaf2.ui", deviceType(), m_nPrivateMulitInstanceIndex)))
return nErr;
if (NULL == (dx = uiutil.X2DX()))
return ERR_POINTER;
X2MutexLocker ml(GetMutex());
// set controls values
if(m_bLinked) {
// new position (set to current )
nErr = m_Aaf2Controller.getPosition(nPosition);
if(nErr)
return nErr;
dx->setEnabled("newPos", true);
dx->setEnabled("pushButtonSet2", true);
dx->setPropertyInt("newPos", "value", nPosition);
}
else {
// disable all controls
dx->setEnabled("newPos", false);
dx->setPropertyInt("newPos", "value", 0);
dx->setEnabled("reverseDir", false);
dx->setEnabled("pushButtonSet2", false);
}
// limit is done in software so it's always enabled.
dx->setEnabled("posLimit", true);
dx->setEnabled("limitEnable", true);
dx->setPropertyInt("posLimit", "value", m_Aaf2Controller.getPosLimit());
if(m_Aaf2Controller.isPosLimitEnabled())
dx->setChecked("limitEnable", true);
else
dx->setChecked("limitEnable", false);
//Display the user interface
mUiEnabled = true;
if ((nErr = ui->exec(bPressedOK)))
return nErr;
mUiEnabled = false;
//Retreive values from the user interface
if (bPressedOK) {
nErr = SB_OK;
// get limit option
bLimitEnabled = dx->isChecked("limitEnable");
dx->propertyInt("posLimit", "value", nPosLimit);
if(bLimitEnabled && nPosLimit>0) { // a position limit of 0 doesn't make sense :)
m_Aaf2Controller.setPosLimit(nPosLimit);
m_Aaf2Controller.enablePosLimit(bLimitEnabled);
} else {
m_Aaf2Controller.enablePosLimit(false);
}
// save values to config
nErr |= m_pIniUtil->writeInt(PARENT_KEY, POS_LIMIT, nPosLimit);
nErr |= m_pIniUtil->writeInt(PARENT_KEY, POS_LIMIT_ENABLED, bLimitEnabled);
}
return nErr;
}
void X2Focuser::uiEvent(X2GUIExchangeInterface* uiex, const char* pszEvent)
{
int nErr = SB_OK;
int nTmpVal;
char szErrorMessage[LOG_BUFFER_SIZE];
// new position
if (!strcmp(pszEvent, "on_pushButton_clicked")) {
uiex->propertyInt("newPos", "value", nTmpVal);
nErr = m_Aaf2Controller.syncMotorPosition(nTmpVal);
if(nErr) {
snprintf(szErrorMessage, LOG_BUFFER_SIZE, "Error setting new position : Error %d", nErr);
uiex->messageBox("Set New Position", szErrorMessage);
return;
}
}
}
#pragma mark - FocuserGotoInterface2
int X2Focuser::focPosition(int& nPosition)
{
int nErr;
if(!m_bLinked)
return NOT_CONNECTED;
X2MutexLocker ml(GetMutex());
nErr = m_Aaf2Controller.getPosition(nPosition);
m_nPosition = nPosition;
return nErr;
}
int X2Focuser::focMinimumLimit(int& nMinLimit)
{
nMinLimit = 0;
return SB_OK;
}
int X2Focuser::focMaximumLimit(int& nPosLimit)
{
X2MutexLocker ml(GetMutex());
if(m_Aaf2Controller.isPosLimitEnabled()) {
nPosLimit = m_Aaf2Controller.getPosLimit();
}
else {
nPosLimit = 100000;
}
return SB_OK;
}
int X2Focuser::focAbort()
{ int nErr;
if(!m_bLinked)
return NOT_CONNECTED;
X2MutexLocker ml(GetMutex());
nErr = m_Aaf2Controller.haltFocuser();
return nErr;
}
int X2Focuser::startFocGoto(const int& nRelativeOffset)
{
if(!m_bLinked)
return NOT_CONNECTED;
X2MutexLocker ml(GetMutex());
m_Aaf2Controller.moveRelativeToPosision(nRelativeOffset);
return SB_OK;
}
int X2Focuser::isCompleteFocGoto(bool& bComplete) const
{
int nErr;
if(!m_bLinked)
return NOT_CONNECTED;
X2Focuser* pMe = (X2Focuser*)this;
X2MutexLocker ml(pMe->GetMutex());
nErr = pMe->m_Aaf2Controller.isGoToComplete(bComplete);
return nErr;
}
int X2Focuser::endFocGoto(void)
{
int nErr;
if(!m_bLinked)
return NOT_CONNECTED;
X2MutexLocker ml(GetMutex());
nErr = m_Aaf2Controller.getPosition(m_nPosition);
return nErr;
}
int X2Focuser::amountCountFocGoto(void) const
{
return 3;
}
int X2Focuser::amountNameFromIndexFocGoto(const int& nZeroBasedIndex, BasicStringInterface& strDisplayName, int& nAmount)
{
switch (nZeroBasedIndex)
{
default:
case 0: strDisplayName="10 steps"; nAmount=10;break;
case 1: strDisplayName="100 steps"; nAmount=100;break;
case 2: strDisplayName="1000 steps"; nAmount=1000;break;
}
return SB_OK;
}
int X2Focuser::amountIndexFocGoto(void)
{
return 0;
}
#pragma mark - FocuserTemperatureInterface
int X2Focuser::focTemperature(double &dTemperature)
{
int nErr = SB_OK;
if(!m_bLinked) {
dTemperature = -100.0;
return NOT_CONNECTED;
}
X2MutexLocker ml(GetMutex());
// Taken from Richard's Robofocus plugin code.
// this prevent us from asking the temperature too often
static CStopWatch timer;
if(timer.GetElapsedSeconds() > 30.0f || m_fLastTemp < -99.0f) {
nErr = m_Aaf2Controller.getTemperature(m_fLastTemp);
timer.Reset();
}
dTemperature = m_fLastTemp;
return nErr;
}
#pragma mark - SerialPortParams2Interface
void X2Focuser::portName(BasicStringInterface& str) const
{
char szPortName[DRIVER_MAX_STRING];
portNameOnToCharPtr(szPortName, DRIVER_MAX_STRING);
str = szPortName;
}
void X2Focuser::setPortName(const char* pszPort)
{
if (m_pIniUtil)
m_pIniUtil->writeString(PARENT_KEY, CHILD_KEY_PORTNAME, pszPort);
}
void X2Focuser::portNameOnToCharPtr(char* pszPort, const int& nMaxSize) const
{
if (NULL == pszPort)
return;
snprintf(pszPort, nMaxSize, DEF_PORT_NAME);
if (m_pIniUtil)
m_pIniUtil->readString(PARENT_KEY, CHILD_KEY_PORTNAME, pszPort, pszPort, nMaxSize);
}