Typica is a free program for professional coffee roasters. https://typica.us
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

phidget22.w 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. @** Phidget22 library.
  2. \noindent Around the same time as the \pn{} 1.8.0 release, Phidgets released a
  3. new hardware communications library with no regard for compatibility with
  4. existing software and poor communication around that fact. They did, however,
  5. provide hardware specimen for testing that requires the new library.
  6. API differences are significant enough that it makes more sense to write new
  7. code for interacting with phidget22 than attempting to retrofit existing
  8. phidget21 code. By leaving both in, there is no configuration disription for
  9. people already using hardware previously supported and it is possible to use
  10. both libraries simultaneously to communicate with different hardware.
  11. The option to configure devices with this library should only be shown if the
  12. library is installed. The library name is different on a Mac.
  13. @<Register top level device configuration nodes@>=
  14. #if __APPLE__
  15. QLibrary phidget22check("Phidget22.framework/Phidget22");
  16. #else
  17. QLibrary phidget22check("phidget22");
  18. #endif
  19. if(phidget22check.load())
  20. {
  21. inserter = new NodeInserter(tr("Phidget22"), tr("Phidget22"), "phidget22",
  22. NULL);
  23. topLevelNodeInserters.append(inserter);
  24. phidget22check.unload();
  25. }
  26. @ A top level configuration is used to group channels using this library while
  27. child nodes provide channel configuration. The configuration widgets need to be
  28. registered so they can be instantiated as appropriate.
  29. @<Register device configuration widgets@>=
  30. app.registerDeviceConfigurationWidget("phidget22",
  31. PhidgetConfWidget::staticMetaObject);
  32. app.registerDeviceConfigurationWidget("phidgetchannel",
  33. PhidgetChannelConfWidget::staticMetaObject);
  34. @ The first configuration widget just serves as a parent to all channels using
  35. this library. There does not seem to be a need for the configuration to mirror
  36. how the hardware is connected, so this serves as a parent node for any number
  37. of devices connected either directly or through a hub.
  38. @<Class declarations@>=
  39. class PhidgetConfWidget : public BasicDeviceConfigurationWidget
  40. {
  41. Q_OBJECT
  42. public:
  43. Q_INVOKABLE PhidgetConfWidget(DeviceTreeModel *model,
  44. const QModelIndex &index);
  45. private slots:
  46. void addChannel();
  47. };
  48. @ The only thing this configuration widget provides is a way to create child
  49. nodes.
  50. @<Phidget implementation@>=
  51. PhidgetConfWidget::PhidgetConfWidget(DeviceTreeModel *model,
  52. const QModelIndex &index)
  53. : BasicDeviceConfigurationWidget(model, index)
  54. {
  55. QHBoxLayout *layout = new QHBoxLayout;
  56. QPushButton *addChannelButton = new QPushButton(tr("Add Channel"));
  57. connect(addChannelButton, SIGNAL(clicked()), this, SLOT(addChannel()));
  58. layout->addWidget(addChannelButton);
  59. setLayout(layout);
  60. }
  61. void PhidgetConfWidget::addChannel()
  62. {
  63. insertChildNode(tr("Channel"), "phidgetchannel");
  64. }
  65. @ For this library, \pn{} supports a broader range of hardware. This requires
  66. slightly more involved hardware configuration to ensure that a given channel
  67. configuration consistently refers to the same sensor.
  68. Channels will be initialized with a device serial number, a channel number, and
  69. other channel specific configuration options as applicable. These other
  70. configuration options depend on the sensor type associated with the channel. A
  71. thermocouple requires different configuration options than an RTD while the
  72. built in ambient temperature sensors on some devices do not require additional
  73. configuration.
  74. At present, only temperature sensors are supported, however this code could be
  75. extended to support other options.
  76. To simplify configuration, a combo box is provided which displays all of the
  77. currently connected channels that \pn{} supports and allows a configuration
  78. widget to obtain relevant channel information when the desired channel is
  79. selected.
  80. @<Class declarations@>=
  81. class PhidgetChannelSelector : public QComboBox
  82. {
  83. Q_OBJECT
  84. public:
  85. PhidgetChannelSelector();
  86. ~PhidgetChannelSelector();
  87. void addChannel(void *device);
  88. void removeChannel(void *device);
  89. private:
  90. QLibrary driver;
  91. void *manager;
  92. @<Phidget22 function pointers@>@;
  93. };
  94. @ At this point, it becomes necessary to call functions in the library. To avoid
  95. a dependency on phidget22.h some function pointer types are created.
  96. @<Additional type definitions@>=
  97. #if defined(__stdcall)
  98. #define CCONV __stdcall
  99. #else
  100. #if defined(_MSC_VER)
  101. #define CCONV __stdcall
  102. #else
  103. #define CCONV
  104. #endif
  105. #endif
  106. typedef int (CCONV *PhidgetPointer)(void *);
  107. typedef int (CCONV *PhidgetPointerStringOut)(void *, char **);
  108. typedef int (CCONV *PhidgetPointerIntOut)(void *, int *);
  109. typedef void (CCONV *PhidgetManagerCallback)(void *, void *, void *);
  110. typedef void (CCONV *PhidgetValueCallback)(void *, void *, double);
  111. typedef int (CCONV *PhidgetPointerCallbackPointer)(void *,
  112. PhidgetManagerCallback,
  113. void *);
  114. typedef int (CCONV *PhidgetPointerVCPointer)(void *,
  115. PhidgetValueCallback,
  116. void *);
  117. typedef int (CCONV *PhidgetPointerIntIn)(void *, int);
  118. @ These are used to define function pointers that will be used to
  119. communicate with the library.
  120. @<Phidget22 function pointers@>=
  121. PhidgetPointer createManager;
  122. PhidgetPointerCallbackPointer setOnAttachManager;
  123. PhidgetPointerCallbackPointer setOnDetachManager;
  124. PhidgetPointer openManager;
  125. PhidgetPointerStringOut getDeviceName;
  126. PhidgetPointerIntOut getDeviceSerialNumber;
  127. PhidgetPointerIntOut getChannel;
  128. PhidgetPointerIntOut getChannelClass;
  129. PhidgetPointerIntOut getChannelSubclass;
  130. PhidgetPointerIntOut getHubPort;
  131. PhidgetPointer closeManager;
  132. PhidgetPointer deleteManager;
  133. @ These pointers must be initialized before they can be used.
  134. @<Initialize phidget22 function pointers@>=
  135. if((createManager = (PhidgetPointer) driver.resolve("PhidgetManager_create")) == 0 ||
  136. (setOnAttachManager = (PhidgetPointerCallbackPointer) driver.resolve("PhidgetManager_setOnAttachHandler")) == 0 ||
  137. (setOnDetachManager = (PhidgetPointerCallbackPointer) driver.resolve("PhidgetManager_setOnDetachHandler")) == 0 ||
  138. (openManager = (PhidgetPointer) driver.resolve("PhidgetManager_open")) == 0 ||
  139. (getDeviceName = (PhidgetPointerStringOut) driver.resolve("Phidget_getDeviceName")) == 0 ||
  140. (getDeviceSerialNumber = (PhidgetPointerIntOut) driver.resolve("Phidget_getDeviceSerialNumber")) == 0 ||
  141. (getChannel = (PhidgetPointerIntOut) driver.resolve("Phidget_getChannel")) == 0 ||
  142. (getChannelClass = (PhidgetPointerIntOut) driver.resolve("Phidget_getChannelClass")) == 0 ||
  143. (getChannelSubclass = (PhidgetPointerIntOut) driver.resolve("Phidget_getChannelSubclass")) == 0 ||
  144. (getHubPort = (PhidgetPointerIntOut) driver.resolve("Phidget_getHubPort")) == 0 ||
  145. (closeManager = (PhidgetPointer) driver.resolve("PhidgetManager_close")) == 0 ||
  146. (deleteManager = (PhidgetPointer) driver.resolve("PhidgetManager_delete")) == 0)
  147. {
  148. QMessageBox::critical(NULL, tr("Typica: Link error"),
  149. tr("Failed to link a required symbol in phidget22."));
  150. return;
  151. }
  152. @ The constuctor sets up a manager so that appropriate channels can be added to
  153. the combo box.
  154. @<Phidget implementation@>=
  155. PhidgetChannelSelector::PhidgetChannelSelector() : QComboBox(), manager(NULL)
  156. {
  157. #if __APPLE__
  158. driver.setFileName("Phidget22.framework/Phidget22");
  159. #else
  160. driver.setFileName("phidget22");
  161. #endif
  162. if(!driver.load())
  163. {
  164. QMessageBox::critical(NULL, tr("Typica: Driver not found"),
  165. tr("Failed to find phidget22. Please install it."));
  166. return;
  167. }
  168. @<Initialize phidget22 function pointers@>@;
  169. createManager(&manager);
  170. setOnAttachManager(manager, ChannelListAddChannel, this);
  171. setOnDetachManager(manager, ChannelListRemoveChannel, this);
  172. openManager(manager);
  173. }
  174. @ The callbacks registered in the constructor pass a pointer to the combo box
  175. so that contents can be updated from the relevant global functions.
  176. @<Additional functions@>=
  177. void CCONV ChannelListAddChannel(void *, void *context, void *device)
  178. {
  179. PhidgetChannelSelector *list =
  180. qobject_cast<PhidgetChannelSelector*>((QObject*)context);
  181. list->addChannel(device);
  182. }
  183. void CCONV ChannelListRemoveChannel(void *, void *context, void *device)
  184. {
  185. PhidgetChannelSelector *list =
  186. qobject_cast<PhidgetChannelSelector*>((QObject*)context);
  187. list->removeChannel(device);
  188. }
  189. @ These require function prototypes.
  190. @<Additional function prototypes@>=
  191. void CCONV ChannelListAddChannel(void *manager, void *context, void *device);
  192. void CCONV ChannelListRemoveChannel(void *manager, void *context,
  193. void *device);
  194. @ Adding and removing channels from the list happens in class where pointers
  195. to the required library functions are known.
  196. @<Phidget implementation@>=
  197. void PhidgetChannelSelector::addChannel(void *device)
  198. {
  199. char *deviceName;
  200. int deviceSerialNumber;
  201. int channel;
  202. int channelClass;
  203. int channelSubclass;
  204. int hubPort;
  205. getDeviceName(device, &deviceName);
  206. getDeviceSerialNumber(device, &deviceSerialNumber);
  207. getChannel(device, &channel);
  208. getChannelClass(device, &channelClass);
  209. getChannelSubclass(device, &channelSubclass);
  210. getHubPort(device, &hubPort);
  211. QMap<QString,QVariant> itemData;
  212. if(channelClass == 28) // Temperature sensor
  213. {
  214. itemData.insert("serialNumber", QString("%1").arg(deviceSerialNumber));
  215. itemData.insert("channel", QString("%1").arg(channel));
  216. itemData.insert("class", QString("%1").arg(channelClass));
  217. itemData.insert("subclass", QString("%1").arg(channelSubclass));
  218. itemData.insert("hubport", QString("%1").arg(hubPort));
  219. addItem(QString("%1: %2").arg(deviceName).arg(channel), QVariant(itemData));
  220. }
  221. }
  222. @ On removal, no attempt is made to match each call to the exact channel
  223. referenced. Rather, the assumption is that all channels on a device with a
  224. matching serial number should be removed at once and in the case of a
  225. multi-channel device getting removed, subsequent calls will just fail to match
  226. anything. The most common expected scenario is that only one device will be
  227. connected or not connected, so removing this results in an empty list for
  228. channels that were removed early.
  229. @<Phidget implementation@>=
  230. void PhidgetChannelSelector::removeChannel(void *device)
  231. {
  232. int deviceSerialNumber;
  233. getDeviceSerialNumber(device, &deviceSerialNumber);
  234. for(int i = count() - 1; i >= 0; i--)
  235. {
  236. QMap<QString,QVariant> data = itemData(i).toMap();
  237. if(data.value("serialNumber").toInt() == deviceSerialNumber)
  238. {
  239. removeItem(i);
  240. }
  241. }
  242. }
  243. @ A destructor closes and deletes the manager.
  244. @<Phidget implementation@>=
  245. PhidgetChannelSelector::~PhidgetChannelSelector()
  246. {
  247. if(manager != NULL)
  248. {
  249. closeManager(manager);
  250. deleteManager(&manager);
  251. }
  252. }
  253. @ Channel configuration provides a |PhidgetChannelSelector| for choosing
  254. among connected devices but also displays the relevant configuration data.
  255. @<Class declarations@>=
  256. class PhidgetChannelConfWidget : public BasicDeviceConfigurationWidget
  257. {
  258. Q_OBJECT
  259. public:
  260. Q_INVOKABLE PhidgetChannelConfWidget(DeviceTreeModel *model,
  261. const QModelIndex &index);
  262. public slots:
  263. void changeSelectedChannel(int index);
  264. void updateSerialNumber(const QString &value);
  265. void updateChannel(const QString &value);
  266. void updateHubPort(const QString &value);
  267. void updateColumnName(const QString &value);
  268. void updateChannelType(int value);
  269. void updateTCType(int value);
  270. void updateRTDType(int value);
  271. void updateRTDWiring(int value);
  272. void updateHidden(int value);
  273. private:
  274. PhidgetChannelSelector *channelSelector;
  275. QLineEdit *serialNumber;
  276. QLineEdit *channel;
  277. QLineEdit *hubPort;
  278. QComboBox *subtype;
  279. QStackedLayout *subtypeLayout;
  280. QComboBox *tctype;
  281. QComboBox *rtdtype;
  282. QComboBox *rtdwiring;
  283. };
  284. @ The constructor is responsible for setting up the interface.
  285. @<Phidget implementation@>=
  286. PhidgetChannelConfWidget::PhidgetChannelConfWidget(DeviceTreeModel *model,
  287. const QModelIndex &index)
  288. : BasicDeviceConfigurationWidget(model, index),
  289. channelSelector(new PhidgetChannelSelector),
  290. serialNumber(new QLineEdit),
  291. channel(new QLineEdit),
  292. hubPort(new QLineEdit),
  293. subtype(new QComboBox),
  294. subtypeLayout(new QStackedLayout),
  295. tctype(new QComboBox),
  296. rtdtype(new QComboBox),
  297. rtdwiring(new QComboBox)
  298. {
  299. QVBoxLayout *outerLayout = new QVBoxLayout;
  300. QFormLayout *layout = new QFormLayout;
  301. QLineEdit *columnName = new QLineEdit;
  302. subtype->addItem(tr("IC"), QVariant(1));
  303. subtype->addItem(tr("RTD"), QVariant(32));
  304. subtype->addItem(tr("Thermocouple"), QVariant(33));
  305. layout->addRow(tr("Channels:"), channelSelector);
  306. layout->addRow(tr("Column Name:"), columnName);
  307. QCheckBox *hidden = new QCheckBox(tr("Hide channel"));
  308. layout->addRow(hidden);
  309. layout->addRow(tr("Serial Number:"), serialNumber);
  310. layout->addRow(tr("Hub Port:"), hubPort);
  311. layout->addRow(tr("Channel Number:"), channel);
  312. layout->addRow(tr("Channel Type:"), subtype);
  313. serialNumber->setEnabled(false);
  314. channel->setEnabled(false);
  315. subtype->setEnabled(false);
  316. outerLayout->addLayout(layout);
  317. QWidget *icconfiguration = new QWidget;
  318. QWidget *rtdconfiguration = new QWidget;
  319. QFormLayout *rtdconfigurationLayout = new QFormLayout;
  320. rtdtype->addItem(tr("PT100 with .00385 curve"), QVariant(1));
  321. rtdtype->addItem(tr("PT1000 with .00385 curve"), QVariant(2));
  322. rtdtype->addItem(tr("PT100 with .00392 curve"), QVariant(3));
  323. rtdtype->addItem(tr("PT1000 with .00392 curve"), QVariant(4));
  324. rtdconfigurationLayout->addRow(tr("RTD type:"), rtdtype);
  325. rtdwiring->addItem(tr("2 wire"), QVariant(1));
  326. rtdwiring->addItem(tr("3 wire"), QVariant(2));
  327. rtdwiring->addItem(tr("4 wire"), QVariant(3));
  328. rtdconfigurationLayout->addRow(tr("RTD wiring:"), rtdwiring);
  329. rtdconfiguration->setLayout(rtdconfigurationLayout);
  330. QWidget *tcconfiguration = new QWidget;
  331. QFormLayout *tcconfigurationLayout = new QFormLayout;
  332. tctype->addItem(tr("Type J"), QVariant(1));
  333. tctype->addItem(tr("Type K"), QVariant(2));
  334. tctype->addItem(tr("Type E"), QVariant(3));
  335. tctype->addItem(tr("Type T"), QVariant(4));
  336. tcconfigurationLayout->addRow(tr("Thermocouple type:"), tctype);
  337. tcconfiguration->setLayout(tcconfigurationLayout);
  338. subtypeLayout->addWidget(icconfiguration);
  339. subtypeLayout->addWidget(rtdconfiguration);
  340. subtypeLayout->addWidget(tcconfiguration);
  341. @<Get device configuration data for current node@>@;
  342. for(int i = 0; i < configData.size(); i++)
  343. {
  344. node = configData.at(i).toElement();
  345. if(node.attribute("name") == "serialnumber")
  346. {
  347. serialNumber->setText(node.attribute("value"));
  348. }
  349. else if(node.attribute("name") == "channel")
  350. {
  351. channel->setText(node.attribute("value"));
  352. }
  353. else if(node.attribute("name") == "columnname")
  354. {
  355. columnName->setText(node.attribute("value"));
  356. }
  357. else if(node.attribute("name") == "channeltype")
  358. {
  359. subtype->setCurrentIndex(subtype->
  360. findData(QVariant(node.attribute("value").toInt())));
  361. subtypeLayout->setCurrentIndex(subtype->currentIndex());
  362. }
  363. else if(node.attribute("name") == "tctype")
  364. {
  365. tctype->setCurrentIndex(tctype->
  366. findData(QVariant(node.attribute("value").toInt())));
  367. }
  368. else if(node.attribute("name") == "rtdtype")
  369. {
  370. rtdtype->setCurrentIndex(rtdtype->
  371. findData(QVariant(node.attribute("value").toInt())));
  372. }
  373. else if(node.attribute("name") == "rtdwiring")
  374. {
  375. rtdwiring->setCurrentIndex(rtdwiring->
  376. findData(QVariant(node.attribute("value").toInt())));
  377. }
  378. else if(node.attribute("name") == "hubport")
  379. {
  380. hubPort->setText(node.attribute("value"));
  381. }
  382. else if(node.attribute("name") == "hidden")
  383. {
  384. hidden->setCheckState(node.attribute("value") == "true" ? Qt::Checked : Qt::Unchecked);
  385. }
  386. }
  387. outerLayout->addLayout(subtypeLayout);
  388. setLayout(outerLayout);
  389. updateSerialNumber(serialNumber->text());
  390. updateChannel(channel->text());
  391. updateColumnName(columnName->text());
  392. updateChannelType(subtype->currentIndex());
  393. updateTCType(tctype->currentIndex());
  394. updateRTDType(rtdtype->currentIndex());
  395. updateRTDWiring(rtdwiring->currentIndex());
  396. updateHubPort(hubPort->text());
  397. updateHidden(hidden->checkState());
  398. connect(channelSelector, SIGNAL(currentIndexChanged(int)),
  399. this, SLOT(changeSelectedChannel(int)));
  400. connect(subtype, SIGNAL(currentIndexChanged(int)),
  401. subtypeLayout, SLOT(setCurrentIndex(int)));
  402. connect(serialNumber, SIGNAL(textChanged(QString)),
  403. this, SLOT(updateSerialNumber(QString)));
  404. connect(channel, SIGNAL(textChanged(QString)),
  405. this, SLOT(updateChannel(QString)));
  406. connect(columnName, SIGNAL(textEdited(QString)),
  407. this, SLOT(updateColumnName(QString)));
  408. connect(subtype, SIGNAL(currentIndexChanged(int)),
  409. this, SLOT(updateChannelType(int)));
  410. connect(tctype, SIGNAL(currentIndexChanged(int)),
  411. this, SLOT(updateTCType(int)));
  412. connect(rtdtype, SIGNAL(currentIndexChanged(int)),
  413. this, SLOT(updateRTDType(int)));
  414. connect(rtdwiring, SIGNAL(currentIndexChanged(int)),
  415. this, SLOT(updateRTDWiring(int)));
  416. connect(hubPort, SIGNAL(textChanged(QString)),
  417. this, SLOT(updateHubPort(QString)));
  418. connect(hidden, SIGNAL(stateChanged(int)),
  419. this, SLOT(updateHidden(int)));
  420. }
  421. @ The combo box provides a convenient way to populate required configuration
  422. fields with values that are not immediately obvious.
  423. @<Phidget implementation@>=
  424. void PhidgetChannelConfWidget::changeSelectedChannel(int index)
  425. {
  426. QMap<QString,QVariant> data = channelSelector->itemData(index).toMap();
  427. serialNumber->setText(data.value("serialNumber").toString());
  428. channel->setText(data.value("channel").toString());
  429. subtype->setCurrentIndex(subtype->
  430. findData(QVariant(data.value("subclass").toString().toInt())));
  431. hubPort->setText(data.value("hubport").toString());
  432. }
  433. @ Channel configuration settings are persisted as they are updated.
  434. @<Phidget implementation@>=
  435. void PhidgetChannelConfWidget::updateSerialNumber(const QString &value)
  436. {
  437. updateAttribute("serialnumber", value);
  438. }
  439. void PhidgetChannelConfWidget::updateChannel(const QString &value)
  440. {
  441. updateAttribute("channel", value);
  442. }
  443. void PhidgetChannelConfWidget::updateColumnName(const QString &value)
  444. {
  445. updateAttribute("columnname", value);
  446. }
  447. void PhidgetChannelConfWidget::updateChannelType(int value)
  448. {
  449. updateAttribute("channeltype", subtype->itemData(value).toString());
  450. }
  451. void PhidgetChannelConfWidget::updateTCType(int value)
  452. {
  453. updateAttribute("tctype", tctype->itemData(value).toString());
  454. }
  455. void PhidgetChannelConfWidget::updateRTDType(int value)
  456. {
  457. updateAttribute("rtdtype", rtdtype->itemData(value).toString());
  458. }
  459. void PhidgetChannelConfWidget::updateRTDWiring(int value)
  460. {
  461. updateAttribute("rtdwiring", rtdwiring->itemData(value).toString());
  462. }
  463. void PhidgetChannelConfWidget::updateHubPort(const QString &value)
  464. {
  465. updateAttribute("hubport", value);
  466. }
  467. void PhidgetChannelConfWidget::updateHidden(int value)
  468. {
  469. updateAttribute("hidden", value == 0 ? "false" : "true");
  470. }
  471. @ The hardware communnications code provides a single class that reads the
  472. saved configuration data, creates |Channel| objects for the logging view to
  473. connect various things to, and pushes data out on those channels. Internally,
  474. there is more variability in how these channels must be set up, so rather than
  475. just having a bunch of lists for the various properties, not all of which might
  476. be relevant, instead, the channel configuration data will all be kept in a
  477. structure.
  478. @<Class declarations@>=
  479. struct PhidgetChannelData
  480. {
  481. Channel *channel;
  482. QString columnName;
  483. QString indicatorLabel;
  484. int serialNumber;
  485. int channelNumber;
  486. int channelType;
  487. int hubPort;
  488. int tcType;
  489. int rtdType;
  490. int wiring;
  491. bool hidden;
  492. void *device;
  493. };
  494. @ The host environment requires a class that handles communication with the
  495. hardware. The public interface has been kept the same as the phidget21 code to
  496. minimize changes required in the configuration files.
  497. @<Class declarations@>=
  498. class Phidget22 : public QObject
  499. {
  500. Q_OBJECT
  501. public:
  502. Q_INVOKABLE Phidget22(const QModelIndex &deviceIndex);
  503. Q_INVOKABLE int channelCount();
  504. Channel* getChannel(int channel);
  505. Q_INVOKABLE bool isChannelHidden(int channel);
  506. Q_INVOKABLE QString channelColumnName(int channel);
  507. Q_INVOKABLE QString channelIndicatorText(int channel);
  508. public slots:
  509. void start();
  510. void stop();
  511. private:
  512. QList<PhidgetChannelData *> channelConfiguration;
  513. QLibrary driver;
  514. PhidgetPointer p_createTemperatureSensor;
  515. PhidgetPointerIntIn p_setSerialNumber;
  516. PhidgetPointerIntIn p_setChannelNumber;
  517. PhidgetPointerIntIn p_setHubPort;
  518. PhidgetPointerIntIn p_setTCType;
  519. PhidgetPointerIntIn p_setRTDType;
  520. PhidgetPointerIntIn p_setRTDWiring;
  521. PhidgetPointerVCPointer p_setNewDataCallback;
  522. PhidgetPointerIntIn p_open;
  523. PhidgetPointer p_close;
  524. PhidgetPointer p_delete;
  525. };
  526. @ The constructor reads the previously saved hardware configuration data and
  527. uses that to create the relevant channels. The channels are not initialized
  528. until the device is started.
  529. @<Phidget implementation@>=
  530. Phidget22::Phidget22(const QModelIndex &index) : QObject(NULL)
  531. {
  532. DeviceTreeModel *model = (DeviceTreeModel *)(index.model());
  533. if(model->hasChildren(index))
  534. {
  535. for(int i = 0; i < model->rowCount(index); i++)
  536. {
  537. QModelIndex channelIndex = model->index(i, 0, index);
  538. QDomElement channelReference = model->
  539. referenceElement(model->data(channelIndex, 32).toString());
  540. QDomElement channelReferenceElement = model->
  541. referenceElement(model->
  542. data(channelIndex, Qt::UserRole).toString());
  543. QDomNodeList channelConfigData =
  544. channelReferenceElement.elementsByTagName("attribute");
  545. PhidgetChannelData *c = new PhidgetChannelData;
  546. c->channel = new Channel;
  547. c->indicatorLabel =
  548. model->data(channelIndex, Qt::DisplayRole).toString();
  549. c->device = NULL;
  550. c->hubPort = -1;
  551. for(int j = 0; j < channelConfigData.size(); j++)
  552. {
  553. QDomElement node = channelConfigData.at(j).toElement();
  554. if(node.attribute("name") == "serialnumber")
  555. {
  556. c->serialNumber = node.attribute("value").toInt();
  557. }
  558. else if(node.attribute("name") == "channel")
  559. {
  560. c->channelNumber = node.attribute("value").toInt();
  561. }
  562. else if(node.attribute("name") == "channeltype")
  563. {
  564. c->channelType = node.attribute("value").toInt();
  565. }
  566. else if(node.attribute("name") == "tctype")
  567. {
  568. c->tcType = node.attribute("value").toInt();
  569. }
  570. else if(node.attribute("name") == "rtdtype")
  571. {
  572. c->rtdType = node.attribute("value").toInt();
  573. }
  574. else if(node.attribute("name") == "rtdwiring")
  575. {
  576. c->wiring = node.attribute("value").toInt();
  577. }
  578. else if(node.attribute("name") == "hidden")
  579. {
  580. c->hidden = (node.attribute("value") == "true");
  581. }
  582. else if(node.attribute("name") == "columnname")
  583. {
  584. c->columnName = node.attribute("value");
  585. }
  586. else if(node.attribute("name") == "hubport")
  587. {
  588. c->hubPort = node.attribute("value").toInt();
  589. }
  590. }
  591. channelConfiguration.append(c);
  592. }
  593. }
  594. }
  595. @ A bit of glue is needed to get the |Channel| objects out to the host
  596. environment.
  597. @<Phidget implementation@>=
  598. int Phidget22::channelCount()
  599. {
  600. return channelConfiguration.length();
  601. }
  602. Channel* Phidget22::getChannel(int channel)
  603. {
  604. return channelConfiguration.at(channel)->channel;
  605. }
  606. @ A little more glue allows the host environment to properly set up UI
  607. elements.
  608. @<Phidget implementation@>=
  609. bool Phidget22::isChannelHidden(int channel)
  610. {
  611. return channelConfiguration.at(channel)->hidden;
  612. }
  613. QString Phidget22::channelColumnName(int channel)
  614. {
  615. return channelConfiguration.at(channel)->columnName;
  616. }
  617. QString Phidget22::channelIndicatorText(int channel)
  618. {
  619. return channelConfiguration.at(channel)->indicatorLabel;
  620. }
  621. @ Once the hardware configuration has been read and the UI has been set up, we
  622. can start talking to the hardware and start getting measurements.
  623. @<Phidget implementation@>=
  624. void Phidget22::start()
  625. {
  626. #if __APPLE__
  627. driver.setFileName("Phidget22.framework/Phidget22");
  628. #else
  629. driver.setFileName("phidget22");
  630. #endif
  631. if(!driver.load())
  632. {
  633. QMessageBox::critical(NULL, tr("Typica: Driver not found"),
  634. tr("Failed to find phidget22. Please install it."));
  635. return;
  636. }
  637. if((p_createTemperatureSensor = (PhidgetPointer)driver.resolve("PhidgetTemperatureSensor_create")) == 0 ||
  638. (p_setSerialNumber = (PhidgetPointerIntIn)driver.resolve("Phidget_setDeviceSerialNumber")) == 0 ||
  639. (p_setChannelNumber = (PhidgetPointerIntIn)driver.resolve("Phidget_setChannel")) == 0 ||
  640. (p_setTCType = (PhidgetPointerIntIn)driver.resolve("PhidgetTemperatureSensor_setThermocoupleType")) == 0 ||
  641. (p_setRTDType = (PhidgetPointerIntIn)driver.resolve("PhidgetTemperatureSensor_setRTDType")) == 0 ||
  642. (p_setRTDWiring = (PhidgetPointerIntIn)driver.resolve("PhidgetTemperatureSensor_setRTDWireSetup")) == 0 ||
  643. (p_setNewDataCallback = (PhidgetPointerVCPointer)driver.resolve("PhidgetTemperatureSensor_setOnTemperatureChangeHandler")) == 0 ||
  644. (p_open = (PhidgetPointerIntIn)driver.resolve("Phidget_openWaitForAttachment")) == 0 ||
  645. (p_close = (PhidgetPointer)driver.resolve("Phidget_close")) == 0 ||
  646. (p_delete = (PhidgetPointer)driver.resolve("PhidgetTemperatureSensor_delete")) == 0 ||
  647. (p_setHubPort = (PhidgetPointerIntIn)driver.resolve("Phidget_setHubPort")) == 0)
  648. {
  649. QMessageBox::critical(NULL, tr("Typica: Link error"),
  650. tr("Failed to link a required symbol in phidget22."));
  651. return;
  652. }
  653. for(int i = 0; i < channelConfiguration.length(); i++)
  654. {
  655. PhidgetChannelData *c = channelConfiguration.at(i);
  656. p_createTemperatureSensor(&(c->device));
  657. p_setSerialNumber(c->device, c->serialNumber);
  658. p_setChannelNumber(c->device, c->channelNumber);
  659. switch(c->channelType)
  660. {
  661. case 32:
  662. p_setRTDType(c->device, c->rtdType);
  663. p_setRTDWiring(c->device, c->wiring);
  664. break;
  665. case 33:
  666. p_setTCType(c->device, c->tcType);
  667. break;
  668. default:
  669. break;
  670. }
  671. if(c->hubPort >= 0)
  672. {
  673. p_setHubPort(c->device, c->hubPort);
  674. }
  675. p_setNewDataCallback(c->device, Phidget22ValueCallback, c->channel);
  676. p_open(c->device, 5000);
  677. }
  678. }
  679. @ New values are delivered to a callback function outside of the class, but
  680. with a pointer to the relevant |Channel| object. This means that all the
  681. callback needs to do is perform the unit conversion, assemble the |Measurement|
  682. and send that out.
  683. Unfortunately, there can be no guarantee that new measurements will be
  684. available on all channels simultaneously. Hopefully this will not be too
  685. problematic.
  686. @<Additional functions@>=
  687. void CCONV Phidget22ValueCallback(void *, void *context, double value)
  688. {
  689. Channel *channel = (Channel*)context;
  690. QTime time = QTime::currentTime();
  691. Measurement measure(value * 9.0 / 5.0 + 32.0, time);
  692. channel->input(measure);
  693. }
  694. @ A function prototype is provided.
  695. @<Additional function prototypes@>=
  696. void CCONV Phidget22ValueCallback(void *device, void *context, double value);
  697. @ When the logging window is closed, it is important to close all open channels
  698. and delete their handles.
  699. @<Phidget implementation@>=
  700. void Phidget22::stop()
  701. {
  702. for(int i = 0; i < channelConfiguration.length(); i++)
  703. {
  704. PhidgetChannelData *c = channelConfiguration.at(i);
  705. p_close(c->device);
  706. p_delete(&(c->device));
  707. }
  708. }
  709. @ Class implementations are currently folded into typica.cpp.
  710. @<Class implementations@>=
  711. @<Phidget implementation@>@;
  712. @ The hardware communications class needs to be available from the host
  713. environment.
  714. @<Set up the scripting engine@>=
  715. constructor = engine->newFunction(constructPhidget22);
  716. value = engine->newQMetaObject(&Phidget22::staticMetaObject, constructor);
  717. engine->globalObject().setProperty("Phidget22", value);
  718. @ Two function prototypes are needed.
  719. @<Function prototypes for scripting@>=
  720. QScriptValue constructPhidget22(QScriptContext *context, QScriptEngine *engine);
  721. QScriptValue Phidget22_getChannel(QScriptContext *context, QScriptEngine *engine);
  722. @ The constructor is trivial.
  723. @<Functions for scripting@>=
  724. QScriptValue constructPhidget22(QScriptContext *context, QScriptEngine *engine)
  725. {
  726. if(context->argumentCount() != 1)
  727. {
  728. context->throwError("Incorrect number of arguments");
  729. }
  730. QScriptValue object = engine->newQObject(new Phidget22(argument<QModelIndex>(0, context)), QScriptEngine::ScriptOwnership);
  731. setQObjectProperties(object, engine);
  732. object.setProperty("getChannel", engine->newFunction(Phidget22_getChannel));
  733. return object;
  734. }
  735. @ A wrapper is used for getting channels.
  736. @<Functions for scripting@>=
  737. QScriptValue Phidget22_getChannel(QScriptContext *context, QScriptEngine *engine)
  738. {
  739. Phidget22 *self = getself<Phidget22 *>(context);
  740. QScriptValue object;
  741. if(self)
  742. {
  743. object = engine->newQObject(self->getChannel(argument<int>(0, context)));
  744. setChannelProperties(object, engine);
  745. }
  746. return object;
  747. }