#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
char iiotype[16][32] = {
''in_voltage0_raw'',
'in_voltage1_raw',
'in_voltage2_raw',
'in_voltage3_raw',
'in_voltage4_raw',
'in_voltage5_raw',
'in_voltage6_raw',
'in_voltage7_raw'
};
// return = 0: voltage value from AIN0, unit: mV
// < 0: failed
int read_AIN0(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 = 3300 * 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[2]);
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 = 3300 * value;
return ret;
}
// return = 0: voltage value from AIN3, unit: mV
// < 0: failed
int read_AIN3(float *fvoltage)
{
int value, ret = 0;
char filename[80];
FILE *fp;
char buf[20];
sprintf( filename, '/sys/bus/iio/devices/iio:device0/%s', iiotype[3]);
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 = 3300 * value;
return ret;
}
// return = 0: voltage value from AIN4, unit: mV
// < 0: failed
int read_AIN4(float *fvoltage)
{
int value, ret = 0;
char filename[80];
FILE *fp;
char buf[20];
sprintf( filename, '/sys/bus/iio/devices/iio:device0/%s', iiotype[4]);
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 = 3300 * value;
return ret;
}
// return = 0: voltage value from AIN5, unit: mV
// < 0: failed
int read_AIN5(float *fvoltage)
{
int value, ret = 0;
char filename[80];
FILE *fp;
char buf[20];
sprintf( filename, '/sys/bus/iio/devices/iio:device0/%s', iiotype[5]);
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 = 3300 * value;
return ret;
}
int main(int argc, char** argv)
{
int ret = 0;
float fvalue;
printf('A5 IIO AD test v2.0\n');
// read AIN1
ret = read_AIN0(&fvalue);
if(ret < 0)
{
return ret;
}
printf('AIN1 = %.2f mV\n', fvalue);
// read AIN2
ret = read_AIN2(&fvalue);
if(ret < 0)
{
return ret;
}
printf('AIN2 = %.2f mV\n', fvalue);
// read AIN3
ret = read_AIN3(&fvalue);
if(ret < 0)
{
return ret;
}
printf('AIN3 = %.2f mV\n', fvalue);
// read AIN4
ret = read_AIN4(&fvalue);
if(ret < 0)
{
return ret;
}
printf('AIN4 = %.2f mV\n', fvalue);
// read AIN5
ret = read_AIN5(&fvalue);
if(ret < 0)
{
return ret;
}
printf('AIN5 = %.2f mV\n', fvalue);
return ret;
}