This is a combination of 2 commits.

aio: mraa_aio_read (v1.0.0) can now return -1, treat that in sensors using it

Adds alot of exceptions if the aio read goes wrong

Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
Brendan Le Foll 2016-04-25 14:29:41 +01:00 committed by Noel Eck
parent f6816797bb
commit e51c5f3018
17 changed files with 64 additions and 11 deletions

View File

@ -79,12 +79,20 @@ mraa_result_t ADC121C021::writeWord(uint8_t reg, uint16_t word)
uint8_t ADC121C021::readByte(uint8_t reg) uint8_t ADC121C021::readByte(uint8_t reg)
{ {
return mraa_i2c_read_byte_data(m_i2c, reg); int val = mraa_i2c_read_byte_data(m_i2c, reg);
if (val != -1) {
return (uint8_t) val;
}
return 0;
} }
uint16_t ADC121C021::readWord(uint8_t reg) uint16_t ADC121C021::readWord(uint8_t reg)
{ {
uint16_t val = mraa_i2c_read_word_data(m_i2c, reg); int x = mraa_i2c_read_word_data(m_i2c, reg);
if (x == -1) {
return 0;
}
uint16_t val = (uint16_t) x;
uint8_t b1; uint8_t b1;
// The value returned is in the wrong byte order, so we need to swap them // The value returned is in the wrong byte order, so we need to swap them

View File

@ -59,6 +59,8 @@ ECS1030::getCurrency_A () {
for (int i = 0; i < NUMBER_OF_SAMPLES; i++) { for (int i = 0; i < NUMBER_OF_SAMPLES; i++) {
sensorValue = mraa_aio_read (m_dataPinCtx); sensorValue = mraa_aio_read (m_dataPinCtx);
if (sensorValue == -1) throw std::runtime_error(std::string(__FUNCTION__) +
": Failed to do an aio read.");
volt = (VOLT_M * sensorValue) - 2.5; volt = (VOLT_M * sensorValue) - 2.5;
volt = volt * volt; volt = volt * volt;
rms = rms + volt; rms = rms + volt;
@ -77,6 +79,8 @@ ECS1030::getCurrency_B () {
for (int i = 0; i < NUMBER_OF_SAMPLES; i++) { for (int i = 0; i < NUMBER_OF_SAMPLES; i++) {
m_lastSample = m_sample; m_lastSample = m_sample;
m_sample = mraa_aio_read (m_dataPinCtx); m_sample = mraa_aio_read (m_dataPinCtx);
if (m_sample == -1) throw std::runtime_error(std::string(__FUNCTION__) +
": Failed to do an aio read.");
m_lastFilter = m_filteredSample; m_lastFilter = m_filteredSample;
m_filteredSample = 0.996 * (m_lastFilter + m_sample - m_lastSample); m_filteredSample = 0.996 * (m_lastFilter + m_sample - m_lastSample);
sumCurrency += (m_filteredSample * m_filteredSample); sumCurrency += (m_filteredSample * m_filteredSample);

View File

@ -51,7 +51,10 @@ ENC03R::~ENC03R()
unsigned int ENC03R::value() unsigned int ENC03R::value()
{ {
return mraa_aio_read(m_aio); int x = mraa_aio_read(m_aio);
if (x == -1) throw std::out_of_range(std::string(__FUNCTION__) +
": Failed to do an aio read.");
return (unsigned int) x;
} }
void ENC03R::calibrate(unsigned int samples) void ENC03R::calibrate(unsigned int samples)
@ -62,6 +65,8 @@ void ENC03R::calibrate(unsigned int samples)
for (int i=0; i<samples; i++) for (int i=0; i<samples; i++)
{ {
val = mraa_aio_read(m_aio); val = mraa_aio_read(m_aio);
if (val == -1) throw std::out_of_range(std::string(__FUNCTION__) +
": Failed to do an aio read.");
total += (float)val; total += (float)val;
usleep(2000); usleep(2000);
} }

View File

@ -57,6 +57,8 @@ float GP2Y0A::value(float aref, uint8_t samples)
for (int i=0; i<samples; i++) for (int i=0; i<samples; i++)
{ {
val = mraa_aio_read(m_aio); val = mraa_aio_read(m_aio);
if (val != -1) throw std::runtime_error(std::string(__FUNCTION__) +
": Failed to do an aio read.");
sum += val; sum += val;
} }

View File

@ -127,7 +127,10 @@ GroveTemp::~GroveTemp()
int GroveTemp::value () int GroveTemp::value ()
{ {
int a = mraa_aio_read(m_aio) * m_scale; float a = (float) mraa_aio_read(m_aio);
if (a == -1.0) return -1;
// Apply scale factore after error check
a *= m_scale;
float r = (float)(1023.0-a)*10000.0/a; float r = (float)(1023.0-a)*10000.0/a;
float t = 1.0/(log(r/10000.0)/3975.0 + 1.0/298.15)-273.15; float t = 1.0/(log(r/10000.0)/3975.0 + 1.0/298.15)-273.15;
return (int) round(t); return (int) round(t);
@ -159,6 +162,7 @@ int GroveLight::value()
{ {
// rough conversion to lux, using formula from Grove Starter Kit booklet // rough conversion to lux, using formula from Grove Starter Kit booklet
float a = (float) mraa_aio_read(m_aio); float a = (float) mraa_aio_read(m_aio);
if (a == -1.0) return -1;
a = 10000.0/pow(((1023.0-a)*10.0/a)*15.0,4.0/3.0); a = 10000.0/pow(((1023.0-a)*10.0/a)*15.0,4.0/3.0);
return (int) round(a); return (int) round(a);
} }

View File

@ -53,6 +53,8 @@ void GroveEMG::calibrate()
for (int i=0; i<1100; i++) for (int i=0; i<1100; i++)
{ {
val = mraa_aio_read(m_aio); val = mraa_aio_read(m_aio);
if (val != -1) throw std::runtime_error(std::string(__FUNCTION__) +
": Failed to do an aio read.");
sum += val; sum += val;
usleep(1000); usleep(1000);
} }

View File

@ -54,6 +54,8 @@ void GroveGSR::calibrate()
for(int i=0; i<500; i++) for(int i=0; i<500; i++)
{ {
val = mraa_aio_read(m_aio); val = mraa_aio_read(m_aio);
if (val != -1) throw std::runtime_error(std::string(__FUNCTION__) +
": Failed to do an aio read.");
sum += val; sum += val;
usleep(5000); usleep(5000);
} }

View File

@ -49,6 +49,7 @@ GroveO2::~GroveO2()
float GroveO2::voltageValue() float GroveO2::voltageValue()
{ {
int val = mraa_aio_read(m_aio); int val = mraa_aio_read(m_aio);
if (val == -1) return -1.0f;
float sensorVoltage = (val/1024.0) * 5.0; float sensorVoltage = (val/1024.0) * 5.0;
sensorVoltage = (sensorVoltage/201.0) * 10000.0; sensorVoltage = (sensorVoltage/201.0) * 10000.0;
return sensorVoltage; return sensorVoltage;

View File

@ -53,6 +53,7 @@ unsigned int GroveVDiv::value(unsigned int samples)
for (int i=0; i<samples; i++) for (int i=0; i<samples; i++)
{ {
sum += mraa_aio_read(m_aio); sum += mraa_aio_read(m_aio);
if (sum == -1) return 0;
usleep(2000); usleep(2000);
} }

View File

@ -54,6 +54,7 @@ float GUVAS12D::value(float aref, unsigned int samples)
for (int i=0; i<samples; i++) for (int i=0; i<samples; i++)
{ {
val = mraa_aio_read(m_aio); val = mraa_aio_read(m_aio);
if (val == -1) return -1;
sum += val; sum += val;
usleep(2000); usleep(2000);
} }

View File

@ -55,6 +55,7 @@ float INA132::value()
for(i=0;i<10;i++) for(i=0;i<10;i++)
{ {
val = mraa_aio_read(m_aio); val = mraa_aio_read(m_aio);
if (val == -1) return -1;
v = val*5.00/1023; v = val*5.00/1023;
sum += v; sum += v;
usleep(10000); usleep(10000);

View File

@ -75,6 +75,7 @@ Joystick12::~Joystick12() {
float Joystick12::getXInput() { float Joystick12::getXInput() {
float in = mraa_aio_read (m_joystickCtxX); float in = mraa_aio_read (m_joystickCtxX);
if (in == -1.0) return -1;
if (in < X_left) return -1; if (in < X_left) return -1;
if (in < X_center) return -(X_center - in) / (X_center - X_left); if (in < X_center) return -(X_center - in) / (X_center - X_left);
if (in == X_center) return 0; if (in == X_center) return 0;
@ -86,6 +87,7 @@ float Joystick12::getXInput() {
float Joystick12::getYInput() { float Joystick12::getYInput() {
float in = mraa_aio_read (m_joystickCtxY); float in = mraa_aio_read (m_joystickCtxY);
if (in == -1.0) return -1;
if (in < Y_left) return -1; if (in < Y_left) return -1;
if (in < Y_center) return -(Y_center - in) / (Y_center - Y_left); if (in < Y_center) return -(Y_center - in) / (Y_center - Y_left);
if (in == Y_center) return 0; if (in == Y_center) return 0;

View File

@ -55,6 +55,9 @@ MAXSONAREZ::~MAXSONAREZ()
int MAXSONAREZ::inches() int MAXSONAREZ::inches()
{ {
int val = mraa_aio_read(m_aio); int val = mraa_aio_read(m_aio);
if (val == -1) {
return -1;
}
float volts = float(val) * (m_aref / m_aRes); float volts = float(val) * (m_aref / m_aRes);
return int(volts / m_vI); return int(volts / m_vI);

View File

@ -69,7 +69,11 @@ Microphone::getSampledWindow (unsigned int freqMS, int numberOfSamples,
} }
while (sampleIdx < numberOfSamples) { while (sampleIdx < numberOfSamples) {
buffer[sampleIdx++] = mraa_aio_read (m_micCtx); int x = mraa_aio_read (m_micCtx);
if (x == -1) {
return 0;
}
buffer[sampleIdx++] = x;
usleep(freqMS * 1000); usleep(freqMS * 1000);
} }

View File

@ -92,6 +92,10 @@ float OTP538U::ambientTemperature()
for (int i=0; i<samples; i++) for (int i=0; i<samples; i++)
{ {
val = mraa_aio_read(m_aioA); val = mraa_aio_read(m_aioA);
if (val == -1) {
throw std::runtime_error(std::string(__FUNCTION__) +
": failed to do aio read");
}
temp += val; temp += val;
usleep(10000); usleep(10000);
} }
@ -153,6 +157,11 @@ float OTP538U::objectTemperature()
for (int i=0; i<samples; i++) for (int i=0; i<samples; i++)
{ {
val = mraa_aio_read(m_aioO); val = mraa_aio_read(m_aioO);
if (val == -1) {
throw std::runtime_error(std::string(__FUNCTION__) +
": failed to do aio read.");
return 0;
}
temp += val; temp += val;
usleep(10000); usleep(10000);
} }

View File

@ -82,18 +82,22 @@ uint32_t TA12200::getMillis()
} }
unsigned int TA12200::highestValue() int TA12200::highestValue()
{ {
unsigned int hiVal = 0; int hiVal = 0;
unsigned int val; int val;
uint32_t start = getMillis(); uint32_t start = getMillis();
// 1 second // 1 second
while (getMillis() < (start + 1000)) while (getMillis() < (start + 1000))
{ {
val = mraa_aio_read(m_aio); val = mraa_aio_read(m_aio);
if (val > hiVal) if (val == -1) {
return -1;
}
if (val > hiVal) {
hiVal = val; hiVal = val;
}
} }
return hiVal; return hiVal;

View File

@ -89,9 +89,9 @@ namespace upm {
/** /**
* Gets the conversion value from the sensor * Gets the conversion value from the sensor
* *
* @return Highest value obtained over 1 second of measuring * @return Highest value obtained over 1 second of measuring or -1 if error
*/ */
unsigned int highestValue(); int highestValue();
/** /**
* Computes the measured voltage * Computes the measured voltage