mma7660: C implementation; FTI; C++ wraps C

Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
Jon Trulson
2016-10-27 15:12:26 -06:00
parent 585f2eb331
commit c09ab37a59
16 changed files with 1252 additions and 510 deletions

View File

@ -44,36 +44,29 @@ int main(int argc, char **argv)
//! [Interesting]
// Instantiate an MMA7660 on I2C bus 0
upm::MMA7660 *accel = new upm::MMA7660(MMA7660_I2C_BUS,
upm::MMA7660 *accel = new upm::MMA7660(MMA7660_DEFAULT_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);
accel->setSampleRate(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
cout << "Acceleration: x = " << ax
<< "g y = " << ay
<< "g z = " << az
<< "g" << endl;
cout << endl;
usleep(500000);
}

View File

@ -130,6 +130,7 @@ add_example (rpr220)
add_example (md)
add_example (linefinder)
add_example (uln200xa)
add_example (mma7660)
# Custom examples
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)

83
examples/c/mma7660.c Normal file
View File

@ -0,0 +1,83 @@
/*
* 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 <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include "upm_utilities.h"
#include "mma7660.h"
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
mma7660_context accel = mma7660_init(MMA7660_DEFAULT_I2C_BUS,
MMA7660_DEFAULT_I2C_ADDR);
if (!accel)
{
printf("mma7660_init() failed\n");
return 1;
}
// place device in standby mode so we can write registers
mma7660_set_mode_standby(accel);
// enable 64 samples per second
mma7660_set_sample_rate(accel, MMA7660_AUTOSLEEP_64);
// place device into active mode
mma7660_set_mode_active(accel);
while (shouldRun)
{
float ax, ay, az;
mma7660_get_acceleration(accel, &ax, &ay, &az);
printf("Acceleration: x = %f y = %f z = %f\n\n",
ax, ay, az);
upm_delay_ms(500);
}
printf("Exiting...\n");
mma7660_close(accel);
//! [Interesting]
return 0;
}

View File

@ -1,5 +1,6 @@
/*
* Author: Stefan Andritoiu <stefan.andritoiu@intel.com>
* Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2015 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
@ -22,34 +23,39 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
//NOT TESTED!!!
public class MMA7660Sample {
import upm_mma7660.MMA7660;
public static void main(String[] args) throws InterruptedException {
// ! [Interesting]
// Instantiate an MMA7660 on I2C bus 0
upm_mma7660.MMA7660 accel = new upm_mma7660.MMA7660(0);
public class MMA7660Sample
{
public static void main(String[] args) throws InterruptedException
{
// ! [Interesting]
// Instantiate an MMA7660 on I2C bus 0
MMA7660 accel = new MMA7660(0);
// place device in standby mode so we can write registers
accel.setModeStandby();
// place device in standby mode so we can write registers
accel.setModeStandby();
// enable 64 samples per second
accel.setSampleRate(upm_mma7660.MMA7660.MMA7660_AUTOSLEEP_T.AUTOSLEEP_64);
// enable 64 samples per second
accel.setSampleRate(upm_mma7660.MMA7660_AUTOSLEEP_T.MMA7660_AUTOSLEEP_64);
// place device into active mode
accel.setModeActive();
// place device into active mode
accel.setModeActive();
while (true) {
int[] rawValues = accel.getRawValues();
System.out.println("Raw Values: x = " + rawValues[0] + " y = " + rawValues[1] + " x = "
+ rawValues[2]);
while (true)
{
float acceleration[] = accel.getAcceleration();
System.out.println("Acceleration: x = "
+ acceleration[0]
+ " y = "
+ acceleration[1]
+ " x = "
+ acceleration[2]);
float[] acceleration = accel.getAcceleration();
System.out.println("Raw Values: x = " + acceleration[0] + " y = " + acceleration[1]
+ " x = " + acceleration[2]);
System.out.println();
Thread.sleep(1000);
}
// ! [Interesting]
Thread.sleep(500);
}
// ! [Interesting]
}
}
}

View File

@ -26,23 +26,18 @@ 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_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);
myDigitalAccelerometer.setSampleRate(digitalAccelerometer.MMA7660_AUTOSLEEP_64);
// place device into active mode
myDigitalAccelerometer.setModeActive();
var x, y, z;
x = digitalAccelerometer.new_intp();
y = digitalAccelerometer.new_intp();
z = digitalAccelerometer.new_intp();
var ax, ay, az;
ax = digitalAccelerometer.new_floatp();
ay = digitalAccelerometer.new_floatp();
@ -52,18 +47,13 @@ var outputStr;
var myInterval = setInterval(function()
{
myDigitalAccelerometer.getRawValues(x, y, z);
outputStr = "Raw values: x = " + digitalAccelerometer.intp_value(x) +
" y = " + digitalAccelerometer.intp_value(y) +
" z = " + digitalAccelerometer.intp_value(z);
console.log(outputStr);
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);
console.log();
}, 500);
// round off output to match C example, which has 6 decimal places

View File

@ -28,7 +28,7 @@ from upm import pyupm_mma7660 as upmMMA7660
def main():
# Instantiate an MMA7660 on I2C bus 0
myDigitalAccelerometer = upmMMA7660.MMA7660(
upmMMA7660.MMA7660_I2C_BUS,
upmMMA7660.MMA7660_DEFAULT_I2C_BUS,
upmMMA7660.MMA7660_DEFAULT_I2C_ADDR);
## Exit handlers ##
@ -49,35 +49,24 @@ def main():
myDigitalAccelerometer.setModeStandby()
# enable 64 samples per second
myDigitalAccelerometer.setSampleRate(upmMMA7660.MMA7660.AUTOSLEEP_64)
myDigitalAccelerometer.setSampleRate(upmMMA7660.MMA7660_AUTOSLEEP_64)
# place device into active mode
myDigitalAccelerometer.setModeActive()
x = upmMMA7660.new_intp()
y = upmMMA7660.new_intp()
z = upmMMA7660.new_intp()
ax = upmMMA7660.new_floatp()
ay = upmMMA7660.new_floatp()
az = upmMMA7660.new_floatp()
while (1):
myDigitalAccelerometer.getRawValues(x, y, z)
outputStr = ("Raw values: x = {0}"
" y = {1}"
" z = {2}").format(upmMMA7660.intp_value(x),
upmMMA7660.intp_value(y),
upmMMA7660.intp_value(z))
print(outputStr)
myDigitalAccelerometer.getAcceleration(ax, ay, az)
outputStr = ("Acceleration: x = {0}"
"g y = {1}"
"g z = {2}g").format(upmMMA7660.floatp_value(ax),
"g y = {1}"
"g z = {2}g").format(upmMMA7660.floatp_value(ax),
upmMMA7660.floatp_value(ay),
upmMMA7660.floatp_value(az))
print(outputStr)
print()
time.sleep(.5)
if __name__ == '__main__':