kx122: Return values instead of receiving pointers

For better compatibility with Python

Signed-off-by: Antoine W. Campagna <AntoineW@Campagna.org>
This commit is contained in:
Antoine W. Campagna 2018-03-25 21:42:39 -04:00
parent 2d0ffaf88c
commit 30a97d891c
2 changed files with 55 additions and 28 deletions

View File

@ -56,11 +56,15 @@ float KX122::getSamplePeriod()
return kx122_get_sample_period(m_kx122); return kx122_get_sample_period(m_kx122);
} }
void KX122::getWhoAmI(uint8_t *data) uint8_t KX122::getWhoAmI()
{ {
if(kx122_get_who_am_i(m_kx122,data)){ uint8_t data;
if(kx122_get_who_am_i(m_kx122, &data)){
throw std::runtime_error(std::string(__FUNCTION__) + "kx122_get_who_am_i failed"); throw std::runtime_error(std::string(__FUNCTION__) + "kx122_get_who_am_i failed");
} }
return data;
} }
void KX122::getRawAccelerationData(float *x, float *y, float *z) void KX122::getRawAccelerationData(float *x, float *y, float *z)
{ {
@ -220,11 +224,14 @@ bool KX122::getInterruptStatus()
return kx122_get_interrupt_status(m_kx122); return kx122_get_interrupt_status(m_kx122);
} }
void KX122::getInterruptSource(uint8_t *data) uint8_t KX122::getInterruptSource()
{ {
if(kx122_get_interrupt_source(m_kx122,data)){ uint8_t data;
if(kx122_get_interrupt_source(m_kx122, &data)){
throw std::runtime_error(std::string(__FUNCTION__) + "kx122_get_interrupt_source failed"); throw std::runtime_error(std::string(__FUNCTION__) + "kx122_get_interrupt_source failed");
} }
return data;
} }
void KX122::clearInterrupt() void KX122::clearInterrupt()
@ -304,25 +311,53 @@ void KX122::setBufferMode(KX122_BUFFER_MODE_T mode)
} }
} }
void KX122::getBufferStatus(uint *samples) uint KX122::getBufferStatus()
{ {
if(kx122_get_buffer_status(m_kx122,samples)){ uint nb_samples = 0;
if(kx122_get_buffer_status(m_kx122, &nb_samples)){
throw std::runtime_error(std::string(__FUNCTION__) + "kx122_get_buffer_status failed"); throw std::runtime_error(std::string(__FUNCTION__) + "kx122_get_buffer_status failed");
} }
return nb_samples;
} }
void KX122::getRawBufferSamples(uint len, float *x_array, float *y_array, float *z_array) //Maximum number of samples that can be stored in the buffer of the KX122
#define MAX_SAMPLES_IN_BUFFER 681
std::vector<float> KX122::getRawBufferSamples(uint len)
{ {
if(kx122_read_buffer_samples_raw(m_kx122,len,x_array,y_array,z_array)){ float bufferx[MAX_SAMPLES_IN_BUFFER], buffery[MAX_SAMPLES_IN_BUFFER], bufferz[MAX_SAMPLES_IN_BUFFER];
if(kx122_read_buffer_samples_raw(m_kx122,len,bufferx,buffery,bufferz)){
throw std::runtime_error(std::string(__FUNCTION__) + "kx122_read_buffer_samples_raw failed"); throw std::runtime_error(std::string(__FUNCTION__) + "kx122_read_buffer_samples_raw failed");
} }
std::vector<float> xyz_array(len * 3);
for (uint i = 0; i < len; i++)
{
xyz_array[i * 3 + 0] = bufferx[i];
xyz_array[i * 3 + 1] = buffery[i];
xyz_array[i * 3 + 2] = bufferz[i];
}
return xyz_array;
} }
void KX122::getBufferSamples(uint len, float *x_array, float *y_array, float *z_array) std::vector<float> KX122::getBufferSamples(uint len)
{ {
if(kx122_read_buffer_samples(m_kx122,len,x_array,y_array,z_array)){ float bufferx[MAX_SAMPLES_IN_BUFFER], buffery[MAX_SAMPLES_IN_BUFFER], bufferz[MAX_SAMPLES_IN_BUFFER];
if(kx122_read_buffer_samples(m_kx122,len,bufferx,buffery,bufferz)){
throw std::runtime_error(std::string(__FUNCTION__) + "kx122_read_buffer_samples failed"); throw std::runtime_error(std::string(__FUNCTION__) + "kx122_read_buffer_samples failed");
} }
std::vector<float> xyz_array(len * 3);
for (uint i = 0; i < len; i++)
{
xyz_array[i * 3 + 0] = bufferx[i];
xyz_array[i * 3 + 1] = buffery[i];
xyz_array[i * 3 + 2] = bufferz[i];
}
return xyz_array;
} }
void KX122::clearBuffer() void KX122::clearBuffer()

