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 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671
|
/*
* Argyll Color Correction System
*
* Image Engineering EX1 related functions
*
* Author: Graeme W. Gill
* Date: 4/4/2015
*
* Copyright 1996 - 2015, Graeme W. Gill
* All rights reserved.
*
* This material is licenced under the GNU GENERAL PUBLIC LICENSE Version 2 or later :-
* see the License2.txt file for licencing details.
*
* Based on specbos.c
*/
/*
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.
*/
/*
TTBD:
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <stdarg.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 "inst.h"
#include "rspec.h"
#include "ex1.h"
#define ENABLE_NONVCAL /* [Def] Enable saving calibration state between program runs in a file */
#undef DEBUG
#undef PLOT_DEBUG /* Use plot to show readings & processing */
#undef TEST_DARK_INTERP /* Test adaptive dark interpolation */
#define EX1_MAX_MEAS_TIME 1.0 /* Maximum measurement time to use */
/* ----------------------------------------------------------------- */
/* High level EX1 commands, implemented at the bottom of the file */
#define EX1_EP 0x1 /* End point to use. Can use 1 and/or 2 */
#define MIN_CYCLE_TIME 0.009 /* Minimum time per measurement cycle */
#define SENSE_AIM (0.85 * ((1 << 14)-1)) /* Sensor level aim point */
#define SENSE_SAT (0.95 * ((1 << 14)-1)) /* Saturation level */
#define DCALTOUT (1 * 60 * 60) /* [1 Hour ??] Dark Calibration timeout in seconds */
static int icoms2ex1_err(int se);
static inst_code ex1_interp_code(inst *pp, int ec);
static char *ex1_interp_native_error(ex1 *p, int ec);
static void ex1_flush(ex1 *p);
static int ex1_get_hw_rev(ex1 *p, int *hwrev);
static int ex1_get_fw_rev(ex1 *p, int *fwrev);
static int ex1_get_slit_width(ex1 *p, int *swidth);
static int ex1_get_fiber_width(ex1 *p, int *fibw);
static int ex1_get_grating(ex1 *p, char **grating);
static int ex1_get_filter(ex1 *p, char **filter);
static int ex1_get_coating(ex1 *p, char **coating);
static int ex1_get_alias(ex1 *p, char **alias);
static int ex1_get_serno(ex1 *p, char **serno);
static int ex1_get_wl_coefs(ex1 *p, unsigned int *nocoefs, double **coefs);
static int ex1_get_nl_coefs(ex1 *p, unsigned int *nocoefs, double **coefs);
static int ex1_get_ir_coefs(ex1 *p, unsigned int *nocoefs, double **coefs, double *area);
static int ex1_get_sl_coefs(ex1 *p, unsigned int *nocoefs, double **coefs);
#define EX1_TRIGMODE_IMED 0x0 /* Immediate software trigger mode. */
#define EX1_TRIGMODE_TRIG 0x1 /* Trigger on ext trigger signal after possible delay. */
#define EX1_TRIGMODE_STROBE 0x2 /* Trigger afer possible delay on internal cont. strobe. */
static int ex1_set_trig_mode(ex1 *p, int trigmode);
#define EX1_INTTIME_MIN 10.0e-6
#define EX1_INTTIME_MAX 10.0
static int ex1_set_inttime(ex1 *p, double *qinttime, double inttime);
#define EX1_DELTIME_MIN 5.0e-6
#define EX1_DELTIME_MAX 335.5e-3
static int ex1_set_trig_delay(ex1 *p, double *qtrigdel, double trigdel);
#define EX1_STRBPER_MIN 50.0e-6
#define EX1_STRBPER_MAX 5.0
static int ex1_set_strobe_period(ex1 *p, double *qstbper, double stbper);
#define EX1_STRB_DISABLE 0x0 /* Continuous strobe disabled */
#define EX1_STRB_ENABLE 0x1 /* Continuous strobe enabled */
static int ex1_set_strobe_enable(ex1 *p, int enable);
static int ex1_set_single_strobe_enable(ex1 *p, int enable);
#define EX1_AVERAGE_MIN 1
#define EX1_AVERAGE_MAX 5000
static int ex1_set_average(ex1 *p, int noavg);
#define EX1_BIN_OFF 0x0 /* Full res, 1024 bins */
#define EX1_BIN_2 0x1 /* 1/2 res, 512 bins */
#define EX1_BIN_4 0x2 /* 1/4 res, 256 bins */
#define EX1_BIN_8 0x3 /* 1/8 res, 128 bins */
static int ex1_set_binning(ex1 *p, int bf);
#define EX1_BOXCAR_MIN 0
#define EX1_BOXCAR_MAX 15
static int ex1_set_boxcar(ex1 *p, int nobox);
static int ex1_measure(ex1 *p, double *raw);
static int ex1_save_calibration(ex1 *p);
static int ex1_restore_calibration(ex1 *p);
static int ex1_touch_calibration(ex1 *p);
/* ----------------------------------------------------------------- */
/* Establish communications with a ex1 */
/* Return EX1_COMS_FAIL on failure to establish communications */
static inst_code
ex1_init_coms(inst *pp, baud_rate br, flow_control fc, double tout) {
ex1 *p = (ex1 *) pp;
instType dtype = pp->dtype;
int se;
inst_code ev = inst_ok;
a1logd(p->log, 2, "ex1_init_coms: called\n");
if (p->icom->port_type(p->icom) != icomt_usb) {
a1logd(p->log, 1, "ex1_init_coms: wrong communications type for device!\n");
return inst_coms_fail;
}
/* Set config, interface, write end point, read end point, read quanta */
if ((se = p->icom->set_usb_port(p->icom, 1, EX1_EP, EX1_EP | 0x80, icomuf_none, 0, NULL)) != ICOM_OK) {
a1logd(p->log, 1, "ex1_init_coms: set_usbe_port failed ICOM err 0x%x\n",se);
return ex1_interp_code((inst *)p, icoms2ex1_err(se));
}
/* Should we reset it ? */
/* Make sure no message is waiting */
ex1_flush(p);
/* Check it is responding */
if ((se = ex1_get_hw_rev(p, &p->hwrev)) != EX1_OK) {
return ex1_interp_code((inst *)p, se);
}
p->gotcoms = 1;
a1logd(p->log, 2, "ex1_init_coms: init coms has succeeded\n");
return inst_ok;
}
/* Initialise the EX1 */
/* return non-zero on an error, with dtp error code */
static inst_code
ex1_init_inst(inst *pp) {
ex1 *p = (ex1 *)pp;
inst_code ev = inst_ok;
rspec_inf *sconf = &p->sconf;
unsigned int count, i;
a1logd(p->log, 2, "ex1_init_inst: called\n");
if (p->gotcoms == 0)
return inst_internal_error; /* Must establish coms before calling init */
p->lo_secs = 2000000000; /* A very long time */
p->max_meastime = EX1_MAX_MEAS_TIME;
/* Get information about the instrument */
if ((ev = ex1_get_alias(p, &p->alias)) != EX1_OK)
return ex1_interp_code((inst *)p, ev);
if ((ev = ex1_get_hw_rev(p, &p->hwrev)) != EX1_OK)
return ex1_interp_code((inst *)p, ev);
if ((ev = ex1_get_fw_rev(p, &p->fwrev)) != EX1_OK)
return ex1_interp_code((inst *)p, ev);
if ((ev = ex1_get_serno(p, &p->serno)) != EX1_OK)
return ex1_interp_code((inst *)p, ev);
if ((ev = ex1_get_slit_width(p, &p->slitw)) != EX1_OK)
return ex1_interp_code((inst *)p, ev);
if ((ev = ex1_get_fiber_width(p, &p->fiberw)) != EX1_OK)
return ex1_interp_code((inst *)p, ev);
if ((ev = ex1_get_grating(p, &p->grating)) != EX1_OK)
return ex1_interp_code((inst *)p, ev);
if ((ev = ex1_get_filter(p, &p->filter)) != EX1_OK)
return ex1_interp_code((inst *)p, ev);
if ((ev = ex1_get_coating(p, &p->coating)) != EX1_OK)
return ex1_interp_code((inst *)p, ev);
/* Set the instrument to sane defaults: */
/* Set trigger mode to software immediate */
if ((ev = ex1_set_trig_mode(p, EX1_TRIGMODE_IMED)) != EX1_OK) {
return ex1_interp_code((inst *)p, ev);
}
/* Set integration time to minimum */
if ((ev = ex1_set_inttime(p, &p->inttime, EX1_INTTIME_MIN)) != EX1_OK) {
return ex1_interp_code((inst *)p, ev);
}
/* Set trigger delay time to minumum */
if ((ev = ex1_set_trig_delay(p, NULL, EX1_DELTIME_MIN)) != EX1_OK) {
return ex1_interp_code((inst *)p, ev);
}
/* Disable continuous strobe */
if ((ev = ex1_set_strobe_enable(p, EX1_STRB_DISABLE)) != EX1_OK) {
return ex1_interp_code((inst *)p, ev);
}
/* Disable single strobe */
if ((ev = ex1_set_single_strobe_enable(p, EX1_STRB_DISABLE)) != EX1_OK) {
return ex1_interp_code((inst *)p, ev);
}
/* Set averages to 1 */
if ((ev = ex1_set_average(p, EX1_AVERAGE_MIN)) != EX1_OK) {
return ex1_interp_code((inst *)p, ev);
}
/* Turn off binning */
if ((ev = ex1_set_binning(p, EX1_BIN_OFF)) != EX1_OK) {
return ex1_interp_code((inst *)p, ev);
}
/* Turn off boxcar filtering */
if ((ev = ex1_set_boxcar(p, EX1_BOXCAR_MIN)) != EX1_OK) {
return ex1_interp_code((inst *)p, ev);
}
/* Setup calibration information and measurement config. */
clear_rspec_inf(sconf);
sconf->log = p->log;
sconf->nsen = 1024;
sconf->nshgrps = 0;
sconf->nilltkgrps = 0;
sconf->lightrange.off = 0; /* All of sensor value go to raw */
sconf->lightrange.num = sconf->nsen;
sconf->nraw = sconf->lightrange.num;
if ((ev = ex1_get_wl_coefs(p, &sconf->nwlcal, &sconf->wlcal)) != EX1_OK) {
return ex1_interp_code((inst *)p, ev);
}
if ((ev = ex1_get_nl_coefs(p, &sconf->nlin, &sconf->lin)) != EX1_OK) {
return ex1_interp_code((inst *)p, ev);
}
sconf->lindiv = 1; /* Divide type polynomial correction */
/* If we every figure out what the format is, convert it */
/* into 2D matrix form and store into sconf. */
if ((ev = ex1_get_sl_coefs(p, &p->nstraylight, &p->straylight)) != EX1_OK) {
return ex1_interp_code((inst *)p, ev);
}
/* Get raw calibration values */
if ((ev = ex1_get_ir_coefs(p, &count, &sconf->ecal, &p->emis_area)) != EX1_OK) {
return ex1_interp_code((inst *)p, ev);
}
if (count != sconf->nraw) {
a1logd(p->log, 1, " Calibration array is unexpected length (is %d, should be %d)\n",
count, sconf->nraw);
return inst_wrong_setup;
}
#ifdef PLOT_DEBUG
printf("emission cal:\n");
plot_ecal(sconf);
#endif
/* Hmm. not 100% sure this is correct, but it gives the right */
/* sort of numbers.. */
for (i = 0; i < sconf->nraw; i++)
sconf->ecal[i] /= p->emis_area;
sconf->ecaltype = rspec_raw;
#ifdef PLOT_DEBUG
printf("adjusted emission cal:\n");
plot_ecal(sconf);
#endif
/* Figure out the calibrated range */
rspec_comp_raw_range_from_ecal(sconf);
//printf("~1 raw range = %d - %d\n",sconf->rawrange.off, sconf->rawrange.off + sconf->rawrange.num-1);
//printf("~1 = %f - %f nm\n",rspec_raw2nm(sconf, sconf->rawrange.off), rspec_raw2nm(sconf, sconf->rawrange.off + sconf->rawrange.num-1));
sconf->ktype = rspec_gausian; /* Default */
// sconf->ktype = rspec_lanczos2;
// sconf->ktype = rspec_lanczos3;
// sconf->ktype = rspec_triangle;
// sconf->ktype = rspec_cubicspline;
sconf->wl_space = 2.0;
sconf->wl_short = rspec_raw2nm(sconf, sconf->rawrange.off);
sconf->wl_short = sconf->wl_space * ceil(sconf->wl_short/sconf->wl_space);
if (sconf->wl_short < 350.0)
sconf->wl_short = 350.0;
sconf->wl_long = rspec_raw2nm(sconf, sconf->rawrange.off + sconf->rawrange.num-1);
sconf->wl_long = sconf->wl_space * floor(sconf->wl_long/sconf->wl_space);
if (sconf->wl_long > 800.0)
sconf->wl_long = 800.0;
sconf->nwav = (int)floor(sconf->wl_long - sconf->wl_short)/sconf->wl_space + 1;
a1logd(p->log, 1, " %d Wavelengths %f - %f spacing %f\n",sconf->nwav,
sconf->wl_short,sconf->wl_long,sconf->wl_space);
/* The EX1 is Vis range 350-800 nm */
/* so the raw spacing is about 0.45 nm */
/* The EX1 has a 100um slit, so FWHM will be 6nm */
/* so aim for calibrated wavlength spacing of 2nm */
rspec_make_resample_filters(sconf);
#ifdef PLOT_DEBUG
plot_resample_filters(sconf);
#endif
p->idark_int_time[0] = EX1_INTTIME_MIN;
p->idark_int_time[1] = 2.0;
#ifdef ENABLE_NONVCAL
/* Restore idarl calibration from the local system */
ex1_restore_calibration(p);
/* Touch it so that we know when the instrument was last opened */
ex1_touch_calibration(p);
#endif
#ifdef NEVER /* Test linearity iversion */
{
double v1, v2, v3;
for (v1 = 0.0; v1 < SENSE_SAT; v1 += 1638.0) {
v2 = linearize_val_rspec(sconf, v1);
v3 = inv_linearize_val_rspec(sconf, v2);
printf("Sens in %f -> lin %f -> non-lin %f (%f)\n",v1,v2,v3,fabs(v1 - v3));
}
}
#endif /* NEVER */
p->conv = new_xsp2cie(icxIT_none, 0.0, NULL, icxOT_CIE_1931_2, NULL, icSigXYZData, icxNoClamp);
if (p->conv == NULL)
return EX1_INT_CIECONVFAIL;
if (p->log->verb) {
a1logv(p->log, 1, " Model: %s\n",p->alias != NULL ? p->alias : "Unknown");
a1logv(p->log, 1, " HW rev: %d\n",p->hwrev);
a1logv(p->log, 1, " FW rev: %d\n",p->fwrev);
a1logv(p->log, 1, " Serial number: %s\n",p->serno);
if (p->slitw != 0)
a1logv(p->log, 1, " Slit width: %d microns\n",p->slitw);
else
a1logv(p->log, 1, " Slit width: Unknown\n");
if (p->fiberw != 0)
a1logv(p->log, 1, " Fiber width: %d microns\n",p->fiberw);
else
a1logv(p->log, 1, " Fiber width: Unknown\n");
a1logv(p->log, 1, " Grating: %s\n",p->grating != NULL ? p->grating : "Unknown");
a1logv(p->log, 1, " Filter: %s\n",p->filter != NULL ? p->filter : "Unknown");
a1logv(p->log, 1, " Coating: %s\n",p->coating != NULL ? p->coating : "Unknown");
}
p->inited = 1;
a1logd(p->log, 2, "ex1_init_inst: instrument inited OK\n");
return inst_ok;
}
/* Do a raw measurement. */
/* Will delete existing *praw */
/* return EX1 error */
static int ex1_do_meas(ex1 *p, rspec **praw, double *inttime, double duration) {
rspec *sens, *raw;
int notoav;
int ec;
notoav = (int)ceil(duration/(MIN_CYCLE_TIME + *inttime));
if (notoav <= 0)
notoav = 1;
if (notoav > EX1_AVERAGE_MAX)
notoav = EX1_AVERAGE_MAX;
/* Set integration time */
if ((ec = ex1_set_inttime(p, &p->inttime, *inttime)) != EX1_OK) {
return ec;
}
*inttime = p->inttime; /* Quantized integration time */
/* Set no. of averages */
if ((ec = ex1_set_average(p, notoav)) != EX1_OK) {
return ec;
}
sens = new_rspec(&p->sconf, rspec_sensor, 1);
if ((ec = ex1_measure(p, sens->samp[0])) != EX1_OK) {
del_rspec(sens);
return ec;
}
sens->mtype = inst_mrt_emission;
sens->inttime = p->inttime;
/* + Any other processing from sens to raw, */
/* i.e. shielded cell tracking, illuminant temp value etc. ? */
raw = extract_raw_from_sensor_rspec(sens);
del_rspec(sens);
if (*praw != NULL)
del_rspec(*praw);
*praw = raw;
return EX1_OK;
}
/* Read a single sample */
/* Return the EX1 error code */
static inst_code
ex1_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 */
) {
ex1 *p = (ex1 *)pp;
int user_trig = 0;
int pos = -1;
inst_code rv = inst_protocol_error;
rspec_inf *sconf = &p->sconf;
rspec *raw = NULL, *wav = NULL;
int ec, i;
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, "ex1: 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 */
}
}
{
double inttime, tinttime;
int six;
double max = 1e6;
double blk;
a1logd(p->log,5,"Starting auto integration time:\n");
/* Find an integration time small enough to avoid overload */
for (inttime = 0.01; max > SENSE_SAT && inttime >= EX1_INTTIME_MIN; inttime *= 0.1) {
a1logd(p->log,5,"Trying inttime %f\n",inttime);
if ((ec = ex1_do_meas(p, &raw, &inttime, 0.05)) != EX1_OK) {
return ex1_interp_code((inst *)p, ec);
}
max = largest_val_rspec(NULL, &six, raw);
a1logd(p->log,5,"Max %.0f at six %d, Sat level %.0f\n",max,six,SENSE_SAT);
if (max <= SENSE_SAT)
break;
}
if (max >= SENSE_SAT) {
a1logd(p->log,1,"Saturated\n");
return ex1_interp_code((inst *)p, EX1_RD_SENSORSATURATED);
}
blk = ex1_interp_idark_val(sconf, 0, six, inttime);
a1logd(p->log,5,"Black at max = %f\n",blk);
/* Find an integration time large enough to measure something significant */
for (; max < (2.0 * blk)
&& inttime <= p->max_meastime
&& inttime <= EX1_INTTIME_MAX;) {
tinttime = inttime * 10.0;
if (tinttime > p->max_meastime
|| tinttime > EX1_INTTIME_MAX)
break;
inttime = tinttime;
a1logd(p->log,5,"Trying intttime %f\n",inttime);
if ((ec = ex1_do_meas(p, &raw, &inttime, 0.05)) != EX1_OK) {
return ex1_interp_code((inst *)p, ec);
}
max = largest_val_rspec(NULL, &six, raw);
blk = ex1_interp_idark_val(sconf, 0, six, inttime);
a1logd(p->log,5,"Max %.0f, black %.0f\n",max,blk);
if (max >= (2.0 * blk))
break;
}
/* If we haven't already tried the maximum time, calculate optimal inttime */
if (inttime <= p->max_meastime
&& max >= (2.0 * blk)) {
double llev, blk2;
/* Compute the light level for our measurement */
llev = linearize_val_rspec(sconf, max - blk)/inttime;
a1logd(p->log,5,"Light level = %f\n",llev);
/* Calculate an initial target integration time to use to estimat black */
tinttime = (SENSE_AIM - blk)/(max - blk) * inttime;
a1logd(p->log,5,"Initial optimal inttime %f\n",tinttime);
/* Itterate */
for (i = 0; i < 5; i++) {
blk2 = ex1_interp_idark_val(sconf, 0, six, tinttime);
tinttime = linearize_val_rspec(sconf, SENSE_AIM - blk2)/llev;
}
a1logd(p->log,5,"Optimal inttime %f\n",tinttime);
if (tinttime < p->max_meastime) {
if (tinttime < EX1_INTTIME_MIN)
tinttime = EX1_INTTIME_MIN;
if (tinttime > EX1_INTTIME_MAX)
tinttime = EX1_INTTIME_MAX;
/* Do the real measurement */
if (inttime <= 1.0) {
inttime = tinttime;
a1logd(p->log,5,"Doing real measurement with inttime %f\n",inttime);
if ((ec = ex1_do_meas(p, &raw, &inttime, 1.0)) != EX1_OK) {
return ex1_interp_code((inst *)p, ec);
}
max = largest_val_rspec(NULL, &six, raw);
a1logd(p->log,5,"Got max %.0f, aimed for %.0f\n",max,SENSE_AIM);
}
}
}
if (max >= SENSE_SAT) {
/* Hmm. Should retry in case light level changed during measurement ? */
return ex1_interp_code((inst *)p, EX1_RD_SENSORSATURATED);
}
}
#ifdef PLOT_DEBUG
printf("Raw:\n");
plot_rspec1(raw);
#endif
subtract_idark_rspec(raw);
#ifdef PLOT_DEBUG
{
rspec *nl = new_rspec_clone(raw);
linearize_rspec(raw);
printf("non-lin and linearized:\n");
plot_rspec2(nl, raw);
del_rspec(nl);
}
#else
linearize_rspec(raw);
#endif
emis_calibrate_rspec(raw);
#ifdef PLOT_DEBUG
printf("Calibrated raw:\n");
plot_rspec1(raw);
#endif
wav = convert_wav_from_raw_rspec(raw);
del_rspec(raw);
inttime_calibrate_rspec(wav);
#ifdef PLOT_DEBUG
printf("Wav:\n");
plot_rspec1(wav);
#endif
val->mtype = wav->mtype;
/* Copy spectral in */
val->sp.spec_n = sconf->nwav;
val->sp.spec_wl_short = sconf->wl_short;
val->sp.spec_wl_long = sconf->wl_long;
val->sp.norm = 1.0; /* Spectral data is in W/nm/m^2 */
for (i = 0; i < val->sp.spec_n; i++) {
val->sp.spec[i] = 1000.0 * wav->samp[0][i];
}
del_rspec(wav);
/* Set the XYZ */
p->conv->convert(p->conv, val->XYZ, &val->sp);
if (clamp)
icmClamp3(val->XYZ, val->XYZ);
val->XYZ_v = 1;
if (user_trig)
return inst_user_trig;
return rv;
}
/* Return needed and available inst_cal_type's */
static inst_code ex1_get_n_a_cals(inst *pp, inst_cal_type *pn_cals, inst_cal_type *pa_cals) {
ex1 *p = (ex1 *)pp;
time_t curtime = time(NULL);
inst_cal_type n_cals = inst_calt_none;
inst_cal_type a_cals = inst_calt_none;
int idark_valid = p->idark_valid;
if ((curtime - p->iddate) > DCALTOUT) {
a1logd(p->log,2,"Invalidating adaptive dark cal as %d secs from last cal\n",curtime - p->iddate);
idark_valid = 0;
}
if (!idark_valid
|| (p->want_dcalib && !p->noinitcalib))
n_cals |= inst_calt_em_dark;
a_cals |= inst_calt_em_dark;
if (pn_cals != NULL)
*pn_cals = n_cals;
if (pa_cals != NULL)
*pa_cals = a_cals;
a1logd(p->log,3,"ex1: returning n_cals 0x%x, a_cals 0x%x\n",n_cals, a_cals);
return inst_ok;
}
/* Request an instrument calibration. */
inst_code ex1_calibrate(
inst *pp,
inst_cal_type *calt, /* Calibration type to do/remaining */
inst_cal_cond *calc, /* Current condition/desired condition */
inst_calc_id_type *idtype, /* Condition identifier type */
char id[CALIDLEN] /* Condition identifier (ie. white reference ID) */
) {
ex1 *p = (ex1 *)pp;
rspec_inf *sconf = &p->sconf;
inst_cal_type needed, available;
inst_code ev;
int ec;
if (!p->gotcoms)
return inst_no_coms;
if (!p->inited)
return inst_no_init;
if ((ev = ex1_get_n_a_cals((inst *)p, &needed, &available)) != inst_ok)
return ev;
/* Translate inst_calt_all/needed into something specific */
if (*calt == inst_calt_all
|| *calt == inst_calt_needed
|| *calt == inst_calt_available) {
if (*calt == inst_calt_all)
*calt = (needed & inst_calt_n_dfrble_mask) | inst_calt_ap_flag;
else if (*calt == inst_calt_needed)
*calt = needed & inst_calt_n_dfrble_mask;
else if (*calt == inst_calt_available)
*calt = available & inst_calt_n_dfrble_mask;
a1logd(p->log,4,"ex1_calibrate: doing calt 0x%x\n",calt);
if ((*calt & inst_calt_n_dfrble_mask) == 0) /* Nothing todo */
return inst_ok;
}
/* See if it's a calibration we understand */
if (*calt & ~available & inst_calt_all_mask) {
return inst_unsupported;
}
/* Adaptive black calibration: */
if (*calt & inst_calt_em_dark) {
time_t cdate = time(NULL);
int i, j, k;
rspec *raw;
if ((*calc & inst_calc_cond_mask) != inst_calc_man_em_dark) {
*calc = inst_calc_man_em_dark;
return inst_cal_setup;
}
a1logd(p->log,2,"\nDoing emis adapative black calibration\n");
/* Bracket the dark level and interpolate. */
if ((ec = ex1_do_meas(p, &sconf->idark[0], &p->idark_int_time[0], 1.0)) != EX1_OK) {
return ex1_interp_code((inst *)p, ec);
}
if ((ec = ex1_do_meas(p, &sconf->idark[1], &p->idark_int_time[1], 1.0)) != EX1_OK) {
return ex1_interp_code((inst *)p, ec);
}
p->idark_valid = 1;
p->want_dcalib = 0;
p->iddate = cdate;
*calt &= ~inst_calt_em_dark;
#ifdef PLOT_DEBUG /* Use plot to show readings & processing */
printf("idark calib:\n");
plot_rspec2(sconf->idark[0], sconf->idark[1]);
#endif
/* Test accuracy of dark level interpolation */
#ifdef TEST_DARK_INTERP
{
double tinttime;
rspec *ref, *interp;
for (tinttime = EX1_INTTIME_MIN; ; tinttime *= 2.0) {
if (tinttime >= EX1_INTTIME_MAX)
tinttime = EX1_INTTIME_MAX;
if ((ec = ex1_do_meas(p, &ref, &tinttime, 1.0)) != EX1_OK)
return ex1_interp_code((inst *)p, ec);
interp = ex1_interp_idark(sconf, tinttime);
fprintf(stderr,"Low gain ref vs. interp dark offset for inttime %f:\n",tinttime);
plot_rspec2(ref, interp);
del_rspec(interp);
del_rspec(ref);
if ((tinttime * 1.1) > EX1_INTTIME_MAX)
break;
}
}
#endif /* NEVER */
#ifdef ENABLE_NONVCAL
/* Save the idark calibration to a file */
ex1_save_calibration(p);
#endif
}
return inst_ok;
}
/* Error codes interpretation */
static char *
ex1_interp_error(inst *pp, int ec) {
char *rv;
ex1 *p = (ex1 *)pp;
ec &= inst_imask;
/* See if it's an native error code */
if ((rv = ex1_interp_native_error(p, ec)) != NULL)
return rv;
switch (ec) {
case EX1_TIMEOUT:
return "Communications timeout";
case EX1_COMS_FAIL:
return "Communications failure";
case EX1_UNKNOWN_MODEL:
return "Not an EX1";
case EX1_SHORT_WRITE:
return "Short USB write";
case EX1_SHORT_READ:
return "Short USB read";
case EX1_LONG_READ:
return "Long USB read";
case EX1_DATA_CHSUM_ERROR:
return "Data checksum error";
case EX1_DATA_PARSE_ERROR:
return "Data from ex1 didn't parse as expected";
case EX1_RD_SENSORSATURATED:
return "Sensor is saturated";
/* Internal software errors */
case EX1_INTERNAL_ERROR:
return "Internal software error";
case EX1_NOT_IMP:
return "Not implemented";
case EX1_MEMORY:
return "Memory allocation failed";
case EX1_INT_THREADFAILED:
return "Thread failed";
case EX1_INTTIME_RANGE:
return "Integration time is out of range";
case EX1_DELTIME_RANGE:
return "Trigger delat time is out of range";
case EX1_STROBE_RANGE:
return "Multi strobe period time is out of range";
case EX1_AVERAGE_RANGE:
return "Number to average is out of range";
case EX1_BOXCAR_RANGE:
return "Boxcar filtering is out of range";
case EX1_INT_CAL_SAVE:
return "Saving calibration file failed";
case EX1_INT_CAL_RESTORE:
return "Restoring calibration file failed";
case EX1_INT_CAL_TOUCH:
return "Touching calibration file failed";
case EX1_INT_CIECONVFAIL:
return "Creating spectral to XYZ conversion failed";
/* Hardware errors */
case EX1_NO_WL_CAL:
return "Instrument doesn't contain wavelength calibration";
case EX1_NO_IR_CAL:
return "Instrument doesn't contain irradiance calibration";
default:
return "Unknown error code";
}
}
/* EX1 Native error codes interpretation */
/* Return NULL if not recognised */
static char *
ex1_interp_native_error(ex1 *p, int ec) {
switch (ec) {
case EX1_OK:
return "No device error";
case EX1_UNSUP_PROTOCOL:
return "Invalid/unsupported protocol";
case EX1_MES_UNKN:
return "Unknown message type";
case EX1_MES_BAD_CHSUM:
return "Bad message checksum";
case EX1_MES_TOO_LARGE:
return "Message is too large";
case EX1_MES_PAYLD_LEN:
return "Payload length doesn't match message type";
case EX1_MES_PAYLD_INV:
return "Payload data is invalid";
case EX1_DEV_NOT_RDY:
return "Device not ready for message type";
case EX1_MES_UNK_CHSUM:
return "Unknown checksum type";
case EX1_DEV_UNX_RST:
return "Unexpected device reset";
case EX1_TOO_MANY_BUSSES:
return "Too many command sources";
case EX1_OUT_OF_MEM:
return "Device is out of memory";
case EX1_NO_INF:
return "Information doesn't exist";
case EX1_DEV_INT_ERR:
return "Device internal error";
case EX1_DECRYPT_ERR:
return "Could not decrypt";
case EX1_FIRM_LAYOUT:
return "Firmware layout is invalid";
case EX1_PACKET_SIZE:
return "Data packet size is not 64 bytes";
case EX1_HW_REV_INCPT:
return "HW rev. is incompatible with firmware";
case EX1_FLASH_MAP:
return "Flash map is incompatible with firmware";
case EX1_DEFERRED:
return "Operation/Response deferred";
default:
return NULL;
}
}
/* Convert a machine specific error code into an abstract dtp code */
static inst_code
ex1_interp_code(inst *pp, int ec) {
ec &= inst_imask;
switch (ec) {
case EX1_OK:
return inst_ok;
case EX1_INTERNAL_ERROR:
case EX1_MEMORY:
case EX1_INT_THREADFAILED:
case EX1_INTTIME_RANGE:
case EX1_DELTIME_RANGE:
case EX1_STROBE_RANGE:
case EX1_AVERAGE_RANGE:
case EX1_BOXCAR_RANGE:
case EX1_INT_CAL_SAVE:
case EX1_INT_CAL_RESTORE:
case EX1_INT_CAL_TOUCH:
case EX1_INT_CIECONVFAIL:
return inst_internal_error | ec;
case EX1_TIMEOUT:
case EX1_COMS_FAIL:
case EX1_SHORT_WRITE:
case EX1_SHORT_READ:
case EX1_LONG_READ:
return inst_coms_fail | ec;
case EX1_UNKNOWN_MODEL:
return inst_unknown_model | ec;
case EX1_DATA_CHSUM_ERROR:
case EX1_DATA_PARSE_ERROR:
return inst_protocol_error | ec;
// return inst_bad_parameter | ec;
// return inst_wrong_config | ec;
case EX1_NO_WL_CAL:
case EX1_NO_IR_CAL:
/* Native instrument error */
case EX1_UNSUP_PROTOCOL:
case EX1_MES_UNKN:
case EX1_MES_BAD_CHSUM:
case EX1_MES_TOO_LARGE:
case EX1_MES_PAYLD_LEN:
case EX1_MES_PAYLD_INV:
case EX1_DEV_NOT_RDY:
case EX1_MES_UNK_CHSUM:
case EX1_DEV_UNX_RST:
case EX1_TOO_MANY_BUSSES:
case EX1_OUT_OF_MEM:
case EX1_NO_INF:
case EX1_DEV_INT_ERR:
case EX1_DECRYPT_ERR:
case EX1_FIRM_LAYOUT:
case EX1_PACKET_SIZE:
case EX1_HW_REV_INCPT:
case EX1_FLASH_MAP:
case EX1_DEFERRED:
return inst_hardware_fail | ec;
case EX1_RD_SENSORSATURATED:
return inst_misread | ec;
case EX1_NOT_IMP:
return inst_unsupported | ec;
}
return inst_other_error | ec;
}
/* Destroy ourselves */
static void
ex1_del(inst *pp) {
if (pp != NULL) {
ex1 *p = (ex1 *)pp;
#ifdef ENABLE_NONVCAL
ex1_touch_calibration(p);
#endif
if (p->icom != NULL)
p->icom->del(p->icom);
if (p->cbuf != NULL)
free(p->cbuf);
if (p->alias != NULL)
free(p->alias);
if (p->serno != NULL)
free(p->serno);
if (p->grating != NULL)
free(p->grating);
if (p->filter != NULL)
free(p->filter);
if (p->coating != NULL)
free(p->coating);
free_rspec_inf(&p->sconf);
if (p->straylight != NULL)
free(p->straylight);
if (p->emis_coef != NULL)
free(p->emis_coef);
if (p->conv != NULL)
p->conv->del(p->conv);
p->vdel(pp);
free(p);
}
}
/* Return the instrument mode capabilities */
static void ex1_capabilities(inst *pp,
inst_mode *pcap1,
inst2_capability *pcap2,
inst3_capability *pcap3) {
ex1 *p = (ex1 *)pp;
inst_mode cap1 = 0;
inst2_capability cap2 = 0;
cap1 |= inst_mode_emis_tele
| inst_mode_colorimeter
| inst_mode_spectral
;
cap2 |= inst2_prog_trig
| inst2_user_trig
;
if (pcap1 != NULL)
*pcap1 = cap1;
if (pcap2 != NULL)
*pcap2 = cap2;
if (pcap3 != NULL)
*pcap3 = inst3_none;
}
/* Return current or given configuration available measurement modes. */
/* NOTE that conf_ix values shoudn't be changed, as it is used as a persistent key */
static inst_code ex1_meas_config(
inst *pp,
inst_mode *mmodes,
inst_cal_cond *cconds,
int *conf_ix
) {
ex1 *p = (ex1 *)pp;
inst_code ev;
inst_mode mval = 0;
if (mmodes != NULL)
*mmodes = inst_mode_none;
if (cconds != NULL)
*cconds = inst_calc_unknown;
/* Add the extra dependent and independent modes */
mval |= inst_mode_emis_tele
| inst_mode_colorimeter
| inst_mode_spectral;
if (mmodes != NULL)
*mmodes = mval;
/* Return configuration index returned */
if (conf_ix != NULL)
*conf_ix = 0; /* There is only one */
return inst_ok;
}
/* Check device measurement mode */
static inst_code ex1_check_mode(inst *pp, inst_mode m) {
inst_mode cap;
if (!pp->gotcoms)
return inst_no_coms;
if (!pp->inited)
return inst_no_init;
pp->capabilities(pp, &cap, NULL, NULL);
/* Simple test */
if (m & ~cap)
return inst_unsupported;
/* Only tele emission mode supported */
if (!IMODETST(m, inst_mode_emis_tele)) {
return inst_unsupported;
}
return inst_ok;
}
/* Set device measurement mode */
static inst_code ex1_set_mode(inst *pp, inst_mode m) {
ex1 *p = (ex1 *)pp;
int refrmode;
inst_code ev;
if ((ev = ex1_check_mode(pp, m)) != inst_ok)
return ev;
p->mode = m;
return inst_ok;
}
/* Set the noinitcalib mode */
static void ex1_set_noinitcalib(ex1 *p, int v, int losecs) {
/* Ignore disabling init calib if more than losecs since instrument was open */
if (v && losecs != 0 && p->lo_secs >= losecs) {
a1logd(p->log,3,"initcalib disable ignored because %d >= %d secs\n",p->lo_secs,losecs);
return;
}
p->noinitcalib = v;
}
/*
* 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
ex1_get_set_opt(inst *pp, inst_opt_type m, ...) {
ex1 *p = (ex1 *)pp;
inst_code ev = inst_ok;
a1logd(p->log, 5, "ex1_get_set_opt: opt type 0x%x\n",m);
if (m == inst_opt_initcalib) { /* default */
ex1_set_noinitcalib(p, 0, 0);
return inst_ok;
} else if (m == inst_opt_noinitcalib) {
va_list args;
int losecs = 0;
va_start(args, m);
losecs = va_arg(args, int);
va_end(args);
ex1_set_noinitcalib(p, 1, losecs);
return inst_ok;
/* Record the trigger mode */
} else 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;
/* 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 ex1 *new_ex1(icoms *icom, instType dtype) {
ex1 *p;
if ((p = (ex1 *)calloc(sizeof(ex1),1)) == NULL) {
a1loge(icom->log, 1, "new_ex1: malloc failed!\n");
return NULL;
}
/* Allocate a default communication buffer */
if ((p->cbuf = calloc(1, 64)) == NULL) {
a1loge(icom->log, 1, "new_ex1: malloc failed!\n");
free(p);
return NULL;
}
p->cbufsz = 64;
p->log = new_a1log_d(icom->log);
p->init_coms = ex1_init_coms;
p->init_inst = ex1_init_inst;
p->capabilities = ex1_capabilities;
p->meas_config = ex1_meas_config;
p->check_mode = ex1_check_mode;
p->set_mode = ex1_set_mode;
p->get_set_opt = ex1_get_set_opt;
p->read_sample = ex1_read_sample;
p->get_n_a_cals = ex1_get_n_a_cals;
p->calibrate = ex1_calibrate;
p->interp_error = ex1_interp_error;
p->del = ex1_del;
p->icom = icom;
p->dtype = dtype;
p->want_dcalib = 1; /* Always do an initial dark calibration */
return p;
}
/* =============================================================================== */
/* Calibration info save/restore */
static int ex1_save_calibration(ex1 *p) {
int ev = EX1_OK;
int i;
char fname[100]; /* Name */
calf x;
int argyllversion = ARGYLL_VERSION;
int ss;
snprintf(fname, 99, ".ex1_%s.cal", p->serno);
if (calf_open(&x, p->log, fname, 1)) {
x.ef = 2;
goto done;
}
ss = sizeof(ex1);
/* Some file identification */
calf_wints(&x, &argyllversion, 1);
calf_wints(&x, &ss, 1);
calf_wstrz(&x, p->serno);
/* Save the black calibration if it's valid */
calf_wints(&x, &p->idark_valid, 1);
calf_wtime_ts(&x, &p->iddate, 1);
calf_wrspec(&x, p->sconf.idark[0]);
calf_wrspec(&x, p->sconf.idark[1]);
a1logd(p->log,3,"nbytes = %d, Checksum = 0x%x\n",x.nbytes,x.chsum);
calf_wints(&x, (int *)(&x.chsum), 1);
if (calf_done(&x))
x.ef = 3;
done:;
if (x.ef != 0) {
a1logd(p->log,2,"Writing calibration file failed with %d\n",x.ef);
ev = EX1_INT_CAL_SAVE;
} else {
a1logd(p->log,2,"Writing calibration file succeeded\n");
}
return ev;
}
/* Restore the all modes calibration from the local system */
static int ex1_restore_calibration(ex1 *p) {
int ev = EX1_OK;
int i, j;
char fname[100]; /* Name */
calf x;
int argyllversion;
int ss, nbytes, chsum1, chsum2;
char *serno = NULL;
snprintf(fname, 99, ".ex1_%s.cal", p->serno);
if (calf_open(&x, p->log, fname, 0)) {
x.ef = 2;
goto done;
}
/* Last modified time */
p->lo_secs = x.lo_secs;
/* Do a dumy read to check the checksum, then a real read */
for (x.rd = 0; x.rd < 2; x.rd++) {
calf_rewind(&x);
/* Check the file identification */
calf_rints2(&x, &argyllversion, 1);
calf_rints2(&x, &ss, 1);
calf_rstrz2(&x, &serno);
if (x.ef != 0
|| argyllversion != ARGYLL_VERSION
|| ss != (sizeof(ex1))
|| strcmp(serno, p->serno) != 0) {
a1logd(p->log,2,"Identification didn't verify\n");
if (x.ef == 0)
x.ef = 4;
goto done;
}
/* Read the black calibration if it's valid */
calf_rints(&x, &p->idark_valid, 1);
calf_rtime_ts(&x, &p->iddate, 1);
calf_rrspec(&x, &p->sconf.idark[0], &p->sconf);
calf_rrspec(&x, &p->sconf.idark[1], &p->sconf);
/* Check the checksum */
chsum1 = x.chsum;
nbytes = x.nbytes;
calf_rints2(&x, &chsum2, 1);
if (x.ef != 0
|| chsum1 != chsum2) {
a1logd(p->log,2,"Checksum didn't verify, bytes %d, got 0x%x, expected 0x%x\n",nbytes,chsum1, chsum2);
if (x.ef == 0)
x.ef = 5;
goto done;
}
}
a1logd(p->log,5,"ex1_restore_calibration done\n");
done:;
free(serno);
if (calf_done(&x))
x.ef = 3;
if (x.ef != 0) {
a1logd(p->log,2,"Reading calibration file failed with %d\n",x.ef);
ev = EX1_INT_CAL_RESTORE;
}
return ev;
}
static int ex1_touch_calibration(ex1 *p) {
int ev = EX1_OK;
char fname[100]; /* Name */
int rv;
snprintf(fname, 99, ".ex1_%s.cal", p->serno);
if (calf_touch(p->log, fname)) {
a1logd(p->log,2,"Touching calibration file time failed with\n");
return EX1_INT_CAL_TOUCH;
}
return EX1_OK;
}
/* =============================================================================== */
/* EX1 lower level communications */
/* Interpret an icoms error into a EX1 error */
static int icoms2ex1_err(int se) {
if (se != ICOM_OK) {
if (se & ICOM_TO)
return EX1_TIMEOUT;
return EX1_COMS_FAIL;
}
return EX1_OK;
}
/* Message format definitions */
#define EX1_FLAG_RESP 0x0001 /* Response to an earlier request */
#define EX1_FLAG_ACK 0x0002 /* Acknowldgement response */
#define EX1_FLAG_RQACK 0x0004 /* Request for acknowldgement */
#define EX1_FLAG_NACK 0x0008 /* Negative acknowldgement response */
#define EX1_FLAG_EXPTN 0x0010 /* Exception occured */
#define EX1_FLAG_PVDEP 0x0020 /* Protocol version is deprecated */
#define EX1_CHSUM_NONE 0x0 /* No checksum */
#define EX1_CHSUM_MD5 0x1 /* MD5 checksum */
#define EX1_TLC_MASK 0xFFF00000 /* Top-level category message type mask */
#define EX1_TLC_GENERAL 0x00000000 /* General device characteristics */
#define EX1_TLC_SPECTRO 0x00100000 /* Spectrometer feature */
#define EX1_TLC_GPIO 0x00200000 /* GPIO feature */
#define EX1_TLC_STROBE 0x00300000 /* Strobe feature */
#define EX1_TLC_TEMP 0x00400000 /* Temperature feature */
static char *cmdtstring(unsigned int cmd);
/* Debug - dump a command packet at debug level deb1 */
static void dump_command(ex1 *p, ORD8 *buf, int len, int deblev) {
unsigned int pver = 0; /* Protocol version */
unsigned int flags = 0;
unsigned int merrno = 0;
char *es;
unsigned int mestype = 0;
unsigned int tlc = 0; /* Top level message category */
unsigned int regarding = 0;
unsigned int chstype = EX1_CHSUM_NONE;
unsigned int imdatlen = 0;
unsigned int bremain = 0;
unsigned int pll = 0; /* Explicit payload length */
int fo; /* Footer offset */
if (deblev < p->log->debug)
return;
if (len < 44) {
a1logd(p->log, 0, " Command packet too short (%d bytes)\n",len);
return;
}
/* First comes the header */
if (buf[0] != 0xC1 || buf[1] != 0xC0) {
a1logd(p->log, 0, " Start bytes wrong (0x%02x, 0x%02x)\n",buf[0],buf[1]);
}
pver = read_ORD16_le(buf + 2);
if (pver < 0x1000) {
a1logd(p->log, 0, " Unknown protocol version (0x%x)\n",pver);
return;
}
a1logd(p->log, 0, " Protocol version: 0x%x\n",pver);
flags = read_ORD16_le(buf + 4);
a1logd(p->log, 0, " Flags: 0x%x\n",flags);
if (flags & EX1_FLAG_RESP)
a1logd(p->log, 0, " Response to an earlier request\n");
if (flags & EX1_FLAG_ACK)
a1logd(p->log, 0, " Acknowldgement response\n");
if (flags & EX1_FLAG_RQACK)
a1logd(p->log, 0, " Request for acknowldgement\n");
if (flags & EX1_FLAG_NACK)
a1logd(p->log, 0, " Negative acknowldgement response\n");
if (flags & EX1_FLAG_EXPTN)
a1logd(p->log, 0, " Exception occurred\n");
if (flags & EX1_FLAG_PVDEP)
a1logd(p->log, 0, " Protocol version is deprecated request\n");
merrno = read_ORD16_le(buf + 6);
a1logd(p->log, 0, " Error no.: 0x%x\n",merrno);
if ((es = ex1_interp_native_error(p, merrno)) != NULL)
a1logd(p->log, 0, " '%s'\n",es);
mestype = read_ORD32_le(buf + 8);
a1logd(p->log, 0, " Mes. Type: 0x%x = %s\n",mestype, cmdtstring(mestype));
tlc = mestype & EX1_TLC_MASK;
switch(tlc) {
case EX1_TLC_GENERAL:
a1logd(p->log, 0, " General device characteristics\n");
break;
case EX1_TLC_SPECTRO:
a1logd(p->log, 0, " Spectrometer feature\n");
break;
case EX1_TLC_GPIO:
a1logd(p->log, 0, " GPIO feature\n");
break;
case EX1_TLC_STROBE:
a1logd(p->log, 0, " Strobe feature\n");
break;
case EX1_TLC_TEMP:
a1logd(p->log, 0, " Temperature feature\n");
break;
}
regarding = read_ORD32_le(buf + 12);
a1logd(p->log, 0, " Regarding: 0x%x\n",regarding);
chstype = read_ORD8(buf + 22);
a1logd(p->log, 0, " checksum: 0x%x\n",chstype);
if (chstype == EX1_CHSUM_NONE)
a1logd(p->log, 0, " none\n");
else if (chstype == EX1_CHSUM_MD5)
a1logd(p->log, 0, " MD5\n");
imdatlen = read_ORD8(buf + 23);
a1logd(p->log, 0, " immediate data %d bytes%s\n",imdatlen, imdatlen > 16 ? " (illegal)" : " ");
if (imdatlen > 0)
a1logd(p->log, 0, " 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x"
" 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
buf[24], buf[25], buf[26], buf[27], buf[28], buf[29], buf[30], buf[31],
buf[32], buf[33], buf[34], buf[35], buf[36], buf[37], buf[38], buf[39]);
bremain = read_ORD32_le(buf + 40);
a1logd(p->log, 0, " bytes remaining %d\n",bremain);
if (bremain < 20) {
a1logd(p->log, 0, " - too small for chsum & footer\n");
return;
}
if ((44 + bremain) > len) {
a1logd(p->log, 0, " - too large for for message size (%d available)\n",len - 44);
return;
}
/* Now comes the payload */
pll = bremain - 20; /* Payload length */
if (pll > 0) {
adump_bytes(p->log, " ", buf + 44, 0, pll);
}
/* Then the checksum */
if (chstype == EX1_CHSUM_NONE) {
a1logd(p->log, 0, " checksum not used\n");
} else if (chstype == EX1_CHSUM_MD5) {
icmMD5 *md5;
if ((md5 = new_icmMD5()) == NULL) {
a1logd(p->log, 0, " new_icmMD5 failed\n");
} else {
ORD8 chsum[16];
int i;
md5->add(md5, buf, 44 + pll);
md5->get(md5, chsum);
for (i = 0; i < 16; i++) {
if (chsum[i] != buf[44+pll+i])
break;
}
if (i < 16)
a1logd(p->log, 0, " MD5 checksum error\n");
else
a1logd(p->log, 0, " MD5 checksum OK\n");
md5->del(md5);
}
} else {
a1logd(p->log, 0, " checksum not checked (unknown type)\n");
}
/* Finally the footer */
fo = 44 + pll + 16;
if (buf[fo] != 0xC5 || buf[fo + 1] != 0xC4 || buf[fo + 2] != 0xC3 || buf[fo + 3] != 0xC2) {
a1logd(p->log, 0, " Footer error (0x%02x 0x%02x 0x%02x 0x%02x)\n",
buf[fo],buf[fo+1],buf[fo+2],buf[fo+3]);
}
}
/* Do a full command/response exchange with the ex1 */
/* (This level is not multi-thread safe) */
/* Return the ex1 error code. */
static int
ex1_command(
ex1 *p,
unsigned int cmd, /* 32 bit command or query code */
ORD8 *in, /* Input data */
int ilen, /* data length */
ORD8 *out, /* Output payload - if NULL, assume it's a command */
int olen, /* Expected returned data length */
int *prlen, /* Actual data returned. If NULL, expect exactly olen */
double to, /* Timeout in seconds */
int nd /* nz to disable debug messages */
) {
int se, rv;
ORD8 *buf;
int bsize = 64; /* Size needed */
int rwbytes = 0;
int stime;
int slen = 64; /* Send length */
int toff; /* Tail offset */
int rlen = 64; /* Receive length */
unsigned int pver = 0; /* Protocol version */
unsigned int flags = 0;
unsigned int merrno = 0;
unsigned int mestype = 0;
unsigned int tlc = 0; /* Top level message category */
unsigned int regarding = 0;
unsigned int chstype = EX1_CHSUM_MD5;
unsigned int imdatlen = 0;
unsigned int bremain = 0;
unsigned int pll = 0; /* Explicit payload length */
unsigned int ardlen = 0; /* Actual read data length (payload) */
int fo; /* Footer offset */
if (in == NULL)
ilen = 0;
if (out == NULL)
olen = 0;
a1logd(p->log,6,"ex1_command: 0x%x '%s' ilen %d olen %d\n", cmd, cmdtstring(cmd), ilen, olen);
if (p->log->debug >= 7 && ilen > 0) {
adump_bytes(p->log, " ", in, 0, ilen);
}
stime = msec_time();
if (ilen > 16) { /* Too big for immediate data */
slen += ilen;
}
if (out != NULL && olen > 16) { /* Too big for immediate data */
rlen += olen;
}
/* Make sure out buffer is big enough to send or revieve */
bsize = slen > rlen ? slen : rlen;
if (bsize > p->cbufsz) {
if ((p->cbuf = realloc(p->cbuf, bsize)) == NULL) {
rv = EX1_MEMORY;
goto done;
}
p->cbufsz = bsize;
}
buf = p->cbuf;
/* Header */
buf[0] = 0xC1;
buf[1] = 0xC0;
/* Protocol version */
write_ORD16_le(buf + 2, 0x1100);
/* If it's a command, request an acknowledge */
flags = 0;
if (out == NULL) {
flags |= EX1_FLAG_RQACK;
}
write_ORD16_le(buf + 4, flags);
/* Error number */
write_ORD16_le(buf + 6, 0);
/* Command */
write_ORD32_le(buf + 8, cmd);
/* Regarding identifier (not used) */
write_ORD32_le(buf + 12, 0);
/* Checksum type 0 = none */
write_ORD8(buf + 22, chstype);
/* Reserved bytes */
memset(buf + 16, 0, 6);
/* Immediate data */
if (ilen <= 16) {
write_ORD8(buf + 23, ilen);
if (ilen > 0)
memcpy(buf + 24, in, ilen);
if (ilen < 16)
memset(buf + 24 + ilen, 0, 16 - ilen);
/* Bytes remaining = checksum + footer = 20 */
write_ORD32_le(buf + 40, 20);
toff = 44;
pll = 0;
/* or Payload */
} else {
write_ORD8(buf + 23, 0);
write_ORD32_le(buf + 40, ilen + 20);
if (ilen > 0)
memcpy(buf + 44, in, ilen);
toff = 44 + ilen;
pll = ilen;
}
/* Checksum bytes */
if (chstype == EX1_CHSUM_MD5) {
icmMD5 *md5;
if ((md5 = new_icmMD5()) == NULL) {
a1logd(p->log, 1, "new_icmMD5 failed\n");
merrno = EX1_INTERNAL_ERROR;
} else {
ORD8 chsum[16];
int i;
md5->add(md5, buf, toff);
md5->get(md5, chsum);
for (i = 0; i < 16; i++)
buf[44+pll+i] = chsum[i];
md5->del(md5);
}
} else {
memset(buf + toff, 0, 16);
}
/* Finally the footer */
buf[toff + 16 + 0] = 0xC5;
buf[toff + 16 + 1] = 0xC4;
buf[toff + 16 + 2] = 0xC3;
buf[toff + 16 + 3] = 0xC2;
if (p->log->debug >= 8) {
a1logd(p->log,1,"\nex1_command: SENDING:\n");
dump_command(p, buf, slen, p->log->debug);
}
/* Send the command */
se = p->icom->usb_write(p->icom, NULL, EX1_EP, buf, slen, &rwbytes, 1.0);
if ((rv = icoms2ex1_err(se)) != EX1_OK) {
a1logd(p->log,1,"ex1_command: send failed with ICOM err 0x%x\n",se);
goto done;
}
if (rwbytes != slen) {
a1logd(p->log,1,"ex1_command: send %d/%d bytes - short\n",rwbytes, slen);
rv = EX1_SHORT_WRITE;
goto done;
}
/* - - - - - - - - - - - - - - */
/* Get the expected reply to a query, or an acknowledgement to a command */
se = p->icom->usb_read(p->icom, NULL, EX1_EP | 0x80, buf, 64, &rwbytes, to);
if ((rv = icoms2ex1_err(se)) != EX1_OK) {
a1logd(p->log,1,"ex1_command: read failed with ICOM err 0x%x\n",se);
goto done;
}
if (p->log->debug >= 8) {
a1logd(p->log,1,"\nex1_command: RECEIVING:\n");
dump_command(p, buf, rwbytes, p->log->debug);
}
if (rwbytes != 64) {
a1logd(p->log,1,"ex1_command: read %d/%d bytes - short\n",rwbytes, 64);
rv = EX1_SHORT_READ;
goto done;
}
/* Parse and check the reply: */
/* First comes the header */
if (buf[0] != 0xC1 || buf[1] != 0xC0) {
a1logd(p->log, 1, "ex1_command: start bytes wrong (0x%02x, 0x%02x)\n",buf[0],buf[1]);
rv = EX1_DATA_PARSE_ERROR;
goto done;
}
pver = read_ORD16_le(buf + 2);
if (pver < 0x1000) {
a1logd(p->log, 1, "Unknown protocol version (0x%x)\n",pver);
rv = EX1_DATA_PARSE_ERROR;
goto done;
}
flags = read_ORD16_le(buf + 4);
merrno = read_ORD16_le(buf + 6);
mestype = read_ORD32_le(buf + 8);
regarding = read_ORD32_le(buf + 12);
chstype = read_ORD8(buf + 22);
imdatlen = read_ORD8(buf + 23);
bremain = read_ORD32_le(buf + 40);
/* If there has been an error, return it */
if (merrno != EX1_OK) {
rv = merrno;
goto done;
}
if (bremain < 20) {
a1logd(p->log, 1, "Bytes remaining %d is too small for chsum & footer\n",bremain);
rv = EX1_DATA_PARSE_ERROR;
goto done;
}
/* Explicit playload length */
pll = bremain - 20; /* Payload length */
if (pll > 0 && imdatlen > 0) {
a1logd(p->log, 1, "Got both immediate payoad %d bytes and explicit %d bytes\n",imdatlen,pll);
rv = EX1_DATA_PARSE_ERROR;
goto done;
}
/* Payload is immediate */
if (imdatlen > 0) {
if (imdatlen > olen) {
a1logd(p->log, 1, "Got %d bytes payload when expecting %d\n",imdatlen, olen);
rv = EX1_LONG_READ;
goto done;
}
memcpy(out, buf + 24, imdatlen);
if (prlen != NULL)
*prlen = imdatlen;
ardlen = imdatlen;
}
/* If there is an explicit payload, read the remaining bytes */
else if (pll > 0) {
/* Make sure buffer is big enough for read */
bsize = pll + 64;
if (bsize > p->cbufsz) {
if ((p->cbuf = realloc(p->cbuf, bsize)) == NULL) {
rv = EX1_MEMORY;
goto done;
}
p->cbufsz = bsize;
}
buf = p->cbuf;
se = p->icom->usb_read(p->icom, NULL, EX1_EP | 0x80, buf + 64, pll, &rwbytes, to);
// adump_bytes(p->log, " ", buf + 44, 0, pll);
if (rwbytes != pll) {
a1logd(p->log,1,"ex1_command: read %d/%d bytes - short\n",rwbytes, pll);
rv = EX1_SHORT_READ;
goto done;
}
if (rwbytes > olen) {
a1logd(p->log, 1, "Got %d bytes payload when expecting %d\n",rwbytes, olen);
rv = EX1_LONG_READ;
goto done;
}
memcpy(out, buf + 44, pll);
if (prlen != NULL)
*prlen = pll;
ardlen = pll;
}
/* Then the checksum */
if (chstype == EX1_CHSUM_MD5) {
icmMD5 *md5;
if ((md5 = new_icmMD5()) == NULL) {
a1logd(p->log, 1, "new_icmMD5 failed\n");
rv = EX1_INTERNAL_ERROR;
goto done;
} else {
ORD8 chsum[16];
int i;
md5->add(md5, buf, 44 + pll);
md5->get(md5, chsum);
for (i = 0; i < 16; i++) {
if (chsum[i] != buf[44+pll+i])
break;
}
if (i < 16) {
a1logd(p->log, 1, "MD5 checksum failed\n");
md5->del(md5);
rv = EX1_DATA_CHSUM_ERROR;
goto done;
}
md5->del(md5);
}
}
/* If we were expecting an exact ammount */
if (prlen == NULL && ardlen != olen) {
a1logd(p->log, 1, "Got %d bytes payload when expecting %d\n",ardlen, olen);
rv = EX1_SHORT_READ;
goto done;
}
/* Finally the footer */
fo = 44 + pll + 16;
if (buf[fo] != 0xC5 || buf[fo + 1] != 0xC4 || buf[fo + 2] != 0xC3 || buf[fo + 3] != 0xC2) {
a1logd(p->log, 1, "Footer error (0x%02x 0x%02x 0x%02x 0x%02x)\n",
buf[fo],buf[fo+1],buf[fo+2],buf[fo+3]);
rv = EX1_DATA_PARSE_ERROR;
goto done;
}
if (p->log->debug >= 7 && out != NULL && olen > 0) {
adump_bytes(p->log, " ", out, 0, olen);
}
done:;
a1logd(p->log,6,"ex1_command: returning 0x%x (%d msec)\n", rv, msec_time()-stime);
return rv;
}
#define EX1_CMD_GET_HW_REV 0x00000080
/* Flush the pipe in case we have an unexpected reply waiting for us. */
/* Note that the instrument will return a measurement initiated from */
/* a previous session if it finishes in this session, i.e. closing */
/* the instrument doesn't abort what it is doing ! */
/* (This helps on MSWin, but stuffs up Linux) */
static void
ex1_flush(
ex1 *p
) {
#ifdef NEVER
char buf[8096];
int debugl = p->log->debug;
p->log->debug = 0;
p->icom->usb_resetep(p->icom, EX1_EP);
p->icom->usb_read(p->icom, NULL, EX1_EP | 0x80, buf, 8096, NULL, 0.1);
ex1_command(p, EX1_CMD_GET_HW_REV, NULL, 0, buf, 1, NULL, 0.1, 0);
p->icom->usb_resetep(p->icom, EX1_EP);
p->log->debug = debugl;
#endif
}
/* ------------------------------------------------------------------- */
/* Implement specific commands */
/* Unimplemented commands/queries */
#define EX1_CMD_RESETDEFAULTS 0x00000001
#define EX1_CMD_GET_NO_USER_STR 0x00000300
#define EX1_CMD_GET_USER_STR_SZ 0x00000301
#define EX1_CMD_GET_USER_STR 0x00000302
/* - - - - - - */
//#define EX1_CMD_GET_HW_REV 0x00000080
/* Return Hardware revision number */
/* Return the ex1 error code. */
static int ex1_get_hw_rev(ex1 *p, int *hwrev) {
ORD8 buf[1];
int ec;
if ((ec = ex1_command(p, EX1_CMD_GET_HW_REV, NULL, 0, buf, 1, NULL, 1.0, 0)) != EX1_OK) {
return ec;
}
*hwrev = read_ORD8(buf);
return EX1_OK;
}
#define EX1_CMD_GET_FW_REV 0x00000090
/* Return Firmware revision number */
/* Return the ex1 error code. */
static int ex1_get_fw_rev(ex1 *p, int *fwrev) {
ORD8 buf[2];
int ec;
if ((ec = ex1_command(p, EX1_CMD_GET_FW_REV, NULL, 0, buf, 2, NULL, 1.0, 0)) != EX1_OK) {
return ec;
}
*fwrev = read_ORD16_le(buf);
return EX1_OK;
}
#define EX1_CMD_GET_SLITW 0x001B0200
/* Get slit width in microns */
/* Return 0 if info. not available */
/* Return the ex1 error code. */
static int ex1_get_slit_width(ex1 *p, int *swidth) {
ORD8 buf[2];
int ec;
if ((ec = ex1_command(p, EX1_CMD_GET_SLITW, NULL, 0, buf, 2, NULL, 1.0, 0)) != EX1_OK) {
if (ec != EX1_NO_INF)
return ec;
*swidth = 0;
} else {
*swidth = read_ORD16_le(buf);
}
return EX1_OK;
}
#define EX1_CMD_GET_FIBW 0x001B0300
/* Get fiber diameter in microns */
/* Return the ex1 error code. */
static int ex1_get_fiber_width(ex1 *p, int *fibw) {
ORD8 buf[2];
int ec;
if ((ec = ex1_command(p, EX1_CMD_GET_FIBW, NULL, 0, buf, 2, NULL, 1.0, 0)) != EX1_OK) {
if (ec != EX1_NO_INF)
return ec;
*fibw = 0;
} else {
*fibw = read_ORD16_le(buf);
}
return EX1_OK;
}
#define EX1_CMD_GET_GRATING 0x001B0400
/* Return grating string, or NULL if none */
/* (free after use) */
/* Return the ex1 error code. */
static int ex1_get_grating(ex1 *p, char **grating) {
int bread;
int len = 32; /* We assume a max of 32 bytes */
int ec;
if ((*grating = malloc(len+1)) == NULL)
return EX1_MEMORY;
if ((ec = ex1_command(p, EX1_CMD_GET_GRATING, NULL, 0, (ORD8 *)*grating, len, &bread, 2.0, 0))
!= EX1_OK) {
if (ec != EX1_NO_INF)
return ec;
free(*grating);
*grating = NULL;
} else {
(*grating)[bread] = '\000';
}
return EX1_OK;
}
#define EX1_CMD_GET_FILTER 0x001B0500
/* Return filter string, or NULL if none */
/* (free after use) */
/* Return the ex1 error code. */
static int ex1_get_filter(ex1 *p, char **filter) {
int bread;
int len = 32; /* We assume a max of 32 bytes */
int ec;
if ((*filter = malloc(len+1)) == NULL)
return EX1_MEMORY;
if ((ec = ex1_command(p, EX1_CMD_GET_FILTER, NULL, 0, (ORD8 *)*filter, len, &bread, 2.0, 0))
!= EX1_OK) {
if (ec != EX1_NO_INF)
return ec;
free(*filter);
*filter = NULL;
} else {
(*filter)[bread] = '\000';
}
return EX1_OK;
}
#define EX1_CMD_GET_COATING 0x001B0600
/* Return coating string, or NULL if none */
/* (free after use) */
/* Return the ex1 error code. */
static int ex1_get_coating(ex1 *p, char **coating) {
int bread;
int len = 32; /* We assume a max of 32 bytes */
int ec;
if ((*coating = malloc(len+1)) == NULL)
return EX1_MEMORY;
if ((ec = ex1_command(p, EX1_CMD_GET_COATING, NULL, 0, (ORD8 *)*coating, len, &bread, 2.0, 0))
!= EX1_OK) {
if (ec != EX1_NO_INF)
return ec;
free(*coating);
*coating = NULL;
} else {
(*coating)[bread] = '\000';
}
return EX1_OK;
}
#define EX1_CMD_GET_ALIAS_LEN 0x00000201
#define EX1_CMD_GET_ALIAS 0x00000200
/* Return device alias string, or NULL if none */
/* (free after use) */
/* Return the ex1 error code. */
static int ex1_get_alias(ex1 *p, char **alias) {
ORD8 buf[3];
int bread;
int len;
int ec;
if ((ec = ex1_command(p, EX1_CMD_GET_ALIAS_LEN, NULL, 0, buf, 1, NULL, 2.0, 0)) != EX1_OK) {
return ec;
}
len = read_ORD8(buf);
if (len == 0) {
*alias = NULL;
return inst_ok;
}
if ((*alias = malloc(len+1)) == NULL) {
return EX1_MEMORY;
}
if ((ec = ex1_command(p, EX1_CMD_GET_ALIAS, NULL, 0, (ORD8 *)*alias, len, &bread, 2.0, 0))
!= EX1_OK) {
if (ec != EX1_NO_INF) {
return ec;
}
free(*alias);
*alias = NULL;
} else {
(*alias)[bread] = '\000';
}
return EX1_OK;
}
#define EX1_CMD_GET_SERNO_LEN 0x00000101
#define EX1_CMD_GET_SERNO 0x00000100
/* Return device serial string, or NULL if none */
/* (free after use) */
/* Return the ex1 error code. */
static int ex1_get_serno(ex1 *p, char **serno) {
ORD8 buf[3];
int bread;
int len;
int ec;
if ((ec = ex1_command(p, EX1_CMD_GET_SERNO_LEN, NULL, 0, buf, 1, NULL, 2.0, 0)) != EX1_OK) {
return ec;
}
len = read_ORD8(buf);
if (len == 0) {
*serno = NULL;
return inst_ok;
}
if ((*serno = malloc(len+1)) == NULL) {
return EX1_MEMORY;
}
if ((ec = ex1_command(p, EX1_CMD_GET_SERNO, NULL, 0, (ORD8 *)*serno, len, &bread, 2.0, 0))
!= EX1_OK) {
if (ec != EX1_NO_INF) {
return ec;
}
free(*serno);
*serno = NULL;
} else {
(*serno)[bread] = '\000';
}
return EX1_OK;
}
/* - - - - - - */
#define EX1_CMD_GET_WL_COEF_COUNT 0x00180100
#define EX1_CMD_GET_WL_COEF 0x00180101
/* Get wavelength cooeficients. */
/* (free after use) */
/* Return the ex1 error code. */
static int ex1_get_wl_coefs(ex1 *p, unsigned int *nocoefs, double **coefs) {
ORD8 buf[4];
unsigned int i, no;
int ec;
if ((ec = ex1_command(p, EX1_CMD_GET_WL_COEF_COUNT, NULL, 0, buf, 1, NULL, 1.0, 0)) != EX1_OK) {
return ec;
}
no = read_ORD8(buf);
/* There should be 4, but 2 is the minimum */
if (no < 2) {
return EX1_NO_WL_CAL;
}
if ((*coefs = malloc(sizeof(double) * no)) == NULL) {
return EX1_MEMORY;
}
for (i = 0; i < no; i++) {
write_ORD8(buf, i);
if ((ec = ex1_command(p, EX1_CMD_GET_WL_COEF, buf, 1, buf, 4, NULL, 1.0, 0)) != EX1_OK) {
*nocoefs = 0;
free(*coefs);
return ec;
}
(*coefs)[i] = IEEE754todouble(read_ORD32_le(buf));
}
*nocoefs = no;
if (p->log->debug >= 6) {
a1logd(p->log,1,"ex1: no. wavelength calib coefs = %d\n",no);
for (i = 0; i < no; i++) {
a1logd(p->log,1," [%d] = %e\n",i,(*coefs)[i]);
}
}
return EX1_OK;
}
#define EX1_CMD_GET_NL_COEF_COUNT 0x00181100
#define EX1_CMD_GET_NL_COEF 0x00181101
/* Get non-linearity coefficient */
/* (free after use) */
/* Return the ex1 error code. */
static int ex1_get_nl_coefs(ex1 *p, unsigned int *nocoefs, double **coefs) {
ORD8 buf[4];
unsigned int i, no;
int ec;
if ((ec = ex1_command(p, EX1_CMD_GET_NL_COEF_COUNT, NULL, 0, buf, 1, NULL, 1.0, 0)) != EX1_OK) {
return ec;
}
no = read_ORD8(buf);
if (no == 0) {
*nocoefs = 0;
*coefs = NULL;
return EX1_OK;
}
if ((*coefs = malloc(sizeof(double) * no)) == NULL) {
return EX1_MEMORY;
}
for (i = 0; i < no; i++) {
write_ORD8(buf, i);
if ((ec = ex1_command(p, EX1_CMD_GET_NL_COEF, buf, 1, buf, 4, NULL, 1.0, 0)) != EX1_OK) {
free(*coefs);
*nocoefs = 0;
*coefs = NULL;
return ec;
}
(*coefs)[i] = IEEE754todouble(read_ORD32_le(buf));
}
*nocoefs = no;
if (p->log->debug >= 6) {
a1logd(p->log,1,"ex1: no. linearity calib coefs = %d\n",no);
for (i = 0; i < no; i++) {
a1logd(p->log,1," [%d] = %e\n",i,(*coefs)[i]);
}
}
return EX1_OK;
}
#define EX1_CMD_GET_IR_COEF_COUNT 0x00182002
#define EX1_CMD_GET_IR_COEF 0x00182001
#define EX1_CMD_GET_IR_AREA 0x00182003
/* Get Irradiance calibration coefficients and collection area */
/* (free after use) */
/* Return the ex1 error code. */
static int ex1_get_ir_coefs(ex1 *p, unsigned int *nocoefs, double **coefs, double *area) {
ORD8 buf[4], *tbuf;
unsigned int i, no;
int ec;
if ((ec = ex1_command(p, EX1_CMD_GET_IR_COEF_COUNT, NULL, 0, buf, 4, NULL, 1.0, 0)) != EX1_OK) {
return ec;
}
no = read_ORD32_le(buf);
if (no == 0) {
*nocoefs = 0;
*coefs = NULL;
return EX1_NO_IR_CAL;
}
if ((tbuf = malloc(4 * no)) == NULL) {
return EX1_MEMORY;
}
if ((*coefs = malloc(sizeof(double) * no)) == NULL) {
free(tbuf);
return EX1_MEMORY;
}
if ((ec = ex1_command(p, EX1_CMD_GET_IR_COEF, NULL, 0, tbuf, 4 * no, NULL, 1.0, 0)) != EX1_OK) {
free(*coefs);
*nocoefs = 0;
*coefs = NULL;
*area = 0.0;
return ec;
}
for (i = 0; i < no; i++) {
(*coefs)[i] = IEEE754todouble(read_ORD32_le(tbuf + 4 * i));
}
*nocoefs = no;
free(tbuf);
if (p->log->debug >= 6) {
a1logd(p->log,1,"ex1: no. Irradiance calib coefs = %d\n",no);
for (i = 0; (i+3) < no; i += 4) {
a1logd(p->log,1," [%d] = %e, %e %e %e\n",i,
(*coefs)[i], (*coefs)[i+1], (*coefs)[i+2], (*coefs)[i+3]);
}
}
if ((ec = ex1_command(p, EX1_CMD_GET_IR_AREA, NULL, 0, buf, 4, NULL, 1.0, 0)) != EX1_OK) {
if (ec != EX1_NO_INF) {
return ec;
}
*area = 0.0;
} else {
*area = IEEE754todouble(read_ORD32_le(buf));
}
a1logd(p->log,1,"ex1: Irradiance collection area = %f\n",*area);
return EX1_OK;
}
#define EX1_CMD_GET_SL_COEF_COUNT 0x00183100
#define EX1_CMD_GET_SL_COEF 0x00183101
/* Get stray light coefficient */
/* (free after use) */
/* Return the ex1 error code. */
static int ex1_get_sl_coefs(ex1 *p, unsigned int *nocoefs, double **coefs) {
ORD8 buf[4];
unsigned int i, no;
int ec;
if ((ec = ex1_command(p, EX1_CMD_GET_SL_COEF_COUNT, NULL, 0, buf, 1, NULL, 1.0, 0)) != EX1_OK) {
return ec;
}
no = read_ORD8(buf);
if (no == 0) {
*nocoefs = 0;
*coefs = NULL;
return EX1_OK;
}
if ((*coefs = malloc(sizeof(double) * no)) == NULL) {
return EX1_MEMORY;
}
for (i = 0; i < no; i++) {
write_ORD8(buf, i);
if ((ec = ex1_command(p, EX1_CMD_GET_SL_COEF, buf, 1, buf, 4, NULL, 1.0, 0)) != EX1_OK) {
free(*coefs);
*nocoefs = 0;
*coefs = NULL;
if (ec != EX1_NO_INF)
return ec;
else
return EX1_OK;
}
(*coefs)[i] = IEEE754todouble(read_ORD32_le(buf));
}
*nocoefs = no;
if (p->log->debug >= 6) {
a1logd(p->log,1,"ex1: no. stray light calib coefs = %d\n",no);
for (i = 0; i < no; i++) {
a1logd(p->log,1," [%d] = %e\n",i,(*coefs)[i]);
}
}
return EX1_OK;
}
/* - - - - - - */
#define EX1_CMD_SET_TRIGMODE 0x00110110
/* Set trigger mode */
static int ex1_set_trig_mode(ex1 *p, int trigmode) {
ORD8 buf[1];
int ec;
write_ORD8(buf, trigmode);
if ((ec = ex1_command(p, EX1_CMD_SET_TRIGMODE, buf, 1, NULL, 0, NULL, 1.0, 0)) != EX1_OK) {
return ec;
}
return EX1_OK;
}
#define EX1_CMD_SET_INTTIME 0x00110010
/* Set integration time, min 10 usec, max 10 sec */
/* Return qualtized integration time in qinttime if != NULL */
static int ex1_set_inttime(ex1 *p, double *qinttime, double inttime) {
ORD8 buf[4];
unsigned int iinttime;
int ec;
inttime = floor(inttime * 1e6 + 0.5); /* To int usec */
if (inttime < (EX1_INTTIME_MIN * 1e6) || inttime > (EX1_INTTIME_MAX * 1e6))
return EX1_INTTIME_RANGE;
iinttime = (unsigned int)inttime;
write_ORD32_le(buf, iinttime);
if ((ec = ex1_command(p, EX1_CMD_SET_INTTIME, buf, 4, NULL, 0, NULL, 1.0, 0)) != EX1_OK) {
return ec;
}
if (qinttime != NULL)
*qinttime = inttime/1e6;
return EX1_OK;
}
#define EX1_CMD_SET_DELTIME 0x00110510
/* Set trigger delay time, min 5 usec, max 355.5 msec */
/* Return quantized trigger delay time in qtrigdel if != NULL */
static int ex1_set_trig_delay(ex1 *p, double *qtrigdel, double trigdel) {
ORD8 buf[4];
unsigned int itrigdel;
int ec;
trigdel = floor(trigdel * 1e6 + 0.5); /* To int usec */
if (trigdel < (EX1_DELTIME_MIN * 1e6) || trigdel > (EX1_DELTIME_MAX * 1e6))
return EX1_DELTIME_RANGE;
itrigdel = (unsigned int)trigdel;
write_ORD32_le(buf, itrigdel);
if ((ec = ex1_command(p, EX1_CMD_SET_DELTIME, buf, 4, NULL, 0, NULL, 1.0, 0)) != EX1_OK) {
return ec;
}
if (qtrigdel != NULL)
*qtrigdel = trigdel/1e6;
return EX1_OK;
}
#define EX1_CMD_SET_STRBPER 0x00310010
/* Set continuous strobe period, min 50 usec, max 5 sec */
/* Return quantized strobe period in qinttime if != NULL */
static int ex1_set_strobe_period(ex1 *p, double *qstbper, double stbper) {
ORD8 buf[4];
unsigned int istbper;
int ec;
stbper = floor(stbper * 1e6 + 0.5); /* To int usec */
if (stbper < (EX1_STRBPER_MIN * 1e6) || stbper > (EX1_STRBPER_MAX * 1e6))
return EX1_STROBE_RANGE;
istbper = (unsigned int)stbper;
write_ORD32_le(buf, istbper);
if ((ec = ex1_command(p, EX1_CMD_SET_STRBPER, buf, 4, NULL, 0, NULL, 1.0, 0)) != EX1_OK) {
return ec;
}
if (qstbper != NULL)
*qstbper = stbper/1e6;
return EX1_OK;
}
#define EX1_CMD_SET_STRBEN 0x00310011
/* Set continuous strobe enable/disable mode */
static int ex1_set_strobe_enable(ex1 *p, int enable) {
ORD8 buf[1];
int ec;
write_ORD8(buf, enable);
if ((ec = ex1_command(p, EX1_CMD_SET_STRBEN, buf, 1, NULL, 0, NULL, 1.0, 0)) != EX1_OK) {
return ec;
}
return EX1_OK;
}
#define EX1_CMD_SET_SING_STRBEN 0x00300012
/* Set single strobe enable/disable mode */
static int ex1_set_single_strobe_enable(ex1 *p, int enable) {
ORD8 buf[1];
int ec;
write_ORD8(buf, enable);
if ((ec = ex1_command(p, EX1_CMD_SET_SING_STRBEN, buf, 1, NULL, 0, NULL, 1.0, 0)) != EX1_OK) {
return ec;
}
return EX1_OK;
}
#define EX1_CMD_SET_AVERAGE 0x00120010
/* Set number of scans to average, range 1 - 5000 */
static int ex1_set_average(ex1 *p, int noavg) {
ORD8 buf[2];
int ec;
if (noavg < EX1_AVERAGE_MIN || noavg > EX1_AVERAGE_MAX)
return EX1_AVERAGE_RANGE;
write_ORD16_le(buf, noavg);
if ((ec = ex1_command(p, EX1_CMD_SET_AVERAGE, buf, 2, NULL, 0, NULL, 1.0, 0)) != EX1_OK) {
return ec;
}
p->noaverage = noavg;
return EX1_OK;
}
#define EX1_CMD_SET_BINNING 0x00110290
/* Set binning factor. */
static int ex1_set_binning(ex1 *p, int bf) {
ORD8 buf[1];
int ec;
write_ORD8(buf, bf);
if ((ec = ex1_command(p, EX1_CMD_SET_BINNING, buf, 1, NULL, 0, NULL, 1.0, 0)) != EX1_OK) {
return ec;
}
return EX1_OK;
}
#define EX1_CMD_SET_BOXCAR 0x00121010
/* Set boxcar filtering width, 0 - 15 */
static int ex1_set_boxcar(ex1 *p, int nobox) {
ORD8 buf[1];
int ec;
if (nobox < EX1_BOXCAR_MIN || nobox > EX1_BOXCAR_MAX)
return EX1_BOXCAR_RANGE;
write_ORD8(buf, nobox);
if ((ec = ex1_command(p, EX1_CMD_SET_BOXCAR, buf, 1, NULL, 0, NULL, 1.0, 0)) != EX1_OK) {
return ec;
}
return EX1_OK;
}
/* - - - - - - */
#define EX1_CMD_GET_COR_SPEC 0x00101000
/* Get corrected spectrum (temp and pattern noise corrected). */
/* raw must be double[1024] */
/* Return the ex1 error code. */
static int ex1_measure(ex1 *p, double *raw) {
ORD8 buf[2048];
unsigned int i, no = 1024;
int ec;
double to;
int stime;
to = 1.0 + p->noaverage * (MIN_CYCLE_TIME + p->inttime) * 1.1;
//printf("~1 ex1_measure: to = %f from %d x %f\n",to,p->noaverage,p->inttime);
stime = msec_time();
if ((ec = ex1_command(p, EX1_CMD_GET_COR_SPEC, NULL, 0, buf, no * 2, NULL, to, 0)) != EX1_OK) {
return ec;
}
//printf("Measure took %d msec\n", msec_time()-stime);
for (i = 0; i < no; i++)
raw[i] = (double)read_ORD16_le(buf + 2 * i);
if (p->log->debug >= 6) {
a1logd(p->log,1,"ex1: spectrum:\n");
for (i = 0; (i+3) < no; i += 4) {
a1logd(p->log,1," [%d] = %.0f, %.0f %.0f %.0f\n",
i, raw[i], raw[i+1], raw[i+2], raw[i+3]);
}
}
return EX1_OK;
}
/* - - - - - - */
/* Return command description */
static char *cmdtstring(unsigned int cmd) {
switch (cmd) {
case EX1_CMD_RESETDEFAULTS:
return "Reset to defaults";
case EX1_CMD_GET_NO_USER_STR:
return "Get number of user strings";
case EX1_CMD_GET_USER_STR_SZ:
return "Get maximum user string size";
case EX1_CMD_GET_USER_STR:
return "Get user string";
case EX1_CMD_GET_HW_REV:
return "Get Hardware Revision";
case EX1_CMD_GET_FW_REV:
return "Get Firmware revision";
case EX1_CMD_GET_ALIAS_LEN:
return "Get Alias string length";
case EX1_CMD_GET_ALIAS:
return "Get Alias string";
case EX1_CMD_GET_SERNO_LEN:
return "Get Serial number length";
case EX1_CMD_GET_SERNO:
return "Get Serial number";
case EX1_CMD_GET_SLITW:
return "Get Slit width";
case EX1_CMD_GET_FIBW:
return "Get Fiber width";
case EX1_CMD_GET_GRATING:
return "Get Grating string";
case EX1_CMD_GET_WL_COEF_COUNT:
return "Get Wavelenght coefficient count";
case EX1_CMD_GET_WL_COEF:
return "Get Wavelenght coefficient";
case EX1_CMD_GET_NL_COEF_COUNT:
return "Get Linearity coefficient count";
case EX1_CMD_GET_NL_COEF:
return "Get Linearity coefficient";
case EX1_CMD_GET_IR_COEF_COUNT:
return "Get Irradiance coefficient count";
case EX1_CMD_GET_IR_COEF:
return "Get Irradiance coefficient";
case EX1_CMD_GET_IR_AREA:
return "Get Irradiance collection area";
case EX1_CMD_GET_SL_COEF_COUNT:
return "Get Stray light coefficient count";
case EX1_CMD_GET_SL_COEF:
return "Get Stray light coefficient";
case EX1_CMD_SET_TRIGMODE:
return "Set Trigger mode";
case EX1_CMD_SET_INTTIME:
return "Set Intergration time";
case EX1_CMD_SET_DELTIME:
return "Set Trigger delay time";
case EX1_CMD_SET_STRBPER:
return "Set Strobe period";
case EX1_CMD_SET_STRBEN:
return "Set Strobe enable";
case EX1_CMD_SET_SING_STRBEN:
return "Set Single Strobe enable";
case EX1_CMD_SET_AVERAGE:
return "Set Averaging count";
case EX1_CMD_SET_BINNING:
return "Set Binning multiple";
case EX1_CMD_SET_BOXCAR:
return "Set Boxcar filtering width";
case EX1_CMD_GET_COR_SPEC:
return "Measure corrected spectral values";
default:
return "Unknown";
}
}
|