From 8c7d6483d3749b946d774756580e05ac8c6d2099 Mon Sep 17 00:00:00 2001 From: Brendan Le Foll Date: Fri, 18 Mar 2016 14:56:23 +0000 Subject: [PATCH] curieimu: modify calls to use update methodology to fix python/java/js API Signed-off-by: Brendan Le Foll --- src/curieimu/curieimu.cpp | 94 ++++++++++++++++++++------------- src/curieimu/curieimu.hpp | 24 +++++++-- src/curieimu/javaupm_curieimu.i | 23 ++++++++ src/curieimu/jsupm_curieimu.i | 7 +++ src/curieimu/pyupm_curieimu.i | 12 ++++- 5 files changed, 118 insertions(+), 42 deletions(-) diff --git a/src/curieimu/curieimu.cpp b/src/curieimu/curieimu.cpp index 7033976c..749db4a5 100644 --- a/src/curieimu/curieimu.cpp +++ b/src/curieimu/curieimu.cpp @@ -155,8 +155,26 @@ CurieImu::processResponse() return; } +int16_t* +CurieImu::getAccel() +{ + return &accel[0]; +} + +int16_t* +CurieImu::getGyro() +{ + return &gyro[0]; +} + +int16_t* +CurieImu::getMotion() +{ + return &motion[0]; +} + void -CurieImu::readAccelerometer(int *xVal, int *yVal, int *zVal) +CurieImu::updateAccel() { char message[4]; message[0] = FIRMATA_START_SYSEX; @@ -172,9 +190,9 @@ CurieImu::readAccelerometer(int *xVal, int *yVal, int *zVal) waitForResponse(); - *xVal = ((m_results[3] & 0x7f) | ((m_results[4] & 0x7f) << 7)); - *yVal = ((m_results[5] & 0x7f) | ((m_results[6] & 0x7f) << 7)); - *zVal = ((m_results[7] & 0x7f) | ((m_results[8] & 0x7f) << 7)); + accel[0] = ((m_results[3] & 0x7f) | ((m_results[4] & 0x7f) << 7)); + accel[1] = ((m_results[5] & 0x7f) | ((m_results[6] & 0x7f) << 7)); + accel[2] = ((m_results[7] & 0x7f) | ((m_results[8] & 0x7f) << 7)); delete m_results; unlock(); @@ -183,7 +201,7 @@ CurieImu::readAccelerometer(int *xVal, int *yVal, int *zVal) } void -CurieImu::readGyro(int *xVal, int *yVal, int *zVal) +CurieImu::updateGyro() { char message[4]; message[0] = FIRMATA_START_SYSEX; @@ -199,9 +217,39 @@ CurieImu::readGyro(int *xVal, int *yVal, int *zVal) waitForResponse(); - *xVal = ((m_results[3] & 0x7f) | ((m_results[4] & 0x7f) << 7)); - *yVal = ((m_results[5] & 0x7f) | ((m_results[6] & 0x7f) << 7)); - *zVal = ((m_results[7] & 0x7f) | ((m_results[8] & 0x7f) << 7)); + gyro[0] = ((m_results[3] & 0x7f) | ((m_results[4] & 0x7f) << 7)); + gyro[1] = ((m_results[5] & 0x7f) | ((m_results[6] & 0x7f) << 7)); + gyro[2] = ((m_results[7] & 0x7f) | ((m_results[8] & 0x7f) << 7)); + + delete m_results; + unlock(); + + return; +} + +void +CurieImu::updateMotion() +{ + char message[4]; + message[0] = FIRMATA_START_SYSEX; + message[1] = FIRMATA_CURIE_IMU; + message[2] = FIRMATA_CURIE_IMU_READ_MOTION; + message[3] = FIRMATA_END_SYSEX; + + lock(); + + mraa_firmata_response_stop(m_firmata); + mraa_firmata_response(m_firmata, handleSyncResponse); + mraa_firmata_write_sysex(m_firmata, &message[0], 4); + + waitForResponse(); + + motion[0] = ((m_results[3] & 0x7f) | ((m_results[4] & 0x7f) << 7)); + motion[1] = ((m_results[5] & 0x7f) | ((m_results[6] & 0x7f) << 7)); + motion[2] = ((m_results[7] & 0x7f) | ((m_results[8] & 0x7f) << 7)); + motion[3] = ((m_results[9] & 0x7f) | ((m_results[10] & 0x7f) << 7)); + motion[4] = ((m_results[11] & 0x7f) | ((m_results[12] & 0x7f) << 7)); + motion[5] = ((m_results[13] & 0x7f) | ((m_results[13] & 0x7f) << 7)); delete m_results; unlock(); @@ -236,36 +284,6 @@ CurieImu::getTemperature() return result; } -void -CurieImu::readMotion(int *xA, int *yA, int *zA, int *xG, int *yG, int *zG) -{ - char message[4]; - message[0] = FIRMATA_START_SYSEX; - message[1] = FIRMATA_CURIE_IMU; - message[2] = FIRMATA_CURIE_IMU_READ_MOTION; - message[3] = FIRMATA_END_SYSEX; - - lock(); - - mraa_firmata_response_stop(m_firmata); - mraa_firmata_response(m_firmata, handleSyncResponse); - mraa_firmata_write_sysex(m_firmata, &message[0], 4); - - waitForResponse(); - - *xA = ((m_results[3] & 0x7f) | ((m_results[4] & 0x7f) << 7)); - *yA = ((m_results[5] & 0x7f) | ((m_results[6] & 0x7f) << 7)); - *zA = ((m_results[7] & 0x7f) | ((m_results[8] & 0x7f) << 7)); - *xG = ((m_results[9] & 0x7f) | ((m_results[10] & 0x7f) << 7)); - *yG = ((m_results[11] & 0x7f) | ((m_results[12] & 0x7f) << 7)); - *zG = ((m_results[13] & 0x7f) | ((m_results[13] & 0x7f) << 7)); - - delete m_results; - unlock(); - - return; -} - void CurieImu::enableShockDetection(bool enable) { diff --git a/src/curieimu/curieimu.hpp b/src/curieimu/curieimu.hpp index bf6ecb0f..6ef005a0 100644 --- a/src/curieimu/curieimu.hpp +++ b/src/curieimu/curieimu.hpp @@ -85,6 +85,20 @@ class CurieImu { */ ~CurieImu(); + /** + */ + void updateAccel(); + + /** + * + */ + void updateGyro(); + + /** + * + */ + void updateMotion(); + /** * Read accelerometer X, Y, and Z axis * @@ -92,7 +106,7 @@ class CurieImu { * @param yVal Pointer to returned Y-axis value * @param zVal Pointer to returned Z-axis value */ - void readAccelerometer(int *xVal, int *yVal, int *zVal); + int16_t* getAccel(); /** * Read gyroscope X, Y, and Z axis @@ -101,7 +115,7 @@ class CurieImu { * @param yVal Pointer to returned Y-axis value * @param zVal Pointer to returned Z-axis value */ - void readGyro(int *xVal, int *yVal, int *zVal); + int16_t* getGyro(); /** * Reads the internal temperature @@ -120,7 +134,7 @@ class CurieImu { * @param yG Pointer to returned Y-axis value of Gyroscope * @param zG Pointer to returned Z-axis value of Gyroscope */ - void readMotion(int *xA, int *yA, int *zA, int *xG, int *yG, int *zG); + int16_t* getMotion(); /** * Turns shock detection notifications on/off @@ -230,6 +244,10 @@ class CurieImu { std::queue m_shockData; std::queue m_stepData; std::queue m_tapData; + + int16_t accel[3]; + int16_t gyro[3]; + int16_t motion[6]; }; } diff --git a/src/curieimu/javaupm_curieimu.i b/src/curieimu/javaupm_curieimu.i index 725e10ec..726f8c2b 100644 --- a/src/curieimu/javaupm_curieimu.i +++ b/src/curieimu/javaupm_curieimu.i @@ -5,6 +5,29 @@ #include "curieimu.hpp" %} +%typemap(jni) int16_t* "jshortArray" +%typemap(jstype) int16_t* "short[]" +%typemap(jtype) int16_t* "short[]" + +%typemap(javaout) int16_t* { + return $jnicall; +} + +%typemap(out) int16_t *getAccel { + $result = JCALL1(NewShortArray, jenv, 3); + JCALL4(SetShortArrayRegion, jenv, $result, 0, 3, (jshort*)$1); +} + +%typemap(out) int16_t *getGyro { + $result = JCALL1(NewShortArray, jenv, 3); + JCALL4(SetShortArrayRegion, jenv, $result, 0, 3, (jshort*)$1); +} + +%typemap(out) int16_t *getMotion { + $result = JCALL1(NewShortArray, jenv, 6); + JCALL4(SetShortArrayRegion, jenv, $result, 0, 6, (jshort*)$1); +} + %include "curieimu.hpp" %pragma(java) jniclasscode=%{ diff --git a/src/curieimu/jsupm_curieimu.i b/src/curieimu/jsupm_curieimu.i index 2ecb1deb..cf2955a2 100644 --- a/src/curieimu/jsupm_curieimu.i +++ b/src/curieimu/jsupm_curieimu.i @@ -1,5 +1,12 @@ %module jsupm_curieimu %include "../upm.i" +%include "../carrays_int16_t.i" + +// Adding this typemap because SWIG is converting int16 into a short by default +// This forces SWIG to convert it correctly +%typemap(out) int16_t* { + jsresult = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_int16Array, 0 | 0 ); +} %{ #include "curieimu.hpp" diff --git a/src/curieimu/pyupm_curieimu.i b/src/curieimu/pyupm_curieimu.i index 53054074..d5a1f6de 100644 --- a/src/curieimu/pyupm_curieimu.i +++ b/src/curieimu/pyupm_curieimu.i @@ -2,9 +2,19 @@ %include "pyupm_doxy2swig.i" %module pyupm_curieimu %include "../upm.i" - +%include "../carrays_int16_t.i" %include "stdint.i" +%feature("autodoc", "3"); + +#ifdef DOXYGEN +%include "curieupm_doc.i" +#endif + +%typemap(out) int16_t* { + $result = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_int16Array, 0 | 0 ); +} + %include "curieimu.hpp" %{ #include "curieimu.hpp"