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 <assam.boudjelthia@fi.rohmeurope.com>
This commit is contained in:
Assam Boudjelthia 2018-06-19 12:33:01 +03:00
parent 136239792c
commit d31d028345
5 changed files with 47 additions and 63 deletions

View File

@ -22,6 +22,8 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <iostream>
#include <iomanip>
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#include "kxtj3.hpp" #include "kxtj3.hpp"
@ -38,17 +40,17 @@ void signal_int_handler(int signo)
isStopped = true; 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; float wait_time = dev.GetAccelerationSamplePeriod() * SECOND_IN_MICRO_S;
uint8_t sample_counter = 0; int sample_counter = SAMPLE_COUNT;
std::vector<float> xyz; std::vector<float> xyz;
while (sample_counter < SAMPLE_COUNT && !isStopped) while ((sample_counter-- > 0) && !isStopped)
{ {
xyz = dev->GetAccelerationVector(); xyz = dev.GetAccelerationVector();
printf("%.02f | %.02f | %.02f\n", xyz[0], xyz[1], xyz[2]); std::cout << std::fixed << std::setprecision(3)
<< xyz[0] << " | " << xyz[1] << " | " << xyz[2] << std::endl;
usleep(wait_time); usleep(wait_time);
sample_counter++;
} }
} }
@ -56,21 +58,15 @@ int main(int argc, char **argv)
{ {
signal(SIGINT, signal_int_handler); signal(SIGINT, signal_int_handler);
printf("Sensor init\n"); std::cout << "Sensor init" << std::endl;
upm::KXTJ3 *dev = new upm::KXTJ3(I2C_BUS, SENSOR_ADDR); upm::KXTJ3 dev(I2C_BUS, SENSOR_ADDR);
if (!dev)
{
printf("kxtj3_init() failed.\n");
return -1;
}
printf("Setting settings:\nODR: 25 Hz\nResolution: High\nAcceleration range: 16g with 14bits"); std::cout << "Setting settings:\nODR: 25 Hz\nResolution: "
dev->SensorInit(KXTJ3_ODR_25, HIGH_RES, KXTJ3_RANGE_16G_14); << "High\nAcceleration range: 16g with 14bits" << std::endl;
printf("Showing acceleration data:\n"); dev.SensorInit(KXTJ3_ODR_25, HIGH_RES, KXTJ3_RANGE_16G_14);
std::cout << "Showing acceleration data:" << std::endl;
print_acceleration_data(dev); print_acceleration_data(dev);
printf("Closing sensor\n"); std::cout << "Closing sensor" << std::endl;
delete dev;
dev = nullptr;
return 0; return 0;
} }

View File

@ -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); int value = mraa_i2c_read_byte_data(dev->i2c, reg);
if (value == -1) 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; 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))) 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); kxtj3_close(dev);
return false; return false;
} }

View File

@ -22,7 +22,10 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <iostream>
#include <stdexcept> #include <stdexcept>
#include <string>
#include "kxtj3.hpp" #include "kxtj3.hpp"
using namespace upm; using namespace upm;
@ -107,22 +110,11 @@ void KXTJ3::SensorSoftwareReset()
throw std::runtime_error(std::string(__FUNCTION__) + "kxtj3_sensor_software_reset() failed"); 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<float> KXTJ3::GetAccelerationRawVector() std::vector<float> KXTJ3::GetAccelerationRawVector()
{ {
std::vector<float> xyz(3); std::vector<float> 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; return xyz;
} }
@ -130,7 +122,8 @@ std::vector<float> KXTJ3::GetAccelerationRawVector()
std::vector<float> KXTJ3::GetAccelerationVector() std::vector<float> KXTJ3::GetAccelerationVector()
{ {
std::vector<float> xyz(3); std::vector<float> 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; 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"); 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() kxtj3_wakeup_axes KXTJ3::GetWakeUpAxisDirection()
{ {
return kxtj3_get_wakeup_axis_and_direction(m_kxtj3); return kxtj3_get_wakeup_axis_and_direction(m_kxtj3);

View File

@ -22,23 +22,26 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#pragma once
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
{ {
#endif #endif
#include <assert.h> #include <assert.h>
#include <unistd.h>
#include <mraa/i2c.h> #include <mraa/i2c.h>
#include <mraa/gpio.h> #include <mraa/gpio.h>
#include <upm/upm_types.h> #include <upm.h>
#include "kxtj3_registers.h" #include "kxtj3_registers.h"
/** /**
* @brief C API for the kxtj3 driver * @brief C API for the kxtj3 driver
* @defgroup kxtj3 libupm-kxtj3 * @defgroup kxtj3 libupm-kxtj3
* @ingroup Kionix i2c acceleromter * @ingroup Kionix i2c accelerometer
* *
* @include kxtj3.c * @include kxtj3.c
*/ */
@ -47,7 +50,7 @@ extern "C"
* @library kxtj3 * @library kxtj3
* @sensor kxtj3 * @sensor kxtj3
* @comname tri-axis accelerometer * @comname tri-axis accelerometer
* @type acceleromter * @type accelerometer
* @man Kionix * @man Kionix
* @con i2c * @con i2c
* *

View File

@ -22,6 +22,8 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#pragma once
#include <vector> #include <vector>
#include <mraa/gpio.hpp> #include <mraa/gpio.hpp>
@ -30,7 +32,7 @@
/** /**
* @brief C API for the kxtj3 driver * @brief C API for the kxtj3 driver
* @defgroup kxtj3 libupm-kxtj3 * @defgroup kxtj3 libupm-kxtj3
* @ingroup Kionix i2c acceleromter * @ingroup Kionix i2c accelerometer
* *
* @include kxtj3.cxx * @include kxtj3.cxx
*/ */
@ -39,7 +41,7 @@
* @library kxtj3 * @library kxtj3
* @sensor kxtj3 * @sensor kxtj3
* @comname tri-axis accelerometer * @comname tri-axis accelerometer
* @type acceleromter * @type accelerometer
* @man Kionix * @man Kionix
* @con i2c * @con i2c
* *
@ -62,7 +64,7 @@ public:
* @param addr I2C address of the sensor * @param addr I2C address of the sensor
* @throws std::runtime_error on initialization failure * @throws std::runtime_error on initialization failure
*/ */
KXTJ3(int bus, uint8_t addr); KXTJ3(int bus = 0x00, uint8_t addr = 0x0f);
/** /**
* @brief KXTJ3 destructor * @brief KXTJ3 destructor
@ -72,7 +74,7 @@ public:
* *
* @throws std::runtime_error on initialization failure * @throws std::runtime_error on initialization failure
*/ */
~KXTJ3(); virtual ~KXTJ3();
/** /**
* @brief Initializes the sensor with given sampling rate, resolution and acceleration range. * @brief Initializes the sensor with given sampling rate, resolution and acceleration range.
@ -194,26 +196,6 @@ public:
*/ */
void SensorSoftwareReset(); 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. * Gets raw acceleration data from the sensor.
* *
@ -575,5 +557,9 @@ public:
private: private:
kxtj3_context m_kxtj3; kxtj3_context m_kxtj3;
/* Disable implicit copy and assignment operators */
KXTJ3(const KXTJ3 &) = delete;
KXTJ3 &operator=(const KXTJ3 &) = delete;
}; };
} // namespace upm } // namespace upm