Skip to content

Commit

Permalink
Added a Makefile to make building less ambiguous.
Browse files Browse the repository at this point in the history
Added an example program to demonstrate library usage.
Added additional information to the README to provide instructions on building and usage.
  • Loading branch information
swedishborgie committed Mar 8, 2018
1 parent 9c9c1bf commit 59e9e40
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 1 deletion.
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
CC=gcc
CFLAGS=-fPIC
CFLAGS_SHARED=-shared
OBJ=mma8451.o
LIBNAME=libmma8451.so
TESTOBJ=mma8451-test.o
TESTNAME=mma8451-test

all: compile

compile: $(LIBNAME) $(TESTNAME)

install: $(LIBNAME)
cp $(LIBNAME) /usr/lib/$(LIBNAME)
cp $(TESTNAME) /usr/bin/$(TESTNAME)

fix-i2c:
echo -n 1 > /sys/module/i2c_bcm2708/parameters/combined

clean:
rm -f $(OBJ) $(TESTOBJ) $(LIBNAME) $(TESTNAME)

%.o: %.c
$(CC) -c -o $@ $< $(CFLAGS)

$(LIBNAME): $(OBJ)
gcc -o $@ $^ $(CFLAGS) $(CFLAGS_SHARED)

$(TESTNAME): $(LIBNAME) $(TESTOBJ)
gcc -o $@ $^ $(CFLAGS) -L. -lmma8451
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
# libmma8451
A small C library for interacting with a MMA8451 accelerometer using libi2c.
A small C library for interacting with a [MMA8451 accelerometer](https://www.adafruit.com/product/2019)
using libi2c.

Built from the [reference documentation](https://www.nxp.com/docs/en/data-sheet/MMA8451Q.pdf)
provided by NXP.

The software was built originally for use with the Raspberry Pi but should work with
any Linux system with the I2C kernel module and libi2c installed. The library supports working
with multiple accelerometers at once.

To build and install the library and test application:

$ make
$ sudo make install

To run the test application:

$ mma8451-test /dev/i2c-1 0x1c
Attempting to open /dev/i2c-1 and talk to I2C device at 0x1c
Successfully opened device, initializing...
Successfully initialized, starting capture. (Press Ctrl-C to stop)
x=-0.612916, y=-9.040505, z=3.524265, samplesPerSecond=1477.832512

On older versions of Raspbian the i2c_bcm2708 kernel module needed to be set to combined mode.
This can be done by doing the following:

$ sudo sh -c "echo -n 1 > /sys/module/i2c_bcm2708/parameters/combined"
136 changes: 136 additions & 0 deletions mma8451-test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* libmma8451 - Library for controlling and reading from MMA8451 accelerometers.
* Copyright (C) 2017 Michael Powers
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

/*
* This is a simple example of how to open and read from an
* accelerometer using the libmma8451 library.
*/
#include "mma8451.h"
#include <stdio.h>
#include <time.h>

/**
* Prints the usage statement for this application.
*/
void printUsage() {
printf("Usage: mma8451-test [device path] [i2c address]\n");
printf(" e.g. mma8451 /dev/i2c-1 0x1c\n\n");
}

/**
* Initializes the accelerometer and gets it ready to capture.
* @param dev Device to initialize.
* @return 1 on success, 0 on failure.
*/
int initializeDevice(mma8451* dev) {
//Reset the device back to defaults.
mma8451_reset(dev);

//Wait one second, it takes the device a short amount of time
//to come back.
sleep(1);

//Set the bit width of the acceleration values.
if(!mma8451_set_output_size(dev, MMA8451_8BIT_OUTPUT)) return 0;
//Set the noise mode.
if(!mma8451_set_low_noise(dev, 1)) return 0;
//Set the power mode.
if(!mma8451_set_power_mode(dev, MMA8451_POWER_MODE_HIGH_RES)) return 0;
//Turn on orientation detection.
if(!mma8451_set_orientation_detection(dev, 1)) return 0;
//Set the accuracy range.
if(!mma8451_set_range(dev, MMA8451_RANGE_2G)) return 0;
//Enable interrupts.
if(!mma8451_set_interrupt_enable(dev, 1)) return 0;
//Set interrupts for pin1 on the accelerometer.
if(!mma8451_set_interrupt_pin1(dev, 1)) return 0;
//Start capturing.
if(!mma8451_set_active(dev, 1)) return 0;

return 1;
}

/**
* Main function, starts the program and attempts to open the accelerometer
* and read from it.
*/
int main(int argc, char** argv) {
struct timespec start;
struct timespec sample;
unsigned int samples = 0;
char* path;
unsigned char address;

//Make sure we have the right number of command line arguments.
if(argc != 3) {
printUsage();
return -1;
}

//Parse the arguments.
path = argv[1];
address = (unsigned char)strtol(argv[2], NULL, 0);

printf("Attempting to open %s and talk to I2C device at 0x%02x\n", path, address);
mma8451* dev = mma8451_open(path, address);
if(dev == NULL) {
perror("Unable to open device.");
return -1;
}

printf("Successfully opened device, initializing...\n");
if(!initializeDevice(dev)) {
perror("Unable to initialize device.");
return -2;
}

printf("Successfully initialized, starting capture. (Press Ctrl-C to stop)\n");
clock_gettime(CLOCK_REALTIME, &start);

printf("\n");
while(1) {
mma8451_acceleration data;
long long duration;
double samplesPerSecond;

//Actually read the acceleration data from the accelerometer.
if(!mma8451_get_acceleration(dev, &data)) {
return 0;
}

clock_gettime(CLOCK_REALTIME, &sample);
samples++;

//Calculate the samples per second.
duration = ((sample.tv_sec - start.tv_sec) * 1000);
duration += (sample.tv_nsec - start.tv_nsec) / 1.0e6;
samplesPerSecond = (duration > 0) ? samples / ((double)duration / 1000) : 0;

//Every 100th sample, print a message showing the latest measurements.
if(samples % 100 == 0) {
//Move up a line and clear to the beginning of the line.
printf("%c[1A%c[K", 0x1b, 0x1b);
//Print the sample.
printf("x=%f, y=%f, z=%f, samplesPerSecond=%f\n", data.x, data.y, data.z, samplesPerSecond);
}
}

mma8451_close(dev);
return 0;
}

0 comments on commit 59e9e40

Please sign in to comment.