@@ -200,7 +200,7 @@ int ADMTController::getChannelIndex(const char *deviceName, const char *channelN
200200
201201double ADMTController::getChannelValue (const char *deviceName, const char *channelName, int bufferSize)
202202{
203- double value;
203+ double value = UINT32_MAX ;
204204 const char *scaleAttrName = " scale" ;
205205 const char *offsetAttrName = " offset" ;
206206 size_t samples = bufferSize;
@@ -218,10 +218,10 @@ double ADMTController::getChannelValue(const char *deviceName, const char *chann
218218 return UINT32_MAX;
219219 if (iio_context_get_devices_count (m_iioCtx) < 1 )
220220 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 )
223223 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);
225225 if (channel == NULL )
226226 return UINT32_MAX;
227227 iio_channel_enable (channel);
@@ -238,33 +238,37 @@ double ADMTController::getChannelValue(const char *deviceName, const char *chann
238238 offsetAttrValue = atoi (offsetDst);
239239 delete[] offsetDst;
240240
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
242242 ssize_t numBytesRead;
243243 char *pointerData, *pointerEnd;
244244 ptrdiff_t pointerIncrement;
245245
246246 numBytesRead = iio_buffer_refill (buffer);
247247
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+ }
263267 }
264268 }
265- }
266269
267- value = values[bufferSize - 1 ];
270+ value = values[bufferSize - 1 ];
271+ }
268272
269273 delete scaleAttrValue;
270274 iio_buffer_destroy (buffer);
@@ -398,6 +402,43 @@ int ADMTController::readDeviceRegistry(const char *deviceName, uint32_t address,
398402 return result;
399403}
400404
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+
401442/* bit reversal from online example */
402443unsigned int ADMTController::bitReverse (unsigned int x, int log2n)
403444{
@@ -1175,19 +1216,21 @@ uint16_t ADMTController::setGeneralRegisterBitMapping(uint16_t currentRegisterVa
11751216
11761217int ADMTController::getAbsAngleTurnCount (uint16_t registerValue)
11771218{
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 ;
11801221
1181- if (turnCount <= 0xD4 ) {
1222+ if (turnCount <= 0x35 ) {
11821223 // 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 ) {
11851226 // Invalid turn count
1186- return turnCount / 4 ;
1227+ return turnCount;
11871228 } else {
11881229 // 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;
11911234 }
11921235}
11931236
@@ -1740,14 +1783,15 @@ int ADMTController::streamIO()
17401783 return 0 ;
17411784}
17421785
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)
17441788{
17451789 int result = -1 ;
17461790 const char *scaleAttrName = " scale" ;
17471791 const char *offsetAttrName = " offset" ;
17481792 size_t samples = bufferSize;
17491793 vector<double > values;
1750- bool isOutput = false , isCyclic = true ;
1794+ bool isOutput = false , isCyclic = false ;
17511795
17521796 unsigned int i, j, major, minor;
17531797 char git_tag[8 ];
@@ -1803,6 +1847,7 @@ int ADMTController::streamChannel(const char *deviceName, const QVector<QString>
18031847
18041848 QMap<QString, double > streamDataMap;
18051849 while (!stopStream.loadAcquire ()) {
1850+ elapsedStreamTimer.start ();
18061851 for (int i = 0 ; i < channels.size (); i++) {
18071852 values.clear ();
18081853
@@ -1839,6 +1884,11 @@ int ADMTController::streamChannel(const char *deviceName, const QVector<QString>
18391884 }
18401885
18411886 Q_EMIT streamChannelData (streamDataMap);
1887+
1888+ qint64 elapsed = elapsedStreamTimer.elapsed ();
1889+ while (elapsed < sampleRate) {
1890+ elapsed = elapsedStreamTimer.elapsed ();
1891+ }
18421892 }
18431893
18441894 iio_buffer_destroy (buffer);
0 commit comments