IGSTK
igstkNDICommandInterpreter.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Image Guided Surgery Software Toolkit
4  Module: $RCSfile: igstkNDICommandInterpreter.h,v $
5  Language: C++
6  Date: $Date: 2008-02-11 01:41:51 $
7  Version: $Revision: 1.16 $
8 
9  Copyright (c) ISC Insight Software Consortium. All rights reserved.
10  See IGSTKCopyright.txt or http://www.igstk.org/copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 
18 #ifndef __igstkNDICommandInterpreter_h
19 #define __igstkNDICommandInterpreter_h
20 
21 #include "igstkObject.h"
23 #include "igstkNDIErrorEvent.h"
24 
25 namespace igstk
26 {
27 
44 {
45 public:
46 
49 
50 public:
51 
64  typedef enum
65  {
66  NDI_OKAY = 0x00,
67  NDI_INVALID = 0x01,
68  NDI_TOO_LONG = 0x02,
69  NDI_TOO_SHORT = 0x03,
70  NDI_BAD_COMMAND_CRC = 0x04,
71  NDI_INTERN_TIMEOUT = 0x05,
72  NDI_COMM_FAIL = 0x06,
73  NDI_PARAMETERS = 0x07,
74  NDI_INVALID_PORT = 0x08,
75  NDI_INVALID_MODE = 0x09,
76  NDI_INVALID_LED = 0x0a,
77  NDI_LED_STATE = 0x0b,
78  NDI_BAD_MODE = 0x0c,
79  NDI_NO_TOOL = 0x0d,
80  NDI_PORT_NOT_INIT = 0x0e,
81  NDI_PORT_DISABLED = 0x0f,
82  NDI_INITIALIZATION = 0x10,
83  NDI_TSTOP_FAIL = 0x11,
84  NDI_TSTART_FAIL = 0x12,
85  NDI_PINIT_FAIL = 0x13,
86  NDI_CAMERA = 0x14,
87  NDI_INIT_FAIL = 0x15,
88  NDI_DSTART_FAIL = 0x16,
89  NDI_DSTOP_FAIL = 0x17,
90  NDI_IRCHK_FAIL = 0x18,
91  NDI_FIRMWARE = 0x19,
92  NDI_INTERNAL = 0x1a,
93  NDI_IRINIT_FAIL = 0x1b,
94  NDI_IRED_FAIL = 0x1c,
95  NDI_SROM_FAIL = 0x1d,
96  NDI_SROM_READ = 0x1e,
97  NDI_SROM_WRITE = 0x1f,
98  NDI_SROM_SELECT = 0x20,
99  NDI_PORT_CURRENT = 0x21,
100  NDI_WAVELENGTH = 0x22,
101  NDI_PARAMETER_RANGE = 0x23,
102  NDI_VOLUME = 0x24,
103  NDI_FEATURES = 0x25,
104  NDI_ERROR_0X26 = 0x26,
105  NDI_ERROR_0X27 = 0x27,
106  NDI_SCU_STATE = 0x28,
107  NDI_CORRUPT = 0x29,
108  NDI_MEMORY = 0x2A,
109  NDI_NOT_ALLOCATED = 0x2B,
110  NDI_NOT_OCCUPIED = 0x2C,
111  NDI_NO_HANDLES = 0x2D,
112  NDI_INCOMPATIBLE = 0x2E,
113  NDI_DESCRIPTION = 0x2F,
114  NDI_ASSIGNED = 0x30,
115  NDI_INVALID_STATE = 0x31,
116  NDI_OPERATION = 0x32,
117  NDI_FEATURE = 0x33,
119  NDI_ENVIRONMENT = 0xf1,
121  NDI_EPROM_ERASE = 0xf4,
122  NDI_EPROM_WRITE = 0xf5,
123  NDI_EPROM_READ = 0xf6,
125  /* error codes generated by the host, rather than by the device */
126 
127  NDI_BAD_CRC = 0x0100,
128  NDI_OPEN_ERROR = 0x0200,
129  NDI_BAD_COMM = 0x0300,
130  NDI_TIMEOUT = 0x0400,
131  NDI_WRITE_ERROR = 0x0500,
132  NDI_READ_ERROR = 0x0600,
133  NDI_RESET_FAIL = 0x0700,
134  NDI_PROBE_FAIL = 0x0800,
136 
138  typedef enum
139  {
140  NDI_9600 = 0,
141  NDI_14400 = 1,
146  } COMMBaudType;
147 
149  typedef enum
150  {
151  NDI_8N1 = 0, /* 8 data bits, no parity, 1 stop bit */
152  NDI_8N2 = 1, /* etc. */
153  NDI_8O1 = 10,
154  NDI_8O2 = 11,
155  NDI_8E1 = 20,
156  NDI_8E2 = 21,
157  NDI_7N1 = 100,
158  NDI_7N2 = 101,
159  NDI_7O1 = 110,
160  NDI_7O2 = 111,
161  NDI_7E1 = 120,
162  NDI_7E2 = 121 ,
163  } COMMDataType;
164 
166  typedef enum
167  {
171 
173  typedef enum
174  {
181  } VERModeType;
182 
184  typedef enum
185  {
191  } PHSRModeType;
192 
194  typedef enum
195  {
196  NDI_STATIC = 'S',
197  NDI_DYNAMIC = 'D',
200 
202  typedef enum
203  {
206  NDI_SINGLE_STRAY = 0x0004,
208  NDI_PASSIVE_STRAY = 0x1000,
209  } TXModeType;
210 
212  typedef enum
213  {
214  NDI_UNOCCUPIED = 0x00,
215  NDI_VALID = 0x01,
216  NDI_MISSING = 0x02,
217  NDI_DISABLED = 0x04,
218  } TXTransformType;
219 
221  typedef enum
222  {
224  NDI_SWITCH_1_ON = 0x0002,
225  NDI_SWITCH_2_ON = 0x0004,
226  NDI_SWITCH_3_ON = 0x0008,
227  NDI_INITIALIZED = 0x0010,
228  NDI_ENABLED = 0x0020,
237 
239  typedef enum
240  {
250 
252  typedef enum
253  {
257  } TXToolInfoType;
258 
260  typedef enum
261  {
267 
269  typedef enum
270  {
271  NDI_BLANK = 'B',
272  NDI_FLASH = 'F',
273  NDI_SOLID = 'S',
274  } LEDStateType;
275 
277  typedef enum
278  {
279  NDI_GPIO_OFF = 'O',
283  } PSOUTStateType;
284 
286  typedef enum
287  {
288  NDI_BASIC = 0x0001,
289  NDI_TESTING = 0x0002,
290  NDI_PART_NUMBER = 0x0004,
291  NDI_ACCESSORIES = 0x0008,
292  NDI_MARKER_TYPE = 0x0010,
294  NDI_GPIO_STATUS = 0x0040,
295  } PHINFModeType;
296 
298  typedef enum
299  {
301  NDI_SWITCH_1 = 0x02,
302  NDI_SWITCH_2 = 0x04,
303  NDI_SWITCH_3 = 0x08,
305  NDI_LED_1 = 0x20,
306  NDI_LED_2 = 0x40,
307  NDI_LED_3 = 0x80,
309 
311  typedef enum
312  {
313  NDI_950NM = 0x00,
314  NDI_850NM = 0x01,
320  } PHINFMarkerType;
321 
323  typedef enum
324  {
336 
338  typedef enum
339  {
340  NDI_CONTROL = 0x0001,
341  NDI_SENSORS = 0x0002,
342  NDI_TIU = 0x0004,
343  } SSTATModeType;
344 
346  typedef enum
347  {
351 
353  typedef enum
354  {
361  } STATSensorType;
362 
364  typedef enum
365  {
369  NDI_SYNC = 0x08,
372  } STATTIUType;
373 
375  typedef enum
376  {
377  NDI_DETECTED = 0x0001,
378  NDI_SOURCES = 0x0002,
379  } IRCHKModeType;
380 
382  typedef enum
383  {
384  NDI_LEFT = 0,
385  NDI_RIGHT = 1,
386  } IRCHKSensorType;
387 
389  typedef enum
390  {
394  NDI_VOLUMES = 0x03,
395  NDI_TIP_PORTS = 0x04,
396  } SFLISTModeType;
397 
399  typedef enum
400  {
406 
409 
411  void SetCommunication(CommunicationType* communication);
412 
415 
444  const char *Command(const char *command);
445 
450  void BEEP(int n) {this->Command("BEEP:%i", n % 10); }
451 
463  void COMM(COMMBaudType baud, COMMDataType dps, COMMHandshakeType handshake) {
464  this->Command("COMM:%d%03d%d", baud, dps, handshake); }
465 
469  void DSTART() {
470  this->Command("DSTART:"); }
471 
473  void DSTOP() {
474  this->Command("DSTOP:"); }
475 
478  void INIT() {
479  this->Command("INIT:"); }
480 
490  void IRCHK(int mode) {
491  this->Command("IRCHK:%04X", mode); }
492 
495  void IRINIT() {
496  this->Command("IRINIT:"); }
497 
505  void LED(int ph, int led, LEDStateType state) {
506  this->Command("LED:%02X%d%c", ph, led, state); }
507 
510  void PDIS(int ph) {
511  this->Command("PDIS:%02X", ph); }
512 
517  void PENA(int ph, int mode) {
518  this->Command("PENA:%02X%c", ph, mode); }
519 
522  void PHF(int ph) {
523  this->Command("PHF:%02X", ph); }
524 
547  void PHINF(int ph, int mode) {
548  this->Command("PHINF:%02X%04X", ph, mode); }
549 
561  void PHRQ(const char* num, const char* sys, const char* tool,
562  const char* port, const char* chan)
563  {
564  this->Command("PHRQ:%-8.8s%1.1s%1.1s%2.2s%2.2s", num, sys, tool,
565  port, chan);
566  }
567 
583  void PHSR(PHSRModeType mode) {
584  this->Command("PHSR:%02X", mode); }
585 
589  void PINIT(int ph) {
590  this->Command("PINIT:%02X", ph); }
591 
600  void PSOUT(int ph, int a, int b, int c) {
601  this->Command("PSOUT:%02X%c%c%c", ph, a, b, c); };
602 
607  void PVCLR(int port) {
608  this->Command("PVCLR:%c", port); }
609 
616  void PVWR(int ph, int a, const char* x) {
617  this->Command("PVWR:%02X%04X%.128s", ph, a, x); }
618 
621  void RESET() {
622  this->Command(0); }
623 
640  void SFLIST(SFLISTModeType mode) {
641  this->Command("SFLIST:%02X", mode); }
642 
657  void SSTAT(int mode) {
658  this->Command("SSTAT:%04X", mode); }
659 
661  void TSTART() {
662  this->Command("TSTART:"); }
663 
665  void TSTOP() {
666  this->Command("TSTOP:"); }
667 
689  void TX(int mode) {
690  this->Command("TX:%04X", mode); }
691 
715  void BX(int mode) {
716  this->Command("BX:%04X", mode); }
717 
727  void VER(VERModeType n) {
728  this->Command("VER:%d", n); }
729 
733  int GetError() const;
734 
738  int GetPHRQHandle() const;
739 
742  int GetPHSRNumberOfHandles() const;
743 
752  int GetPHSRHandle(int i) const;
753 
770  int GetPHSRInformation(int i) const;
771 
785  int GetPHINFPortStatus() const;
786 
805  int GetPHINFToolInfo(char information[32]) const;
806 
824  int GetPHINFToolType() const;
825 
833  unsigned int GetPHINFCurrentTest() const;
834 
849  int GetPHINFPartNumber(char part[21]) const;
850 
868  int GetPHINFAccessories() const;
869 
887  int GetPHINFMarkerType() const;
888 
902  int GetPHINFPortLocation(char location[15]) const;
903 
910  int GetPHINFGPIOStatus() const;
911 
931  int GetTXTransform(int ph, double transform[8]) const;
932 
949  int GetTXPortStatus(int ph) const;
950 
959  unsigned int GetTXFrame(int ph) const;
960 
972  int GetTXToolInfo(int ph) const;
973 
987  int GetTXMarkerInfo(int ph, int marker) const;
988 
1004  int GetTXSingleStray(int ph, double coord[3]) const;
1005 
1012  int GetTXNumberOfPassiveStrays() const;
1013 
1029  int GetTXPassiveStray(int i, double coord[3]) const;
1030 
1045  int GetTXPassiveStrayOutOfVolume(int i) const;
1046 
1062  int GetTXSystemStatus() const;
1063 
1084  int GetBXTransform(int ph, double transform[8]) const;
1085 
1107  int GetBXPortStatus(int ph) const;
1108 
1117  unsigned int GetBXFrame(int ph) const;
1118 
1130  int GetBXToolInfo(int ph) const;
1131 
1145  int GetBXMarkerInfo(int ph, int marker) const;
1146 
1162  int GetBXSingleStray(int ph, double coord[3]) const;
1163 
1169  int GetBXNumberOfPassiveStrays() const;
1170 
1185  int GetBXPassiveStray(int i, double coord[3]) const;
1186 
1201  int GetBXPassiveStrayOutOfVolume(int i) const;
1202 
1218  int GetBXSystemStatus() const;
1219 
1228  int GetSSTATControl() const;
1229 
1242  int GetSSTATSensors() const;
1243 
1256  int GetSSTATTIU() const;
1257 
1259  const char *GetVERText() const;
1260 
1267  int GetIRCHKDetected() const;
1268 
1278  int GetIRCHKNumberOfSources(int side) const;
1279 
1295  int GetIRCHKSourceXY(int side, int i, double xy[2]) const;
1296 
1300  static const char* ErrorString(int errnum);
1301 
1312  static char* HexEncode(char* cp, const void* data, int n);
1313 
1321  static void* HexDecode(void* data, const char* cp, int n);
1322 
1323 protected:
1326 
1328  virtual ~NDICommandInterpreter();
1329 
1331  virtual void PrintSelf( std::ostream& os, itk::Indent indent ) const;
1332 
1333 private:
1334 
1336  itkStaticConstMacro( NDI_MAX_HANDLES, int, 24 );
1337 
1339  CommunicationType::Pointer m_Communication;
1340 
1342  char *m_CommandReply; /* reply without CRC and <CR> */
1343  char *m_SerialCommand; /* raw text to send to device */
1344  char *m_SerialReply; /* raw reply from device */
1345 
1347  bool m_Tracking; /* 'is tracking' flag */
1348 
1350  int m_ErrorCode; /* error code (zero if no error) */
1351 
1353  char m_SSTATControl[2]; /* control processor status */
1354  char m_SSTATSensor[2]; /* sensor processors status */
1355  char m_SSTATTIU[2]; /* tiu processor status */
1356 
1358  int m_IRCHKDetected; /* irchk detected infrared */
1359  char m_IRCHKSources[128]; /* coordinates of sources */
1360 
1362  char m_PHRQReply[2];
1363 
1365  char m_PHSRReply[1284];
1366 
1368  int m_PHINFOccupied;
1369  char m_PHINFBasic[34];
1370  char m_PHINFTesting[8];
1371  char m_PHINFPartNumber[20];
1372  char m_PHINFAccessories[2];
1373  char m_PHINFMarkerType[2];
1374  char m_PHINFPortLocation[14];
1375  char m_PHINFGPIOStatus[2];
1376 
1378  int m_TXNumberOfHandles;
1379  unsigned char m_TXHandles[NDI_MAX_HANDLES];
1380  unsigned char m_TXHandleStatus[NDI_MAX_HANDLES];
1381  char m_TXSystemStatus[4];
1382 
1384  char m_TXTransforms[NDI_MAX_HANDLES][52];
1385  char m_TXPortStatus[NDI_MAX_HANDLES][8];
1386  char m_TXFrame[NDI_MAX_HANDLES][8];
1387 
1389  char m_TXInformation[NDI_MAX_HANDLES][12];
1390 
1392  char m_TXSingleStray[NDI_MAX_HANDLES][24];
1393 
1395  int m_TXNumberOfPassiveStrays;
1396  char m_TXPassiveStrayOutOfVolume[14];
1397  char m_TXPassiveStray[1052];
1398 
1400  int m_BXNumberOfHandles;
1401  unsigned char m_BXHandles[NDI_MAX_HANDLES];
1402  unsigned char m_BXHandleStatus[NDI_MAX_HANDLES];
1403  unsigned short m_BXSystemStatus;
1404 
1406  float m_BXTransforms[NDI_MAX_HANDLES][8];
1407  unsigned int m_BXPortStatus[NDI_MAX_HANDLES];
1408  unsigned int m_BXFrame[NDI_MAX_HANDLES];
1409 
1411  unsigned char m_BXToolInformation[NDI_MAX_HANDLES];
1412  unsigned char m_BXMarkerInformation[NDI_MAX_HANDLES][20];
1413 
1415  unsigned char m_BXSingleStrayStatus[NDI_MAX_HANDLES];
1416  float m_BXSingleStrayPosition[NDI_MAX_HANDLES][3];
1417 
1419  int m_BXNumberOfPassiveStrays;
1420  unsigned char m_BXPassiveStrayOutOfVolume[7];
1421  float m_BXPassiveStrayPosition[50][3];
1422 
1424  char m_VERText[1028];
1425 
1427  const char* Command(const char* format, int a);
1429  const char* Command(const char* format, int a, int b);
1431  const char* Command(const char* format, int a, int b, int c);
1433  const char* Command(const char* format, int a, int b, int c, int d);
1435  const char* Command(const char* format, int a, int b, const char* c);
1437  const char* Command(const char* format, const char* a, const char* b,
1438  const char* c, const char* d, const char* e);
1439 
1441  int WriteSerialBreak();
1442 
1446  int WriteCommand(unsigned int *nc);
1447 
1453  int ReadBinaryReply(unsigned int offset);
1454 
1460  int ReadAsciiReply(unsigned int offset);
1461 
1463  int SetErrorCode(int errnum);
1464 
1468  void HelperForCOMM(const char* cp, const char* crp);
1469  void HelperForPHINF(const char* cp, const char* crp);
1470  void HelperForPHRQ(const char* cp, const char* crp);
1471  void HelperForPHSR(const char* cp, const char* crp);
1472  void HelperForTX(const char* cp, const char* crp);
1473  void HelperForBX(const char* cp, const char* crp);
1474  void HelperForIRCHK(const char* cp, const char* crp);
1475  void HelperForSSTAT(const char* cp, const char* crp);
1476  void HelperForVER(const char* cp, const char* crp);
1477 
1480  int TXIndexFromPortHandle(int ph, int *ip) const;
1481 
1487  static unsigned int HexadecimalStringToUnsignedInt(const char* cp, int n);
1488 
1494  static int HexadecimalStringToInt(const char* cp, int n);
1495 
1501  static int StringToInt(const char* cp, int n);
1502 
1509  static int SignedStringToInt(const char* cp, int n);
1510 
1513  int BXIndexFromPortHandle(int ph, int *ip) const;
1514 
1517  static unsigned char BinaryToUnsignedChar(const char *cp) {
1518  const unsigned char *ucp = (const unsigned char *)cp;
1519  return ucp[0]; }
1520 
1523  static unsigned short BinaryToUnsignedShort(const char *cp) {
1524  const unsigned char *ucp = (const unsigned char *)cp;
1525  return ((ucp[1] << 8) | ucp[0]); }
1526 
1528  static unsigned int BinaryToUnsignedInt(const char *cp) {
1529  const unsigned char *ucp = (const unsigned char *)cp;
1530  return (((ucp[3] << 8) | ucp[2]) << 16) | ((ucp[1] << 8) | ucp[0]); }
1531 
1533  static float BinaryToFloat(const char *cp) {
1534  const unsigned char *ucp = (const unsigned char *)cp;
1535  union { float f; unsigned int i; } u;
1536  u.i = (((ucp[3] << 8) | ucp[2]) << 16) | ((ucp[1] << 8) | ucp[0]);
1537  return u.f; }
1538 
1539 
1540  NDICommandInterpreter(const Self&); //purposely not implemented
1541  void operator=(const Self&); //purposely not implemented
1542 };
1543 
1544 }
1545 #endif