Debian Codegraph @

src/sensors/huey/huey-ctx.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2013 Richard Hughes <[email protected]>
4 *
5 * Licensed under the GNU General Public License Version 2
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include "config.h"
23
24 #include <glib.h>
25 #include <lcms2.h>
26 #include <stdlib.h>
27
28 #include "huey-ctx.h"
29 #include "huey-device.h"
30 #include "huey-enum.h"
31
32 static void huey_ctx_class_init (HueyCtxClass *klass);
33 static void huey_ctx_init (HueyCtx *ctx);
34 static void huey_ctx_finalize (GObject *object);
35
36 #define GET_PRIVATE(o) (huey_ctx_get_instance_private (o))
37
38 #define HUEY_CONTROL_MESSAGE_TIMEOUT 50000 /* ms */
39 #define HUEY_MAX_READ_RETRIES 5
40
41 /* The CY7C63001 is paired with a 6.00Mhz crystal */
42 #define HUEY_CLOCK_FREQUENCY 6e6
43
44 /* It takes 6 clock pulses to process a single 16bit increment (INC)
45 * instruction and check for the carry so this is the fastest a loop
46 * can be processed. */
47 #define HUEY_POLL_FREQUENCY 1e6
48
49 /* Picked out of thin air, just to try to match reality...
50 * I have no idea why we need to do this, although it probably
51 * indicates we doing something wrong. */
52 #define HUEY_XYZ_POST_MULTIPLY_FACTOR 3.428
53
54 typedef struct
55 {
56 CdMat3x3 calibration_crt;
57 CdMat3x3 calibration_lcd;
58 CdVec3 dark_offset;
59 gchar *unlock_string;
60 gfloat calibration_value;
61 GUsbDevice *device;
62 } HueyCtxPrivate;
63
64 enum {
65 PROP_0,
66 PROP_DEVICE,
67 PROP_LAST
68 };
69
70 G_DEFINE_TYPE_WITH_PRIVATE (HueyCtx, huey_ctx, G_TYPE_OBJECT)
71
72 GUsbDevice *
73 huey_ctx_get_device (HueyCtx *ctx)
74 {
75 HueyCtxPrivate *priv = GET_PRIVATE (ctx);
76 g_return_val_if_fail (HUEY_IS_CTX (ctx), NULL);
77 return priv->device;
78 }
79
80 void
81 huey_ctx_set_device (HueyCtx *ctx, GUsbDevice *device)
82 {
83 HueyCtxPrivate *priv = GET_PRIVATE (ctx);
84 g_return_if_fail (HUEY_IS_CTX (ctx));
85 priv->device = g_object_ref (device);
86 }
87
88 gboolean
89 huey_ctx_setup (HueyCtx *ctx, GError **error)
90 {
91 gboolean ret;
92 HueyCtxPrivate *priv = GET_PRIVATE (ctx);
93 g_autofree gchar *calibration_crt = NULL;
94
95 g_return_val_if_fail (HUEY_IS_CTX (ctx), FALSE);
96 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
97
98 /* get matrix */
99 cd_mat33_clear (&priv->calibration_lcd);
100 ret = huey_device_read_register_matrix (priv->device,
101 HUEY_EEPROM_ADDR_CALIBRATION_DATA_LCD,
102 &priv->calibration_lcd,
103 error);
104 if (!ret)
105 return FALSE;
106 g_debug ("device calibration LCD: %s",
107 cd_mat33_to_string (&priv->calibration_lcd));
108
109 /* get another matrix, although this one is different... */
110 cd_mat33_clear (&priv->calibration_crt);
111 ret = huey_device_read_register_matrix (priv->device,
112 HUEY_EEPROM_ADDR_CALIBRATION_DATA_CRT,
113 &priv->calibration_crt,
114 error);
115 if (!ret)
116 return FALSE;
117 calibration_crt = cd_mat33_to_string (&priv->calibration_crt);
118 g_debug ("device calibration CRT: %s", calibration_crt);
119
120 /* this number is different on all three hueys */
121 ret = huey_device_read_register_float (priv->device,
122 HUEY_EEPROM_ADDR_AMBIENT_CALIB_VALUE,
123 &priv->calibration_value,
124 error);
125 if (!ret)
126 return FALSE;
127
128 /* this vector changes between sensor 1 and 3 */
129 ret = huey_device_read_register_vector (priv->device,
130 HUEY_EEPROM_ADDR_DARK_OFFSET,
131 &priv->dark_offset,
132 error);
133 if (!ret)
134 return FALSE;
135 return TRUE;
136 }
137
138 const CdMat3x3 *
139 huey_ctx_get_calibration_lcd (HueyCtx *ctx)
140 {
141 HueyCtxPrivate *priv = GET_PRIVATE (ctx);
142 g_return_val_if_fail (HUEY_IS_CTX (ctx), NULL);
143 return &priv->calibration_lcd;
144 }
145
146 const CdMat3x3 *
147 huey_ctx_get_calibration_crt (HueyCtx *ctx)
148 {
149 HueyCtxPrivate *priv = GET_PRIVATE (ctx);
150 g_return_val_if_fail (HUEY_IS_CTX (ctx), NULL);
151 return &priv->calibration_crt;
152 }
153
154 gfloat
155 huey_ctx_get_calibration_value (HueyCtx *ctx)
156 {
157 HueyCtxPrivate *priv = GET_PRIVATE (ctx);
158 g_return_val_if_fail (HUEY_IS_CTX (ctx), -1);
159 return priv->calibration_value;
160 }
161
162 const CdVec3 *
163 huey_ctx_get_dark_offset (HueyCtx *ctx)
164 {
165 HueyCtxPrivate *priv = GET_PRIVATE (ctx);
166 g_return_val_if_fail (HUEY_IS_CTX (ctx), NULL);
167 return &priv->dark_offset;
168 }
169
170 const gchar *
171 huey_ctx_get_unlock_string (HueyCtx *ctx)
172 {
173 HueyCtxPrivate *priv = GET_PRIVATE (ctx);
174 g_return_val_if_fail (HUEY_IS_CTX (ctx), NULL);
175 return priv->unlock_string;
176 }
177
178 typedef struct {
179 guint16 R;
180 guint16 G;
181 guint16 B;
182 } HueyCtxMultiplier;
183
184 typedef struct {
185 guint32 R;
186 guint32 G;
187 guint32 B;
188 } HueyCtxDeviceRaw;
189
190 static gboolean
191 huey_ctx_sample_for_threshold (HueyCtx *ctx,
192 HueyCtxMultiplier *threshold,
193 HueyCtxDeviceRaw *raw,
194 GError **error)
195 {
196 HueyCtxPrivate *priv = GET_PRIVATE (ctx);
197 guint8 request[] = { HUEY_CMD_SENSOR_MEASURE_RGB,
198 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
199 guint8 reply[8];
200 gboolean ret;
201 gsize reply_read;
202
203 /* these are 16 bit gain values */
204 cd_buffer_write_uint16_be (request + 1, threshold->R);
205 cd_buffer_write_uint16_be (request + 3, threshold->G);
206 cd_buffer_write_uint16_be (request + 5, threshold->B);
207
208 /* measure, and get red */
209 ret = huey_device_send_data (priv->device,
210 request, 8,
211 reply, 8,
212 &reply_read,
213 error);
214 if (!ret)
215 return FALSE;
216
217 /* get value */
218 raw->R = cd_buffer_read_uint32_be (reply+2);
219
220 /* get green */
221 request[0] = HUEY_CMD_READ_GREEN;
222 ret = huey_device_send_data (priv->device,
223 request, 8,
224 reply, 8,
225 &reply_read,
226 error);
227 if (!ret)
228 return FALSE;
229
230 /* get value */
231 raw->G = cd_buffer_read_uint32_be (reply+2);
232
233 /* get blue */
234 request[0] = HUEY_CMD_READ_BLUE;
235 ret = huey_device_send_data (priv->device,
236 request, 8,
237 reply, 8,
238 &reply_read,
239 error);
240 if (!ret)
241 return FALSE;
242
243 /* get value */
244 raw->B = cd_buffer_read_uint32_be (reply+2);
245 return TRUE;
246 }
247
248 /**
249 * huey_ctx_convert_device_RGB_to_XYZ:
250 *
251 * / X \ ( / R \ / c a l \ )
252 * | Y | = ( | G | * | m a t | ) x post_scale
253 * \ Z / ( \ B / \ l c d / )
254 *
255 **/
256 static void
257 huey_ctx_convert_device_RGB_to_XYZ (CdColorRGB *src,
258 CdColorXYZ *dest,
259 CdMat3x3 *calibration,
260 gdouble post_scale)
261 {
262 CdVec3 *result;
263
264 /* convolve */
265 result = (CdVec3 *) dest;
266 cd_mat33_vector_multiply (calibration, (CdVec3 *) src, result);
267
268 /* post-multiply */
269 cd_vec3_scalar_multiply (result,
270 post_scale,
271 result);
272 }
273
274
275 CdColorXYZ *
276 huey_ctx_take_sample (HueyCtx *ctx, CdSensorCap cap, GError **error)
277 {
278 HueyCtxPrivate *priv = GET_PRIVATE (ctx);
279 CdColorRGB values;
280 CdColorXYZ color_result;
281 CdMat3x3 *device_calibration;
282 CdVec3 *temp;
283 gboolean ret;
284 HueyCtxDeviceRaw color_native;
285 HueyCtxMultiplier multiplier;
286
287 g_return_val_if_fail (HUEY_IS_CTX (ctx), NULL);
288 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
289
290 /* no hardware support */
291 if (cap == CD_SENSOR_CAP_PROJECTOR) {
292 g_set_error_literal (error,
293 G_IO_ERROR,
294 G_IO_ERROR_NOT_SUPPORTED,
295 "Huey cannot measure in projector mode");
296 return NULL;
297 }
298
299 /* set this to one value for a quick approximate value */
300 multiplier.R = 1;
301 multiplier.G = 1;
302 multiplier.B = 1;
303 ret = huey_ctx_sample_for_threshold (ctx,
304 &multiplier,
305 &color_native,
306 error);
307 if (!ret)
308 return NULL;
309 g_debug ("initial values: red=%u, green=%u, blue=%u",
310 color_native.R, color_native.G, color_native.B);
311
312 /* try to fill the 16 bit register for accuracy */
313 if (color_native.R != 0)
314 multiplier.R = HUEY_POLL_FREQUENCY / color_native.R;
315 if (color_native.G != 0)
316 multiplier.G = HUEY_POLL_FREQUENCY / color_native.G;
317 if (color_native.B != 0)
318 multiplier.B = HUEY_POLL_FREQUENCY / color_native.B;
319
320 /* don't allow a value of zero */
321 if (multiplier.R == 0)
322 multiplier.R = 1;
323 if (multiplier.G == 0)
324 multiplier.G = 1;
325 if (multiplier.B == 0)
326 multiplier.B = 1;
327 g_debug ("using multiplier factor: red=%i, green=%i, blue=%i",
328 multiplier.R, multiplier.G, multiplier.B);
329 ret = huey_ctx_sample_for_threshold (ctx,
330 &multiplier,
331 &color_native,
332 error);
333 if (!ret)
334 return NULL;
335 g_debug ("raw values: red=%u, green=%u, blue=%u",
336 color_native.R, color_native.G, color_native.B);
337
338 /* get DeviceRGB values */
339 values.R = (gdouble) multiplier.R * 0.5f * HUEY_POLL_FREQUENCY / ((gdouble) color_native.R);
340 values.G = (gdouble) multiplier.G * 0.5f * HUEY_POLL_FREQUENCY / ((gdouble) color_native.G);
341 values.B = (gdouble) multiplier.B * 0.5f * HUEY_POLL_FREQUENCY / ((gdouble) color_native.B);
342 g_debug ("scaled values: red=%0.6lf, green=%0.6lf, blue=%0.6lf",
343 values.R, values.G, values.B);
344
345 /* remove dark offset */
346 temp = (CdVec3*) &values;
347 cd_vec3_subtract (temp,
348 &priv->dark_offset,
349 temp);
350
351 g_debug ("dark offset values: red=%0.6lf, green=%0.6lf, blue=%0.6lf",
352 values.R, values.G, values.B);
353
354 /* negative values don't make sense (device needs recalibration) */
355 if (values.R < 0.0f)
356 values.R = 0.0f;
357 if (values.G < 0.0f)
358 values.G = 0.0f;
359 if (values.B < 0.0f)
360 values.B = 0.0f;
361
362 /* we use different calibration matrices for each output type */
363 switch (cap) {
364 case CD_SENSOR_CAP_CRT:
365 case CD_SENSOR_CAP_PLASMA:
366 g_debug ("using CRT calibration matrix");
367 device_calibration = &priv->calibration_crt;
368 break;
369 default:
370 g_debug ("using LCD calibration matrix");
371 device_calibration = &priv->calibration_lcd;
372 break;
373 }
374
375 /* convert from device RGB to XYZ */
376 huey_ctx_convert_device_RGB_to_XYZ (&values,
377 &color_result,
378 device_calibration,
379 HUEY_XYZ_POST_MULTIPLY_FACTOR);
380 g_debug ("finished values: red=%0.6lf, green=%0.6lf, blue=%0.6lf",
381 color_result.X, color_result.Y, color_result.Z);
382
383 /* save result */
384 return cd_color_xyz_dup (&color_result);
385 }
386
387 static void
388 huey_ctx_get_property (GObject *object,
389 guint prop_id,
390 GValue *value,
391 GParamSpec *pspec)
392 {
393 HueyCtx *ctx = HUEY_CTX (object);
394 HueyCtxPrivate *priv = GET_PRIVATE (ctx);
395
396 switch (prop_id) {
397 case PROP_DEVICE:
398 g_value_set_object (value, priv->device);
399 break;
400 default:
401 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
402 break;
403 }
404 }
405
406
407 static void
408 huey_ctx_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
409 {
410 HueyCtx *ctx = HUEY_CTX (object);
411 HueyCtxPrivate *priv = GET_PRIVATE (ctx);
412
413 switch (prop_id) {
414 case PROP_DEVICE:
415 priv->device = g_value_dup_object (value);
416 break;
417 default:
418 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
419 break;
420 }
421 }
422
423 static void
424 huey_ctx_class_init (HueyCtxClass *klass)
425 {
426 GObjectClass *object_class = G_OBJECT_CLASS (klass);
427
428 object_class->get_property = huey_ctx_get_property;
429 object_class->set_property = huey_ctx_set_property;
430 object_class->finalize = huey_ctx_finalize;
431
432 g_object_class_install_property (object_class,
433 PROP_DEVICE,
434 g_param_spec_object ("device",
435 NULL, NULL,
436 G_USB_TYPE_DEVICE,
437 G_PARAM_READWRITE));
438 }
439
440 static void
441 huey_ctx_init (HueyCtx *ctx)
442 {
443 HueyCtxPrivate *priv = GET_PRIVATE (ctx);
444
445 cd_mat33_clear (&priv->calibration_lcd);
446 cd_mat33_clear (&priv->calibration_crt);
447 }
448
449 static void
450 huey_ctx_finalize (GObject *object)
451 {
452 HueyCtx *ctx = HUEY_CTX (object);
453 HueyCtxPrivate *priv = GET_PRIVATE (ctx);
454
455 g_return_if_fail (HUEY_IS_CTX (object));
456
457 g_free (priv->unlock_string);
458
459 G_OBJECT_CLASS (huey_ctx_parent_class)->finalize (object);
460 }
461
462 HueyCtx *
463 huey_ctx_new (void)
464 {
465 HueyCtx *ctx;
466 ctx = g_object_new (HUEY_TYPE_CTX, NULL);
467 return HUEY_CTX (ctx);
468 }