#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
char iiotype[16][32] = {
'in_voltage1_raw',
'in_voltage2_raw',
};
// return = 0: voltage value from AIN1, unit: mV
// < 0: failed
int read_AIN1(float *fvoltage)
{
int value, ret = 0;
char filename[80];
FILE *fp;
char buf[20];
sprintf( filename, '/sys/bus/iio/devices/iio:device0/%s', iiotype[0]);
fp = fopen(filename, 'rt' );
if( fp==NULL )
{
printf('open %s fail!\n', filename);
*fvoltage = 0.0;
ret = -1;
return ret;
}
fread( buf, 1, sizeof(buf), fp );
fclose(fp);
// convert to integer
sscanf( buf, '%d', &value );
*fvoltage = 0.8 * value;
return ret;
}
// return = 0: voltage value from AIN2, unit: mV
// < 0: failed
int read_AIN2(float *fvoltage)
{
int value, ret = 0;
char filename[80];
FILE *fp;
char buf[20];
sprintf( filename, '/sys/bus/iio/devices/iio:device0/%s', iiotype[1]);
fp = fopen(filename, 'rt' );
if( fp==NULL )
{
printf('open %s fail!\n', filename);
*fvoltage = 0.0;
ret = -1;
return ret;
}
fread( buf, 1, sizeof(buf), fp );
fclose(fp);
// convert to integer
sscanf( buf, '%d', &value );
*fvoltage = 0.8 * value;
return ret;
}
int main(int argc, char** argv)
{
int ret = 0;
float fvalue;
// read AIN1
ret = read_AIN1(&fvalue);
if(ret < 0)
{
return ret;
}
printf('AIN1 = %.2f mV\n', fvalue);
// read AIN2
ret = read_AIN2(&fvalue);
if(ret < 0)
{
return ret;
}
}