From e64f192fb755ce9ca4fc62caf7c4df68f4b8eebc Mon Sep 17 00:00:00 2001 From: "Lay, Kuan Loon" Date: Mon, 20 Jun 2016 15:42:19 +0800 Subject: [PATCH] apds9930: enable sensor before read out sensor data Changes in library: - enableProximity() function is to enable or disable proximity sensor - enableIlluminance() function is to enable or disable illuminance sensor - run clang-format Changes in example: - proximity and illuminance kernel IIO-based driver init state is power off, require enable before read out sensor data. Sleep time is needed after enable, the sleep time may vary on different platform. - run clang-format Signed-off-by: Lay, Kuan Loon Signed-off-by: Mihai Tudor Panu --- examples/c++/apds9930.cxx | 13 ++++++++++++- src/apds9930/apds9930.cxx | 26 ++++++++++++++++++++++++-- src/apds9930/apds9930.hpp | 12 +++++++++++- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/examples/c++/apds9930.cxx b/examples/c++/apds9930.cxx index 6da24a96..37520693 100644 --- a/examples/c++/apds9930.cxx +++ b/examples/c++/apds9930.cxx @@ -1,6 +1,6 @@ /* * Author: Lay, Kuan Loon - * Copyright (c) 2015 Intel Corporation. + * Copyright (c) 2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -46,6 +46,15 @@ main() // Instantiate a Digital Proximity and Ambient Light sensor on iio device 4 upm::APDS9930* light_proximity = new upm::APDS9930(4); + // Kernel driver implement sleep 5000-5100us after enable illuminance sensor + light_proximity->enableIlluminance(true); + + // Kernel driver implement sleep 5000-5100us after enable proximity sensor + light_proximity->enableProximity(true); + + // Tested this value works. Please change it on your platform + usleep(120000); + while (shouldRun) { float lux = light_proximity->getAmbient(); cout << "Luminance value is " << lux << endl; @@ -53,6 +62,8 @@ main() cout << "Proximity value is " << proximity << endl; sleep(1); } + light_proximity->enableProximity(false); + light_proximity->enableIlluminance(false); //! [Interesting] cout << "Exiting" << endl; diff --git a/src/apds9930/apds9930.cxx b/src/apds9930/apds9930.cxx index 4b1084d7..085ee923 100644 --- a/src/apds9930/apds9930.cxx +++ b/src/apds9930/apds9930.cxx @@ -1,6 +1,6 @@ /* * Author: Lay, Kuan Loon - * Copyright (c) 2015 Intel Corporation. + * Copyright (c) 2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -41,7 +41,7 @@ APDS9930::APDS9930(int device) APDS9930::~APDS9930() { - if(m_iio) + if (m_iio) mraa_iio_close(m_iio); } @@ -60,3 +60,25 @@ APDS9930::getProximity() mraa_iio_read_int(m_iio, "in_proximity_raw", &iio_value); return iio_value; } + +bool +APDS9930::enableProximity(bool enable) +{ + if (enable) + mraa_iio_write_int(m_iio, "in_proximity_en", 1); + else + mraa_iio_write_int(m_iio, "in_proximity_en", 0); + + return true; +} + +bool +APDS9930::enableIlluminance(bool enable) +{ + if (enable) + mraa_iio_write_int(m_iio, "in_illuminance_en", 1); + else + mraa_iio_write_int(m_iio, "in_illuminance_en", 0); + + return true; +} \ No newline at end of file diff --git a/src/apds9930/apds9930.hpp b/src/apds9930/apds9930.hpp index a5c3a817..db6efbee 100644 --- a/src/apds9930/apds9930.hpp +++ b/src/apds9930/apds9930.hpp @@ -1,6 +1,6 @@ /* * Author: Lay, Kuan Loon - * Copyright (c) 2015 Intel Corporation. + * Copyright (c) 2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -75,6 +75,16 @@ class APDS9930 * @return Proximity value */ int getProximity(); + /** + * Enable proximity + * @param enable state + */ + bool enableProximity(bool enable); + /** + * Enable illuminance + * @param enable state + */ + bool enableIlluminance(bool enable); private: mraa_iio_context m_iio;