[175] | 1 | /*
|
---|
| 2 | ****************************************************************************
|
---|
| 3 | * This file is part of Arduino Lucky Shield Library
|
---|
| 4 | *
|
---|
| 5 | * Copyright (c) 2016 Arduino srl. All right reserved.
|
---|
| 6 | *
|
---|
| 7 | * File : MMA8491Q.cpp
|
---|
| 8 | * Date : 2016/03/21
|
---|
| 9 | * Revision : 0.0.1 $
|
---|
| 10 | * Author: andrea[at]arduino[dot]org
|
---|
| 11 | *
|
---|
| 12 | ****************************************************************************
|
---|
| 13 |
|
---|
| 14 | This library is free software; you can redistribute it and/or
|
---|
| 15 | modify it under the terms of the GNU Lesser General Public
|
---|
| 16 | License as published by the Free Software Foundation; either
|
---|
| 17 | version 2.1 of the License, or (at your option) any later version.
|
---|
| 18 |
|
---|
| 19 | This library is distributed in the hope that it will be useful,
|
---|
| 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 22 | Lesser General Public License for more details.
|
---|
| 23 |
|
---|
| 24 | You should have received a copy of the GNU Lesser General Public
|
---|
| 25 | License along with this library; if not, write to the Free Software
|
---|
| 26 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
| 27 |
|
---|
| 28 | ******************************************************************************/
|
---|
| 29 |
|
---|
| 30 | #include "MMA8491Q.h"
|
---|
| 31 | #include <Wire.h>
|
---|
| 32 |
|
---|
[224] | 33 | #ifdef __SAM3X8E__
|
---|
| 34 | #define Wire Wire1
|
---|
| 35 | #endif
|
---|
| 36 |
|
---|
[175] | 37 | // CONSTRUCTUR
|
---|
| 38 | MMA8491Q::MMA8491Q(uint8_t addr)
|
---|
| 39 | {
|
---|
| 40 | address = addr; // Store address into private variable
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | void MMA8491Q::read(){
|
---|
| 44 | readRegister(STATE);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | void MMA8491Q::enable(){
|
---|
| 48 |
|
---|
| 49 | Wire.beginTransmission(GPIO_ADDRESS); //read the gpio expander value
|
---|
| 50 | Wire.write(GPIO_READ);
|
---|
| 51 | Wire.endTransmission();
|
---|
| 52 | Wire.requestFrom(GPIO_ADDRESS,(byte)1);
|
---|
| 53 | if(Wire.available()) {
|
---|
| 54 | data_read = Wire.read();
|
---|
| 55 | }
|
---|
| 56 | data_read = data_read | 0x01; //enable
|
---|
| 57 | Wire.beginTransmission(GPIO_ADDRESS);
|
---|
| 58 | Wire.write(GPIO_WRITE_PORT0);
|
---|
| 59 | Wire.write(data_read);
|
---|
| 60 | Wire.endTransmission();
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | void MMA8491Q::disable(){
|
---|
| 64 |
|
---|
| 65 | Wire.beginTransmission(GPIO_ADDRESS);
|
---|
| 66 | Wire.write(GPIO_WRITE_PORT0);
|
---|
| 67 | Wire.write(data_read & 0xFE); //disable
|
---|
| 68 | Wire.endTransmission();
|
---|
| 69 |
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | float MMA8491Q::value(int val){
|
---|
| 73 |
|
---|
| 74 | if (val < 0x2000)
|
---|
| 75 | return float(val)/1024; // read value is between 0 and 8191 (0g 8g)
|
---|
| 76 | else
|
---|
| 77 | return float((0x1FFF & val) - 0x2000)/1024;
|
---|
| 78 |
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | void MMA8491Q::readRegister(int reg)
|
---|
| 82 | {
|
---|
| 83 | enable();
|
---|
| 84 | delay(5);
|
---|
| 85 | Wire.beginTransmission(address);
|
---|
| 86 | Wire.write(reg);
|
---|
| 87 | Wire.endTransmission();
|
---|
| 88 | Wire.requestFrom(address, (byte)7);
|
---|
| 89 | state = Wire.read();
|
---|
| 90 | cx = Wire.read(); cx <<= 8; cx |= Wire.read(); cx >>= 2;
|
---|
| 91 | cy = Wire.read(); cy <<= 8; cy |= Wire.read(); cy >>= 2;
|
---|
| 92 | cz = Wire.read(); cz <<= 8; cz |= Wire.read(); cz >>= 2;
|
---|
| 93 | x_ = value(cx);
|
---|
| 94 | y_ = value(cy);
|
---|
| 95 | z_ = value(cz);
|
---|
| 96 | disable();
|
---|
| 97 |
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | MMA8491Q mma8491q;
|
---|