mirror of
https://github.com/eclipse/upm.git
synced 2025-03-24 01:10:22 +03:00
mma7660: Initial implementation
This was tested on the Grove I2C 3-axis digital accelerometer. Signed-off-by: Jon Trulson <jtrulson@ics.com> Signed-off-by: Zion Orent <zorent@ics.com> Signed-off-by: John Van Drasek <john.r.van.drasek@intel.com>
This commit is contained in:
parent
e0e9405634
commit
12c290de70
@ -80,6 +80,7 @@ add_executable (rotaryencoder-example rotaryencoder.cxx)
|
|||||||
add_executable (adxl345-example adxl345.cxx)
|
add_executable (adxl345-example adxl345.cxx)
|
||||||
add_executable (rpr220-example rpr220.cxx)
|
add_executable (rpr220-example rpr220.cxx)
|
||||||
add_executable (rpr220-intr-example rpr220-intr.cxx)
|
add_executable (rpr220-intr-example rpr220-intr.cxx)
|
||||||
|
add_executable (mma7660-example mma7660.cxx)
|
||||||
|
|
||||||
include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
|
include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
|
||||||
include_directories (${PROJECT_SOURCE_DIR}/src/grove)
|
include_directories (${PROJECT_SOURCE_DIR}/src/grove)
|
||||||
@ -145,6 +146,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/my9221)
|
|||||||
include_directories (${PROJECT_SOURCE_DIR}/src/rotaryencoder)
|
include_directories (${PROJECT_SOURCE_DIR}/src/rotaryencoder)
|
||||||
include_directories (${PROJECT_SOURCE_DIR}/src/adxl345)
|
include_directories (${PROJECT_SOURCE_DIR}/src/adxl345)
|
||||||
include_directories (${PROJECT_SOURCE_DIR}/src/rpr220)
|
include_directories (${PROJECT_SOURCE_DIR}/src/rpr220)
|
||||||
|
include_directories (${PROJECT_SOURCE_DIR}/src/mma7660)
|
||||||
|
|
||||||
target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
|
target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
|
||||||
target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
|
target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
|
||||||
@ -228,3 +230,4 @@ target_link_libraries (rotaryencoder-example rotaryencoder ${CMAKE_THREAD_LIBS_I
|
|||||||
target_link_libraries (adxl345-example adxl345 ${CMAKE_THREAD_LIBS_INIT})
|
target_link_libraries (adxl345-example adxl345 ${CMAKE_THREAD_LIBS_INIT})
|
||||||
target_link_libraries (rpr220-example rpr220 ${CMAKE_THREAD_LIBS_INIT})
|
target_link_libraries (rpr220-example rpr220 ${CMAKE_THREAD_LIBS_INIT})
|
||||||
target_link_libraries (rpr220-intr-example rpr220 ${CMAKE_THREAD_LIBS_INIT})
|
target_link_libraries (rpr220-intr-example rpr220 ${CMAKE_THREAD_LIBS_INIT})
|
||||||
|
target_link_libraries (mma7660-example mma7660 ${CMAKE_THREAD_LIBS_INIT})
|
||||||
|
83
examples/javascript/mma7660.js
Normal file
83
examples/javascript/mma7660.js
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
/*jslint node:true, vars:true, bitwise:true, unparam:true */
|
||||||
|
/*jshint unused:true */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Author: Zion Orent <zorent@ics.com>
|
||||||
|
* Copyright (c) 2015 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 digitalAccelerometer = require('jsupm_mma7660');
|
||||||
|
|
||||||
|
// Instantiate an MMA7660 on I2C bus 0
|
||||||
|
var myDigitalAccelerometer = new digitalAccelerometer.MMA7660(
|
||||||
|
digitalAccelerometer.MMA7660_I2C_BUS,
|
||||||
|
digitalAccelerometer.MMA7660_DEFAULT_I2C_ADDR
|
||||||
|
);
|
||||||
|
|
||||||
|
// place device in standby mode so we can write registers
|
||||||
|
myDigitalAccelerometer.setModeStandby();
|
||||||
|
|
||||||
|
// enable 64 samples per second
|
||||||
|
myDigitalAccelerometer.setSampleRate(digitalAccelerometer.MMA7660.AUTOSLEEP_64);
|
||||||
|
|
||||||
|
// place device into active mode
|
||||||
|
myDigitalAccelerometer.setModeActive();
|
||||||
|
|
||||||
|
var myInterval = setInterval(function()
|
||||||
|
{
|
||||||
|
var x, y, z;
|
||||||
|
x = digitalAccelerometer.new_intp();
|
||||||
|
y = digitalAccelerometer.new_intp();
|
||||||
|
z = digitalAccelerometer.new_intp();
|
||||||
|
|
||||||
|
myDigitalAccelerometer.getRawValues(x, y, z);
|
||||||
|
var outputStr = "Raw values: x = " + digitalAccelerometer.intp_value(x) +
|
||||||
|
" y = " + digitalAccelerometer.intp_value(y) +
|
||||||
|
" z = " + digitalAccelerometer.intp_value(z);
|
||||||
|
console.log(outputStr);
|
||||||
|
|
||||||
|
var ax, ay, az;
|
||||||
|
ax = digitalAccelerometer.new_floatp();
|
||||||
|
ay = digitalAccelerometer.new_floatp();
|
||||||
|
az = digitalAccelerometer.new_floatp();
|
||||||
|
|
||||||
|
myDigitalAccelerometer.getAcceleration(ax, ay, az);
|
||||||
|
outputStr = "Acceleration: x = " + roundNum(digitalAccelerometer.floatp_value(ax), 6) +
|
||||||
|
"g y = " + roundNum(digitalAccelerometer.floatp_value(ay), 6) +
|
||||||
|
"g z = " + roundNum(digitalAccelerometer.floatp_value(az), 6) + "g";
|
||||||
|
console.log(outputStr);
|
||||||
|
}, 500);
|
||||||
|
|
||||||
|
// round off output to match C example, which has 6 decimal places
|
||||||
|
function roundNum(num, decimalPlaces)
|
||||||
|
{
|
||||||
|
var extraNum = (1 / (Math.pow(10, decimalPlaces) * 1000));
|
||||||
|
return (Math.round((num + extraNum) * (Math.pow(10, decimalPlaces))) / Math.pow(10, decimalPlaces));
|
||||||
|
}
|
||||||
|
|
||||||
|
// When exiting: clear interval and print message
|
||||||
|
process.on('SIGINT', function()
|
||||||
|
{
|
||||||
|
clearInterval(myInterval);
|
||||||
|
console.log("Exiting...");
|
||||||
|
process.exit(0);
|
||||||
|
});
|
86
examples/mma7660.cxx
Normal file
86
examples/mma7660.cxx
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
/*
|
||||||
|
* Author: Jon Trulson <jtrulson@ics.com>
|
||||||
|
* Copyright (c) 2015 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include "mma7660.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int shouldRun = true;
|
||||||
|
|
||||||
|
void sig_handler(int signo)
|
||||||
|
{
|
||||||
|
if (signo == SIGINT)
|
||||||
|
shouldRun = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
signal(SIGINT, sig_handler);
|
||||||
|
|
||||||
|
//! [Interesting]
|
||||||
|
// Instantiate an MMA7660 on I2C bus 0
|
||||||
|
|
||||||
|
upm::MMA7660 *accel = new upm::MMA7660(MMA7660_I2C_BUS,
|
||||||
|
MMA7660_DEFAULT_I2C_ADDR);
|
||||||
|
|
||||||
|
// place device in standby mode so we can write registers
|
||||||
|
accel->setModeStandby();
|
||||||
|
|
||||||
|
// enable 64 samples per second
|
||||||
|
accel->setSampleRate(upm::MMA7660::AUTOSLEEP_64);
|
||||||
|
|
||||||
|
// place device into active mode
|
||||||
|
accel->setModeActive();
|
||||||
|
|
||||||
|
while (shouldRun)
|
||||||
|
{
|
||||||
|
int x, y, z;
|
||||||
|
|
||||||
|
accel->getRawValues(&x, &y, &z);
|
||||||
|
cout << "Raw values: x = " << x
|
||||||
|
<< " y = " << y
|
||||||
|
<< " z = " << z
|
||||||
|
<< endl;
|
||||||
|
|
||||||
|
float ax, ay, az;
|
||||||
|
|
||||||
|
accel->getAcceleration(&ax, &ay, &az);
|
||||||
|
cout << "Acceleration: x = " << ax
|
||||||
|
<< "g y = " << ay
|
||||||
|
<< "g z = " << az
|
||||||
|
<< "g" << endl;
|
||||||
|
|
||||||
|
usleep(500000);
|
||||||
|
}
|
||||||
|
|
||||||
|
//! [Interesting]
|
||||||
|
|
||||||
|
cout << "Exiting..." << endl;
|
||||||
|
|
||||||
|
delete accel;
|
||||||
|
return 0;
|
||||||
|
}
|
5
src/mma7660/CMakeLists.txt
Normal file
5
src/mma7660/CMakeLists.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
set (libname "mma7660")
|
||||||
|
set (libdescription "upm mma7660 I2C 3-axis digital accelermeter (1.5g)")
|
||||||
|
set (module_src ${libname}.cxx)
|
||||||
|
set (module_h ${libname}.h)
|
||||||
|
upm_module_init()
|
13
src/mma7660/jsupm_mma7660.i
Normal file
13
src/mma7660/jsupm_mma7660.i
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
%module jsupm_mma7660
|
||||||
|
%include "../upm.i"
|
||||||
|
%include "cpointer.i"
|
||||||
|
|
||||||
|
/* Send "int *" and "float *" to JavaScript as intp and floatp */
|
||||||
|
%pointer_functions(int, intp);
|
||||||
|
%pointer_functions(float, floatp);
|
||||||
|
|
||||||
|
%{
|
||||||
|
#include "mma7660.h"
|
||||||
|
%}
|
||||||
|
|
||||||
|
%include "mma7660.h"
|
249
src/mma7660/mma7660.cxx
Normal file
249
src/mma7660/mma7660.cxx
Normal file
@ -0,0 +1,249 @@
|
|||||||
|
/*
|
||||||
|
* Author: Jon Trulson <jtrulson@ics.com>
|
||||||
|
* Copyright (c) 2015 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "mma7660.h"
|
||||||
|
|
||||||
|
using namespace upm;
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
|
MMA7660::MMA7660(int bus, uint8_t address)
|
||||||
|
{
|
||||||
|
m_addr = address;
|
||||||
|
m_isrInstalled = false;
|
||||||
|
|
||||||
|
// setup our i2c link
|
||||||
|
if ( !(m_i2c = mraa_i2c_init(bus)) )
|
||||||
|
{
|
||||||
|
cerr << "MMA7660: mraa_i2c_init() failed." << endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
mraa_result_t rv;
|
||||||
|
|
||||||
|
if ( (rv = mraa_i2c_address(m_i2c, m_addr)) != MRAA_SUCCESS)
|
||||||
|
{
|
||||||
|
cerr << "MMA7660: Could not initialize i2c bus. " << endl;
|
||||||
|
mraa_result_print(rv);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MMA7660::~MMA7660()
|
||||||
|
{
|
||||||
|
if (m_isrInstalled)
|
||||||
|
uninstallISR();
|
||||||
|
|
||||||
|
setModeStandby();
|
||||||
|
mraa_i2c_stop(m_i2c);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MMA7660::writeByte(uint8_t reg, uint8_t byte)
|
||||||
|
{
|
||||||
|
mraa_result_t rv = mraa_i2c_write_byte_data(m_i2c, byte, reg);
|
||||||
|
|
||||||
|
if (rv != MRAA_SUCCESS)
|
||||||
|
{
|
||||||
|
cerr << __FUNCTION__ << ": mraa_i2c_write_byte() failed." << endl;
|
||||||
|
mraa_result_print(rv);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t MMA7660::readByte(uint8_t reg)
|
||||||
|
{
|
||||||
|
return mraa_i2c_read_byte_data(m_i2c, reg);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MMA7660::getRawValues(int *x, int *y, int *z)
|
||||||
|
{
|
||||||
|
*x = getVerifiedAxis(REG_XOUT);
|
||||||
|
*y = getVerifiedAxis(REG_YOUT);
|
||||||
|
*z = getVerifiedAxis(REG_ZOUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MMA7660::setModeActive()
|
||||||
|
{
|
||||||
|
uint8_t modeReg = readByte(REG_MODE);
|
||||||
|
|
||||||
|
// The D2 (TON bit) should be cleared, and the MODE bit set
|
||||||
|
|
||||||
|
modeReg &= ~MODE_TON;
|
||||||
|
modeReg |= MODE_MODE;
|
||||||
|
|
||||||
|
writeByte(REG_MODE, modeReg);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MMA7660::setModeStandby()
|
||||||
|
{
|
||||||
|
uint8_t modeReg = readByte(REG_MODE);
|
||||||
|
|
||||||
|
// the D0 (mode bit) and D2 (TON bit) should be cleared.
|
||||||
|
|
||||||
|
modeReg &= ~MODE_TON;
|
||||||
|
modeReg &= ~MODE_MODE;
|
||||||
|
|
||||||
|
writeByte(REG_MODE, modeReg);
|
||||||
|
}
|
||||||
|
|
||||||
|
// read an axis value, verifying it's validity
|
||||||
|
int MMA7660::getVerifiedAxis(MMA7660_REG_T axis)
|
||||||
|
{
|
||||||
|
// We only want one of the 3 axes
|
||||||
|
|
||||||
|
if (axis > 2)
|
||||||
|
{
|
||||||
|
cerr << __FUNCTION__ << ": axis must be 0, 1, or 2." << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// we need to check the alert bit and sign bits if the alert bit is
|
||||||
|
// set, this means that the register was being updated when the
|
||||||
|
// register was read, so re-read until it's clear.
|
||||||
|
|
||||||
|
uint8_t val;
|
||||||
|
do {
|
||||||
|
val = readByte(axis);
|
||||||
|
|
||||||
|
// check alert bit
|
||||||
|
} while (val & 0x40);
|
||||||
|
|
||||||
|
// shift the sign bit over, and compensate
|
||||||
|
return (char(val << 2) / 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
// read the tilt register, verifying it's validity
|
||||||
|
uint8_t MMA7660::getVerifiedTilt()
|
||||||
|
{
|
||||||
|
// we need to check the alert bit and sign bits if the alert bit is
|
||||||
|
// set, this means that the register was being updated when the
|
||||||
|
// register was read, so re-read until it's clear.
|
||||||
|
|
||||||
|
uint8_t val;
|
||||||
|
do {
|
||||||
|
val = readByte(REG_TILT);
|
||||||
|
|
||||||
|
// check alert bit
|
||||||
|
} while (val & 0x40);
|
||||||
|
|
||||||
|
// shift the sign bit over, and compensate
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t MMA7660::tiltBackFront()
|
||||||
|
{
|
||||||
|
uint8_t val = getVerifiedTilt();
|
||||||
|
|
||||||
|
// mask off the bits we don't care about
|
||||||
|
val &= 0x03;
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t MMA7660::tiltLandscapePortrait()
|
||||||
|
{
|
||||||
|
uint8_t val = getVerifiedTilt();
|
||||||
|
|
||||||
|
// mask off the bits we don't care about
|
||||||
|
val >>= 2;
|
||||||
|
val &= 0x07;
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MMA7660::tiltTap()
|
||||||
|
{
|
||||||
|
uint8_t val = getVerifiedTilt();
|
||||||
|
|
||||||
|
if (val & 0x20)
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MMA7660::tiltShake()
|
||||||
|
{
|
||||||
|
uint8_t val = getVerifiedTilt();
|
||||||
|
|
||||||
|
if (val & 0x80)
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MMA7660::installISR(int pin, void (*isr)(void *), void *arg)
|
||||||
|
{
|
||||||
|
if (m_isrInstalled)
|
||||||
|
uninstallISR();
|
||||||
|
|
||||||
|
if ( !(m_gpio = mraa_gpio_init(pin)) )
|
||||||
|
{
|
||||||
|
cerr << __FUNCTION__ << ": mraa_gpio_init() failed" << endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
mraa_gpio_dir(m_gpio, MRAA_GPIO_IN);
|
||||||
|
|
||||||
|
// install our interrupt handler
|
||||||
|
mraa_gpio_isr(m_gpio, MRAA_GPIO_EDGE_RISING,
|
||||||
|
isr, arg);
|
||||||
|
m_isrInstalled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MMA7660::uninstallISR()
|
||||||
|
{
|
||||||
|
if (!m_isrInstalled)
|
||||||
|
return;
|
||||||
|
|
||||||
|
mraa_gpio_isr_exit(m_gpio);
|
||||||
|
m_isrInstalled = false;
|
||||||
|
mraa_gpio_close(m_gpio);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MMA7660::setInterruptBits(uint8_t ibits)
|
||||||
|
{
|
||||||
|
return writeByte(REG_INTSU, ibits);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MMA7660::setSampleRate(MMA7660_AUTOSLEEP_T sr)
|
||||||
|
{
|
||||||
|
return writeByte(REG_SR, sr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MMA7660::getAcceleration(float *ax, float *ay, float *az)
|
||||||
|
{
|
||||||
|
int x, y, z;
|
||||||
|
|
||||||
|
getRawValues(&x, &y, &z);
|
||||||
|
|
||||||
|
// 21.33, typical counts/g
|
||||||
|
|
||||||
|
*ax = x/21.33;
|
||||||
|
*ay = y/21.33;
|
||||||
|
*az = z/21.33;
|
||||||
|
}
|
||||||
|
|
273
src/mma7660/mma7660.h
Normal file
273
src/mma7660/mma7660.h
Normal file
@ -0,0 +1,273 @@
|
|||||||
|
/*
|
||||||
|
* Author: Jon Trulson <jtrulson@ics.com>
|
||||||
|
* Copyright (c) 2015 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.
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <mraa/i2c.h>
|
||||||
|
#include <mraa/gpio.h>
|
||||||
|
|
||||||
|
#define MMA7660_I2C_BUS 0
|
||||||
|
#define MMA7660_DEFAULT_I2C_ADDR 0x4c
|
||||||
|
|
||||||
|
namespace upm {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief C++ API for the MMA7660 I2C 3-axis digital accelerometer
|
||||||
|
*
|
||||||
|
* UPM module for the MMA7660 I2C 3-axis digital accelerometer.
|
||||||
|
* This device supports a variety of capabilities, including the
|
||||||
|
* generation of interrupts for various conditions, tilt and basic
|
||||||
|
* gesture detection, and of course X/Y/Z measurements of g-forces
|
||||||
|
* being applied (up to 1.5g).
|
||||||
|
*
|
||||||
|
* This module was tested with the Grove 3-Axis Digital
|
||||||
|
* Accelerometer (1.5g)
|
||||||
|
*
|
||||||
|
* @ingroup i2c gpio mma7660
|
||||||
|
* @snippet mma7660.cxx Interesting
|
||||||
|
*/
|
||||||
|
class MMA7660 {
|
||||||
|
public:
|
||||||
|
|
||||||
|
// MMA7660 registers
|
||||||
|
typedef enum { REG_XOUT = 0x00,
|
||||||
|
REG_YOUT = 0x01,
|
||||||
|
REG_ZOUT = 0x02,
|
||||||
|
REG_TILT = 0x03,
|
||||||
|
REG_SRST = 0x04, // Sampling Rate Status
|
||||||
|
REG_SPCNT = 0x05, // sleep count
|
||||||
|
REG_INTSU = 0x06, // Interrupt setup
|
||||||
|
REG_MODE = 0x07, // operating mode
|
||||||
|
REG_SR = 0x08, // auto wake/sleep, SPS and debounce
|
||||||
|
REG_PDET = 0x09, // tap detection
|
||||||
|
REG_PD = 0x0a // tap debounce count
|
||||||
|
// 0x0b-0x1f reserved
|
||||||
|
} MMA7660_REG_T;
|
||||||
|
|
||||||
|
// interrupt enable register bits
|
||||||
|
typedef enum { INTR_NONE = 0x00, // disabled
|
||||||
|
INTR_FBINT = 0x01, // front/back
|
||||||
|
INTR_PLINT = 0x02, // up/down/right/left
|
||||||
|
INTR_PDINT = 0x04, // tap detection
|
||||||
|
INTR_ASINT = 0x08, // exit autosleep
|
||||||
|
INTR_GINT = 0x10, // measurement intr
|
||||||
|
INTR_SHINTZ = 0x20, // shake on Z
|
||||||
|
INTR_SHINTY = 0x40, // shake on Y
|
||||||
|
INTR_SHINTX = 0x80 // shake on X
|
||||||
|
} MMA7660_INTR_T;
|
||||||
|
|
||||||
|
// operating mode register bits
|
||||||
|
typedef enum { MODE_MODE = 0x01, // determines mode with MODE_TON
|
||||||
|
// 0x02 reserved
|
||||||
|
MODE_TON = 0x04, // determines mode with MODE_MODE
|
||||||
|
MODE_AWE = 0x08, // auto-wake
|
||||||
|
MODE_ASE = 0x10, // auto-sleep
|
||||||
|
MODE_SCPS = 0x20, // sleep count pre-scale
|
||||||
|
MODE_IPP = 0x40, // intr out push-pull/open drain
|
||||||
|
MODE_IAH = 0x80 // intr active low/high
|
||||||
|
} MMA7660_MODE_T;
|
||||||
|
|
||||||
|
// tilt BackFront (BF) bits
|
||||||
|
typedef enum { BF_UNKNOWN = 0x00,
|
||||||
|
BF_LYING_FRONT = 0x01,
|
||||||
|
BF_LYING_BACK = 0x02
|
||||||
|
} MMA7660_TILT_BF_T;
|
||||||
|
|
||||||
|
// tilt LandscapePortrait (LP) bits
|
||||||
|
typedef enum { LP_UNKNOWN = 0x00,
|
||||||
|
LP_LANDSCAPE_LEFT = 0x01,
|
||||||
|
LP_LANDSCAPE_RIGHT = 0x02,
|
||||||
|
LP_VERT_DOWN = 0x05,
|
||||||
|
LP_VERT_UP = 0x06
|
||||||
|
} MMA7660_TILT_LP_T;
|
||||||
|
|
||||||
|
// sample rate (auto sleep) values
|
||||||
|
typedef enum { AUTOSLEEP_120 = 0x00,
|
||||||
|
AUTOSLEEP_64 = 0x01,
|
||||||
|
AUTOSLEEP_32 = 0x02,
|
||||||
|
AUTOSLEEP_16 = 0x03,
|
||||||
|
AUTOSLEEP_8 = 0x04,
|
||||||
|
AUTOSLEEP_4 = 0x05,
|
||||||
|
AUTOSLEEP_2 = 0x06,
|
||||||
|
AUTOSLEEP_1 = 0x07
|
||||||
|
} MMA7660_AUTOSLEEP_T;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* mma7660 constructor
|
||||||
|
*
|
||||||
|
* @param bus i2c bus to use
|
||||||
|
* @param address the address for this sensor; default is 0x55
|
||||||
|
*/
|
||||||
|
MMA7660(int bus, uint8_t address = MMA7660_DEFAULT_I2C_ADDR);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MMA7660 Destructor
|
||||||
|
*/
|
||||||
|
~MMA7660();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write byte value into register
|
||||||
|
*
|
||||||
|
* @param reg register location to write into
|
||||||
|
* @param byte byte to write
|
||||||
|
* @return true if successful
|
||||||
|
*/
|
||||||
|
bool writeByte(uint8_t reg, uint8_t byte);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read byte value from register
|
||||||
|
*
|
||||||
|
* @param reg register location to read from
|
||||||
|
* @return value at specified register
|
||||||
|
*/
|
||||||
|
uint8_t readByte(uint8_t reg);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read current value of conversion
|
||||||
|
*
|
||||||
|
* @param x returned x value
|
||||||
|
* @param y returned y value
|
||||||
|
* @param z returned z value
|
||||||
|
*/
|
||||||
|
void getRawValues(int *x, int *y, int *z);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the computed acceleration
|
||||||
|
*
|
||||||
|
* @param ax returned computed acceleration of X axis
|
||||||
|
* @param ay returned computed acceleration of Y axis
|
||||||
|
* @param az returned computed acceleration of Z axis
|
||||||
|
*/
|
||||||
|
void getAcceleration(float *ax, float *ay, float *az);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read an axis, verifying it's validity. The value passed must
|
||||||
|
* be one of REG_XOUT, REG_YOUT, or REG_ZOUT.
|
||||||
|
*
|
||||||
|
* @param axis axis to read
|
||||||
|
* @return axis value
|
||||||
|
*/
|
||||||
|
int getVerifiedAxis(MMA7660_REG_T axis);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read the tilt register, verifying it's validity.
|
||||||
|
*
|
||||||
|
* @return tilt value
|
||||||
|
*/
|
||||||
|
uint8_t getVerifiedTilt();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Put the device into active mode. In active mode, register
|
||||||
|
* write are not allowed. Place the device in Standby mode before
|
||||||
|
* attempting to write registers.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void setModeActive();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Put the device into Standby (power saving) mode. Note, when in
|
||||||
|
* standby mode, there will be no valid data in the registers. In
|
||||||
|
* addition, the only way to write a register is to place the
|
||||||
|
* device in standby mode.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void setModeStandby();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read tilt BackFront bits
|
||||||
|
*
|
||||||
|
* The value returned will be one of the MMA7660_TILT_BF_T values
|
||||||
|
*
|
||||||
|
* @return the bits corresponding to the BackFront tilt status
|
||||||
|
*/
|
||||||
|
uint8_t tiltBackFront();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read tilt LandscapePortrait bits
|
||||||
|
*
|
||||||
|
* The value returned will be one of the MMA7660_TILT_LP_T values
|
||||||
|
*
|
||||||
|
* @return the bits corresponding to the LandscapePortrait tilt status
|
||||||
|
*/
|
||||||
|
uint8_t tiltLandscapePortrait();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* read the tilt Tap status
|
||||||
|
*
|
||||||
|
* @return true if a tap was detected
|
||||||
|
*/
|
||||||
|
bool tiltTap();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* read the tilt Shake status
|
||||||
|
*
|
||||||
|
* @return true if a Shake was detected
|
||||||
|
*/
|
||||||
|
bool tiltShake();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Install an Interrupt Service Routine (ISR) to be called when
|
||||||
|
* an interrupt occurs
|
||||||
|
*
|
||||||
|
* @param pin gpio pin to use as interrupt pin
|
||||||
|
* @param fptr function pointer to function to be called on interrupt
|
||||||
|
* @param arg pointer to an object that will be supplied as an
|
||||||
|
* argument to the ISR.
|
||||||
|
*/
|
||||||
|
void installISR(int pin, void (*isr)(void *), void *arg);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uninstall the previously installed Interrupt Service Routine (ISR)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void uninstallISR();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enable interrupt generation based on the passed interrupt bits.
|
||||||
|
* The bits are a bit mask of the requested MMA7660_INTR_T values.
|
||||||
|
* Note: The device must be in standby mode to set this register.
|
||||||
|
*
|
||||||
|
* @param ibits set the requested interrupt bits
|
||||||
|
* @return true if successful
|
||||||
|
*/
|
||||||
|
bool setInterruptBits(uint8_t ibits);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the sampling rate of the sensor. The value supplied must
|
||||||
|
* be one of the MMA7660_AUTOSLEEP_T values.
|
||||||
|
*
|
||||||
|
* @param sr one of the MMA7660_AUTOSLEEP_T values
|
||||||
|
* @return true if successful
|
||||||
|
*/
|
||||||
|
bool setSampleRate(MMA7660_AUTOSLEEP_T sr);
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool m_isrInstalled;
|
||||||
|
mraa_i2c_context m_i2c;
|
||||||
|
mraa_gpio_context m_gpio;
|
||||||
|
uint8_t m_addr;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
13
src/mma7660/pyupm_mma7660.i
Normal file
13
src/mma7660/pyupm_mma7660.i
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
%module pyupm_mma7660
|
||||||
|
%include "../upm.i"
|
||||||
|
|
||||||
|
%feature("autodoc", "3");
|
||||||
|
|
||||||
|
#ifdef DOXYGEN
|
||||||
|
%include "mma7660_doc.i"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
%include "mma7660.h"
|
||||||
|
%{
|
||||||
|
#include "mma7660.h"
|
||||||
|
%}
|
Loading…
x
Reference in New Issue
Block a user