2929#include < cstring>
3030#include < iioutil/connectionprovider.h>
3131#include < iterator>
32- #include < list>
3332#include < math.h>
3433#include < numeric>
3534#include < string>
@@ -46,7 +45,6 @@ ADMTController::ADMTController(QString uri, QObject *parent)
4645 : QObject(parent)
4746 , uri(uri)
4847{
49- connect (this , &ADMTController::streamData, this , &ADMTController::handleStreamData);
5048 connect (this , &ADMTController::streamBufferedData, this , &ADMTController::handleStreamBufferedData);
5149}
5250
@@ -205,7 +203,8 @@ double ADMTController::getChannelValue(const char *deviceName, const char *chann
205203 double value;
206204 const char *scaleAttrName = " scale" ;
207205 const char *offsetAttrName = " offset" ;
208- size_t samples = 1 ;
206+ size_t samples = bufferSize;
207+ vector<double > values;
209208 bool isOutput = false , isCyclic = false ;
210209
211210 unsigned int i, j, major, minor;
@@ -255,15 +254,21 @@ double ADMTController::getChannelValue(const char *deviceName, const char *chann
255254 for (pointerData = static_cast <char *>(iio_buffer_first (buffer, channel)); pointerData < pointerEnd;
256255 pointerData += pointerIncrement) {
257256 for (int j = 0 ; j < repeat; j++) {
258- if (format->length / 8 == sizeof (int16_t )) {
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 )) {
259261 int16_t rawValue = (reinterpret_cast <int16_t *>(pointerData))[j];
260- value = ( rawValue - offsetAttrValue) * *scaleAttrValue;
262+ values. push_back (( rawValue - offsetAttrValue) * *scaleAttrValue) ;
261263 }
262264 }
263265 }
264266
267+ value = values[bufferSize - 1 ];
268+
265269 delete scaleAttrValue;
266270 iio_buffer_destroy (buffer);
271+ iio_channel_disable (channel);
267272 return value;
268273}
269274
@@ -1703,7 +1708,7 @@ int ADMTController::streamIO()
17031708 delete[] offsetDst;
17041709 struct iio_buffer *buffer = iio_device_create_buffer (admtDevice, samples, isCyclic); // Create a buffer
17051710
1706- while (!stopStream) {
1711+ while (!stopStream. loadAcquire () ) {
17071712 ssize_t numBytesRead;
17081713 char *pointerData, *pointerEnd;
17091714 ptrdiff_t pointerIncrement;
@@ -1735,20 +1740,131 @@ int ADMTController::streamIO()
17351740 return 0 ;
17361741}
17371742
1743+ int ADMTController::streamChannel (const char *deviceName, const QVector<QString> channelNames, int bufferSize)
1744+ {
1745+ int result = -1 ;
1746+ const char *scaleAttrName = " scale" ;
1747+ const char *offsetAttrName = " offset" ;
1748+ size_t samples = bufferSize;
1749+ vector<double > values;
1750+ bool isOutput = false , isCyclic = true ;
1751+
1752+ unsigned int i, j, major, minor;
1753+ char git_tag[8 ];
1754+ iio_library_get_version (&major, &minor, git_tag);
1755+ bool has_repeat = ((major * 10000 ) + minor) >= 8 ? true : false ;
1756+
1757+ if (!m_iioCtx)
1758+ return result; // Check if the context is valid
1759+ if (iio_context_get_devices_count (m_iioCtx) < 1 )
1760+ return result; // Check if there are devices in the context
1761+ struct iio_device *device = iio_context_find_device (m_iioCtx, deviceName); // Find the ADMT device
1762+
1763+ if (device == NULL )
1764+ return result;
1765+
1766+ QVector<struct iio_channel *> channels;
1767+ QVector<double > scales;
1768+ QVector<int > offsets;
1769+
1770+ for (int i = 0 ; i < channelNames.size (); i++) {
1771+ struct iio_channel *channel =
1772+ iio_device_find_channel (device, channelNames.at (i).toLocal8Bit ().data (), isOutput);
1773+ if (channel == NULL )
1774+ break ;
1775+ iio_channel_enable (channel);
1776+ channels.append (channel);
1777+ }
1778+
1779+ if (channels.size () == 0 && channels.size () != channelNames.size ())
1780+ return result;
1781+
1782+ double *scaleAttrValue = new double ();
1783+ for (int i = 0 ; i < channels.size (); i++) {
1784+ int scaleRet = iio_channel_attr_read_double (const_cast <iio_channel *>(channels[i]), scaleAttrName,
1785+ scaleAttrValue); // Read the scale attribute
1786+ if (scaleRet != 0 )
1787+ break ;
1788+ scales.insert (i, *scaleAttrValue);
1789+ }
1790+
1791+ delete scaleAttrValue;
1792+
1793+ for (int i = 0 ; i < channels.size (); i++) {
1794+ char *offsetDst = new char [maxAttrSize];
1795+ iio_channel_attr_read (const_cast <iio_channel *>(channels[i]), offsetAttrName, offsetDst,
1796+ maxAttrSize); // Read the offset attribute
1797+ int offsetAttrValue = atoi (offsetDst);
1798+ delete[] offsetDst;
1799+ offsets.insert (i, offsetAttrValue);
1800+ }
1801+
1802+ struct iio_buffer *buffer = iio_device_create_buffer (device, samples, isCyclic); // Create a buffer
1803+
1804+ QMap<QString, double > streamDataMap;
1805+ while (!stopStream.loadAcquire ()) {
1806+ for (int i = 0 ; i < channels.size (); i++) {
1807+ values.clear ();
1808+
1809+ ssize_t numBytesRead;
1810+ char *pointerData, *pointerEnd;
1811+ ptrdiff_t pointerIncrement;
1812+
1813+ numBytesRead = iio_buffer_refill (buffer);
1814+ if (numBytesRead < 0 )
1815+ break ;
1816+
1817+ pointerIncrement = iio_buffer_step (buffer);
1818+ pointerEnd = static_cast <char *>(iio_buffer_end (buffer));
1819+
1820+ const struct iio_data_format *format =
1821+ iio_channel_get_data_format (const_cast <iio_channel *>(channels[i]));
1822+ unsigned int repeat = has_repeat ? format->repeat : 1 ;
1823+
1824+ for (pointerData = static_cast <char *>(
1825+ iio_buffer_first (buffer, const_cast <iio_channel *>(channels[i])));
1826+ pointerData < pointerEnd; pointerData += pointerIncrement) {
1827+ for (int j = 0 ; j < repeat; j++) {
1828+ if (format->bits <= 8 ) {
1829+ int8_t rawValue = (reinterpret_cast <int8_t *>(pointerData))[j];
1830+ values.push_back ((rawValue - offsets.at (i)) * scales.at (i));
1831+ } else if (format->length / 8 == sizeof (int16_t )) {
1832+ int16_t rawValue = (reinterpret_cast <int16_t *>(pointerData))[j];
1833+ values.push_back ((rawValue - offsets.at (i)) * scales.at (i));
1834+ }
1835+ }
1836+ }
1837+
1838+ streamDataMap[channelNames.at (i)] = values[bufferSize - 1 ];
1839+ }
1840+
1841+ Q_EMIT streamChannelData (streamDataMap);
1842+ }
1843+
1844+ iio_buffer_destroy (buffer);
1845+
1846+ for (int i = 0 ; i < channels.size (); i++) {
1847+ iio_channel_disable (const_cast <iio_channel *>(channels[i]));
1848+ }
1849+
1850+ return 0 ;
1851+ }
1852+
17381853void ADMTController::handleStreamData (double value) { streamedValue = value; }
17391854
1740- void ADMTController::bufferedStreamIO (int totalSamples, int targetSampleRate)
1855+ void ADMTController::handleStreamChannelData (QMap<QString, double > dataMap) { streamedChannelDataMap = dataMap; }
1856+
1857+ void ADMTController::bufferedStreamIO (int totalSamples, int targetSampleRate, int bufferSize)
17411858{
1742- QVector<double > bufferedValues;
1743- vector<uint16_t > rawBufferedValues;
1859+ vector<double > values;
17441860 sampleCount = 0 ;
17451861
17461862 int result = -1 ;
17471863 const char *deviceName = " admt4000" ;
1748- const char *channelName = " rot " ;
1864+ const char *channelName = " angl " ;
17491865 const char *scaleAttrName = " scale" ;
17501866 const char *offsetAttrName = " offset" ;
1751- size_t samples = 1 ;
1867+ size_t samples = bufferSize ;
17521868 bool isOutput = false ;
17531869 bool isCyclic = true ;
17541870
@@ -1784,8 +1900,9 @@ void ADMTController::bufferedStreamIO(int totalSamples, int targetSampleRate)
17841900 offsetAttrValue = atoi (offsetDst);
17851901 struct iio_buffer *buffer = iio_device_create_buffer (admtDevice, samples, isCyclic); // Create a buffer
17861902
1787- while (!stopStream && sampleCount < totalSamples) {
1903+ while (!stopStream. loadAcquire () && sampleCount < totalSamples) {
17881904 elapsedStreamTimer.start ();
1905+ values.clear ();
17891906
17901907 ssize_t numBytesRead;
17911908 char *pointerData, *pointerEnd;
@@ -1806,40 +1923,36 @@ void ADMTController::bufferedStreamIO(int totalSamples, int targetSampleRate)
18061923 pointerData += pointerIncrement) {
18071924 for (j = 0 ; j < repeat; j++) {
18081925 if (format->length / 8 == sizeof (int16_t )) {
1809- rawBufferedValues.push_back ((reinterpret_cast <int16_t *>(pointerData))[j]);
1810- sampleCount++;
1811- continue ;
1926+ int16_t rawValue = (reinterpret_cast <int16_t *>(pointerData))[j];
1927+ values.push_back ((rawValue - offsetAttrValue) * *scaleAttrValue);
18121928 }
18131929 }
18141930 }
18151931
1932+ double value = values[bufferSize - 1 ];
1933+ Q_EMIT streamData (value);
1934+ sampleCount++;
1935+
18161936 qint64 elapsedNanoseconds = elapsedStreamTimer.nsecsElapsed ();
18171937 while (elapsedNanoseconds < targetSampleRate) {
18181938 elapsedNanoseconds = elapsedStreamTimer.nsecsElapsed ();
18191939 }
18201940 }
18211941 iio_buffer_destroy (buffer);
1822-
1823- for (int i = 0 ; i < rawBufferedValues.size (); i++) {
1824- double scaledValue = (rawBufferedValues[i] - offsetAttrValue) * *scaleAttrValue;
1825- bufferedValues.append (scaledValue);
1826- }
1827-
1828- Q_EMIT streamBufferedData (bufferedValues);
1942+ iio_channel_disable (channel);
18291943
18301944 delete scaleAttrValue;
18311945 delete[] offsetDst;
18321946}
18331947
18341948void ADMTController::registryStream (int totalSamples, int targetSampleRate)
18351949{
1836- QVector<double > values;
18371950 sampleCount = 0 ;
18381951 double angle = 0 ;
18391952 bool readSuccess = false ;
18401953 uint32_t *registerValue = new uint32_t ;
18411954
1842- while (!stopStream && sampleCount < totalSamples) {
1955+ while (!stopStream. loadAcquire () && sampleCount < totalSamples) {
18431956 elapsedStreamTimer.start ();
18441957
18451958 qint64 elapsedNanoseconds = elapsedStreamTimer.nsecsElapsed ();
@@ -1852,18 +1965,17 @@ void ADMTController::registryStream(int totalSamples, int targetSampleRate)
18521965
18531966 angle = getAngle (static_cast <uint16_t >(*registerValue));
18541967
1855- values.append (angle);
18561968 sampleCount++;
18571969
1970+ Q_EMIT streamData (angle);
1971+
18581972 elapsedNanoseconds = elapsedStreamTimer.nsecsElapsed ();
18591973 while (elapsedNanoseconds < targetSampleRate) {
18601974 elapsedNanoseconds = elapsedStreamTimer.nsecsElapsed ();
18611975 }
18621976 }
18631977
18641978 delete registerValue;
1865-
1866- Q_EMIT streamBufferedData (values);
18671979}
18681980
18691981void ADMTController::handleStreamBufferedData (const QVector<double > &value) { streamBufferedValues = value; }
0 commit comments