mhz16: Split the getData(int* gas, int *temp) function into two separate functions and updated the C++ an Java examples

Signed-off-by: Stefan Andritoiu <stefan.andritoiu@intel.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Stefan Andritoiu 2015-11-10 13:44:42 +02:00 committed by Mihai Tudor Panu
parent f8a105adbf
commit 7f7fdb8441
4 changed files with 45 additions and 19 deletions

View File

@ -62,14 +62,15 @@ int main (int argc, char **argv)
sleep(1); sleep(1);
int gas;
int temp;
while (shouldRun) while (shouldRun)
{ {
co2->getData(&gas, &temp); if(!co2->getData()){
cout << "CO2 concentration: " << gas << " PPM, " cerr << "Failed to retrieve data" << endl;
<< "Temperature (in C): " << temp <<endl; continue;
}
cout << "CO2 concentration: " << co2->getGas() << " PPM, "
<< "Temperature (in C): " << co2->getTemperature() <<endl;
sleep(2); sleep(2);
} }

View File

@ -36,9 +36,6 @@ public class MHZ16Sample {
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
// ! [Interesting] // ! [Interesting]
int[] gas = new int[1];
int[] temp = new int[1];
// Instantiate a MHZ16 serial CO2 sensor on uart 0. // Instantiate a MHZ16 serial CO2 sensor on uart 0.
upm_mhz16.MHZ16 co2 = new upm_mhz16.MHZ16(0); upm_mhz16.MHZ16 co2 = new upm_mhz16.MHZ16(0);
@ -48,9 +45,12 @@ public class MHZ16Sample {
System.out.println("but rather the temperature of the sensor elements."); System.out.println("but rather the temperature of the sensor elements.");
while (true) { while (true) {
co2.getData(gas, temp); if (!co2.getData()) {
System.out.println("CO2 concentration: " + gas[0] + "PPM, Temperature (in C): " System.out.println("Failed to retrieve data");
+ temp[0]); continue;
}
System.out.println("CO2 concentration: " + co2.getGas() + "PPM, Temperature (in C): "
+ co2.getTemperature());
Thread.sleep(2000); Thread.sleep(2000);
} }

View File

@ -183,7 +183,7 @@ bool MHZ16::verifyPacket(uint8_t *pkt, int len)
return true; return true;
} }
bool MHZ16::getData(int *gas, int *temp) bool MHZ16::getData()
{ {
// the query command // the query command
const unsigned char cmd[9] = const unsigned char cmd[9] =
@ -214,12 +214,22 @@ bool MHZ16::getData(int *gas, int *temp)
verifyPacket(packet, sizeof(packet)); verifyPacket(packet, sizeof(packet));
// pull out the data and return it. // pull out the data and return it.
*gas = (packet[2] << 8) | packet[3]; gas = (packet[2] << 8) | packet[3];
*temp = packet[4] - 40; temp = packet[4] - 40;
return true; return true;
} }
int MHZ16::getGas()
{
return gas;
}
int MHZ16::getTemperature()
{
return temp;
}
void MHZ16::calibrateZeroPoint() void MHZ16::calibrateZeroPoint()
{ {
// the query command // the query command

View File

@ -135,14 +135,27 @@ namespace upm {
bool verifyPacket(uint8_t *pkt, int len); bool verifyPacket(uint8_t *pkt, int len);
/** /**
* Queries the sensor and returns gas (CO2) concentration and * Queries the sensor and gets the gas (CO2) concentration and
* temperature data. * temperature data.
* Should be called before other "get" functions.
* *
* @param gas Returned gas concentration
* @param temp Returned temperature in Celsius
* @return True if successful * @return True if successful
*/ */
bool getData(int *gas, int *temp); bool getData();
/**
* Returns the gas (CO2) concentration data.
*
* @return Gas concentration
*/
int getGas();
/**
* Returns the temperature data.
*
* @return Temperature in Celsius
*/
int getTemperature();
/** /**
* Sets the zero point of the sensor * Sets the zero point of the sensor
@ -156,6 +169,8 @@ namespace upm {
private: private:
mraa_uart_context m_uart; mraa_uart_context m_uart;
int m_ttyFd; int m_ttyFd;
int gas;
int temp;
}; };
} }