diff --git a/examples/c++/curieimu.cxx b/examples/c++/curieimu.cxx index 0f306387..c9ff3e6d 100644 --- a/examples/c++/curieimu.cxx +++ b/examples/c++/curieimu.cxx @@ -58,7 +58,9 @@ main(int argc, char **argv) sensor->enableShockDetection(true); for(int i=0; i<300; i++) { if (sensor->isShockDetected()) { - sensor->getShockDetectData(&axis, &direction); + sensor->getNextShock(); + axis = sensor->getAxis(); + direction = sensor->getDirection(); printf("shock data is: %d, %d\n", axis, direction); } usleep(10000); diff --git a/examples/javascript/curieimu.js b/examples/javascript/curieimu.js new file mode 100644 index 00000000..68b21cf6 --- /dev/null +++ b/examples/javascript/curieimu.js @@ -0,0 +1,47 @@ +/*jslint node:true, vars:true, bitwise:true, unparam:true */ +/*jshint unused:true */ +/* +* Author: Ron Evans (@deadprogram) +* 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 +* "Software"), to deal in the Software without restriction, including +* without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to +* permit persons to whom the Software is furnished to do so, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +var curieImu = require('jsupm_curieimu'); + +var outputStr; +var myInterval = setInterval(function() +{ + // get accelerometer readings + curieImu.updateAccel(); + outputStr = "accel: x " + curieImu.getAccelX() + + " - y " + curieImu.getAccelY() + + " - z " + curieImu.getAccelZ(); + console.log(outputStr); + console.log(" "); +}, 1000); + +// Print message when exiting +process.on('SIGINT', function() +{ + clearInterval(myInterval); + console.log("Exiting"); + process.exit(0); +}); diff --git a/src/curieimu/curieimu.cpp b/src/curieimu/curieimu.cpp index 9ce925b6..00df7a35 100644 --- a/src/curieimu/curieimu.cpp +++ b/src/curieimu/curieimu.cpp @@ -326,6 +326,18 @@ CurieImu::getTemperature() return result; } +int16_t +CurieImu::getAxis() +{ + return m_axis; +} + +int16_t +CurieImu::getDirection() +{ + return m_direction; +} + void CurieImu::enableShockDetection(bool enable) { @@ -355,15 +367,15 @@ CurieImu::isShockDetected() } void -CurieImu::getShockDetectData(int *axis, int *direction) +CurieImu::getNextShock() { - if (m_shockData.size() > 0) { - IMUDataItem* item = m_shockData.front(); - *axis = item->axis; - *direction = item->direction; - m_shockData.pop(); - delete item; - } + if (m_shockData.size() > 0) { + IMUDataItem* item = m_shockData.front(); + m_axis = item->axis; + m_direction = item->direction; + m_shockData.pop(); + delete item; + } } void @@ -394,13 +406,15 @@ CurieImu::isStepDetected() return (m_stepData.size() > 0); } -void -CurieImu::getStepCount(int *count) +int16_t +CurieImu::getStepCount() { + int16_t count = 0; if (m_stepData.size() > 0) { - *count = m_stepData.front(); + count = m_stepData.front(); m_stepData.pop(); } + return count; } void @@ -432,13 +446,13 @@ CurieImu::isTapDetected() } void -CurieImu::getTapDetectData(int *axis, int *direction) +CurieImu::getNextTap() { - if (m_tapData.size() > 0) { - IMUDataItem* item = m_tapData.front(); - *axis = item->axis; - *direction = item->direction; - m_tapData.pop(); - delete item; - } + if (m_tapData.size() > 0) { + IMUDataItem* item = m_tapData.front(); + m_axis = item->axis; + m_direction = item->direction; + m_tapData.pop(); + delete item; + } } diff --git a/src/curieimu/curieimu.hpp b/src/curieimu/curieimu.hpp index c8c19343..61186cc2 100644 --- a/src/curieimu/curieimu.hpp +++ b/src/curieimu/curieimu.hpp @@ -177,6 +177,20 @@ class CurieImu { */ int16_t* getMotion(); + /** + * Returns last shock or tap axis reading + * + * @return axis value + */ + int16_t getAxis(); + + /** + * Returns last shock or tap direction reading + * + * @return direction value + */ + int16_t getDirection(); + /** * Turns shock detection notifications on/off * @@ -192,12 +206,10 @@ class CurieImu { bool isShockDetected(); /** - * Gets shock detect data from queue - * - * @param axis gets axis data - * @param direction gets direction data + * Gets shock detect data from queue. Then m_axis gets axis data, and + * m_direction gets direction data */ - void getShockDetectData(int *axis, int *direction); + void getNextShock(); /** * Turns step counter notifications on/off @@ -216,9 +228,9 @@ class CurieImu { /** * Gets step count data from queue * - * @param count the total number of steps taken + * @return the total number of steps taken */ - void getStepCount(int *count); + int16_t getStepCount(); /** * Turns tap detection notifications on/off @@ -235,12 +247,10 @@ class CurieImu { bool isTapDetected(); /** - * Gets tap detect data from queue - * - * @param axis gets axis data - * @param direction gets direction data + * Gets tap detect data from queue. Then m_axis gets axis data, and + * m_direction gets direction data */ - void getTapDetectData(int *axis, int *direction); + void getNextTap(); /** * Locks responses from Firmata @@ -289,6 +299,9 @@ class CurieImu { int16_t m_accel[3]; int16_t m_gyro[3]; int16_t m_motion[6]; + + int16_t m_axis; + int16_t m_direction; }; }