View File

@ -82,10 +82,10 @@ namespace upm{
/** /**
Gets who am i value of the sensor. Gets who am i value of the sensor.
@param data Pointer to a uint8_t variable to store the value. @return Who am I value of the sensor.
@throws std::runtime_error on failure. @throws std::runtime_error on failure.
*/ */
void getWhoAmI(uint8_t *data); uint8_t getWhoAmI();
/** /**
Gets raw accelerometer data from the sensor. Gets raw accelerometer data from the sensor.
@ -339,10 +339,10 @@ namespace upm{
See datasheet for more details. See datasheet for more details.
@param data Pointer to a uint8_t variable to store the value. @return Interrupt source value.
@throws std::runtime_error on failure. @throws std::runtime_error on failure.
*/ */
void getInterruptSource(uint8_t *data); uint8_t getInterruptSource();
/** /**
Clears latching interrupts (Wakeup, Data Ready). Clears latching interrupts (Wakeup, Data Ready).
@ -470,36 +470,28 @@ namespace upm{
/** /**
Gets the current amount of samples in the buffer. Gets the current amount of samples in the buffer.
@param samples Pointer to an uint variable to store the data. @return number of samples in the buffer.
@throws std::runtime_error on failure. @throws std::runtime_error on failure.
*/ */
void getBufferStatus(uint *samples); uint getBufferStatus();
/** /**
Gets the specified amount of raw acceleration samples from the buffer. Gets the specified amount of raw acceleration samples from the buffer.
Make sure the array size is at least the amount of samples to be read.
@param len The amount of samples to read from the buffer. @param len The amount of samples to read from the buffer.
@param x_array Pointer to an floating point array to store the x-axis data. Can be set to NULL if not wanted. @return vector containing x, y & z-axis data
@param y_array Pointer to an floating point array to store the y-axis data. Can be set to NULL if not wanted.
@param z_array Pointer to an floating point array to store the z-axis data. Can be set to NULL if not wanted.
@throws std::runtime_error on failure. @throws std::runtime_error on failure.
*/ */
void getRawBufferSamples(uint len, float *x_array, float *y_array, float *z_array); std::vector<float> getRawBufferSamples(uint len);
/** /**
Gets the specified amount of converted (m/s^2) acceleration samples from the buffer. Gets the specified amount of converted (m/s^2) acceleration samples from the buffer.
Make sure the array size is at least the amount of samples to be read.
@param len The amount of samples to read from the buffer. @param len The amount of samples to read from the buffer.
@param x_array Pointer to an floating point array to store the x-axis data. Can be set to NULL if not wanted. @return vector containing x, y & z-axis data
@param y_array Pointer to an floating point array to store the y-axis data. Can be set to NULL if not wanted.
@param z_array Pointer to an floating point array to store the z-axis data. Can be set to NULL if not wanted.
@throws std::runtime_error on failure. @throws std::runtime_error on failure.
*/ */
void getBufferSamples(uint len, float *x_array, float *y_array, float *z_array); std::vector<float> getBufferSamples(uint len);
/** /**
Clears the buffer, removing all existing samples from the buffer. Clears the buffer, removing all existing samples from the buffer.