Debian Codegraph @

fli_cfw.cpp

raw
1 #if 0
2 FLI Filter Wheels
3 INDI Interface for Finger Lakes Instrument Filter Wheels
4 Copyright (C) 2003 - 2020 Jasem Mutlaq ([email protected])
5
6 This library is free software;
7 you can redistribute it and / or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation;
10 either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY;
15 without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library;
21 if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 - 1301 USA
23
24 #endif
25
26 #include <memory>
27 #include <time.h>
28 #include <math.h>
29 #include <sys/time.h>
30
31 #include "indidevapi.h"
32 #include "eventloop.h"
33
34 #include "config.h"
35 #include "fli_cfw.h"
36
37 static std::unique_ptr<FLICFW> fliCFW(new FLICFW());
38
39 const flidomain_t Domains[] = { FLIDOMAIN_USB, FLIDOMAIN_SERIAL, FLIDOMAIN_PARALLEL_PORT, FLIDOMAIN_INET };
40
41 FLICFW::FLICFW()
42 {
43 setVersion(FLI_CCD_VERSION_MAJOR, FLI_CCD_VERSION_MINOR);
44 }
45
46 const char *FLICFW::getDefaultName()
47 {
48 return "FLI CFW";
49 }
50
51 bool FLICFW::initProperties()
52 {
53 INDI::FilterWheel::initProperties();
54
55 IUFillSwitch(&PortS[0], "USB", "USB", ISS_ON);
56 IUFillSwitch(&PortS[1], "SERIAL", "Serial", ISS_OFF);
57 IUFillSwitch(&PortS[2], "PARALLEL", "Parallel", ISS_OFF);
58 IUFillSwitch(&PortS[3], "INET", "INet", ISS_OFF);
59 IUFillSwitchVector(&PortSP, PortS, 4, getDeviceName(), "PORTS", "Port", MAIN_CONTROL_TAB, IP_WO, ISR_1OFMANY, 0,
60 IPS_IDLE);
61
62 IUFillText(&FilterInfoT[0], "Model", "", "");
63 IUFillText(&FilterInfoT[1], "HW Rev", "", "");
64 IUFillText(&FilterInfoT[2], "FW Rev", "", "");
65 IUFillTextVector(&FilterInfoTP, FilterInfoT, 3, getDeviceName(), "Model", "Model", "Filter Info", IP_RO, 60, IPS_IDLE);
66
67 IUFillSwitch(&TurnWheelS[0], "FILTER_CW", "+", ISS_OFF);
68 IUFillSwitch(&TurnWheelS[1], "FILTER_CCW", "-", ISS_OFF);
69 IUFillSwitchVector(&TurnWheelSP, TurnWheelS, 2, getDeviceName(), "FILTER_WHEEL_MOTION", "Turn Wheel", FILTER_TAB, IP_RW,
70 ISR_1OFMANY, 60, IPS_IDLE);
71 return true;
72 }
73
74 void FLICFW::ISGetProperties(const char *dev)
75 {
76 INDI::FilterWheel::ISGetProperties(dev);
77
78 defineProperty(&PortSP);
79
80 addAuxControls();
81 }
82
83 bool FLICFW::updateProperties()
84 {
85 INDI::FilterWheel::updateProperties();
86
87 if (isConnected())
88 {
89 defineProperty(&TurnWheelSP);
90 defineProperty(&FilterInfoTP);
91 }
92 else
93 {
94 deleteProperty(TurnWheelSP.name);
95 deleteProperty(FilterInfoTP.name);
96 }
97
98 return true;
99 }
100
101 bool FLICFW::ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
102 {
103 if (strcmp(dev, getDeviceName()) == 0)
104 {
105 // Ports
106 if (!strcmp(name, PortSP.name))
107 {
108 if (IUUpdateSwitch(&PortSP, states, names, n) < 0)
109 return false;
110
111 PortSP.s = IPS_OK;
112 IDSetSwitch(&PortSP, nullptr);
113 return true;
114 }
115
116 // Turn Wheel
117 if (!strcmp(name, TurnWheelSP.name))
118 {
119 if (IUUpdateSwitch(&TurnWheelSP, states, names, n) < 0)
120 return false;
121 turnWheel();
122 return true;
123 }
124 }
125
126 return INDI::FilterWheel::ISNewSwitch(dev, name, states, names, n);
127 }
128
129 bool FLICFW::Connect()
130 {
131 LOG_INFO("Connecting to FLI CFW...");
132
133 int portSwitchIndex = IUFindOnSwitchIndex(&PortSP);
134
135 if (findFLICFW(Domains[portSwitchIndex]) == false)
136 {
137 LOG_ERROR("Error: no filter wheels were detected.");
138 return false;
139 }
140
141 int errorCode = 0;
142
143 if ((errorCode = FLIOpen(&fli_dev, FLIFilter.name, FLIDEVICE_FILTERWHEEL | FLIFilter.domain)))
144 {
145 LOGF_ERROR("Error: FLIOpen() failed: %s.", strerror(-errorCode));
146 return false;
147 }
148
149 LOG_INFO("Filter wheel is online. Retrieving basic data.");
150
151 setupParams();
152
153 return true;
154 }
155
156 bool FLICFW::Disconnect()
157 {
158 int errorCode = 0;
159
160 if ((errorCode = FLIClose(fli_dev)))
161 {
162 LOGF_ERROR("Error: FLIClose() failed: %s.", strerror(-errorCode));
163 return false;
164 }
165
166 return true;
167 }
168
169 bool FLICFW::setupParams()
170 {
171 int errorCode = 0;
172
173 char hw_rev[16] = {0}, fw_rev[16] = {0};
174
175 ////////////////////////////
176 // 1. Get Filter wheels Model
177 ////////////////////////////
178 if ((errorCode = FLIGetModel(fli_dev, FLIFilter.model, MAXINDINAME)))
179 {
180 LOGF_ERROR("FLIGetModel() failed: %s.", strerror(-errorCode));
181 return false;
182 }
183
184 IUSaveText(&FilterInfoT[0], FLIFilter.model);
185
186 ///////////////////////////
187 // 2. Get Hardware revision
188 ///////////////////////////
189 if ((errorCode = FLIGetHWRevision(fli_dev, &FLIFilter.HWRevision)))
190 {
191 LOGF_ERROR("FLIGetHWRevision() failed: %s.", strerror(errorCode));
192 return false;
193 }
194
195 snprintf(hw_rev, 16, "%ld", FLIFilter.HWRevision);
196 IUSaveText(&FilterInfoT[1], hw_rev);
197
198 ///////////////////////////
199 // 3. Get Firmware revision
200 ///////////////////////////
201 if ((errorCode = FLIGetFWRevision(fli_dev, &FLIFilter.FWRevision)))
202 {
203 IDMessage(getDeviceName(), "FLIGetFWRevision() failed. %s.", strerror(-errorCode));
204 return false;
205 }
206
207 snprintf(fw_rev, 16, "%ld", FLIFilter.FWRevision);
208 IUSaveText(&FilterInfoT[2], fw_rev);
209
210 IDSetText(&FilterInfoTP, nullptr);
211
212 ///////////////////////////
213 // 4. Filter position
214 ///////////////////////////
215
216 // on first contact fliter wheel reports position -1
217 // to avoid wrong number presented in client dialog:
218 //SelectFilter(FilterSlotN[0].min);
219
220 if ((errorCode = FLIGetFilterPos(fli_dev, &FLIFilter.raw_pos)))
221 {
222 LOGF_DEBUG("FLIGetFilterPos() failed. %s.", strerror(-errorCode));
223 return false;
224 }
225
226 ///////////////////////////
227 // 5. filter max limit
228 ///////////////////////////
229 if ((errorCode = FLIGetFilterCount(fli_dev, &FLIFilter.count)))
230 {
231 LOGF_ERROR("FLIGetFilterCount() failed: %s.", strerror(-errorCode));
232 return false;
233 }
234
235 FilterSlotN[0].min = 1;
236 FilterSlotN[0].max = FLIFilter.count;
237 FilterSlotN[0].value = FLIFilter.raw_pos + 1;
238
239 return true;
240 }
241
242 bool FLICFW::SelectFilter(int targetFilter)
243 {
244 int errorCode = 0;
245 long filter = targetFilter - 1;
246
247 LOGF_DEBUG("Requested position is %ld", targetFilter);
248
249 if ((errorCode = FLISetFilterPos(fli_dev, filter)))
250 {
251 LOGF_ERROR("FLIGetFilterCount() failed: %s.", strerror(-errorCode));
252 return false;
253 }
254
255 FLIFilter.raw_pos = filter;
256
257 SelectFilterDone(targetFilter);
258
259 return true;
260 }
261
262 int FLICFW::QueryFilter()
263 {
264 int errorCode = 0;
265
266 if ((errorCode = FLIGetFilterPos(fli_dev, &FLIFilter.raw_pos)))
267 {
268 LOGF_ERROR("FLIGetFilterPos() failed: %s.", strerror(-errorCode));
269 return false;
270 }
271
272 LOGF_DEBUG("Current position: %ld", FLIFilter.raw_pos + 1);
273
274 return FLIFilter.raw_pos + 1;
275 }
276
277 void FLICFW::turnWheel()
278 {
279 long current_filter = FLIFilter.raw_pos + 1;
280 if (current_filter > FLIFilter.count)
281 current_filter = FLIFilter.count;
282
283 long target_filter = current_filter;
284
285 switch (TurnWheelS[0].s)
286 {
287 case ISS_ON:
288 if (current_filter < FilterSlotN[0].max)
289 target_filter = current_filter + 1;
290 else
291 target_filter = FilterSlotN[0].min;
292 break;
293
294 case ISS_OFF:
295 if (current_filter > FilterSlotN[0].min)
296 target_filter = current_filter - 1;
297 else
298 target_filter = FilterSlotN[0].max;
299 break;
300 }
301
302 LOGF_DEBUG("Turning CFW %s from %ld to %ld", (TurnWheelS[0].s == ISS_ON) ? "CW" : "CCW", current_filter, target_filter);
303
304 SelectFilter(target_filter);
305
306 IUResetSwitch(&TurnWheelSP);
307 TurnWheelSP.s = IPS_OK;
308 IDSetSwitch(&TurnWheelSP, nullptr);
309 }
310
311 bool FLICFW::findFLICFW(flidomain_t domain)
312 {
313 char **names;
314 int errorCode = 0;
315
316 if ((errorCode = FLIList(domain | FLIDEVICE_FILTERWHEEL, &names)))
317 {
318 LOGF_ERROR("FLIList() failed: %s", strerror(-errorCode));
319 return false;
320 }
321
322 if (names != nullptr && names[0] != nullptr)
323 {
324 for (int i = 0; names[i] != nullptr; i++)
325 {
326 for (int j = 0; names[i][j] != '\0'; j++)
327 if (names[i][j] == ';')
328 {
329 names[i][j] = '\0';
330 break;
331 }
332 }
333
334 FLIFilter.domain = domain;
335
336 switch (domain)
337 {
338 case FLIDOMAIN_PARALLEL_PORT:
339 FLIFilter.dname = strdup("parallel port");
340 break;
341
342 case FLIDOMAIN_USB:
343 FLIFilter.dname = strdup("USB");
344 break;
345
346 case FLIDOMAIN_SERIAL:
347 FLIFilter.dname = strdup("serial");
348 break;
349
350 case FLIDOMAIN_INET:
351 FLIFilter.dname = strdup("inet");
352 break;
353
354 default:
355 FLIFilter.dname = strdup("Unknown domain");
356 }
357
358 FLIFilter.name = strdup(names[0]);
359
360 if ((errorCode = FLIFreeList(names)))
361 {
362 LOGF_ERROR("FLIFreeList() failed: %s.", strerror(-errorCode));
363 return false;
364 }
365
366 }
367 else
368 {
369 if ((errorCode = FLIFreeList(names)))
370 {
371 LOGF_ERROR("FLIFreeList() failed: %s.", strerror(-errorCode));
372 return false;
373 }
374
375 return false;
376 }
377
378 return true;
379 }
380
381 void FLICFW::debugTriggered(bool enable)
382 {
383 FLISetDebugLevel(nullptr, enable ? FLIDEBUG_INFO : FLIDEBUG_WARN);
384 }