Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Websocket_Ethernet_acc app-board-Sprint-WS-Acc app-board-Ethernet-Websocket app-board-Wifly-Websocket ... more
Revision 2:a8e20db7901e, committed 2012-10-17
- Comitter:
- Sissors
- Date:
- Wed Oct 17 16:38:05 2012 +0000
- Parent:
- 1:8997a1b348dd
- Child:
- 3:89cb08cc663b
- Commit message:
- Version 1.0
Changed in this revision
| MMA7660.cpp | Show annotated file Show diff for this revision Revisions of this file |
| MMA7660.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/MMA7660.cpp Tue Oct 16 19:42:19 2012 +0000
+++ b/MMA7660.cpp Wed Oct 17 16:38:05 2012 +0000
@@ -1,9 +1,8 @@
#include "MMA7660.h"
-MMA7660::MMA7660(PinName sda, PinName scl, PinName interrupt) : _i2c(sda, scl)
+MMA7660::MMA7660(PinName sda, PinName scl, bool active) : _i2c(sda, scl)
{
- _interrupt = interrupt;
- active = false;
+ setActive(active);
samplerate = 64;
}
@@ -32,7 +31,6 @@
write(MMA7660_MODE_R, modereg);
}
-//Add timeout!
void MMA7660::readData(int *data)
{
if (!active) {
@@ -55,6 +53,9 @@
data[i] = (signed char)temp[i];
}
} while (alert);
+
+ if (!active)
+ setActive(false);
}
@@ -66,17 +67,17 @@
data[i] = intdata[i]/MMA7660_SENSITIVITY;
}
-float MMA7660::getX( void )
+float MMA7660::x( void )
{
return getSingle(0);
}
-float MMA7660::getY( void )
+float MMA7660::y( void )
{
return getSingle(1);
}
-float MMA7660::getZ( void )
+float MMA7660::z( void )
{
return getSingle(2);
}
@@ -97,36 +98,13 @@
//Update the samplerate reg
temp = read(MMA7660_SR_R);
- temp &= ~0x07; //Awake sample rate are lowest 3 bit
+ temp &= ~0x07; //Awake sample rate are lowest 3 bit
temp |= sampleLoc;
write(MMA7660_SR_R, temp);
this->samplerate = rates[sampleLoc];
setActive(active); //Restore previous active state
}
-MMA7660::Orientation MMA7660::getGlobalOrientation( void )
-{
- int retval = MMA7660::Unknown;
-
- int accelerations[3];
- readData(accelerations);
-
- //Check which side is up
- int max = 0;
- for (int i = 0; i<3; i++) {
- if (-accelerations[i] > max) {
- max = -accelerations[i];
- retval = 2 * i;
- }
- if (accelerations[i] > max) {
- max = accelerations[i];
- retval = 2 * i + 1;
- }
- }
- return (MMA7660::Orientation)retval;
-
-
-}
MMA7660::Orientation MMA7660::getSide( void )
{
@@ -159,17 +137,7 @@
return MMA7660::Unknown;
}
-bool MMA7660::isTapped( void )
-{
- char tiltreg = read(MMA7660_TILT_R);
-
- //Tap is bit 5
- tiltreg >>= 5;
- tiltreg &= 0x01;
-
- return tiltreg==1;
-}
-
+
//////////////////////////////////////////////
///////////////PRIVATE////////////////////////
@@ -203,7 +171,6 @@
{
if (!active) {
setActive(true);
- active = true;
wait(0.012 + 1/samplerate); //Wait until new sample is ready
}
@@ -217,9 +184,10 @@
alert = true;
if (temp > 31)
temp += 128+64;
- }
+ } while (alert);
- while (alert);
+ if (!active)
+ setActive(false);
return temp / MMA7660_SENSITIVITY;
}
\ No newline at end of file
--- a/MMA7660.h Tue Oct 16 19:42:19 2012 +0000
+++ b/MMA7660.h Wed Oct 17 16:38:05 2012 +0000
@@ -1,3 +1,21 @@
+/* Copyright (c) <year> <copyright holders>, 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 "mbed.h"
@@ -16,30 +34,57 @@
#define MMA7660_SR_R 0x08
+/** An interface for the MMA7660 triple axis accelerometer
+ *
+ * @code
+ * //Uses the measured z-acceleration to drive leds 2 and 3 of the mbed
+ *
+ * #include "mbed.h"
+ * #include "MMA7660.h"
+ *
+ * MMA7660 MMA(p28, p27);
+ *
+ * DigitalOut connectionLed(LED1);
+ * PwmOut Zaxis_p(LED2);
+ * PwmOut Zaxis_n(LED3);
+ *
+ * int main() {
+ * if (MMA.testConnection())
+ * connectionLed = 1;
+ *
+ * while(1) {
+ * Zaxis_p = MMA.z();
+ * Zaxis_n = -MMA.z();
+ * }
+ *
+ * }
+ * @endcode
+ */
class MMA7660
{
public:
- /**
+ /**
* The 6 different orientations and unknown
*
* Up & Down = X-axis
* Right & Left = Y-axis
* Back & Front = Z-axis
- *
+ *
*/
- enum Orientation{Up, Down,
- Right, Left,
- Back, Front,
- Unknown};
-
+ enum Orientation {Up, Down,
+ Right, Left,
+ Back, Front,
+ Unknown
+ };
+
/**
* Creates a new MMA7660 object
*
* @param sda - I2C data pin
* @param scl - I2C clock pin
- * @param interrupt - Interrupt pin (default = NC)
+ * @param active - true (default) to enable the device, false to keep it standby
*/
- MMA7660(PinName sda, PinName scl, PinName interrupt = NC);
+ MMA7660(PinName sda, PinName scl, bool active = true);
/**
* Tests if communication is possible with the MMA7660
@@ -50,7 +95,7 @@
* @param return - true for successfull connection, false for no connection
*/
bool testConnection( void );
-
+
/**
* Sets the active state of the MMA7660
*
@@ -59,40 +104,40 @@
* @param state - true for active, false for standby
*/
void setActive( bool state);
-
+
/**
* Reads acceleration data from the sensor
*
- * When the parameter is a pointer to an integer array it will be the raw data.
+ * When the parameter is a pointer to an integer array it will be the raw data.
* When it is a pointer to a float array it will be the acceleration in g's
- *
- * @param data - pointer to array with length 3 where the acceleration data will be stores, X-Y-Z
+ *
+ * @param data - pointer to array with length 3 where the acceleration data will be stored, X-Y-Z
*/
void readData( int *data);
void readData( float *data);
-
- /**
+
+ /**
* Get X-data
*
* @param return - X-acceleration in g's
*/
- float getX( void );
-
- /**
+ float x( void );
+
+ /**
* Get Y-data
*
* @param return - Y-acceleration in g's
*/
- float getY( void );
-
- /**
+ float y( void );
+
+ /**
* Get Z-data
*
* @param return - Z-acceleration in g's
*/
- float getZ( void );
-
- /**
+ float z( void );
+
+ /**
* Sets the active samplerate
*
* The entered samplerate will be rounded to nearest supported samplerate.
@@ -100,16 +145,9 @@
*
* @param samplerate - the samplerate that will be set
*/
- void setSampleRate(int samplerate);
+ void setSampleRate(int samplerate);
/**
- * Returns which side is pointing down
- *
- * @param return - Orientation which is closest to down
- */
- Orientation getGlobalOrientation( void );
-
- /**
* Returns if it is on its front, back, or unknown side
*
* This is read from MMA7760s registers, page 12 of datasheet
@@ -117,7 +155,7 @@
* @param return - Front, Back or Unknown orientation
*/
Orientation getSide( void );
-
+
/**
* Returns if it is on it left, right, down or up side
*
@@ -126,14 +164,6 @@
* @param return - Left, Right, Down, Up or Unknown orientation
*/
Orientation getOrientation ( void );
-
- /**
- * Returns if since last check there has been a tap
- *
- * @param return - bool that is true when a tap has been detected
- */
- bool isTapped( void );
-
private:
@@ -153,24 +183,23 @@
* @return - data from the register specified by RA
*/
char read( char adress);
-
+
/**
- * Read multiple regigsters from the device, more efficient than using multiple normal reads.
+ * Read multiple regigsters from the device, more efficient than using multiple normal reads.
*
* @param adress - register address to write to
* @param length - number of bytes to read
- * @param data - pointer where the data needs to be written to
+ * @param data - pointer where the data needs to be written to
*/
- void read( char adress, char *data, int length);
-
- /**
- * Reads single axis
- */
- float getSingle(int number);
+ void read( char adress, char *data, int length);
+
+ /**
+ * Reads single axis
+ */
+ float getSingle(int number);
I2C _i2c;
bool active;
- PinName _interrupt;
float samplerate;
};
MMA7660