lsm303agr: Initial implementation, C, FTI, C++ wraps C

Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
Jon Trulson
2017-04-14 16:24:57 -06:00
parent 12c59a6aca
commit 1f5f466691
16 changed files with 3015 additions and 0 deletions

View File

@ -0,0 +1,87 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2017 Intel Corporation.
*
* The MIT License
*
* 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 <iostream>
#include <signal.h>
#include "lsm303agr.hpp"
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 LSM303AGR using default I2C parameters
upm::LSM303AGR sensor;
// now output data every 250 milliseconds
while (shouldRun)
{
float x, y, z;
sensor.update();
sensor.getAccelerometer(&x, &y, &z);
cout << "Accelerometer x: " << x
<< " y: " << y
<< " z: " << z
<< " g"
<< endl;
sensor.getMagnetometer(&x, &y, &z);
cout << "Magnetometer x: " << x
<< " y: " << y
<< " z: " << z
<< " uT"
<< endl;
cout << "Temperature: "
<< sensor.getTemperature()
<< " C"
<< endl;
cout << endl;
usleep(250000);
}
//! [Interesting]
cout << "Exiting..." << endl;
return 0;
}

90
examples/c/lsm303agr.c Normal file
View File

@ -0,0 +1,90 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2017 Intel Corporation.
*
* The MIT License
*
* 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 <stdio.h>
#include <signal.h>
#include "upm_utilities.h"
#include "lsm303agr.h"
bool shouldRun = true;
void sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
}
int main(int argc, char **argv)
{
signal(SIGINT, sig_handler);
//! [Interesting]
// Instantiate a LSM303AGR instance using default i2c bus and addresses
lsm303agr_context sensor = lsm303agr_init(LSM303AGR_DEFAULT_I2C_BUS,
LSM303AGR_DEFAULT_ACC_ADDR,
LSM303AGR_DEFAULT_MAG_ADDR);
if (!sensor)
{
printf("lsm303agr_init() failed.\n");
return 1;
}
// now output data every 250 milliseconds
while (shouldRun)
{
float x, y, z;
if (lsm303agr_update(sensor))
{
printf("lsm303agr_update() failed\n");
return 1;
}
lsm303agr_get_accelerometer(sensor, &x, &y, &z);
printf("Accelerometer x: %f y: %f z: %f g\n",
x, y, z);
lsm303agr_get_magnetometer(sensor, &x, &y, &z);
printf("Magnetometer x: %f y: %f z: %f uT\n",
x, y, z);
printf("Temperature: %f C\n\n", lsm303agr_get_temperature(sensor));
upm_delay_ms(250);
}
printf("Exiting...\n");
lsm303agr_close(sensor);
//! [Interesting]
return 0;
}

View File

@ -169,6 +169,7 @@ add_example(P9813Sample p9813)
add_example(BMG160_Example bmg160)
add_example(BMA250E_Example bma250e)
add_example(BMM150_Example bmm150)
add_example(LSM303AGR_Example lsm303agr)
add_example_with_path(Jhd1313m1_lcdSample jhd1313m1 jhd1313m1)
add_example_with_path(Jhd1313m1Sample jhd1313m1 jhd1313m1)

View File

@ -0,0 +1,65 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2017 Intel Corporation.
*
* The MIT License
*
* 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.
*/
import upm_lsm303agr.LSM303AGR;
public class LSM303AGR_Example
{
public static void main(String[] args) throws InterruptedException
{
// ! [Interesting]
// Instantiate a LSM303AGR instance using default i2c bus and address
LSM303AGR sensor = new LSM303AGR();
while (true)
{
// update our values from the sensor
sensor.update();
upm_lsm303agr.floatVector data = sensor.getAccelerometer();
System.out.println("Accelerometer x: " + data.get(0)
+ " y: " + data.get(1)
+ " z: " + data.get(2)
+ " g");
data = sensor.getMagnetometer();
System.out.println("Magnetometer x: " + data.get(0)
+ " y: " + data.get(1)
+ " z: " + data.get(2)
+ " uT");
System.out.println("Temperature: "
+ sensor.getTemperature());
System.out.println();
Thread.sleep(250);
}
// ! [Interesting]
}
}

View File

@ -0,0 +1,67 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2017 Intel Corporation.
*
* The MIT License
*
* 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 sensorObj = require('jsupm_lsm303agr');
// Instantiate a LSM303AGR instance using default i2c bus and address
var sensor = new sensorObj.LSM303AGR();
// now output data every 250 milliseconds
setInterval(function()
{
// update our values from the sensor
sensor.update();
var data = sensor.getAccelerometer();
console.log("Accelerometer x: "
+ data.get(0)
+ " y: " + data.get(1)
+ " z: " + data.get(2)
+ " g");
data = sensor.getMagnetometer();
console.log("Magnetometer x: "
+ data.get(0)
+ " y: " + data.get(1)
+ " z: " + data.get(2)
+ " uT");
console.log("Temperature: "
+ sensor.getTemperature());
console.log();
}, 250);
// exit on ^C
process.on('SIGINT', function()
{
sensor = null;
sensorObj.cleanUp();
sensorObj = null;
console.log("Exiting.");
process.exit(0);
});

71
examples/python/lsm303agr.py Executable file
View File

@ -0,0 +1,71 @@
#!/usr/bin/python
# Author: Jon Trulson <jtrulson@ics.com>
# Copyright (c) 2017 Intel Corporation.
#
# The MIT License
#
# 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.
from __future__ import print_function
import time, sys, signal, atexit
from upm import pyupm_lsm303agr as sensorObj
def main():
# Instantiate a BMP250E instance using default i2c bus and address
sensor = sensorObj.LSM303AGR()
## Exit handlers ##
# This function stops python from printing a stacktrace when you
# hit control-C
def SIGINTHandler(signum, frame):
raise SystemExit
# This function lets you run code on exit
def exitHandler():
print("Exiting")
sys.exit(0)
# Register exit handlers
atexit.register(exitHandler)
signal.signal(signal.SIGINT, SIGINTHandler)
# now output data every 250 milliseconds
while (1):
sensor.update()
data = sensor.getAccelerometer()
print("Accelerometer x:", data[0], end=' ')
print(" y:", data[1], end=' ')
print(" z:", data[2], end=' ')
print(" g")
data = sensor.getMagnetometer()
print("Magnetometer x:", data[0], end=' ')
print(" y:", data[1], end=' ')
print(" z:", data[2], end=' ')
print(" uT")
print("Temperature: ", sensor.getTemperature())
print()
time.sleep(.250)
if __name__ == '__main__':
main()