From 01cc3a0734e70928796cf23fd5cea74395287ff6 Mon Sep 17 00:00:00 2001 From: Assam Boudjelthia Date: Tue, 19 Jun 2018 12:33:01 +0300 Subject: [PATCH] kxtj3: fixed requested issues * Virtual destructor * Add header guards * Constructor default values * Remove methods with pointer parameters in C++ code Signed-off-by: Assam Boudjelthia Signed-off-by: Noel Eck --- examples/c++/kxtj3.cxx | 36 ++++++++++++++++-------------------- src/kxtj3/kxtj3.c | 4 ++-- src/kxtj3/kxtj3.cxx | 27 +++++++++++++-------------- src/kxtj3/kxtj3.h | 9 ++++++--- src/kxtj3/kxtj3.hpp | 34 ++++++++++------------------------ 5 files changed, 47 insertions(+), 63 deletions(-) diff --git a/examples/c++/kxtj3.cxx b/examples/c++/kxtj3.cxx index d0607ff2..af4d1be7 100755 --- a/examples/c++/kxtj3.cxx +++ b/examples/c++/kxtj3.cxx @@ -22,6 +22,8 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include +#include #include #include #include "kxtj3.hpp" @@ -38,17 +40,17 @@ void signal_int_handler(int signo) isStopped = true; } -void print_acceleration_data(upm::KXTJ3 *dev) +void print_acceleration_data(upm::KXTJ3 &dev) { - float wait_time = dev->GetAccelerationSamplePeriod() * SECOND_IN_MICRO_S; - uint8_t sample_counter = 0; + float wait_time = dev.GetAccelerationSamplePeriod() * SECOND_IN_MICRO_S; + int sample_counter = SAMPLE_COUNT; std::vector xyz; - while (sample_counter < SAMPLE_COUNT && !isStopped) + while ((sample_counter-- > 0) && !isStopped) { - xyz = dev->GetAccelerationVector(); - printf("%.02f | %.02f | %.02f\n", xyz[0], xyz[1], xyz[2]); + xyz = dev.GetAccelerationVector(); + std::cout << std::fixed << std::setprecision(3) + << xyz[0] << " | " << xyz[1] << " | " << xyz[2] << std::endl; usleep(wait_time); - sample_counter++; } } @@ -56,21 +58,15 @@ int main(int argc, char **argv) { signal(SIGINT, signal_int_handler); - printf("Sensor init\n"); - upm::KXTJ3 *dev = new upm::KXTJ3(I2C_BUS, SENSOR_ADDR); - if (!dev) - { - printf("kxtj3_init() failed.\n"); - return -1; - } + std::cout << "Sensor init" << std::endl; + upm::KXTJ3 dev(I2C_BUS, SENSOR_ADDR); - printf("Setting settings:\nODR: 25 Hz\nResolution: High\nAcceleration range: 16g with 14bits"); - dev->SensorInit(KXTJ3_ODR_25, HIGH_RES, KXTJ3_RANGE_16G_14); - printf("Showing acceleration data:\n"); + std::cout << "Setting settings:\nODR: 25 Hz\nResolution: " + << "High\nAcceleration range: 16g with 14bits" << std::endl; + dev.SensorInit(KXTJ3_ODR_25, HIGH_RES, KXTJ3_RANGE_16G_14); + std::cout << "Showing acceleration data:" << std::endl; print_acceleration_data(dev); - printf("Closing sensor\n"); - delete dev; - dev = nullptr; + std::cout << "Closing sensor" << std::endl; return 0; } diff --git a/src/kxtj3/kxtj3.c b/src/kxtj3/kxtj3.c index bb5c92e0..fd144e40 100755 --- a/src/kxtj3/kxtj3.c +++ b/src/kxtj3/kxtj3.c @@ -274,7 +274,7 @@ static upm_result_t kxtj3_read_register(const kxtj3_context dev, uint8_t reg, ui int value = mraa_i2c_read_byte_data(dev->i2c, reg); if (value == -1) { - printf("%s: mraa_i2c_read_byte_data() failed.\n", __FUNCTION__, reg); + printf("%s: mraa_i2c_read_byte_data() failed.\n", __FUNCTION__); return UPM_ERROR_OPERATION_FAILED; } @@ -344,7 +344,7 @@ static bool kxtj3_check_mraa_i2c_connection(kxtj3_context dev, int bus, uint8_t if (!(dev->i2c = mraa_i2c_init(bus))) { - printf("%s: mraa_i2c_init() failed.\n", __FUNCTION__, bus); + printf("%s: mraa_i2c_init() failed.\n", __FUNCTION__); kxtj3_close(dev); return false; } diff --git a/src/kxtj3/kxtj3.cxx b/src/kxtj3/kxtj3.cxx index 3b78858b..1afd3fe6 100755 --- a/src/kxtj3/kxtj3.cxx +++ b/src/kxtj3/kxtj3.cxx @@ -22,7 +22,10 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include #include +#include + #include "kxtj3.hpp" using namespace upm; @@ -107,22 +110,11 @@ void KXTJ3::SensorSoftwareReset() throw std::runtime_error(std::string(__FUNCTION__) + "kxtj3_sensor_software_reset() failed"); } -void KXTJ3::GetAccelerationRaw(float *x, float *y, float *z) -{ - if (kxtj3_get_acceleration_data_raw(m_kxtj3, x, y, z) != UPM_SUCCESS) - throw std::runtime_error(std::string(__FUNCTION__) + "kxtj3_get_acceleration_data_raw() failed"); -} - -void KXTJ3::GetAcceleration(float *x, float *y, float *z) -{ - if (kxtj3_get_acceleration_data(m_kxtj3, x, y, z) != UPM_SUCCESS) - throw std::runtime_error(std::string(__FUNCTION__) + "kxtj3_get_acceleration_data() failed"); -} - std::vector KXTJ3::GetAccelerationRawVector() { std::vector xyz(3); - GetAccelerationRaw(&xyz[0], &xyz[1], &xyz[2]); + if (kxtj3_get_acceleration_data_raw(m_kxtj3, &xyz[0], &xyz[1], &xyz[2]) != UPM_SUCCESS) + throw std::runtime_error(std::string(__FUNCTION__) + "kxtj3_get_acceleration_data_raw() failed"); return xyz; } @@ -130,7 +122,8 @@ std::vector KXTJ3::GetAccelerationRawVector() std::vector KXTJ3::GetAccelerationVector() { std::vector xyz(3); - GetAcceleration(&xyz[0], &xyz[1], &xyz[2]); + if (kxtj3_get_acceleration_data(m_kxtj3, &xyz[0], &xyz[1], &xyz[2]) != UPM_SUCCESS) + throw std::runtime_error(std::string(__FUNCTION__) + "kxtj3_get_acceleration_data() failed"); return xyz; } @@ -235,6 +228,12 @@ void KXTJ3::EnableWakeUpSingleAxisDirection(KXTJ3_WAKEUP_SOURCE_T axis) throw std::runtime_error(std::string(__FUNCTION__) + "kxtj3_enable_wakeup_single_axis_direction() failed"); } +void KXTJ3::DisableWakeUpSingleAxisDirection(KXTJ3_WAKEUP_SOURCE_T axis) +{ + if (kxtj3_disable_wakeup_single_axis_direction(m_kxtj3, axis) != UPM_SUCCESS) + throw std::runtime_error(std::string(__FUNCTION__) + "kxtj3_disable_wakeup_single_axis_direction() failed"); +} + kxtj3_wakeup_axes KXTJ3::GetWakeUpAxisDirection() { return kxtj3_get_wakeup_axis_and_direction(m_kxtj3); diff --git a/src/kxtj3/kxtj3.h b/src/kxtj3/kxtj3.h index 4124aebc..bac24122 100755 --- a/src/kxtj3/kxtj3.h +++ b/src/kxtj3/kxtj3.h @@ -22,23 +22,26 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#pragma once + #ifdef __cplusplus extern "C" { #endif #include +#include #include #include -#include +#include #include "kxtj3_registers.h" /** * @brief C API for the kxtj3 driver * @defgroup kxtj3 libupm-kxtj3 - * @ingroup Kionix i2c acceleromter + * @ingroup Kionix i2c accelerometer * * @include kxtj3.c */ @@ -47,7 +50,7 @@ extern "C" * @library kxtj3 * @sensor kxtj3 * @comname tri-axis accelerometer - * @type acceleromter + * @type accelerometer * @man Kionix * @con i2c * diff --git a/src/kxtj3/kxtj3.hpp b/src/kxtj3/kxtj3.hpp index 85ca275d..271456c9 100755 --- a/src/kxtj3/kxtj3.hpp +++ b/src/kxtj3/kxtj3.hpp @@ -22,6 +22,8 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#pragma once + #include #include @@ -30,7 +32,7 @@ /** * @brief C API for the kxtj3 driver * @defgroup kxtj3 libupm-kxtj3 - * @ingroup Kionix i2c acceleromter + * @ingroup Kionix i2c accelerometer * * @include kxtj3.cxx */ @@ -39,7 +41,7 @@ * @library kxtj3 * @sensor kxtj3 * @comname tri-axis accelerometer - * @type acceleromter + * @type accelerometer * @man Kionix * @con i2c * @@ -62,7 +64,7 @@ public: * @param addr I2C address of the sensor * @throws std::runtime_error on initialization failure */ - KXTJ3(int bus, uint8_t addr); + KXTJ3(int bus, uint8_t addr = 0x0f); /** * @brief KXTJ3 destructor @@ -72,7 +74,7 @@ public: * * @throws std::runtime_error on initialization failure */ - ~KXTJ3(); + virtual ~KXTJ3(); /** * @brief Initializes the sensor with given sampling rate, resolution and acceleration range. @@ -194,26 +196,6 @@ public: */ void SensorSoftwareReset(); - /** - * @brief Gets raw accelerometer data from the sensor - * - * @param x Pointer to a floating point variable to store the x-axis value. Set to NULL if not wanted. - * @param y Pointer to a floating point variable to store the y-axis value. Set to NULL if not wanted. - * @param z Pointer to a floating point variable to store the z-axis value. Set to NULL if not wanted. - * @throws std::runtime_error on failure - */ - void GetAccelerationRaw(float *x, float *y, float *z); - - /** - * @brief Gets converted (m/s^2) accelerometer data from the sensor - * - * @param x Pointer to a floating point variable to store the x-axis value. Set to NULL if not wanted. - * @param y Pointer to a floating point variable to store the y-axis value. Set to NULL if not wanted. - * @param z Pointer to a floating point variable to store the z-axis value. Set to NULL if not wanted. - * @throws std::runtime_error on failure - */ - void GetAcceleration(float *x, float *y, float *z); - /** * Gets raw acceleration data from the sensor. * @@ -575,5 +557,9 @@ public: private: kxtj3_context m_kxtj3; + + /* Disable implicit copy and assignment operators */ + KXTJ3(const KXTJ3 &) = delete; + KXTJ3 &operator=(const KXTJ3 &) = delete; }; } // namespace upm