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 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780
|
/*
* Argyll Color Correction System
*
* GretagMacbeth Huey related functions
*
* Author: Graeme W. Gill
* Date: 18/10/2006
*
* Copyright 2006 - 2014, Graeme W. Gill
* All rights reserved.
*
* (Based on i1disp.c)
*
* This material is licenced under the GNU GENERAL PUBLIC LICENSE Version 2 or later :-
* see the License2.txt file for licencing details.
*/
/*
If you make use of the instrument driver code here, please note
that it is the author(s) of the code who take responsibility
for its operation. Any problems or queries regarding driving
instruments with the Argyll drivers, should be directed to
the Argyll's author(s), and not to any other party.
If there is some instrument feature or function that you
would like supported here, it is recommended that you
contact Argyll's author(s) first, rather than attempt to
modify the software yourself, if you don't have firm knowledge
of the instrument communicate protocols. There is a chance
that an instrument could be damaged by an incautious command
sequence, and the instrument companies generally cannot and
will not support developers that they have not qualified
and agreed to support.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <stdarg.h>
#include <math.h>
#ifndef SALONEINSTLIB
#include "copyright.h"
#include "aconfig.h"
#include "numlib.h"
#else /* SALONEINSTLIB */
#include "sa_config.h"
#include "numsup.h"
#endif /* SALONEINSTLIB */
#include "cgats.h"
#include "xspect.h"
#include "insttypes.h"
#include "conv.h"
#include "icoms.h"
#include "huey.h"
#define dbgo stderr
static inst_code huey_interp_code(inst *pp, int ec);
static inst_code huey_check_unlock(huey *p);
#define CALFACTOR 3.428 /* Emissive magic calibration factor */
//#define AMB_SCALE_FACTOR 5.772e-3 /* Ambient mode scale factor Lux/pi*/
#define AMB_SCALE_FACTOR 18.1333e-3 /* Ambient mode scale factor (Lux) */
/* This is only approximate, and were derived */
/* by matching readings from the i1pro. */
/* ------------------------------------------------------------------------ */
/* Implementation */
/* Interpret an icoms error into a HUEY error */
/* If torc is nz, then a trigger or command is OK, */
/* othewise they are treated as an abort. */
static int icoms2huey_err(int se, int torc) {
if (se != ICOM_OK)
return HUEY_COMS_FAIL;
return HUEY_OK;
}
/* i1Display command codes */
/* B = byte (8bit), S = short (16bit), W = word (32bit), A = string */
/* U = unused byte, - = no arguments/results */
/* The is a 7 byte command buffer and 6 response receive buffer. */
/* :2 means the read is from a second 8 byte ep x81 read. */
/* cbuf[-] is command byte */
/* rbuf[-2] is continuation byte */
/* rbuf[-1] is echo of command byte */
/* rbuf2[-2] is an error byte if nz */
typedef enum {
i1d_status = 0x00, /* -:A Get status string starting at 0, 1sec */
i1d_rd_green = 0x02, /* -:W Read the green channel, 1sec */
i1d_rd_blue = 0x03, /* -:W Read the blue channel, 1sec */
i1d_setintgt = 0x05, /* W:- Set the integration time, 1sec */
i1d_getintgt = 0x06, /* -:W Get the integration time, 1sec */
i1d_wrreg = 0x07, /* BB:? Write a register value, 1sec */
i1d_rdreg = 0x08, /* B:B Read a register value, 1sec */
i1d_unlock = 0x0e, /* BBBB:- Unlock the interface */
i1d_m_red_2 = 0x13, /* B:2:W Measure the red channel in freq mode, 1,10sec */
/* B = sync mode, typically 2 */
i1d_m_rgb_edge_2 = 0x16, /* SSS:2:WB Measure RGB edge/period mode, 1.70sec, ret red */
/* 2nd return value is not used ? */
i1d_rdambient = 0x17, /* BB:2:BWB Read Ambient, 1,10sec */
/* Returns first B param as first response */
/* Returns W as value read */
/* Returns aditional byte at end */
i1d_set_leds = 0x18, /* BB:B Set 4 LEDs state, 1sec */
/* 1st B is always 0 in practice */
/* 2nd B bits 0-4, 0 == on */
/* Echo's led state with returned B */
i1d_rgb_edge_3 = 0x19 /* SB:2:WB Unknown measurement command 1,10sec */
/* S is number of edges ?? */
/* B is the channel */
/* W is the reading */
/* B is not used ? */
} i1DispCC;
/* Diagnostic - return a description given the instruction code */
static char *inst_desc(int cc) {
static char buf[40]; /* Fallback string */
switch(cc) {
case 0x00:
return "GetStatus";
case 0x02:
return "RdGreen";
case 0x03:
return "RdBlue";
case 0x05:
return "SetIntTime";
case 0x06:
return "GetIntTime";
case 0x07:
return "WrReg";
case 0x08:
return "RdReg";
case 0x09:
return "GetMeasPeriod";
case 0x0e:
return "Unlock";
case 0x13:
return "RdRedFreqMode";
case 0x16:
return "MeasRGBPeriMode";
case 0x17:
return "RdAmbient";
case 0x18:
return "SetLEDs";
case 0x19:
return "MeasRGBPeriMode2";
}
sprintf(buf,"Unknown %02x",cc);
return buf;
}
/* Do a command/response exchange with the huey. */
/* Return the error code */
/* The Huey is set up as an HID device, which can ease the need */
/* for providing a kernel driver on MSWindows systems, */
/* but it doesn't seem to actually be used as an HID device. */
/* We allow for communicating via usbio, or an HID driver. */
static inst_code
huey_command(
huey *p, /* huey object */
i1DispCC cc, /* Command code */
unsigned char *in, /* 7 Command bytes to send */
unsigned char *out, /* 6 Response bytes returned */
double to, /* Timeout in seconds */
double to2 /* Timeout in seconds for 2nd read */
) {
int i;
unsigned char buf[8]; /* 8 bytes to write/read */
int wbytes; /* bytes written */
int rbytes; /* bytes read from ep */
int se, ua = 0, rv = inst_ok;
int ishid = p->icom->port_type(p->icom) == icomt_hid;
a1logd(p->log,5,"huey_command: Sending '%s' args '%s'\n",inst_desc(cc), icoms_tohex(in, 7));
/* Send the command using the control interface */
buf[0] = cc; /* Construct the command == HID report number */
memmove(buf + 1, in, 7);
if (ishid) {
se = p->icom->hid_write(p->icom, buf, 8, &wbytes, to);
} else {
se = p->icom->usb_control(p->icom,
IUSB_ENDPOINT_OUT | IUSB_REQ_TYPE_CLASS | IUSB_REQ_RECIP_INTERFACE, 0x9, 0x200, 0, buf, 8, to);
wbytes = 8;
}
if (se != 0) {
a1logd(p->log,1,"huey_command: command send failed with ICOM err 0x%x\n",se);
return huey_interp_code((inst *)p, HUEY_COMS_FAIL);
}
rv = huey_interp_code((inst *)p, icoms2huey_err(ua, 0));
if (rv == inst_ok && wbytes != 8)
rv = huey_interp_code((inst *)p, HUEY_BAD_WR_LENGTH);
a1logd(p->log,6,"huey_command: get inst code\n",rv);
if (rv != inst_ok) {
/* Flush any response if write failed */
if (ishid)
p->icom->hid_read(p->icom, buf, 8, &rbytes, to);
else
p->icom->usb_read(p->icom, NULL, 0x81, buf, 8, &rbytes, to);
return rv;
}
/* Now fetch the response */
a1logd(p->log,6,"huey_command: Reading response\n");
if (ishid) {
se = p->icom->hid_read(p->icom, buf, 8, &rbytes, to);
} else {
se = p->icom->usb_read(p->icom, NULL, 0x81, buf, 8, &rbytes, to);
}
if (se != 0) {
a1logd(p->log,1,"huey_command: read failed with ICOM err 0x%x\n",se);
return huey_interp_code((inst *)p, HUEY_COMS_FAIL);
}
rv = huey_interp_code((inst *)p, icoms2huey_err(ua, 0));
if (rv == inst_ok && rbytes != 8)
rv = huey_interp_code((inst *)p, HUEY_BAD_RD_LENGTH);
if (rv == inst_ok && buf[1] != cc)
rv = huey_interp_code((inst *)p, HUEY_BAD_RET_CMD);
/* Some commands don't use the first response, but need to */
/* fetch a second response, with a longer timeout. */
/* This seems to be all of the measurement trigger commands */
if (rv == inst_ok && buf[0] == 0x90) { /* there is more */
a1logd(p->log,6,"huey_command: Reading extended response\n");
if (ishid) {
se = p->icom->hid_read(p->icom, buf, 8, &rbytes, to2);
} else {
se = p->icom->usb_read(p->icom, NULL, 0x81, buf, 8, &rbytes, to2);
}
if (se != 0) {
a1logd(p->log,1,"huey_command: read failed with ICOM err 0x%x\n",se);
return huey_interp_code((inst *)p, HUEY_COMS_FAIL);
}
rv = huey_interp_code((inst *)p, icoms2huey_err(ua, 0));
if (rv == inst_ok && rbytes != 8)
rv = huey_interp_code((inst *)p, HUEY_BAD_RD_LENGTH);
if (rv == inst_ok && buf[1] != cc) {
rv = huey_interp_code((inst *)p, HUEY_BAD_RET_CMD);
}
}
/* The first byte returned seems to be a command result error code. */
/* Not all codes are known, but it seems that the 6 byte payload */
/* is an error message, for instance 0x80 "NoCmd". */
/* The second byte is always the command code being echo'd back. */
if (rv == inst_ok && cc != 0x00 && buf[0] != 0x00) {
ua = HUEY_BAD_RET_STAT;
if (buf[0] == 0x80)
ua = HUEY_BAD_COMMAND;
rv = huey_interp_code((inst *)p, ua);
}
if (rv == inst_ok) {
memmove(out, buf + 2, 6);
} else {
memset(out, 0, 6);
}
a1logd(p->log,5,"huey_command: returning '%s' ICOM err 0x%x\n",icoms_tohex(out, 6),ua);
return rv;
}
/* Take an int, and convert it into a byte buffer */
static void int2buf(unsigned char *buf, int inv) {
buf[0] = (inv >> 24) & 0xff;
buf[1] = (inv >> 16) & 0xff;
buf[2] = (inv >> 8) & 0xff;
buf[3] = (inv >> 0) & 0xff;
}
/* Take a short, and convert it into a byte buffer */
static void short2buf(unsigned char *buf, int inv) {
buf[0] = (inv >> 8) & 0xff;
buf[1] = (inv >> 0) & 0xff;
}
/* Take a word sized return buffer, and convert it to an int */
static int buf2int(unsigned char *buf) {
int val;
val = buf[0];
val = ((val << 8) + (0xff & buf[1]));
val = ((val << 8) + (0xff & buf[2]));
val = ((val << 8) + (0xff & buf[3]));
return val;
}
/* Read a byte from a register */
static inst_code
huey_rdreg_byte(
huey *p, /* Object */
int *outp, /* Where to write value */
int addr /* Register Address, 0 - 255 */
) {
unsigned char buf[8];
int rsize;
inst_code ev;
if (addr < 0 || addr > 255)
return huey_interp_code((inst *)p, HUEY_BAD_REG_ADDRESS);
/* Read a byte */
memset(buf, 0, 7);
buf[0] = addr;
if ((ev = huey_command(p, i1d_rdreg, buf, buf, 1.0, 1.0)) != inst_ok)
return ev;
/* We expect the address to be returned */
if ((buf[0] & 0xff) != addr)
return huey_interp_code((inst *)p, HUEY_UNEXPECTED_RET_VAL);
*outp = (int)(buf[1] & 0xff);
return inst_ok;
}
/* Read a short from a register */
static inst_code
huey_rdreg_short(
huey *p, /* Object */
int *outp, /* Where to write value */
int addr /* Register Address, 0 - 126 */
) {
inst_code ev;
int v, val;
if ((ev = huey_rdreg_byte(p, &v, addr)) != inst_ok)
return ev;
val = v;
if ((ev = huey_rdreg_byte(p, &v, addr+1)) != inst_ok)
return ev;
val = ((val << 8) + (0xff & v));
*outp = val;
return inst_ok;
}
/* Read a word from a register */
static inst_code
huey_rdreg_word(
huey *p, /* Object */
int *outp, /* Where to write value */
int addr /* Register Address, 0 - 124 */
) {
inst_code ev;
int v, val;
if ((ev = huey_rdreg_byte(p, &v, addr)) != inst_ok)
return ev;
val = v;
if ((ev = huey_rdreg_byte(p, &v, addr+1)) != inst_ok)
return ev;
val = ((val << 8) + (0xff & v));
if ((ev = huey_rdreg_byte(p, &v, addr+2)) != inst_ok)
return ev;
val = ((val << 8) + (0xff & v));
if ((ev = huey_rdreg_byte(p, &v, addr+3)) != inst_ok)
return ev;
val = ((val << 8) + (0xff & v));
*outp = val;
return inst_ok;
}
/* Read a float from a register */
/* Will return HUEY_FLOAT_NOT_SET if the float value was 0xffffffff */
static inst_code
huey_rdreg_float(
huey *p, /* Object */
double *outp, /* Where to write value */
int addr /* Register Address, 0 - 124 */
) {
inst_code ev;
int val;
if ((ev = huey_rdreg_word(p, &val, addr)) != inst_ok)
return ev;
if (val == 0xffffffff) {
return inst_ok;
}
*outp = IEEE754todouble((unsigned int)val);
return inst_ok;
}
/* Write a byte to a register */
static inst_code
huey_wrreg_byte(
huey *p, /* Object */
int inv, /* Input value */
int addr /* Register Address, 0 - 127 */
) {
int cval;
unsigned char ibuf[8], obuf[8];
int rsize;
inst_code ev;
memset(ibuf, 0, 7);
ibuf[0] = addr;
ibuf[1] = inv;
/* Write a byte */
if ((ev = huey_command(p, i1d_wrreg, ibuf, obuf, 1.0, 1.0)) != inst_ok)
return ev;
return inst_ok;
}
/* Write a word to a register */
static inst_code
huey_wrreg_word(
huey *p, /* Object */
int inv, /* Where to write value */
int addr /* Register Address, 0 - 124 */
) {
inst_code ev;
int v;
v = (inv >> 24) & 0xff;
if ((ev = huey_wrreg_byte(p, v, addr) ) != inst_ok)
return ev;
v = (inv >> 16) & 0xff;
if ((ev = huey_wrreg_byte(p, v, addr+1) ) != inst_ok)
return ev;
v = (inv >> 8) & 0xff;
if ((ev = huey_wrreg_byte(p, v, addr+2) ) != inst_ok)
return ev;
v = (inv >> 0) & 0xff;
if ((ev = huey_wrreg_byte(p, v, addr+3) ) != inst_ok)
return ev;
return inst_ok;
}
/* Write a float to a register */
static inst_code
huey_wrreg_float(
huey *p, /* Object */
double inv, /* Value to write */
int addr /* Register Address, 0 - 124 */
) {
inst_code ev;
int val;
val = (int)doubletoIEEE754(inv);
if ((ev = huey_wrreg_word(p, val, addr)) != inst_ok)
return ev;
return inst_ok;
}
/* Read the integration time */
static inst_code
huey_rd_int_time(
huey *p, /* Object */
int *outp /* Where to write value */
) {
unsigned char buf[8];
int rsize;
inst_code ev;
memset(buf, 0, 7);
if ((ev = huey_command(p, i1d_getintgt, buf, buf, 1.0, 1.0)) != inst_ok)
return ev;
*outp = buf2int(buf);
return inst_ok;
}
/* Set the integration time */
/* (Not used for Huey ?) */
static inst_code
huey_wr_int_time(
huey *p, /* Object */
int inv /* Value to write */
) {
unsigned char buf[16];
int rsize;
inst_code ev;
memset(buf, 0, 7);
int2buf(buf, inv);
if ((ev = huey_command(p, i1d_setintgt, buf, buf, 1.0, 1.0)) != inst_ok)
return ev;
return inst_ok;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Take a raw measurement using a given integration time. */
/* The measureent is the count of (both) edges from the L2V */
/* over the integration time */
static inst_code
huey_freq_measure(
huey *p, /* Object */
double rgb[3] /* Return the RGB edge count values */
) {
int i;
unsigned char ibuf[8];
unsigned char obuf[8];
inst_code ev;
/* Do the measurement, and return the Red value */
memset(ibuf, 0, 7);
ibuf[0] = 2; /* Sync mode 2 for CRT */
if ((ev = huey_command(p, i1d_m_red_2, ibuf, obuf, 1.0, 10.0)) != inst_ok)
return ev;
rgb[0] = (double)buf2int(obuf);
/* Get the green value */
memset(ibuf, 0, 7);
if ((ev = huey_command(p, i1d_rd_green, ibuf, obuf, 1.0, 1.0)) != inst_ok)
return ev;
rgb[1] = (double)buf2int(obuf);
/* Get the blue value */
memset(ibuf, 0, 7);
if ((ev = huey_command(p, i1d_rd_blue, ibuf, obuf, 1.0, 1.0)) != inst_ok)
return ev;
rgb[2] = (double)buf2int(obuf);
return inst_ok;
}
/* Take a raw measurement that returns the number of clocks */
/* between and initial edge and edgec[] subsequent edges of the L2F. */
/* Both edges are counted. */
static inst_code
huey_period_measure(
huey *p, /* Object */
int edgec[3], /* Measurement edge count for each channel */
double rgb[3] /* Return the RGB values */
) {
int i;
unsigned char ibuf[16];
unsigned char obuf[16];
int rsize;
inst_code ev;
/* Set the edge count */
memset(ibuf, 0, 7);
short2buf(ibuf + 0, edgec[0]);
short2buf(ibuf + 2, edgec[1]);
short2buf(ibuf + 4, edgec[2]);
/* Do the measurement, and return the Red value */
if ((ev = huey_command(p, i1d_m_rgb_edge_2, ibuf, obuf, 1.0, 70.0)) != inst_ok)
return ev;
rgb[0] = (double)buf2int(obuf);
/* Get the green value */
memset(ibuf, 0, 7);
if ((ev = huey_command(p, i1d_rd_green, ibuf, obuf, 1.0, 1.0)) != inst_ok)
return ev;
rgb[1] = (double)buf2int(obuf);
/* Get the blue value */
memset(ibuf, 0, 7);
if ((ev = huey_command(p, i1d_rd_blue, ibuf, obuf, 1.0, 1.0)) != inst_ok)
return ev;
rgb[2] = (double)buf2int(obuf);
return inst_ok;
}
/* Take a another mode raw mesurement from the device for */
/* (not currently used ?) */
static inst_code
huey_take_raw_measurement_3(
huey *p, /* Object */
int edgec[3], /* Measurement edge count for each channel */
double rgb[3] /* Return the RGB values */
) {
int i;
unsigned char ibuf[16];
unsigned char obuf[16];
int rsize;
inst_code ev;
/* Do the measurement, and return the Red value */
memset(ibuf, 0, 7);
short2buf(ibuf + 0, edgec[0]);
ibuf[2] = 0; /* Channel */
if ((ev = huey_command(p, i1d_m_rgb_edge_2, ibuf, obuf, 1.0, 10.0)) != inst_ok)
return ev;
rgb[0] = (double)buf2int(obuf);
/* Do the measurement, and return the Green value */
memset(ibuf, 0, 7);
short2buf(ibuf + 0, edgec[1]);
ibuf[2] = 1; /* Channel */
if ((ev = huey_command(p, i1d_m_rgb_edge_2, ibuf, obuf, 1.0, 10.0)) != inst_ok)
return ev;
rgb[1] = (double)buf2int(obuf);
/* Do the measurement, and return the Blue value */
memset(ibuf, 0, 7);
short2buf(ibuf + 0, edgec[2]);
ibuf[2] = 2; /* Channel */
if ((ev = huey_command(p, i1d_m_rgb_edge_2, ibuf, obuf, 1.0, 10.0)) != inst_ok)
return ev;
rgb[2] = (double)buf2int(obuf);
return inst_ok;
}
/* Take a cooked measurement from the device for Huey. */
/* The sensors are likely to be light to frequency converters */
/* such as the TAOS TSL237, and there are only so many ways */
/* the output can be measured. If the light level is high enough, */
/* you could simply count the number of output transitions (edges) */
/* in a fixed period of time. If the frequency is low though, */
/* this limits precision due to quantization of the count. */
/* Another way is to time how long it takes to count a certain */
/* number of edges. This has higher precision at low frequencies, */
/* but has the problem of having an unknown measurement duration, */
/* and seems to be the main method chosen for the Huey. */
static inst_code
huey_take_measurement_2(
huey *p, /* Object */
int refr, /* nz if crt mode */
double rgb[3] /* Return the rgb values */
) {
int i, j;
int edgec[3] = {1,1,1}; /* Measurement edge count for each channel */
int rem[3] = {1,1,1}; /* remeasure flags */
inst_code ev;
if (p->inited == 0)
return huey_interp_code((inst *)p, HUEY_NOT_INITED);
a1logd(p->log,4,"take_measurement_2 called with refr = %d\n",refr);
/* For Refresh mode, do an initial set of measurements over a fixed period */
if (refr) {
if ((ev = huey_freq_measure(p, rgb)) != inst_ok)
return ev;
a1logd(p->log,4,"Raw initial CRT RGB = %f %f %f\n",rgb[0],rgb[1],rgb[2]);
/* Decide whether any channels need re-measuring, */
/* and computed cooked values. Threshold is typically 75 */
for (i = 0; i < 3; i++) {
rem[i] = (rgb[i] <= (0.75 * (double)p->sampno)) ? 1 : 0;
rgb[i] = 0.5 * rgb[i] * 1e6/(double)p->int_clocks;
}
a1logd(p->log,4,"Re-measure flags = %d %d %d\n",rem[0],rem[1],rem[2]);
}
/* If any need re-measuring */
if (rem[0] || rem[1] || rem[2]) {
double rgb2[3];
/* Do a first or second set of measurements */
if ((ev = huey_period_measure(p, edgec, rgb2)) != inst_ok)
return ev;
a1logd(p->log,4,"Raw initial/subsequent ecount %d %d %d RGB = %f %f %f\n",
edgec[0], edgec[1], edgec[2], rgb2[0], rgb2[1], rgb2[2]);
/* Compute adjusted edge count for channels we're remeasuring, */
/* aiming for count values of clk_freq (~1e6). */
for (i = 0; i < 3; i++) {
double ns;
if (rem[i]) {
if (p->clk_freq > ((2000.0 - 0.5) * rgb2[i]))
ns = 2000.0;
else {
ns = floor(p->clk_freq/rgb2[i]) + 0.5;
if (ns < 1.0)
ns = 1.0;
}
edgec[i] = (int)ns;
}
}
/* If we compute a different edge count, read again */
if (edgec[0] > 1 || edgec[1] > 1 || edgec[2] > 1) {
double rgb3[3]; /* 2nd RGB Readings */
if ((ev = huey_period_measure(p, edgec, rgb3)) != inst_ok)
return ev;
a1logd(p->log,4,"Raw subsequent2 ecount %d %d %d RGB = %f %f %f\n",
edgec[0], edgec[1], edgec[2], rgb3[0], rgb3[1], rgb3[2]);
/* Average readings if we repeated a measurement with the same threshold */
/* (Minor advantage, but may as well use it) */
for (i = 0; i < 3; i++) {
if (edgec[i] == 1)
rgb2[i] = 0.5 * (rgb2[i] + rgb3[i]);
else
rgb2[i] = rgb3[i];
}
}
/* Compute adjusted readings, ovewritting initial cooked values */
for (i = 0; i < 3; i++) {
if (rem[i]) {
rgb[i] = ((double)edgec[i])/(rgb2[i] * 2.0 * p->clk_prd);
a1logd(p->log,4,"%d after scale = %f\n",i,rgb[i]);
rgb[i] -= p->dark_cal[i]; /* Subtract black level */
a1logd(p->log,4,"%d after sub black = %f\n",i,rgb[i]);
if (rgb[i] < 0.0001)
rgb[i] = 0.0001;
a1logd(p->log,4,"%d after limit min = %f\n",i,rgb[i]);
}
}
}
a1logd(p->log,4,"Cooked RGB = %f %f %f\n",rgb[0],rgb[1],rgb[2]);
return inst_ok;
}
/* Take a raw ambient measurement */
static inst_code
huey_take_amb_measurement_1(
huey *p, /* Object */
int a1, /* 8 bit argument - unknown */
int syncmode, /* 8 bit argument - sync mode */
double *amb, /* Return the raw ambient value */
int *rb /* Returned byte */
) {
int i;
unsigned char ibuf[16];
unsigned char obuf[16];
int rsize;
inst_code ev;
memset(ibuf, 0, 7);
a1 &= 0xff;
ibuf[0] = a1;
ibuf[1] = syncmode;
/* Do the measurement */
if ((ev = huey_command(p, i1d_rdambient, ibuf, obuf, 1.0, 10.0)) != inst_ok)
return ev;
/* Expect first argument to be returned */
if (obuf[0] != a1)
return huey_interp_code((inst *)p, HUEY_UNEXPECTED_RET_VAL);
*amb = (double)buf2int(obuf+1);
*rb = 0xff & obuf[5];
return inst_ok;
}
/* Take a cooked ambient measurement */
static inst_code
huey_take_amb_measurement(
huey *p, /* Object */
int refr, /* nz if refresh mode */
double *amb /* Return the ambient value */
) {
int rb; /* Returned byte - not used */
inst_code ev;
if (p->inited == 0)
return huey_interp_code((inst *)p, HUEY_NOT_INITED);
a1logd(p->log,4,"take_amb_measurement_2 called with refr = %d\n",refr);
/* First param is always 3, second is sync mode */
if ((ev = huey_take_amb_measurement_1(p, 3, refr ? 2 : 0, amb, &rb)) != inst_ok)
return ev;
a1logd(p->log,4,"Raw ambient = %f\n",*amb);
return inst_ok;
}
/* Set the indicator LED's state. */
/* The bottom 4 bits set the LED state from bottom (0) */
/* to top (3), 0 = off, 1 = on */
static inst_code
huey_set_LEDs(
huey *p, /* Object */
int mask /* 8 bit LED mask */
) {
int i;
unsigned char ibuf[8];
unsigned char obuf[8];
inst_code ev;
memset(ibuf, 0, 7);
mask &= 0xf;
p->led_state = mask;
ibuf[0] = 0;
ibuf[1] = 0xf & (~mask);
/* Do command */
if ((ev = huey_command(p, i1d_set_leds, ibuf, obuf, 1.0, 1.0)) != inst_ok)
return ev;
return inst_ok;
}
/* . . . . . . . . . . . . . . . . . . . . . . . . */
/* Take a XYZ measurement from the device */
static inst_code
huey_take_XYZ_measurement(
huey *p, /* Object */
double XYZ[3] /* Return the XYZ values */
) {
int i, j;
double rgb[3]; /* RGB Readings */
inst_code ev;
double *mat; /* Pointer to matrix */
if (IMODETST(p->mode, inst_mode_emis_ambient)) {
if ((ev = huey_take_amb_measurement(p, 0, &XYZ[1])) != inst_ok)
return ev;
XYZ[1] *= AMB_SCALE_FACTOR; /* Times aproximate fudge factor to Lux */
XYZ[0] = icmD50.X * XYZ[1]; /* Convert to D50 neutral */
XYZ[2] = icmD50.Z * XYZ[1];
} else {
if ((ev = huey_take_measurement_2(p, p->refrmode, rgb)) != inst_ok)
return ev;
if (p->icx)
mat = p->CRT_cal; /* CRT/factory matrix */
else
mat = p->LCD_cal; /* LCD/user matrix */
for (i = 0; i < 3; i++) {
XYZ[i] = 0.0;
for (j = 0; j < 3; j++) {
XYZ[i] += mat[i * 3 + j] * rgb[j];
}
XYZ[i] *= CALFACTOR; /* Times magic scale factor */
}
/* Apply the colorimeter correction matrix */
icmMulBy3x3(XYZ, p->ccmat, XYZ);
}
a1logd(p->log,3,"huey_take_XYZ_measurement: XYZ = %f %f %f\n",XYZ[0],XYZ[1],XYZ[2]);
return inst_ok;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Check the device is responding, and unlock if necessary */
static inst_code
huey_check_unlock(
huey *p /* Object */
) {
unsigned char buf[8];
int rsize;
inst_code ev;
int vv;
double ver;
a1logd(p->log,2,"huey_check_unlock: called\n");
/* Check the instrument status */
memset(buf, 0, 7);
if ((ev = huey_command(p, i1d_status, buf, buf, 1.0,1.0)) != inst_ok)
return ev;
/* Hmm. Some Lenovo Huey's (the HueyColors ?) aren't locked, */
/* so the unlock command fails. */
if (p->lenovo || strncmp((char *)buf, "Locked", 6) == 0) {
memset(buf, 0, 7);
/* Set unlock codes */
if (p->lenovo)
strcpy((char *)buf,"huyL");
else
strcpy((char *)buf,"GrMb");
if ((ev = huey_command(p, i1d_unlock, buf, buf, 1.0,1.0)) != inst_ok) {
a1logd(p->log,2,"huey_check_unlock: warning, unlock command returned error\n");
/* Perhaps the error code is for wrong unlock code, so ignore this */
/* and try a status command */
}
memset(buf, 0, 7);
if ((ev = huey_command(p, i1d_status, buf, buf, 1.0,1.0)) != inst_ok)
return ev;
}
/*
Apparently the "ECCM3" is a "hueyCOLOR", which is actually
a monochrome sensor! It uses different instructions to the Huey,
and wouldn't be terribly useful to ArgyllCMS.
See <https://github.com/hughsie/colord/tree/master/src/sensors/huey2>
*/
if (strncmp((char *)buf, "huL002", 6) != 0 /* Lenovo Huey ? */
&& strncmp((char *)buf, "ECCM2 ", 6) != 0 /* Lenovo Thinkpad W530 HueyPro ? */
// && strncmp((char *)buf, "ECCM3 ", 6) != 0 /* Lenovo Thinkpad P70 HueyColor ? */
&& strncmp((char *)buf, "Cir001", 6) != 0) { /* Huey */
a1logd(p->log,1,"huey_check_unlock: unknown model '%s'\n",buf);
return huey_interp_code((inst *)p, HUEY_UNKNOWN_MODEL);
}
a1logd(p->log,2,"huey_check_unlock: instrument is responding, unlocked, and right type\n");
return inst_ok;
}
/* Read all the relevant register values */
static inst_code
huey_read_all_regs(
huey *p /* Object */
) {
inst_code ev;
int i;
a1logd(p->log,2,"huey_read_all_regs: about to read all the registers\n");
/* Serial number */
if ((ev = huey_rdreg_word(p, &p->ser_no, 0) ) != inst_ok)
return ev;
a1logd(p->log,4,"serial number = %d\n",p->ser_no);
/* LCD/user calibration values */
for (i = 0; i < 9; i++) {
if ((ev = huey_rdreg_float(p, &p->LCD_cal[i], 4 + 4 * i) ) != inst_ok)
return ev;
a1logd(p->log,4,"LCD/user cal[%d] = %f\n",i,p->LCD_cal[i]);
}
/* LCD/user calibration time */
if ((ev = huey_rdreg_word(p, &p->LCD_caltime, 50) ) != inst_ok)
return ev;
a1logd(p->log,2,"LCD/user calibration time = 0x%x = %s\n",p->LCD_caltime, ctime_32(&p->LCD_caltime));
/* CRT/factory calibration values */
for (i = 0; i < 9; i++) {
if ((ev = huey_rdreg_float(p, &p->CRT_cal[i], 54 + 4 * i) ) != inst_ok)
return ev;
a1logd(p->log,3,"CRT/factory cal[%d] = %f\n",i,p->CRT_cal[i]);
}
/* CRT/factory calibration flag */
if ((ev = huey_rdreg_word(p, &p->CRT_caltime, 90) ) != inst_ok)
return ev;
a1logd(p->log,3,"CRT/factory flag = 0x%x = %s\n",p->CRT_caltime, ctime_32(&p->CRT_caltime));
/* Hard coded in Huey */
p->clk_prd = 1e-6;
a1logd(p->log,3,"Clock period = %f\n",p->clk_prd);
/* Dark current calibration values */
for (i = 0; i < 3; i++) {
if ((ev = huey_rdreg_float(p, &p->dark_cal[i], 103 + 4 * i)) != inst_ok) {
if ((ev & inst_imask) != HUEY_FLOAT_NOT_SET)
return ev;
p->dark_cal[i] = 0.0;
}
a1logd(p->log,3,"darkcal[%d] = %f\n",i,p->dark_cal[i]);
}
/* Ambient darkcurrent calibration value ? */
if ((ev = huey_rdreg_float(p, &p->amb_cal, 148)) != inst_ok) {
if ((ev & inst_imask) != HUEY_FLOAT_NOT_SET)
return ev;
p->amb_cal = 0.0;
}
a1logd(p->log,3,"Ambient cal = %f\n",p->amb_cal);
/* Unlock string */
for (i = 0; i < 4; i++) {
int vv;
if ((ev = huey_rdreg_byte(p, &vv, 122 + i) ) != inst_ok)
return ev;
p->unlk_string[i] = (char)vv;
}
p->unlk_string[i] = '\000';
a1logd(p->log,3,"unlock string = '%s'\n",p->unlk_string);
/* Read the integration time */
if ((ev = huey_rd_int_time(p, &p->int_clocks) ) != inst_ok)
return ev;
a1logd(p->log,3,"Integration time = %d\n",p->int_clocks);
a1logd(p->log,2,"huey_read_all_regs: all registers read OK\n");
return inst_ok;
}
/* Compute factors that depend on the register values */
static inst_code
huey_compute_factors(
huey *p /* Object */
) {
int i;
/* Check that certain value are valid */
if (p->ser_no == 0xffffffff) /* (It appears that some instruments have no serial number!) */
a1logw(p->log,"huey: bad instrument serial number\n");
if (p->LCD_caltime == 0xffffffff)
return huey_interp_code((inst *)p, HUEY_BAD_LCD_CALIBRATION);
if (p->CRT_caltime == 0xffffffff)
return huey_interp_code((inst *)p, HUEY_BAD_CRT_CALIBRATION);
/* clk_prd inversion */
p->clk_freq = 1.0/p->clk_prd;
a1logd(p->log,3,"clk_freq = %f\n",p->clk_freq);
/* Set some defaults */
p->sampno = 100; /* Minimum sampling count */
return inst_ok;
}
/* ------------------------------------------------------------------------ */
static inst_code set_default_disp_type(huey *p);
/* Establish communications with a HUEY */
/* If it's a serial port, use the baud rate given, and timeout in to secs */
/* Return HUEY_COMS_FAIL on failure to establish communications */
static inst_code
huey_init_coms(inst *pp, baud_rate br, flow_control fc, double tout) {
huey *p = (huey *) pp;
unsigned char buf[8];
int rsize;
long etime;
int bi, i, se, rv;
inst_code ev = inst_ok;
char **pnames = NULL;
int retries = 0;
a1logd(p->log, 2, "huey_init_coms: About to init coms\n");
/* Open as an HID if available */
if (p->icom->port_type(p->icom) == icomt_hid) {
a1logd(p->log, 3, "huey_init_coms: About to init HID\n");
/* Set config, interface */
if ((se = p->icom->set_hid_port(p->icom, icomuf_none, retries, pnames)) != ICOM_OK) {
a1logd(p->log, 1, "huey_init_coms: set_hid_port failed ICOM err 0x%x\n",se);
return huey_interp_code((inst *)p, icoms2huey_err(se, 0));
}
} else if (p->icom->port_type(p->icom) == icomt_usb) {
a1logd(p->log, 3, "huey_init_coms: About to init USB\n");
/* Set config, interface, write end point, read end point */
/* ("serial" end points aren't used - the huey uses USB control messages) */
/* We need to detatch the HID driver on Linux */
if ((se = p->icom->set_usb_port(p->icom, 1, 0x00, 0x00, icomuf_detach, 0, NULL))
!= ICOM_OK) {
a1logd(p->log, 1, "huey_init_coms: set_usb_port failed ICOM err 0x%x\n",se);
return huey_interp_code((inst *)p, icoms2huey_err(se, 0));
}
} else {
a1logd(p->log, 1, "huey_init_coms: wrong communications type for device!\n");
return inst_coms_fail;
}
if ((p->icom->vid == 0x0765 && p->icom->pid == 0x5001)
|| (p->icom->vid == 0x0765 && p->icom->pid == 0x5010)) {
a1logd(p->log, 2, "huey_init_coms: Lenovo version\n");
p->lenovo = 1;
}
/* Check instrument is responding */
memset(buf, 0, 7);
if ((ev = huey_command(p, i1d_status, buf, buf, 1.0, 1.0)) != inst_ok) {
a1logd(p->log, 1, "huey_init_coms: instrument didn't respond 0x%x\n",ev);
return ev;
}
/* Setup the default display type */
if ((ev = set_default_disp_type(p)) != inst_ok) {
return ev;
}
a1logd(p->log, 2, "huey_init_coms: inited coms OK\n");
p->gotcoms = 1;
return inst_ok;
}
/* Initialise the HUEY */
/* return non-zero on an error, with dtp error code */
static inst_code
huey_init_inst(inst *pp) {
huey *p = (huey *)pp;
inst_code ev = inst_ok;
a1logd(p->log, 2, "huey_init_inst: called\n");
if (p->gotcoms == 0)
return huey_interp_code((inst *)p, HUEY_NO_COMS); /* Must establish coms first */
/* Check instrument is responding, and right type */
if ((ev = huey_check_unlock(p)) != inst_ok)
return ev;
/* Turn the LEDs off */
if ((ev = huey_set_LEDs(p, 0x0)) != inst_ok)
return ev;
/* Read all the registers and store their contents */
if ((ev = huey_read_all_regs(p)) != inst_ok)
return ev;
if ((ev = huey_compute_factors(p)) != inst_ok)
return ev;
p->trig = inst_opt_trig_user;
p->inited = 1;
a1logd(p->log, 2, "huey_init_inst: inited OK\n");
/* Flash the LEDs, just cos we can! */
if ((ev = huey_set_LEDs(p, 0x1)) != inst_ok)
return ev;
msec_sleep(50);
if ((ev = huey_set_LEDs(p, 0x2)) != inst_ok)
return ev;
msec_sleep(50);
if ((ev = huey_set_LEDs(p, 0x4)) != inst_ok)
return ev;
msec_sleep(50);
if ((ev = huey_set_LEDs(p, 0x8)) != inst_ok)
return ev;
msec_sleep(50);
if ((ev = huey_set_LEDs(p, 0x4)) != inst_ok)
return ev;
msec_sleep(50);
if ((ev = huey_set_LEDs(p, 0x2)) != inst_ok)
return ev;
msec_sleep(50);
if ((ev = huey_set_LEDs(p, 0x1)) != inst_ok)
return ev;
msec_sleep(50);
if ((ev = huey_set_LEDs(p, 0x0)) != inst_ok)
return ev;
return inst_ok;
}
/* Read a single sample */
/* Return the dtp error code */
static inst_code
huey_read_sample(
inst *pp,
char *name, /* Strip name (7 chars) */
ipatch *val, /* Pointer to instrument patch value */
instClamping clamp) { /* NZ if clamp XYZ/Lab to be +ve */
huey *p = (huey *)pp;
int user_trig = 0;
int rv = inst_protocol_error;
a1logd(p->log, 1, "huey: huey_read_sample called\n");
if (!p->gotcoms)
return inst_no_coms;
if (!p->inited)
return inst_no_init;
if (p->trig == inst_opt_trig_user) {
if (p->uicallback == NULL) {
a1logd(p->log, 1, "huey: inst_opt_trig_user but no uicallback function set!\n");
return inst_unsupported;
}
for (;;) {
if ((rv = p->uicallback(p->uic_cntx, inst_armed)) != inst_ok) {
if (rv == inst_user_abort)
return rv; /* Abort */
if (rv == inst_user_trig) {
user_trig = 1;
break; /* Trigger */
}
}
msec_sleep(200);
}
/* Notify of trigger */
if (p->uicallback)
p->uicallback(p->uic_cntx, inst_triggered);
/* Progromatic Trigger */
} else {
/* Check for abort */
if (p->uicallback != NULL
&& (rv = p->uicallback(p->uic_cntx, inst_armed)) == inst_user_abort)
return rv; /* Abort */
}
/* Read the XYZ value */
if ((rv = huey_take_XYZ_measurement(p, val->XYZ)) != inst_ok) {
return rv;
}
/* This may not change anything since instrument may clamp */
if (clamp)
icmClamp3(val->XYZ, val->XYZ);
val->loc[0] = '\000';
if (IMODETST(p->mode, inst_mode_emis_ambient))
val->mtype = inst_mrt_ambient;
else
val->mtype = inst_mrt_none;
val->XYZ_v = 1; /* These are absolute XYZ readings ? */
val->sp.spec_n = 0;
val->duration = 0.0;
if (user_trig)
return inst_user_trig;
return rv;
}
static inst_code set_base_disp_type(huey *p, int cbid);
/* Insert a colorimetric correction matrix in the instrument XYZ readings */
/* This is only valid for colorimetric instruments. */
/* To remove the matrix, pass NULL for the filter filename */
inst_code huey_col_cor_mat(
inst *pp,
disptech dtech, /* Use disptech_unknown if not known */ \
int cbid, /* Calibration display type base ID, 1 if unknown */\
double mtx[3][3]
) {
huey *p = (huey *)pp;
inst_code ev;
if (!p->gotcoms)
return inst_no_coms;
if (!p->inited)
return inst_no_init;
if ((ev = set_base_disp_type(p, cbid)) != inst_ok)
return ev;
if (mtx == NULL)
icmSetUnity3x3(p->ccmat);
else
icmCpy3x3(p->ccmat, mtx);
p->dtech = dtech;
p->refrmode = disptech_get_id(dtech)->refr;
p->cbid = 0; /* Can't be base type now */
if (p->log->debug >= 4) {
a1logd(p->log,4,"ccmat = %f %f %f\n",
p->ccmat[0][0], p->ccmat[0][1], p->ccmat[0][2]);
a1logd(p->log,4," %f %f %f\n",
p->ccmat[1][0], p->ccmat[1][1], p->ccmat[1][2]);
a1logd(p->log,4," %f %f %f\n\n",
p->ccmat[2][0], p->ccmat[2][1], p->ccmat[2][2]);
a1logd(p->log,4,"ucbid = %d, cbid = %d\n",p->ucbid, p->cbid);
a1logd(p->log,4,"\n");
}
return inst_ok;
}
/* Error codes interpretation */
static char *
huey_interp_error(inst *pp, int ec) {
// huey *p = (huey *)pp;
ec &= inst_imask;
switch (ec) {
case HUEY_INTERNAL_ERROR:
return "Internal software error";
case HUEY_COMS_FAIL:
return "Communications failure";
case HUEY_UNKNOWN_MODEL:
return "Not a known Huey Model";
case HUEY_DATA_PARSE_ERROR:
return "Data from i1 Display didn't parse as expected";
case HUEY_OK:
return "No device error";
case HUEY_FLOAT_NOT_SET:
return "Float value is not set in EEPROM";
case HUEY_NOT_READY:
return "Command didn't return command code - not ready ?";
case HUEY_BAD_SERIAL_NUMBER:
return "Serial number isn't set";
case HUEY_BAD_LCD_CALIBRATION:
return "LCD calibration values aren't set";
case HUEY_BAD_CRT_CALIBRATION:
return "CRT calibration values aren't set";
case HUEY_EEPROM_WRITE_FAIL:
return "Write to EEPROM failed to verify";
case HUEY_BAD_WR_LENGTH:
return "Unable to write full message to instrument";
case HUEY_BAD_RD_LENGTH:
return "Unable to read full message to instrument";
case HUEY_BAD_RET_CMD:
return "Message from instrument didn't echo command code";
case HUEY_BAD_RET_STAT:
return "Message from instrument had bad status code";
case HUEY_UNEXPECTED_RET_VAL:
return "Message from instrument has unexpected value";
case HUEY_BAD_STATUS:
return "Instrument status is unrecognised format";
case HUEY_UNKNOWN_VERS_ID:
return "Instrument version number or ID byte not recognised";
case HUEY_BAD_COMMAND:
return "Instrument didn't recognise the command";
/* Internal errors */
case HUEY_BAD_REG_ADDRESS:
return "Out of range register address";
case HUEY_BAD_INT_THRESH:
return "Out of range integration threshold";
case HUEY_NO_COMS:
return "Communications hasn't been established";
case HUEY_NOT_INITED:
return "Insrument hasn't been initialised";
case HUEY_CANT_BLACK_CALIB:
return "Device doesn't support black calibration";
case HUEY_CANT_MEASP_CALIB:
return "Device doesn't support measurment period calibration";
case HUEY_WRONG_DEVICE:
return "Wrong type of device for called function";
default:
return "Unknown error code";
}
}
/* Convert a machine specific error code into an abstract dtp code */
static inst_code
huey_interp_code(inst *pp, int ec) {
// huey *p = (huey *)pp;
ec &= inst_imask;
switch (ec) {
case HUEY_OK:
case HUEY_FLOAT_NOT_SET: /* Internal indication */
case HUEY_NOT_READY: /* Internal indication */
return inst_ok;
case HUEY_INTERNAL_ERROR:
case HUEY_BAD_REG_ADDRESS:
case HUEY_BAD_INT_THRESH:
case HUEY_NO_COMS:
case HUEY_NOT_INITED:
case HUEY_CANT_BLACK_CALIB:
case HUEY_CANT_MEASP_CALIB:
case HUEY_WRONG_DEVICE:
return inst_internal_error | ec;
case HUEY_COMS_FAIL:
return inst_coms_fail | ec;
case HUEY_UNKNOWN_MODEL:
case HUEY_BAD_STATUS:
case HUEY_UNKNOWN_VERS_ID:
return inst_unknown_model | ec;
case HUEY_DATA_PARSE_ERROR:
case HUEY_BAD_WR_LENGTH:
case HUEY_BAD_RD_LENGTH:
case HUEY_BAD_RET_CMD:
case HUEY_BAD_RET_STAT:
case HUEY_UNEXPECTED_RET_VAL:
case HUEY_BAD_COMMAND:
return inst_protocol_error | ec;
case HUEY_BAD_SERIAL_NUMBER:
case HUEY_BAD_LCD_CALIBRATION:
case HUEY_BAD_CRT_CALIBRATION:
case HUEY_EEPROM_WRITE_FAIL:
return inst_hardware_fail | ec;
/* return inst_misread | ec; */
/* return inst_needs_cal_2 | ec; */
}
return inst_other_error | ec;
}
/* Destroy ourselves */
static void
huey_del(inst *pp) {
huey *p = (huey *)pp;
if (p->icom != NULL)
p->icom->del(p->icom);
inst_del_disptype_list(p->dtlist, p->ndtlist);
p->vdel(pp);
free(p);
}
/* Return the instrument mode capabilities */
static void huey_capabilities(inst *pp,
inst_mode *pcap1,
inst2_capability *pcap2,
inst3_capability *pcap3) {
huey *p = (huey *)pp;
inst_mode cap1 = 0;
inst2_capability cap2 = 0;
cap1 |= inst_mode_emis_spot
| inst_mode_emis_ambient
| inst_mode_colorimeter
;
cap2 |= inst2_prog_trig
| inst2_user_trig
| inst2_has_leds
| inst2_disptype
| inst2_ambient_mono
| inst2_ccmx
;
if (pcap1 != NULL)
*pcap1 = cap1;
if (pcap2 != NULL)
*pcap2 = cap2;
if (pcap3 != NULL)
*pcap3 = inst3_none;
}
/* Check device measurement mode */
static inst_code huey_check_mode(inst *pp, inst_mode m) {
huey *p = (huey *)pp;
inst_mode cap;
if (!p->gotcoms)
return inst_no_coms;
if (!p->inited)
return inst_no_init;
pp->capabilities(pp, &cap, NULL, NULL);
/* Simple test */
if (m & ~cap)
return inst_unsupported;
/* only display emission mode and ambient supported */
if (!IMODETST(m, inst_mode_emis_spot)
&& !IMODETST(m, inst_mode_emis_ambient)) {
return inst_unsupported;
}
return inst_ok;
}
/* Set device measurement mode */
static inst_code huey_set_mode(inst *pp, inst_mode m) {
huey *p = (huey *)pp;
inst_code ev;
if ((ev = huey_check_mode(pp, m)) != inst_ok)
return ev;
p->mode = m;
return inst_ok;
}
static inst_disptypesel huey_disptypesel[3] = {
{
inst_dtflags_default,
1,
"l",
"LCD display",
0,
disptech_lcd,
0
},
{
inst_dtflags_none, /* flags */
2, /* cbid */
"c", /* sel */
"CRT display", /* desc */
1, /* refr */
disptech_crt, /* disptype */
1 /* ix */
},
{
inst_dtflags_end,
0,
"",
"",
0,
disptech_none,
0
}
};
/* Get mode and option details */
static inst_code huey_get_disptypesel(
inst *pp,
int *pnsels, /* Return number of display types */
inst_disptypesel **psels, /* Return the array of display types */
int allconfig, /* nz to return list for all configs, not just current. */
int recreate /* nz to re-check for new ccmx & ccss files */
) {
huey *p = (huey *)pp;
inst_code rv = inst_ok;
/* Create/Re-create a current list of abailable display types */
if (p->dtlist == NULL || recreate) {
if ((rv = inst_creat_disptype_list(pp, &p->ndtlist, &p->dtlist,
huey_disptypesel, 0 /* doccss*/, 1 /* doccmx */)) != inst_ok)
return rv;
}
if (pnsels != NULL)
*pnsels = p->ndtlist;
if (psels != NULL)
*psels = p->dtlist;
return inst_ok;
}
/* Given a display type entry, setup for that type */
static inst_code set_disp_type(huey *p, inst_disptypesel *dentry) {
if (dentry->flags & inst_dtflags_ccmx) {
inst_code ev;
if ((ev = set_base_disp_type(p, dentry->cc_cbid)) != inst_ok)
return ev;
icmCpy3x3(p->ccmat, dentry->mat);
p->dtech = dentry->dtech;
p->cbid = 0; /* Can't be a base type */
} else {
p->icx = dentry->ix;
p->dtech = dentry->dtech;
p->cbid = dentry->cbid;
p->ucbid = dentry->cbid; /* This is underying base if dentry is base selection */
icmSetUnity3x3(p->ccmat);
}
p->refrmode = dentry->refr;
if (p->log->debug >= 4) {
a1logd(p->log,4,"ccmat = %f %f %f\n",
p->ccmat[0][0], p->ccmat[0][1], p->ccmat[0][2]);
a1logd(p->log,4," %f %f %f\n",
p->ccmat[1][0], p->ccmat[1][1], p->ccmat[1][2]);
a1logd(p->log,4," %f %f %f\n\n",
p->ccmat[2][0], p->ccmat[2][1], p->ccmat[2][2]);
a1logd(p->log,4,"ucbid = %d, cbid = %d\n",p->ucbid, p->cbid);
a1logd(p->log,4,"\n");
}
return inst_ok;
}
/* Set the display type */
static inst_code huey_set_disptype(inst *pp, int ix) {
huey *p = (huey *)pp;
inst_code ev;
inst_disptypesel *dentry;
if (p->dtlist == NULL) {
if ((ev = inst_creat_disptype_list(pp, &p->ndtlist, &p->dtlist,
huey_disptypesel, 0 /* doccss*/, 1 /* doccmx */)) != inst_ok)
return ev;
}
if (ix < 0 || ix >= p->ndtlist)
return inst_unsupported;
dentry = &p->dtlist[ix];
if ((ev = set_disp_type(p, dentry)) != inst_ok) {
return ev;
}
return inst_ok;
}
/* Setup the default display type */
static inst_code set_default_disp_type(huey *p) {
inst_code ev;
int i;
if (p->dtlist == NULL) {
if ((ev = inst_creat_disptype_list((inst *)p, &p->ndtlist, &p->dtlist,
huey_disptypesel, 0 /* doccss*/, 1 /* doccmx */)) != inst_ok)
return ev;
}
for (i = 0; !(p->dtlist[i].flags & inst_dtflags_end); i++) {
if (p->dtlist[i].flags & inst_dtflags_default)
break;
}
if (p->dtlist[i].flags & inst_dtflags_end) {
a1loge(p->log, 1, "set_default_disp_type: failed to find type!\n");
return inst_internal_error;
}
if ((ev = set_disp_type(p, &p->dtlist[i])) != inst_ok) {
return ev;
}
return inst_ok;
}
/* Setup the display type to the given base type */
static inst_code set_base_disp_type(huey *p, int cbid) {
inst_code ev;
int i;
if (cbid == 0) {
a1loge(p->log, 1, "huey set_base_disp_type: can't set base display type of 0\n");
return inst_wrong_setup;
}
if (p->dtlist == NULL) {
if ((ev = inst_creat_disptype_list((inst *)p, &p->ndtlist, &p->dtlist,
huey_disptypesel, 0 /* doccss*/, 1 /* doccmx */)) != inst_ok)
return ev;
}
for (i = 0; !(p->dtlist[i].flags & inst_dtflags_end); i++) {
if (!(p->dtlist[i].flags & inst_dtflags_ccmx) /* Prevent infinite recursion */
&& p->dtlist[i].cbid == cbid)
break;
}
if (p->dtlist[i].flags & inst_dtflags_end) {
a1loge(p->log, 1, "set_base_disp_type: failed to find cbid %d!\n",cbid);
return inst_wrong_setup;
}
if ((ev = set_disp_type(p, &p->dtlist[i])) != inst_ok) {
return ev;
}
return inst_ok;
}
/* Get the disptech and other corresponding info for the current */
/* selected display type. Returns disptype_unknown by default. */
/* Because refrmode can be overridden, it may not match the refrmode */
/* of the dtech. (Pointers may be NULL if not needed) */
static inst_code huey_get_disptechi(
inst *pp,
disptech *dtech,
int *refrmode,
int *cbid) {
huey *p = (huey *)pp;
if (dtech != NULL)
*dtech = p->dtech;
if (refrmode != NULL)
*refrmode = p->refrmode;
if (cbid != NULL)
*cbid = p->cbid;
return inst_ok;
}
/*
* set or reset an optional mode
*
* Some options talk to the instrument, and these will
* error if it hasn't been initialised.
*/
static inst_code
huey_get_set_opt(inst *pp, inst_opt_type m, ...) {
huey *p = (huey *)pp;
inst_code ev = inst_ok;
/* Record the trigger mode */
if (m == inst_opt_trig_prog
|| m == inst_opt_trig_user) {
p->trig = m;
return inst_ok;
}
if (!p->gotcoms)
return inst_no_coms;
if (!p->inited)
return inst_no_init;
/* Operate the LEDS */
if (m == inst_opt_get_gen_ledmask) {
va_list args;
int *mask = NULL;
va_start(args, m);
mask = va_arg(args, int *);
va_end(args);
*mask = 0xf; /* Four general LEDs */
return inst_ok;
} else if (m == inst_opt_get_led_state) {
va_list args;
int *mask = NULL;
va_start(args, m);
mask = va_arg(args, int *);
va_end(args);
*mask = p->led_state;
return inst_ok;
} else if (m == inst_opt_set_led_state) {
va_list args;
int mask = 0;
va_start(args, m);
mask = va_arg(args, int);
va_end(args);
return huey_set_LEDs(p, mask);
}
/* Use default implementation of other inst_opt_type's */
{
inst_code rv;
va_list args;
va_start(args, m);
rv = inst_get_set_opt_def(pp, m, args);
va_end(args);
return rv;
}
}
/* Constructor */
extern huey *new_huey(icoms *icom, instType dtype) {
huey *p;
if ((p = (huey *)calloc(sizeof(huey),1)) == NULL) {
a1loge(icom->log, 1, "new_huey: malloc failed!\n");
return NULL;
}
p->log = new_a1log_d(icom->log);
p->init_coms = huey_init_coms;
p->init_inst = huey_init_inst;
p->capabilities = huey_capabilities;
p->check_mode = huey_check_mode;
p->set_mode = huey_set_mode;
p->get_disptypesel = huey_get_disptypesel;
p->set_disptype = huey_set_disptype;
p->get_disptechi = huey_get_disptechi;
p->get_set_opt = huey_get_set_opt;
p->read_sample = huey_read_sample;
p->col_cor_mat = huey_col_cor_mat;
p->interp_error = huey_interp_error;
p->del = huey_del;
p->icom = icom;
p->dtype = dtype;
icmSetUnity3x3(p->ccmat); /* Set the colorimeter correction matrix to do nothing */
p->dtech = disptech_unknown;
return p;
}
|