@@ -200,7 +200,7 @@ int ADMTController::getChannelIndex(const char *deviceName, const char *channelN
200
200
201
201
double ADMTController::getChannelValue (const char *deviceName, const char *channelName, int bufferSize)
202
202
{
203
- double value;
203
+ double value = UINT32_MAX ;
204
204
const char *scaleAttrName = " scale" ;
205
205
const char *offsetAttrName = " offset" ;
206
206
size_t samples = bufferSize;
@@ -218,10 +218,10 @@ double ADMTController::getChannelValue(const char *deviceName, const char *chann
218
218
return UINT32_MAX;
219
219
if (iio_context_get_devices_count (m_iioCtx) < 1 )
220
220
return UINT32_MAX;
221
- struct iio_device *admtDevice = iio_context_find_device (m_iioCtx, deviceName);
222
- if (admtDevice == NULL )
221
+ struct iio_device *iioDevice = iio_context_find_device (m_iioCtx, deviceName);
222
+ if (iioDevice == NULL )
223
223
return UINT32_MAX;
224
- struct iio_channel *channel = iio_device_find_channel (admtDevice , channelName, isOutput);
224
+ struct iio_channel *channel = iio_device_find_channel (iioDevice , channelName, isOutput);
225
225
if (channel == NULL )
226
226
return UINT32_MAX;
227
227
iio_channel_enable (channel);
@@ -238,33 +238,37 @@ double ADMTController::getChannelValue(const char *deviceName, const char *chann
238
238
offsetAttrValue = atoi (offsetDst);
239
239
delete[] offsetDst;
240
240
241
- struct iio_buffer *buffer = iio_device_create_buffer (admtDevice , samples, isCyclic); // Create a buffer
241
+ struct iio_buffer *buffer = iio_device_create_buffer (iioDevice , samples, isCyclic); // Create a buffer
242
242
ssize_t numBytesRead;
243
243
char *pointerData, *pointerEnd;
244
244
ptrdiff_t pointerIncrement;
245
245
246
246
numBytesRead = iio_buffer_refill (buffer);
247
247
248
- pointerIncrement = iio_buffer_step (buffer);
249
- pointerEnd = static_cast <char *>(iio_buffer_end (buffer));
250
-
251
- const struct iio_data_format *format = iio_channel_get_data_format (channel);
252
- unsigned int repeat = has_repeat ? format->repeat : 1 ;
253
-
254
- for (pointerData = static_cast <char *>(iio_buffer_first (buffer, channel)); pointerData < pointerEnd;
255
- pointerData += pointerIncrement) {
256
- for (int j = 0 ; j < repeat; j++) {
257
- if (format->bits <= 8 ) {
258
- int8_t rawValue = (reinterpret_cast <int8_t *>(pointerData))[j];
259
- values.push_back ((rawValue - offsetAttrValue) * *scaleAttrValue);
260
- } else if (format->length / 8 == sizeof (int16_t )) {
261
- int16_t rawValue = (reinterpret_cast <int16_t *>(pointerData))[j];
262
- values.push_back ((rawValue - offsetAttrValue) * *scaleAttrValue);
248
+ if (numBytesRead >= 0 ) {
249
+ pointerIncrement = iio_buffer_step (buffer);
250
+ pointerEnd = static_cast <char *>(iio_buffer_end (buffer));
251
+
252
+ const struct iio_data_format *format = iio_channel_get_data_format (channel);
253
+ unsigned int repeat = has_repeat ? format->repeat : 1 ;
254
+
255
+ for (pointerData = static_cast <char *>(iio_buffer_first (buffer, channel)); pointerData < pointerEnd;
256
+ pointerData += pointerIncrement) {
257
+ for (int j = 0 ; j < repeat; j++) {
258
+ if (format->bits <= 8 ) {
259
+ int8_t rawValue = (reinterpret_cast <int8_t *>(pointerData))[j];
260
+ values.push_back ((rawValue - offsetAttrValue) * *scaleAttrValue);
261
+ } else if (format->is_fully_defined ) {
262
+ values.push_back ((reinterpret_cast <int16_t *>(pointerData))[j]);
263
+ } else if (format->length / 8 == sizeof (int16_t )) {
264
+ int16_t rawValue = (reinterpret_cast <int16_t *>(pointerData))[j];
265
+ values.push_back ((rawValue - offsetAttrValue) * *scaleAttrValue);
266
+ }
263
267
}
264
268
}
265
- }
266
269
267
- value = values[bufferSize - 1 ];
270
+ value = values[bufferSize - 1 ];
271
+ }
268
272
269
273
delete scaleAttrValue;
270
274
iio_buffer_destroy (buffer);
@@ -398,6 +402,43 @@ int ADMTController::readDeviceRegistry(const char *deviceName, uint32_t address,
398
402
return result;
399
403
}
400
404
405
+ int ADMTController::readDeviceRegistry (const char *deviceName, uint32_t address, uint8_t page, uint32_t *returnValue)
406
+ {
407
+ if (!m_iioCtx)
408
+ return -1 ;
409
+ if (address == UINT32_MAX)
410
+ return -1 ;
411
+
412
+ int result = -1 ;
413
+ int deviceCount = iio_context_get_devices_count (m_iioCtx);
414
+ if (deviceCount == 0 ) {
415
+ return result;
416
+ }
417
+ iio_device *iioDevice = iio_context_find_device (m_iioCtx, deviceName);
418
+ if (iioDevice == NULL ) {
419
+ return result;
420
+ }
421
+
422
+ if (page != UINT8_MAX) {
423
+ uint32_t *readCNVValue = new uint32_t ;
424
+
425
+ if (iio_device_reg_read (iioDevice, getConfigurationRegister (ConfigurationRegister::CNVPAGE),
426
+ readCNVValue) == 0 ) {
427
+ iio_device_reg_write (iioDevice, getConfigurationRegister (ConfigurationRegister::CNVPAGE),
428
+ changeCNVPage (static_cast <uint16_t >(*readCNVValue), page));
429
+ } else {
430
+ delete readCNVValue;
431
+ return -1 ;
432
+ }
433
+
434
+ delete readCNVValue;
435
+ }
436
+
437
+ result = iio_device_reg_read (iioDevice, address, returnValue);
438
+
439
+ return result;
440
+ }
441
+
401
442
/* bit reversal from online example */
402
443
unsigned int ADMTController::bitReverse (unsigned int x, int log2n)
403
444
{
@@ -1175,19 +1216,21 @@ uint16_t ADMTController::setGeneralRegisterBitMapping(uint16_t currentRegisterVa
1175
1216
1176
1217
int ADMTController::getAbsAngleTurnCount (uint16_t registerValue)
1177
1218
{
1178
- // Bits 15:8: Turn count in quarter turns
1179
- uint8_t turnCount = ( registerValue >> 8 ) & 0xFC ;
1219
+ // Bits 15:10: Number of whole turns
1220
+ int8_t turnCount = registerValue >> 10 ;
1180
1221
1181
- if (turnCount <= 0xD4 ) {
1222
+ if (turnCount <= 0x35 ) {
1182
1223
// Straight binary turn count
1183
- return turnCount / 4 ; // Convert from quarter turns to whole turns
1184
- } else if (turnCount == 0xD8 ) {
1224
+ return turnCount; // Convert from quarter turns to whole turns
1225
+ } else if (turnCount == 0x36 ) {
1185
1226
// Invalid turn count
1186
- return turnCount / 4 ;
1227
+ return turnCount;
1187
1228
} else {
1188
1229
// 2's complement turn count
1189
- int8_t signedTurnCount = static_cast <int8_t >(turnCount); // Handle as signed value
1190
- return signedTurnCount / 4 ; // Convert from quarter turns to whole turns
1230
+ if (turnCount & (1 << 5 )) {
1231
+ turnCount -= 64 ;
1232
+ }
1233
+ return turnCount;
1191
1234
}
1192
1235
}
1193
1236
@@ -1740,14 +1783,15 @@ int ADMTController::streamIO()
1740
1783
return 0 ;
1741
1784
}
1742
1785
1743
- int ADMTController::streamChannel (const char *deviceName, const QVector<QString> channelNames, int bufferSize)
1786
+ int ADMTController::streamChannel (const char *deviceName, const QVector<QString> channelNames, int bufferSize,
1787
+ int sampleRate)
1744
1788
{
1745
1789
int result = -1 ;
1746
1790
const char *scaleAttrName = " scale" ;
1747
1791
const char *offsetAttrName = " offset" ;
1748
1792
size_t samples = bufferSize;
1749
1793
vector<double > values;
1750
- bool isOutput = false , isCyclic = true ;
1794
+ bool isOutput = false , isCyclic = false ;
1751
1795
1752
1796
unsigned int i, j, major, minor;
1753
1797
char git_tag[8 ];
@@ -1803,6 +1847,7 @@ int ADMTController::streamChannel(const char *deviceName, const QVector<QString>
1803
1847
1804
1848
QMap<QString, double > streamDataMap;
1805
1849
while (!stopStream.loadAcquire ()) {
1850
+ elapsedStreamTimer.start ();
1806
1851
for (int i = 0 ; i < channels.size (); i++) {
1807
1852
values.clear ();
1808
1853
@@ -1839,6 +1884,11 @@ int ADMTController::streamChannel(const char *deviceName, const QVector<QString>
1839
1884
}
1840
1885
1841
1886
Q_EMIT streamChannelData (streamDataMap);
1887
+
1888
+ qint64 elapsed = elapsedStreamTimer.elapsed ();
1889
+ while (elapsed < sampleRate) {
1890
+ elapsed = elapsedStreamTimer.elapsed ();
1891
+ }
1842
1892
}
1843
1893
1844
1894
iio_buffer_destroy (buffer);
0 commit comments