Skip to content

Commit e69f542

Browse files
committed
admt: Updated channel read value
Signed-off-by: John Lloyd Juanillo <[email protected]>
1 parent 01409e8 commit e69f542

File tree

1 file changed

+42
-84
lines changed

1 file changed

+42
-84
lines changed

plugins/admt/src/admtcontroller.cpp

Lines changed: 42 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -202,111 +202,69 @@ int ADMTController::getChannelIndex(const char *deviceName, const char *channelN
202202

203203
double ADMTController::getChannelValue(const char *deviceName, const char *channelName, int bufferSize)
204204
{
205-
if(!m_iioCtx) {
206-
return UINT32_MAX;
207-
} // return QString("No context available.");
208205
double value;
206+
const char *scaleAttrName = "scale";
207+
const char *offsetAttrName = "offset";
208+
size_t samples = 1;
209+
bool isOutput = false, isCyclic = false;
209210

210-
int deviceCount = iio_context_get_devices_count(m_iioCtx);
211-
if(deviceCount < 1)
212-
return UINT32_MAX; // return QString("No devices found");
213-
214-
iio_device *admtDevice = iio_context_find_device(m_iioCtx, deviceName);
215-
if(admtDevice == NULL)
216-
return UINT32_MAX; // return QString("No ADMT4000 device");
217-
218-
int channelCount = iio_device_get_channels_count(admtDevice);
219-
if(channelCount < 1)
220-
return UINT32_MAX; // return QString("No channels found.");
211+
unsigned int i, j, major, minor;
212+
char git_tag[8];
213+
iio_library_get_version(&major, &minor, git_tag);
214+
bool has_repeat = ((major * 10000) + minor) >= 8 ? true : false;
221215

222-
iio_channel *channel;
223-
std::string message = "";
224-
for(int i = 0; i < channelCount; i++) {
225-
channel = iio_device_get_channel(admtDevice, i);
226-
const char *deviceChannel = iio_channel_get_id(channel);
216+
int offsetAttrValue = 0;
227217

228-
if(deviceChannel != nullptr && std::string(deviceChannel) == std::string(channelName)) {
229-
message = message + "[" + std::to_string(i) + "]" + std::string(deviceChannel) + ", ";
230-
break;
231-
} else {
232-
channel = NULL;
233-
}
234-
}
218+
if(!m_iioCtx)
219+
return UINT32_MAX;
220+
if(iio_context_get_devices_count(m_iioCtx) < 1)
221+
return UINT32_MAX;
222+
struct iio_device *admtDevice = iio_context_find_device(m_iioCtx, deviceName);
223+
if(admtDevice == NULL)
224+
return UINT32_MAX;
225+
struct iio_channel *channel = iio_device_find_channel(admtDevice, channelName, isOutput);
235226
if(channel == NULL)
236-
return UINT32_MAX; // return QString("Channel not found.");
227+
return UINT32_MAX;
237228
iio_channel_enable(channel);
238-
239-
double scale = 1.0;
240-
int offsetAttrVal = 0;
241-
const char *scaleAttrName = "scale";
242-
const char *offsetAttrName = "offset";
243-
const char *scaleAttr = iio_channel_find_attr(channel, scaleAttrName);
244-
if(scaleAttr == NULL)
245-
return UINT32_MAX; // return QString("No scale attribute");
246-
const char *offsetAttr = iio_channel_find_attr(channel, offsetAttrName);
247-
if(offsetAttr == NULL)
248-
return UINT32_MAX; // return QString("No offset attribute");
249-
250-
double *scaleVal = new double(1);
251-
int scaleRet = iio_channel_attr_read_double(channel, scaleAttr, scaleVal);
229+
double *scaleAttrValue = new double(1);
230+
int scaleRet = iio_channel_attr_read_double(channel, scaleAttrName, scaleAttrValue); // Read the scale attribute
252231
if(scaleRet != 0) {
253-
delete scaleVal;
254-
return UINT32_MAX; // return QString("Cannot read scale attribute");
232+
delete scaleAttrValue;
233+
return scaleRet;
255234
}
256-
scale = *scaleVal;
257-
delete scaleVal;
258235

259236
char *offsetDst = new char[maxAttrSize];
260-
iio_channel_attr_read(channel, offsetAttr, offsetDst, maxAttrSize);
261-
offsetAttrVal = std::atoi(offsetDst);
237+
iio_channel_attr_read(channel, offsetAttrName, offsetDst,
238+
maxAttrSize); // Read the offset attribute
239+
offsetAttrValue = atoi(offsetDst);
262240
delete[] offsetDst;
263241

264-
iio_buffer *iioBuffer = iio_device_create_buffer(admtDevice, bufferSize, false);
265-
if(iioBuffer == NULL)
266-
return UINT32_MAX; // return QString("Cannot create buffer.");
267-
242+
struct iio_buffer *buffer = iio_device_create_buffer(admtDevice, samples, isCyclic); // Create a buffer
268243
ssize_t numBytesRead;
269-
int8_t *pointerData, *pointerEnd;
270-
void *buffer;
244+
char *pointerData, *pointerEnd;
271245
ptrdiff_t pointerIncrement;
272246

273-
numBytesRead = iio_buffer_refill(iioBuffer);
274-
if(numBytesRead < 0)
275-
return UINT32_MAX; // return QString("Cannot refill buffer.");
247+
numBytesRead = iio_buffer_refill(buffer);
248+
249+
pointerIncrement = iio_buffer_step(buffer);
250+
pointerEnd = static_cast<char *>(iio_buffer_end(buffer));
276251

277252
const struct iio_data_format *format = iio_channel_get_data_format(channel);
278-
const struct iio_data_format channelFormat = *format;
279-
unsigned int repeat = channelFormat.repeat;
280-
281-
QString result;
282-
std::list<char> rawSamples;
283-
// std::list<uint16_t> unsignedSamples;
284-
std::list<int16_t> castSamples;
285-
286-
size_t sample, bytes;
287-
288-
size_t sampleSize = channelFormat.length / 8 * repeat;
289-
// if(sampleSize == 0) return QString("Sample size is zero.");
290-
291-
buffer = malloc(sampleSize * bufferSize);
292-
// if(!buffer) return QString("Cannot allocate memory for buffer.");
293-
294-
bytes = iio_channel_read(channel, iioBuffer, buffer, sampleSize * bufferSize);
295-
for(sample = 0; sample < bytes / sampleSize; ++sample) {
296-
for(int j = 0; j < repeat; ++j) {
297-
if(channelFormat.length / 8 == sizeof(int16_t)) {
298-
rawSamples.push_back(*((int8_t *)buffer));
299-
int16_t rawValue = ((int16_t *)buffer)[sample + j];
300-
castSamples.push_back(rawValue);
301-
value = (rawValue - static_cast<int16_t>(offsetAttrVal)) * scale;
302-
result = QString::number(value);
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->length / 8 == sizeof(int16_t)) {
259+
int16_t rawValue = (reinterpret_cast<int16_t *>(pointerData))[j];
260+
value = (rawValue - offsetAttrValue) * *scaleAttrValue;
303261
}
304262
}
305263
}
306264

307-
message = message + result.toStdString();
308-
iio_buffer_destroy(iioBuffer);
309-
return value; // QString::fromStdString(message);
265+
delete scaleAttrValue;
266+
iio_buffer_destroy(buffer);
267+
return value;
310268
}
311269

312270
/** @brief Get the attribute value of a device

0 commit comments

Comments
 (0)