From 389bd32f814c54f5a2f0008d1df3c41e816d70e6 Mon Sep 17 00:00:00 2001 From: Henry Bruce Date: Wed, 6 Jan 2016 15:32:56 -0800 Subject: [PATCH] examples: Added c++ interface based sensor/actuator examples Signed-off-by: Henry Bruce Signed-off-by: Abhishek Malik --- examples/c++/CMakeLists.txt | 7 +++ examples/c++/adc-sensor.cxx | 76 ++++++++++++++++++++++ examples/c++/co2-sensor.cxx | 71 +++++++++++++++++++++ examples/c++/humidity-sensor.cxx | 82 ++++++++++++++++++++++++ examples/c++/light-controller.cxx | 98 +++++++++++++++++++++++++++++ examples/c++/light-sensor.cxx | 78 +++++++++++++++++++++++ examples/c++/pressure-sensor.cxx | 81 ++++++++++++++++++++++++ examples/c++/temperature-sensor.cxx | 90 ++++++++++++++++++++++++++ 8 files changed, 583 insertions(+) create mode 100644 examples/c++/adc-sensor.cxx create mode 100644 examples/c++/co2-sensor.cxx create mode 100644 examples/c++/humidity-sensor.cxx create mode 100644 examples/c++/light-controller.cxx create mode 100644 examples/c++/light-sensor.cxx create mode 100644 examples/c++/pressure-sensor.cxx create mode 100644 examples/c++/temperature-sensor.cxx diff --git a/examples/c++/CMakeLists.txt b/examples/c++/CMakeLists.txt index b2277035..182097d2 100644 --- a/examples/c++/CMakeLists.txt +++ b/examples/c++/CMakeLists.txt @@ -258,3 +258,10 @@ add_custom_example (ak8975-example ak8975.cxx mpu9150) add_custom_example (mpu9250-example mpu9250.cxx mpu9150) add_custom_example (groveledbar-example groveledbar.cxx my9221) add_custom_example (grovecircularled-example grovecircularled.cxx my9221) +add_custom_example (temperature-sensor-example temperature-sensor.cxx "si7005;bmp180;bme280") +add_custom_example (humidity-sensor-example humidity-sensor.cxx "si7005;bme280") +add_custom_example (pressure-sensor-example pressure-sensor.cxx "bmp180;bme280") +add_custom_example (co2-sensor-example co2-sensor.cxx "t6713") +#add_custom_example (adc-example adc-sensor.cxx ads1015) +add_custom_example (light-sensor-example light-sensor.cxx "si1132;max44009") +add_custom_example (light-controller-example light-controller.cxx "lp8860;ds1808lc;hlg150h") diff --git a/examples/c++/adc-sensor.cxx b/examples/c++/adc-sensor.cxx new file mode 100644 index 00000000..55139268 --- /dev/null +++ b/examples/c++/adc-sensor.cxx @@ -0,0 +1,76 @@ +/* + * Author: Henry Bruce + * Copyright (c) 2015 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include "ads1015.h" + +#define EDISON_I2C_BUS 1 +#define FT4222_I2C_BUS 0 + + +//! [Interesting] +// Simple example of using IADC to determine +// which sensor is present and return its name. +// IADC is then used to get readings from sensor + + +upm::IADC* getADC() +{ + upm::IADC* adc = NULL; + try { + adc = new upm::ADS1015(EDISON_I2C_BUS); + return adc; + } catch (std::exception& e) { + std::cerr << "ADS1015: " << e.what() << std::endl; + } + return adc; +} + +int main () +{ + upm::IADC* adc = getADC(); + if (adc == NULL) { + std::cout << "ADC not detected" << std::endl; + return 1; + } + std::cout << "ADC " << adc->getModuleName() << " detected. " ; + std::cout << adc->getNumInputs() << " inputs available" << std::endl; + while (true) { + for (int i=0; igetNumInputs(); ++i) { + std::cout << "Input " << i; + try { + float voltage = adc->getVoltage(i); + std::cout << ": Voltage = " << voltage << "V" << std::endl; + } catch (std::exception& e) { + std::cerr << e.what() << std::endl; + } + } + sleep(1); + } + delete adc; + return 0; +} + +//! [Interesting] \ No newline at end of file diff --git a/examples/c++/co2-sensor.cxx b/examples/c++/co2-sensor.cxx new file mode 100644 index 00000000..978e6c00 --- /dev/null +++ b/examples/c++/co2-sensor.cxx @@ -0,0 +1,71 @@ +/* + * Author: Henry Bruce + * Copyright (c) 2015 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include "t6713.h" + +#define EDISON_I2C_BUS 1 +#define FT4222_I2C_BUS 0 + +//! [Interesting] +// Simple example of using ICO2Sensor to determine +// which sensor is present and return its name. +// ICO2Sensor is then used to get readings from sensor + + +upm::ICO2Sensor* getCO2Sensor() +{ + upm::ICO2Sensor* cO2Sensor = NULL; + try { + cO2Sensor = new upm::T6713(mraa_get_sub_platform_id(FT4222_I2C_BUS)); + return cO2Sensor; + } catch (std::exception& e) { + std::cerr << "T6713: " << e.what() << std::endl; + } + return cO2Sensor; +} + +int main () +{ + upm::ICO2Sensor* cO2Sensor = getCO2Sensor(); + if (cO2Sensor == NULL) { + std::cout << "CO2 sensor not detected" << std::endl; + return 1; + } + std::cout << "CO2 sensor " << cO2Sensor->getModuleName() << " detected" << std::endl; + while (true) { + try { + uint16_t value = cO2Sensor->getPpm(); + std::cout << "CO2 level = " << value << " ppm" << std::endl; + } catch (std::exception& e) { + std::cerr << e.what() << std::endl; + } + sleep(1); + } + delete cO2Sensor; + return 0; +} + +//! [Interesting] diff --git a/examples/c++/humidity-sensor.cxx b/examples/c++/humidity-sensor.cxx new file mode 100644 index 00000000..241901d8 --- /dev/null +++ b/examples/c++/humidity-sensor.cxx @@ -0,0 +1,82 @@ +/* + * Author: Henry Bruce + * Copyright (c) 2015 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include "si7005.h" +#include "bme280.h" + +#define EDISON_I2C_BUS 1 +#define FT4222_I2C_BUS 0 + + #define EDISON_GPIO_SI7005_CS 20 + +//! [Interesting] +// Simple example of using ILightSensor to determine +// which sensor is present and return its name. +// ILightSensor is then used to get readings from sensor + + +upm::IHumiditySensor* getHumiditySensor() +{ + upm::IHumiditySensor* humiditySensor = NULL; + try { + humiditySensor = new upm::BME280 (mraa_get_sub_platform_id(FT4222_I2C_BUS)); + return humiditySensor ; + } catch (std::exception& e) + { + std::cerr <<"BME280: "<getModuleName() << " detected" << std::endl; + while (true) { + try { + int value = humiditySensor->getHumidityRelative(); + std::cout << "Humidity = " << value << "%" << std::endl; + } catch (std::exception& e) { + std::cerr << e.what() << std::endl; + } + sleep(1); + } + delete humiditySensor; + return 0; +} + +//! [Interesting] diff --git a/examples/c++/light-controller.cxx b/examples/c++/light-controller.cxx new file mode 100644 index 00000000..c2c5a2f2 --- /dev/null +++ b/examples/c++/light-controller.cxx @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include +#include "lp8860.h" +#include "ds1808lc.h" +#include "hlg150h.h" + +#define EDISON_I2C_BUS 1 // Edison I2C-1 +#define GPIO_SI7005_CS 20 // Edison GP12 +#define HLG150H_GPIO_RELAY 21 +#define HLG150H_GPIO_PWM 22 +#define LP8860_GPIO_PWR 45 // Edison GP45 +#define DS1808_GPIO_PWR 15 // Edison GP165 +#define DS1808_GPIO_EDISON_LIVE 36 // Edison GP14 + +//! [Interesting] +// Simple example of using ILightController to determine +// which controller is present and return its name. +// ILightController is then used to get readings from sensor + +upm::ILightController* getLightController() +{ + upm::ILightController* lightController = NULL; + try { + lightController = new upm::LP8860(LP8860_GPIO_PWR, EDISON_I2C_BUS); + return lightController; + } catch (std::exception& e) { + std::cerr << "LP8860: " << e.what() << std::endl; + } + try { + lightController = new upm::DS1808LC(DS1808_GPIO_PWR, EDISON_I2C_BUS); + return lightController; + } catch (std::exception& e) { + std::cerr << "DS1808LC: " << e.what() << std::endl; + } + try { + lightController = new upm::HLG150H(HLG150H_GPIO_RELAY, HLG150H_GPIO_PWM); + return lightController; + } catch (std::exception& e) { + std::cerr << "HLG150H: " << e.what() << std::endl; + } + return lightController; +} + + +void printState(upm::ILightController *lightController) +{ + if (lightController->isPowered()) + { + std::cout << "Light is powered, brightness = " << lightController->getBrightness() << std::endl; + } + else + { + std::cout << "Light is not powered." << std::endl; + } +} + +int main( int argc, char **argv ) +{ + int status = 0; + // MraaUtils::setGpio(GPIO_SI7005_CS, 1); + + upm::ILightController *lightController = getLightController(); + if (lightController != NULL) + { + std::cout << "Detected light controller " << lightController->getModuleName() << std::endl; + } + else + { + std::cerr << "Error. Unsupported platform." << std::endl; + return 1; + } + + try { + std::cout << "Existing state: "; printState(lightController); + if (argc == 2) + { + std::string arg = argv[1]; + int brightness = ::atoi(argv[1]); + if (brightness > 0) { + lightController->setPowerOn(); + lightController->setBrightness(brightness); + } else + lightController->setPowerOff(); + } + std::cout << "Now: ";printState(lightController); + } catch (std::exception& e) { + std::cout << "Error: " << e.what() << std::endl; + status = 1; + } + + delete lightController; + return status; +} + + diff --git a/examples/c++/light-sensor.cxx b/examples/c++/light-sensor.cxx new file mode 100644 index 00000000..fce5668c --- /dev/null +++ b/examples/c++/light-sensor.cxx @@ -0,0 +1,78 @@ +/* + * Author: Henry Bruce + * Copyright (c) 2015 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include "max44009.h" +#include "si1132.h" + +#define EDISON_I2C_BUS 1 +#define FT4222_I2C_BUS 0 + +//! [Interesting] +// Simple example of using ILightSensor to determine +// which sensor is present and return its name. +// ILightSensor is then used to get readings from sensor + + +upm::ILightSensor* getLightSensor() +{ + upm::ILightSensor* lightSensor = NULL; + try { + lightSensor = new upm::SI1132(mraa_get_sub_platform_id(FT4222_I2C_BUS)); + return lightSensor; + } catch (std::exception& e) { + std::cerr << "SI1132: " << e.what() << std::endl; + } + try { + lightSensor = new upm::MAX44009(EDISON_I2C_BUS); + return lightSensor; + } catch (std::exception& e) { + std::cerr << "MAX44009: " << e.what() << std::endl; + } + return lightSensor; +} + +int main () +{ + upm::ILightSensor* lightSensor = getLightSensor(); + if (lightSensor == NULL) { + std::cout << "Light sensor not detected" << std::endl; + return 1; + } + std::cout << "Light sensor " << lightSensor->getModuleName() << " detected" << std::endl; + while (true) { + try { + float value = lightSensor->getVisibleLux(); + std::cout << "Light level = " << value << " lux" << std::endl; + } catch (std::exception& e) { + std::cerr << e.what() << std::endl; + } + sleep(1); + } + delete lightSensor; + return 0; +} + +//! [Interesting] \ No newline at end of file diff --git a/examples/c++/pressure-sensor.cxx b/examples/c++/pressure-sensor.cxx new file mode 100644 index 00000000..d92b9e9b --- /dev/null +++ b/examples/c++/pressure-sensor.cxx @@ -0,0 +1,81 @@ +/* + * Author: Henry Bruce + * Copyright (c) 2015 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include "bmp180.h" +#include "bme280.h" + + +#define EDISON_I2C_BUS 1 +#define FT4222_I2C_BUS 0 + +//! [Interesting] +// Simple example of using ILightSensor to determine +// which sensor is present and return its name. +// ILightSensor is then used to get readings from sensor + + +upm::IPressureSensor* getPressureSensor() +{ + upm::IPressureSensor* pressureSensor = NULL; + try { + pressureSensor = new upm::BME280 (mraa_get_sub_platform_id(FT4222_I2C_BUS)); + return pressureSensor ; + } catch (std::exception& e) + { + std::cerr <<"BME280: "<getModuleName() << " detected" << std::endl; + while (true) { + try { + int value = pressureSensor->getPressurePa(); + std::cout << "Pressure = " << value << " Pa" << std::endl; + } catch (std::exception& e) { + std::cerr << e.what() << std::endl; + } + sleep(1); + } + delete pressureSensor; + return 0; +} + +//! [Interesting] diff --git a/examples/c++/temperature-sensor.cxx b/examples/c++/temperature-sensor.cxx new file mode 100644 index 00000000..0683c6f9 --- /dev/null +++ b/examples/c++/temperature-sensor.cxx @@ -0,0 +1,90 @@ +/* + * Author: Henry Bruce + * Copyright (c) 2015 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include "si7005.h" +#include "bmp180.h" +#include "bme280.h" + +#define EDISON_I2C_BUS 1 +#define FT4222_I2C_BUS 0 + + #define EDISON_GPIO_SI7005_CS 20 + +//! [Interesting] +// Simple example of using ITemperatureSensor to determine +// which sensor is present and return its name. +// ITemperatureSensor is then used to get readings from sensor + + +upm::ITemperatureSensor* getTemperatureSensor() +{ + upm::ITemperatureSensor* temperatureSensor = NULL; + try { + temperatureSensor = new upm::BME280 (mraa_get_sub_platform_id(FT4222_I2C_BUS)); + return temperatureSensor; + } catch (std::exception& e) + { + std::cerr <<"BME280: "<getModuleName() << " detected" << std::endl; + while (true) { + try { + int value = temperatureSensor->getTemperatureCelcius(); + std::cout << "Temperature = " << value << "C" << std::endl; + } catch (std::exception& e) { + std::cerr << e.what() << std::endl; + } + sleep(1); + } + delete temperatureSensor; + return 0; +} + +//! [Interesting]