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.

dataqsdk.w 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. @** Support for Devices Using the DATAQ SDK.
  2. \noindent Support for hardware from DATAQ Instruments is currently provided
  3. through the DATAQ SDK. This means that this support is currently only available
  4. on Microsoft Windows. The first planned supported device is the DI-145 with
  5. support for the DI-148U planned later. The devices are sufficiently similar
  6. that adding support for other devices from this manufacturer should be easy,
  7. but I do not have hardware samples to use for testing with other devices. The
  8. DI-145 additionally has a documented serial protocol which should make the
  9. hardware usable without the DATAQ SDK both on Microsoft Windows and on other
  10. platforms for which there is a suitable serial driver.
  11. Originally the classes were surrounded with conditional compilation directives
  12. but moc failed to generate the appropriate meta-objects on Windows when this
  13. was done. The |DataqSdkDevice| and |DataqSdkDeviceImplementation| classes will
  14. truly only work on Microsoft Windows at this time. Attempts to use it elsewhere
  15. will not end well.
  16. @<Class declarations@>=
  17. class DataqSdkDeviceImplementation;
  18. class DataqSdkDevice : public QObject
  19. {
  20. Q_OBJECT
  21. DataqSdkDeviceImplementation *imp;
  22. private slots:
  23. void threadFinished();
  24. public:
  25. DataqSdkDevice(QString device);
  26. ~DataqSdkDevice();
  27. Channel* newChannel(Units::Unit scale);
  28. Q_INVOKABLE void setClockRate(double Hz);
  29. Q_INVOKABLE void start();
  30. };
  31. @ The |DataqSdkDevice| class has as a private member an instance of a class
  32. called |DataqSdkDeviceImplementation|. The two classes together create and run
  33. a new thread of execution. This thread spends most of its time blocking while
  34. waiting for a new measurement to become available. When a new measurement is
  35. available, that measurement is passed to the appropriate channel which in turn
  36. passes it to any interested object.
  37. Note that subclassing |QThread| in this way is no longer considered best
  38. practice. This particular code architecture is based on code written when this
  39. was considered the right thing to do, but it would be good to rewrite this to
  40. not subclass |QThread| now that this is no longer required.
  41. @<Class declarations@>=
  42. class DataqSdkDeviceImplementation : public QThread
  43. {
  44. Q_OBJECT
  45. public:
  46. DataqSdkDeviceImplementation();
  47. ~DataqSdkDeviceImplementation();
  48. void run();
  49. @<DATAQ SDK library function pointers@>@;
  50. @<DataqSdkDeviceImplementation member data@>@;
  51. public slots:
  52. void measure();
  53. private:
  54. qint16 *buffer;
  55. };
  56. @ While the |DAQ| class for communicating with National Instruments devices
  57. uses a single function pointer type, increased variety of function signatures
  58. in the DATAQ SDK makes using several types a better option. This also
  59. eliminates the need for explicit casts on the arguments.
  60. @<DATAQ SDK library function pointers@>=
  61. typedef struct di_inlist_struct {
  62. unsigned short chan;
  63. unsigned short diff;
  64. unsigned short gain;
  65. unsigned short unipolar;
  66. unsigned short dig_out_enable;
  67. unsigned short dig_out;
  68. unsigned short ave;
  69. unsigned short counter;
  70. } DI_INLIST_STRUCT;
  71. typedef int (PASCAL *FPDIOPEN)(unsigned);
  72. typedef int (PASCAL *FPDICLOSE)(void);
  73. typedef double (PASCAL *FPDISAMPLERATE)(double, long*, long*);
  74. typedef double (PASCAL *FPDIMAXIMUMRATE)(double);
  75. typedef int (PASCAL *FPDILISTLENGTH)(unsigned, unsigned);
  76. typedef int (PASCAL *FPDIINLIST)(di_inlist_struct*);
  77. typedef int* (PASCAL *FPDIBUFFERALLOC)(unsigned, unsigned);
  78. typedef int (PASCAL *FPDISTARTSCAN)(void);
  79. typedef unsigned (PASCAL *FPDISTATUSREAD)(short*, unsigned);
  80. typedef unsigned (PASCAL *FPDIBUFFERSTATUS)(unsigned);
  81. typedef int (PASCAL *FPDIBURSTRATE)(unsigned);
  82. typedef int (PASCAL *FPDISTOPSCAN)(void);
  83. FPDIOPEN di_open;
  84. FPDICLOSE di_close;
  85. FPDISAMPLERATE di_sample_rate;
  86. FPDIMAXIMUMRATE di_maximum_rate;
  87. FPDILISTLENGTH di_list_length;
  88. FPDIINLIST di_inlist;
  89. FPDIBUFFERALLOC di_buffer_alloc;
  90. FPDISTARTSCAN di_start_scan;
  91. FPDISTATUSREAD di_status_read;
  92. FPDIBUFFERSTATUS di_buffer_status;
  93. FPDIBURSTRATE di_burst_rate;
  94. FPDISTOPSCAN di_stop_scan;
  95. @ The |PASCAL| macro is defined in the {\tt windef.h} header file which will
  96. need to be included. This modifies the mechanics of the function call. A
  97. feature of the C language which C++ inherits is the ability to create variadic
  98. functions. To facilitate this, when one function calls another, the function
  99. making that call is responsible for cleaning up the stack. The function being
  100. called has no reliable way of knowing how many and what type of arguments have
  101. been passed if it is a variadic function, but this can be determined in the
  102. calling function at compile time. This is effectively a compiler implementation
  103. detail which is unimportant to the vast majority of application code. Use of
  104. the |PASCAL| macro informs the compiler that the function being called will
  105. clean up the stack itself. This precludes the use of variadic functions, but
  106. results in a smaller executable. The choice of name for that macro is
  107. unfortunate as arguments are placed on the stack in the order opposite of
  108. calling conventions of the Pascal programming language, but these are
  109. unimportant details so long as the resulting program works.
  110. @<Header files to include@>=
  111. #ifdef Q_OS_WIN32
  112. #include <windef.h>
  113. #else
  114. #define PASCAL
  115. #endif
  116. @ |DataqSdkDeviceImplementation| maintains information about the device and the
  117. channels the measurements are sent to.
  118. @<DataqSdkDeviceImplementation member data@>=
  119. bool isOpen;
  120. double sampleRate;
  121. long oversample;
  122. long burstDivisor;
  123. QString device;
  124. unsigned deviceNumber;
  125. QVector<Channel*> channelMap;
  126. int error;
  127. int channels;
  128. bool ready;
  129. QLibrary *driver;
  130. QVector<Units::Unit> unitMap;
  131. int *input_buffer;
  132. QTimer *eventClock;
  133. QMultiMap<int, double> smoother;
  134. @ Most of the interesting work associated with the |DataqSdkDevice| class is
  135. handled in the |measure()| method of |DataqSdkDeviceImplementation|. This
  136. method will block until a measurement is available. Once |buffer| is filled by
  137. |di_status_read()| that function returns and new |Measurement| objects are
  138. created based on the information in the buffer. These measurements are sent to
  139. |Channel| objects tracked by |channelMap|.
  140. The buffered values are presented in terms of ADC counts. Before using these
  141. values to convert to a voltage measurement, the two least significant binary
  142. digits of the count are set to 0 to improve measurement accuracy as recommended
  143. in the DATAQ SDK reference documentation.
  144. One of the use cases for this class is using the data port provided on some
  145. roasters from Diedrich Manufacturing. In this case there are three channels
  146. that are used: one provides a 0-10V signal that maps to temperature
  147. measurements of 32 to 1832 degrees F, one provides a signal in the same range
  148. requiring distinguishing among three values for air flow settings, and one is
  149. intended to show a percentage for the fuel setting. After experimenting with
  150. the most direct approach, there are limitations of the hardware that complicate
  151. matters for the channel representing bean temperature. The hardware is
  152. providing a 14 bit value representing a signal in the range of +/-10V so as a
  153. practical matter we only have 13 bits for temperature values. There is a desire
  154. to present measurements with at least one digit after the decimal point,
  155. meaning that we require 18,000 distinct values despite likely only ever seeing
  156. values in the lower third of that range. A 13 bit value only allows 8,192
  157. distinct values to be represented. The result of this is that stable signals
  158. between representable values are coded in an inconsistent fashion which can be
  159. seen as displayed measurements varying erratically. The usual solution to this
  160. problem is to collect many measurements quickly and average them, which is a
  161. reasonable thing to do with the sample rates available on DATAQ hardware.
  162. Examining measurements at a higher sample rate unfortunately reveals a periodic
  163. structure to the measurement error which averaging alone is not adequate to
  164. solve. The quality of the measurements can be improved somewhat by removing the
  165. extreme values from each set of measurements prior to averaging, however this
  166. does not fully address the lower frequency error sources. Further improvements
  167. can be made by maintaining a multimap of recent ADC count values to averaged
  168. voltage values and producing results that take this slightly longer term data
  169. into account. This is essential for obtaining a sufficiently stable low
  170. temperature calibration value and introduces minimal additional measurement
  171. latency during a roast.
  172. At present smoothing is applied to the first data channel and no others. It
  173. should be possible to enable or disable adaptive smoothing for all channels
  174. independently to better handle different hardware configurations.
  175. @<DataqSdkDevice implementation@>=
  176. void DataqSdkDeviceImplementation::measure()
  177. {
  178. unsigned count = channels * 40;
  179. di_status_read(buffer, count);
  180. QTime time = QTime::currentTime();
  181. for(unsigned int i = 0; i < count; i++)
  182. {
  183. buffer[i] = buffer[i] & 0xFFFC;
  184. }
  185. QList<int> countList;
  186. for(unsigned int i = 0; i < (unsigned)channels; i++)
  187. {
  188. QList<double> channelBuffer;
  189. for(unsigned int j = 0; j < 40; j++)
  190. {
  191. channelBuffer << ((double)buffer[i+(channels*j)] * 10.0) / 32768.0;
  192. if(i == 0)
  193. {
  194. countList << buffer[i+(channels*j)];
  195. }
  196. }
  197. double value = 0.0;
  198. for(unsigned int j = 0; j < 40; j++)
  199. {
  200. value += channelBuffer[j];
  201. }
  202. value /= 40.0;
  203. if(i == 0)
  204. {
  205. QList<double> smoothingList;
  206. smoothingList << value;
  207. QList<int> smoothingKeys = smoother.uniqueKeys();
  208. for(int j = 0; j < smoothingKeys.size(); j++)
  209. {
  210. if(countList.contains(smoothingKeys[j]))
  211. {
  212. QList<double> keyValues = smoother.values(smoothingKeys[j]);
  213. for(int k = 0; k < keyValues.size(); k++)
  214. {
  215. smoothingList << keyValues[k];
  216. }
  217. }
  218. else
  219. {
  220. smoother.remove(smoothingKeys[j]);
  221. }
  222. }
  223. qSort(countList);
  224. int lastCount = 0;
  225. for(int j = 0; j < countList.size(); j++)
  226. {
  227. if(j == 0 || countList[j] != lastCount)
  228. {
  229. smoother.insert(countList[j], value);
  230. lastCount = countList[j];
  231. }
  232. }
  233. value = 0.0;
  234. for(int j = 0; j < smoothingList.size(); j++)
  235. {
  236. value += smoothingList[j];
  237. }
  238. value /= smoothingList.size();
  239. }
  240. Measurement measure(value, time, unitMap[i]);
  241. channelMap[i]->input(measure);
  242. }
  243. }
  244. @ It was noted that |di_status_read()| blocks until it is able to fill the
  245. |buffer| passed to it. To prevent this behavior from having adverse effects on
  246. the rest of the program, |measure()| is called from a loop running in its own
  247. thread of execution. When the thread is started, it begins its execution from
  248. the |run()| method of |DataqSdkDeviceImplementation| which overrides the
  249. |run()| method of |QThread|.
  250. The while loop is controlled by |ready| which is set to |false| when there is
  251. an error in collecting a measurement or when there is a desire to stop logging.
  252. It could also be set to |false| for reconfiguration events.
  253. All device initialization happens in this method.
  254. Note that while the equivalent method when communicating with National
  255. Instruments hardware sets a time critical thread priority in an attempt to cut
  256. down on the variation in time between recorded measurements, that is a really
  257. bad idea when using the DATAQ SDK. The result was that the main thread never
  258. got enough time to report measurements and responsiveness throughout the entire
  259. system became barely usable to the point that it was difficult to kill the
  260. process. If anybody reading this can provide some insight into why setting the
  261. thread priority is fine with interacting with either DAQmx or DAQmxBase but not
  262. when interacting with the DATAQ SDK, I would like to read such an explanation.
  263. @<DataqSdkDevice implementation@>=
  264. void DataqSdkDeviceImplementation::run()
  265. {
  266. if(!ready)
  267. {
  268. error = 9; // Device data not available
  269. return;
  270. }
  271. driver = new QLibrary(device);
  272. if(!driver->load())
  273. {
  274. error = 1; // Failed to load driver.
  275. qDebug() << "Failed to load driver: " << device;
  276. return;
  277. }
  278. di_open = (FPDIOPEN)driver->resolve("di_open");
  279. di_close = (FPDICLOSE)driver->resolve("di_close");
  280. di_sample_rate = (FPDISAMPLERATE)driver->resolve("di_sample_rate");
  281. di_maximum_rate = (FPDIMAXIMUMRATE)driver->resolve("di_maximum_rate");
  282. di_list_length = (FPDILISTLENGTH)driver->resolve("di_list_length");
  283. di_inlist = (FPDIINLIST)driver->resolve("di_inlist");
  284. di_buffer_alloc = (FPDIBUFFERALLOC)driver->resolve("di_buffer_alloc");
  285. di_start_scan = (FPDISTARTSCAN)driver->resolve("di_start_scan");
  286. di_status_read = (FPDISTATUSREAD)driver->resolve("di_status_read");
  287. di_buffer_status = (FPDIBUFFERSTATUS)driver->resolve("di_buffer_status");
  288. di_burst_rate = (FPDIBURSTRATE)driver->resolve("di_burst_rate");
  289. di_stop_scan = (FPDISTOPSCAN)driver->resolve("di_stop_scan");
  290. if((!di_open) || (!di_close) || (!di_sample_rate) || (!di_maximum_rate) ||
  291. (!di_list_length) || (!di_inlist) || (!di_buffer_alloc) ||
  292. (!di_start_scan) || (!di_status_read) || (!di_buffer_status) ||
  293. (!di_burst_rate) || (!di_stop_scan))
  294. {
  295. error = 2; // Failed to link required symbol
  296. return;
  297. }
  298. error = di_open(deviceNumber);
  299. if(error)
  300. {
  301. di_close();
  302. error = di_open(deviceNumber);
  303. if(error)
  304. {
  305. error = 3; // Failed to open device
  306. di_close();
  307. return;
  308. }
  309. }
  310. isOpen = true;
  311. di_maximum_rate(240.0);
  312. sampleRate = di_sample_rate(sampleRate * channels * 40, &oversample,
  313. &burstDivisor);
  314. buffer = new qint16[(int)sampleRate];
  315. di_inlist_struct inlist[16] = {{0}};
  316. for(unsigned short i = 0; i < channels; i++)
  317. {
  318. inlist[i].chan = i;
  319. inlist[i].gain = 0;
  320. inlist[i].ave = 1;
  321. inlist[i].counter = (oversample - 1);
  322. }
  323. error = di_list_length(channels, 0);
  324. if(error)
  325. {
  326. error = 4; // List length error
  327. return;
  328. }
  329. error = di_inlist(inlist);
  330. if(error)
  331. {
  332. error = 5; // Inlist error
  333. return;
  334. }
  335. input_buffer = di_buffer_alloc(0, 4096);
  336. if(input_buffer == NULL)
  337. {
  338. error = 6; // Failed to allocate buffer
  339. return;
  340. }
  341. error = di_start_scan();
  342. if(error)
  343. {
  344. error = 7; // Failed to start scanning
  345. return;
  346. }
  347. while(ready)
  348. {
  349. measure();
  350. }
  351. }
  352. @ When the loop exits, |DataqSdkDeviceImplementation| emits a finished signal
  353. to indicate that the thread is no longer running. This could be due to normal
  354. conditions or there could be a problem that should be reported. That signal is
  355. connected to a function that checks for error conditions and reports them if
  356. needed.
  357. @<DataqSdkDevice implementation@>=
  358. void DataqSdkDevice::threadFinished()
  359. {
  360. if(imp->error)
  361. {
  362. @<Display DATAQ SDK Error@>@;
  363. }
  364. }
  365. @ The DATAQ SDK does not have a single method for reporting errors. Instead,
  366. any method that can return an error code has its return value checked and
  367. |error| is set to a value that allows the source of the problem to be
  368. determined. At present, error handling is very poor.
  369. @<Display DATAQ SDK Error@>=
  370. imp->ready = false;
  371. QMessageBox warning;
  372. warning.setStandardButtons(QMessageBox::Cancel);
  373. warning.setIcon(QMessageBox::Warning);
  374. warning.setText(QString(tr("Error: %1")).arg(imp->error));
  375. warning.setInformativeText(tr("An error occurred"));
  376. warning.setWindowTitle(QString(PROGRAM_NAME));
  377. warning.exec();
  378. @ Starting the thread is very simple. Device initialization happens in the new
  379. thread which then begins taking measurements. The call to |imp->start()| starts
  380. the new thread and passes control of that thread to |imp->run()|. The main
  381. thread of execution returns without waiting for the new thread to do anything.
  382. When the thread is finished, the |finished()| signal is emitted which we have
  383. connected to |threadFinished()|.
  384. @<DataqSdkDevice implementation@>=
  385. void DataqSdkDevice::start()
  386. {
  387. connect(imp, SIGNAL(finished()), this, SLOT(threadFinished()));
  388. imp->start();
  389. }
  390. @ Setting up the device begins by constructing a new |DataqSdkDevice| object.
  391. The constructor takes as its argument a string which identifies the device. For
  392. legacy reasons this currently accepts device names such as |"Dev1"| and looks
  393. up currently connected devices to determine which serial port should be used.
  394. Now that it is preferred to configure devices graphically this is not a good
  395. way to do this. This should be changed before release.
  396. @<DataqSdkDevice implementation@>=
  397. DataqSdkDevice::DataqSdkDevice(QString device) : imp(new DataqSdkDeviceImplementation)
  398. {
  399. QSettings deviceLookup("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services\\usbser\\Enum",
  400. QSettings::NativeFormat);
  401. QStringList keys = deviceLookup.childKeys();
  402. QStringList devices;
  403. for(int i = 0; i < keys.size(); i++)
  404. {
  405. QString value = deviceLookup.value(keys.at(i)).toString();
  406. if(value.startsWith("USB\\VID_0683&PID_1450\\"))
  407. {
  408. devices.append(value.split("\\").at(2));
  409. }
  410. }
  411. device = device.remove(0, 3);
  412. int index = device.toInt() - 1;
  413. if(index >= 0 && index < devices.size())
  414. {
  415. QString deviceKey = QString(
  416. "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\USB\\VID_0683&PID_1450\\%1").
  417. arg(devices.at(index));
  418. QSettings deviceEntry(deviceKey, QSettings::NativeFormat);
  419. QString portString = deviceEntry.value("FriendlyName").toString();
  420. int rstart = portString.indexOf("COM");
  421. portString.remove(0, rstart + 3);
  422. portString.chop(1);
  423. if(portString.toInt() < 10)
  424. {
  425. imp->device = QString("DI10%1NT.DLL").arg(portString);
  426. }
  427. else
  428. {
  429. imp->device = QString("DI1%1NT.DLL").arg(portString);
  430. }
  431. imp->deviceNumber = 0x12C02D00;
  432. imp->deviceNumber += portString.toInt();
  433. imp->ready = true;
  434. }
  435. else
  436. {
  437. imp->error = 8; // Failed to find device.
  438. }
  439. }
  440. @ Once the |DataqSdkDevice| is created, one or more channels can be added.
  441. @<DataqSdkDevice implementation@>=
  442. Channel* DataqSdkDevice::newChannel(Units::Unit scale)
  443. {
  444. Channel *retval = NULL;
  445. if(imp->ready)
  446. {
  447. retval = new Channel();
  448. imp->channelMap[imp->channels] = retval;
  449. imp->unitMap[imp->channels] = scale;
  450. imp->channels++;
  451. }
  452. return retval;
  453. }
  454. @ Once the channels are created, it is necessary to set the clock rate of the
  455. device. The DATAQ SDK will set the clock rate to be whichever value is closest
  456. to the specified value that is supported by the hardware. Note that when
  457. measuring multiple channels the device clock rate should be the desired sample
  458. rate per channel multiplied by the number of channels.
  459. The amount of time between measurements may vary slightly. Tests have shown
  460. that while most measurements come within 1ms of the expected time, some
  461. measurements do not come in within 100ms of the expected time.
  462. @<DataqSdkDevice implementation@>=
  463. void DataqSdkDevice::setClockRate(double Hz)
  464. {
  465. imp->sampleRate = Hz;
  466. }
  467. @ The destructor instructs the measurement thread to stop, waits for it to
  468. finish, and resets the device. If this is not done, an error would be issued
  469. the next time a program attempted to use the device.
  470. @<DataqSdkDevice implementation@>=
  471. DataqSdkDevice::~DataqSdkDevice()
  472. {
  473. if(imp->ready)
  474. {
  475. imp->ready = false;
  476. }
  477. imp->wait(ULONG_MAX);
  478. delete imp;
  479. }
  480. @ The constructor and destructor in |DataqSdkDeviceImplementation| currently
  481. limit the number of channels to 4. As additional devices are supported this
  482. restriction should be lifted.
  483. Very little is needed from the constructor. The destructor is responsible for
  484. closing the device and unloading the device driver.
  485. @<DataqSdkDevice implementation@>=
  486. DataqSdkDeviceImplementation::DataqSdkDeviceImplementation() : QThread(NULL),
  487. channelMap(4), error(0), channels(0), ready(false), unitMap(4)
  488. {
  489. /* Nothing needs to be done here. */
  490. }
  491. DataqSdkDeviceImplementation::~DataqSdkDeviceImplementation()
  492. {
  493. if(isOpen)
  494. {
  495. di_stop_scan();
  496. di_close();
  497. }
  498. if(driver->isLoaded())
  499. {
  500. driver->unload();
  501. }
  502. }
  503. @ This is exposed to the scripting engine in the usual way.
  504. @<Function prototypes for scripting@>=
  505. QScriptValue constructDataqSdkDevice(QScriptContext *context, QScriptEngine *engine);
  506. QScriptValue DataqSdkDevice_newChannel(QScriptContext *context, QScriptEngine *engine);
  507. void setDataqSdkDeviceProperties(QScriptValue value, QScriptEngine *engine);
  508. @ These functions are made known to the scripting engine.
  509. @<Set up the scripting engine@>=
  510. constructor = engine->newFunction(constructDataqSdkDevice);
  511. value = engine->newQMetaObject(&DataqSdkDevice::staticMetaObject, constructor);
  512. engine->globalObject().setProperty("DataqSdkDevice", value);
  513. @ When creating a new device we make sure that it is owned by the script
  514. engine. This is necessary to ensure that the destructor is called before \pn{}
  515. exits. Just as the constructor requires an argument that specifies the device
  516. name, the constructor available from a script also requires this argument.
  517. @<Functions for scripting@>=
  518. QScriptValue constructDataqSdkDevice(QScriptContext *context, QScriptEngine *engine)
  519. {
  520. QScriptValue object;
  521. if(context->argumentCount() == 1)
  522. {
  523. object = engine->newQObject(new DataqSdkDevice(argument<QString>(0, context)),
  524. QScriptEngine::ScriptOwnership);
  525. setDataqSdkDeviceProperties(object, engine);
  526. }
  527. else
  528. {
  529. context->throwError("Incorrect number of arguments passed to "
  530. "DataqSdkDevice. The constructor takes one string "
  531. "as an argument specifying a device name. "
  532. "Example: Dev1");
  533. }
  534. return object;
  535. }
  536. @ As |DataqSdkDevice| inherits |QObject| we add the |newChannel()| property
  537. after adding any |QObject| properties.
  538. @<Functions for scripting@>=
  539. void setDataqSdkDeviceProperties(QScriptValue value, QScriptEngine *engine)
  540. {
  541. setQObjectProperties(value, engine);
  542. value.setProperty("newChannel", engine->newFunction(DataqSdkDevice_newChannel));
  543. }
  544. @ The |newChannel()| wrapper requires two arguments.
  545. @<Functions for scripting@>=
  546. QScriptValue DataqSdkDevice_newChannel(QScriptContext *context, QScriptEngine *engine)
  547. {
  548. DataqSdkDevice *self = getself<DataqSdkDevice *>(context);
  549. QScriptValue object;
  550. if(self)
  551. {
  552. object = engine->newQObject(self->newChannel((Units::Unit)argument<int>(0, context)));
  553. setChannelProperties(object, engine);
  554. }
  555. return object;
  556. }