Debian Codegraph @

spectro/instlib.api.txt

raw
1 Instlib API
2 -----------
3
4 Although instlib is writen in the C language, it uses an
5 object oriented style of API.
6
7 The instlib API is declared in the inst.h file, with suplimental
8 definitions in other .h files such as insttypes.h, disptechs.h,
9 icoms.h, xspect.h etc.
10
11 1) Instrument discovery
12 --------------------
13
14 A list of potential instruments is returned by a call to new_icompaths().
15
16 The diagnostic log argument should be set to the default global log g_log, if
17 it is not being overridden to use a different log.
18
19 The list of instruments can then be itterated through, to select which
20 instrument to use:
21
22 icompaths *icmps;
23
24 icmps = new_icompaths(g_log);
25
26 if (icmps != NULL && icmps->paths != NULL) {
27 int i;
28 for (i = 0; ;i++) {
29 if (icmps->paths[i] == NULL)
30 break;
31 printf(" %d = '%s'\n",i+1,icmps->paths[i]->name);
32 }
33 }
34
35 The chosen instrument device path can be retrieved using the "comport"
36 index 1..N, which corresponds to the index in the icompaths list (see above):
37
38 icompath *ipath;
39
40 if ((ipath = icmps->get_path(icmps, comport)) == NULL)
41 ... error ...
42
43
44 2) User interface callback function
45 --------------------------------
46
47 There is the option of setting a user interface callback function,
48 either at the creation of the instrument object, or afterwards.
49
50 The callback is used for one of four different purposes:
51
52 To poll for a user abort during communication setup.
53 To signal that the instrument measurement has been triggered.
54 To poll for an abort while waiting to trigger.
55 To poll for a user abort during measurement.
56
57 The callback function will have the purpose paramater set appropriately.
58
59 For inst_negcoms, the return value of inst_user_abort
60 will abort the communication negotiation.
61
62 For inst_armed return value should be one of:
63 inst_ok to do nothing, inst_user_abort to abort the measurement,
64 or inst_user_trig to trigger the measurement.
65
66 For inst_triggered, the return value of the callback is ignored.
67
68 For inst_measuring the return value should be one of:
69 inst_ok to do nothing, or inst_user_abort to abort the measurement.
70
71 typedef struct _uicontext {
72 ... your context here ...
73 } uicontext;
74
75 uicontext cb_cntx;
76
77 inst_code ui_cb_func(void *cntx, inst_ui_purp purp) {
78 uicontext *p = (uicontext *)cntx;
79
80 /* Negotiating communications with instrument. */
81 if (purp == inst_triggered) {
82
83 // Can abort by returning inst_user_abort;
84
85 return inst_ok;
86
87 /* Instrument is armed and ready for measurement */
88 } else if (purp == inst_armed) {
89
90 // Can trigger by returning inst_user_trig;
91 // Can abort by returning inst_user_abort;
92
93 return inst_ok; // Wait some more
94
95 /* Instrument measurement has been triggerted
96 } else if (purp == inst_triggered) {
97
98 ... show in user interface that the instrument measurement
99 has triggered ...
100
101 return inst_ok;
102
103 /* Instrument is busy measuring */
104 } else if (purp == inst_measuring) {
105
106 // Can abort by returning inst_user_abort;
107
108 return inst_ok;
109 }
110 return inst_ok;
111 }
112
113
114 3) Opening instrument
115 ------------------
116
117 Given the instrument device path ipath (see above), first create an instrument
118 object. A user interface callback function can be provided (see above).
119
120 inst *it;
121
122 if ((it = new_inst(ipath, 0, g_log, ui_cb_func, cb_cntx)) == NULL)
123 ... error ...
124
125 and establish communications:
126
127 baud_rate br = baud_38400; /* Target baud rate - ignored for i1pro */
128 flow_control fc = fc_nc; /* flow control - ignored for i1pro */
129
130 if ((rv = it->init_coms(it, br, fc, 15.0)) != inst_ok)
131 ... error ...
132
133 and initialise the instrument:
134
135 if ((rv = it->init_inst(it)) != inst_ok)
136 ... error ...
137
138
139 4) Getting instrument capabilities and configuration
140 -------------------------------------------------
141
142 Because instlib handles many different types of instruments,
143 it is very useful to be able to determine what capabililities
144 an instrument has, to determine what sort of measurements it is
145 capable of making, and how to best configure and operate it.
146
147 Capabilities are indicated by three 32 bit flag values, and are
148 retrieved by the capabilities() function:
149
150 inst_mode cap;
151 inst2_capability cap2;
152 inst3_capability cap3;
153
154 it->capabilities(it, &cap, &cap2, &cap3);
155
156 The first set of flags correspond to the measurement mode capabilities.
157
158 Modes consist of basic mode combinations (light measurement mode + action),
159 + dependent modes + independent modes.
160
161 It is generaly best to test and set for mode combinations rather than independent
162 flags, and to use the IMODETST(flags, mode) macro and check_mode() function
163 to test for a particular mode combination capability. (see the inst_mode enum).
164
165 For instance, if we are intending to do strip reflectance measurement, we
166 should test if it has this capability:
167
168 if (!IMODETST(cap, inst_mode_ref_strip)
169 || it->check_mode(it, inst_mode_ref_strip) != inst_ok)
170 ... error ...
171
172 Configuration consists of two aspects: setting the operating mode and setting options.
173
174 If we want to configure for reflective strip reading with spectral:
175
176 inst_mode mode = inst_mode_ref_strip;
177
178 if (!IMODETST(cap, inst_mode_spectral))
179 ... error, no spectral support ...
180
181 mode |= inst_mode_spectral;
182
183 if ((rv = it->set_mode(it, mode)) != inst_ok)
184 ... error ...
185
186 The exact capabilities may change with mode, so they should be re-fetched
187 if they are needed again:
188
189 it->capabilities(it, &cap, &cap2, &cap3);
190
191 For reflectance measurement with an X-Rite type instrument, we may want to
192 set the XRGA handling. i.e. to get xrga standard measurements:
193
194 if ((rv = it->get_set_opt(it, inst_opt_set_xcalstd, xcalstd_xrga)) != inst_ok)
195 ... warning, XRGA not supported ...
196
197 Many instruments normally need a calibration every time they
198 are opened, but if several measurements are being done in quick
199 succession the calibration after opening the instrument
200 can be supressed by setting the inst_opt_noinitcalib option:
201
202 if ((rv = it->get_set_opt(it, inst_opt_noinitcalib, 0)) != inst_ok) {
203 ... warning, not supported ...
204
205 6) Asynchronous callback handler
206 -----------------------------
207
208 To handle certain asynchronouos events, you can register
209 a callback handler. This function is typically called from
210 a different thread from the one making calls to inst functions.
211
212 Events are:
213 inst_event_switch: Instrument measure/calibrate/user trigger switch pressed.
214 inst_event_mconf: The measurement configuration has changed (ie. sensor position)
215 inst_event_scan_ready: Ready for manual strip scan (i.e. issue audible prompt to user)
216
217 Which events are issued will depend on the type of instrument and what mode and state
218 it is in.
219
220 void event_callback_handler(void *cntx, inst_event_type event) {
221 if (event == inst_event_scan_ready)
222 normal_beep();
223 }
224
225 it->set_event_callback(it, event_callback_handler, (void *)it);
226
227 If no event handler is registered, then the inst_event_scan_ready event will
228 generate a "beep" using operating system facilities.
229
230 7) Dealing with calibration
231 ------------------------
232
233 Often it is better to do a proactive calibration, rather than waiting for
234 a calibration error to be returned by a measurement request. A need for
235 calibration can be determined by calling needs_calibration():
236
237 if (it->needs_calibration(it) & inst_calt_n_dfrble_mask)
238 ... do needed calibration ...
239
240 Performing a calibration involves (usually) repeated calls to
241 calibrate(), with arguments *calt containing the mask of calibrations
242 to be performed, and *calc set to the current calibration condition.
243
244 Alternately, one of the psudo-calibration types inst_calt_all,
245 inst_calt_needed or inst_calt_available can be used,
246 and/or the *calc of inst_calc_none to get calibrate()
247 to determine the required calibration types and conditions.
248 (The corresponding calibration types will be used & returned.)
249
250 If no error is returned to the first call to calibrate(),
251 then the instrument was capable of calibrating without user or
252 application intervention. If on the other hand calibrate() returns
253 inst_cal_setup, then the appropriate action indicated by the value
254 returned in *calc should be taken by the user or application,
255 before retrying calibration() with the current setup indicated
256 by *calc. If more than one calibration type is requested, then
257 several retries may be needed with different calibration conditions.
258 Each call to calibrate() will update *calt to reflect the remaining
259 calibration to be performed. calibrate() returns inst_ok when no
260 more calibrations remain.
261
262 If the calc has the inst_calc_optional_flag flag set,
263 then the user should be offered the option of skipping the
264 calibration. If they decide to skip it, return a calc with
265 inst_calc_optional_flag set, and if they want to proceed,
266 make sure the inst_calc_optional_flag is cleared in the returned
267 calc.
268
269 The id parameter allows passing back messages to the user, such
270 as the serial number of the calibration plaque, or messages,
271 while the idtype parameter indicates the nature of what is
272 passed back in id, to allow for internationalization of
273 any messages.
274
275 The following is a cut down version of inst_handle_calibrate()
276 in the file instappsup.c.
277
278 inst_cal_type calt = inst_calt_needed;
279 inst_cal_cond calc = inst_calc_none;
280 inst_code ev;
281 int usermes = 0; /* User was given a message */
282 inst_calc_id_type idtype; /* Condition identifier type */
283 char id[200]; /* Condition identifier */
284
285 /* Untill we're done with the calibration */
286 for (;;) {
287
288 ev = p->calibrate(p, &calt, &calc, &idtype, id);
289
290 /* We're done */
291 if ((ev & inst_mask) == inst_ok) {
292 if ((calc & inst_calc_cond_mask) == inst_calc_message)
293 ... display message id to user, or substitute our
294 own message based on the value of idtype ...
295 ... done calibration ...
296 }
297
298 /* User aborted */
299 if ((ev & inst_mask) == inst_user_abort)
300 ... done calibration with user abort ...
301
302 /* Retry on an error */
303 if ((ev & inst_mask) != inst_cal_setup) {
304 if ((ev & inst_mask) == inst_unsupported)
305 ... requested calibration type is unsupported ...
306
307 ... ask user whether to rety or abort,
308 and don't continue on abort ...
309
310 } else {
311
312 /* Ask user to do/setup calibration */
313 switch (calc & inst_calc_cond_mask) {
314
315 case inst_calc_man_ref_white:
316 ... ask user to place instrument on its reflective white
317 reference S/N id string, and continue when done ...
318 break;
319
320 ... cases not needed for i1pro have been omitted ...
321
322 default:
323 ... we're not handling some calc ...
324 }
325
326 if (calc & inst_calc_optional_flag)
327 ... offer user option to skip or abort calibration & measurement ...
328 else
329 ... offer user option to abort calibration & measurement ...
330
331 ... get user response ...
332
333 /* If optional calib. and user wants to skip it */
334 /* Loop back to calibrate() with inst_calc_optional_flag still set */
335 if ((calc & inst_calc_optional_flag) != 0 && ... user says skip ...)
336 goto oloop;
337
338 if (... user says abort ...)
339 ... abort calibration & measurement ...
340
341 /* Remove any skip flag and continue with the calibration */
342 calc &= inst_calc_cond_mask;
343 }
344 oloop:;
345 }
346
347 8) Taking a single measurement
348 ---------------------------
349
350 We may want to set a particular trigger mode before doing this. The appropriate
351 trigger mode depends on the instrument capabilities, as well as the way we
352 want to use it.
353
354 We can check what trigger modes are available by looking at the cap2 values
355 and comparing against the inst2_prog_trig, inst2_user_trig, inst2_switch_trig &
356 inst2_user_switch_trig flags, and then choose which mode to use. i.e. if we
357 want the user to initiate the measurement we might use:
358
359 /* Enable switch or user via uicallback trigger if possible */
360 if (cap2 & inst2_user_switch_trig) {
361 if ((rv = it->get_set_opt(it, inst_opt_trig_user_switch)) != inst_ok)
362 ... error ...
363
364 /* Or go for user via uicallback trigger */
365 } else if (cap2 & inst2_user_trig) {
366 if ((rv = it->get_set_opt(it, inst_opt_trig_user)) != inst_ok)
367 ... error ...
368 } else
369 ... error, no usable trigger mode ...
370
371 or if we wanted to trigger progromatically:
372
373 if (cap2 & inst2_prog_trig) {
374 if ((rv = it->get_set_opt(it, inst_opt_trig_prog)) != inst_ok)
375 ... error ...
376
377 } else
378 ... error, no usable trigger mode ...
379
380 To measure one patch:
381
382 ipatch val;
383
384 rv = it->read_sample(it, "SPOT", &val, 1);
385
386 if (rv == inst_ok
387 || (rv & inst_mask) == inst_user_trig)
388 ... OK ...
389
390 else if ((rv & inst_mask) == inst_user_abort)
391 ... abort measurements ...
392
393 else if ((rv & inst_mask) == inst_needs_cal)
394 ... do calibration ...
395
396 else if ((rv & inst_mask) == inst_misread)
397 ... deal with misread, i.e. retry ? ...
398
399 else
400 ... some other error, probably fatal ...
401
402 ipatch values are in XYZ % (0..100) for reflective & transmissive,
403 cd/m^2 for emissive, and Lux for ambient.
404 Spectral will be analogous to the XYZ (see inst_meas_type).
405 By default values may be -ve due to noise (depending on instrument),
406 unless the clamp flag is set.
407
408 9) Taking strip measurements
409 -------------------------
410
411 We may want to set a particular trigger mode before doing this (see above).
412
413 Each strip may need certain parameters, depending on the type of instrument
414 used. For instance, a DTP51 displays a 7 character strip name,
415 and a 3 character pass name and a guide number, whereas an i1pro instrument
416 will ignore these parameters.
417 Similarly, the patch width, gap width and trailer width are only
418 needed by a DTP20 and DTP41.
419
420 To measure a strip:
421
422 #define NPATCHES 21
423 ipatch vals[NPATCHES];
424
425 rv = it->read_strip(it, "STRIP", NPATCHES, "001", 1, 7.366, 2.032, 18.8, vals);
426
427 if (rv == inst_ok
428 || (rv & inst_mask) == inst_user_trig)
429 ... OK ...
430
431 else if ((rv & inst_mask) == inst_user_abort)
432 ... abort measurements ...
433
434 else if ((rv & inst_mask) == inst_needs_cal)
435 ... do calibration ...
436
437 else if if ((rv & inst_mask) == inst_wrong_config)
438 ... deal with bad sensor position, i.e. ColorMunki ...
439
440 else if ((rv & inst_mask) == inst_misread)
441 ... deal with misread, i.e. retry ? ...
442
443 else
444 ... some other error, probably fatal ...
445
446 10) Closing instrument
447 ------------------
448
449 To close an instrument and the communication port, call the del() function:
450
451
452 it->del(it);
453
454
455 12) Error codes
456 -----------
457
458 The inst_code return codes generally consist of two parts; an instrument specific
459 16 bit error code in the lower 16 bits, and an abstract error code in the upper 8 bits.
460
461 These two parts can be separated using the inst_code inst_mask and inst_imask values.
462
463 An English language interpretation of the 16 bit instrument specific error code
464 can be obtained using the inst_interp_error() function:
465
466 it->inst_interp_error(it, rv)
467
468 and an English language interpretation of the 8 bit abstract error code
469 can be obtained using the interp_error() function:
470
471 it->interp_error(it, rv));
472