source: rtos_arduino/trunk/arduino_lib/libraries/LuckyShield/src/lib/MMA8491Q.cpp@ 175

Last change on this file since 175 was 175, checked in by ertl-honda, 8 years ago

ライブラリを Arduino IDE 1.7.9 にupdate

File size: 2.5 KB
Line 
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
33// CONSTRUCTUR
34MMA8491Q::MMA8491Q(uint8_t addr)
35{
36 address = addr; // Store address into private variable
37}
38
39void MMA8491Q::read(){
40 readRegister(STATE);
41}
42
43void MMA8491Q::enable(){
44
45 Wire.beginTransmission(GPIO_ADDRESS); //read the gpio expander value
46 Wire.write(GPIO_READ);
47 Wire.endTransmission();
48 Wire.requestFrom(GPIO_ADDRESS,(byte)1);
49 if(Wire.available()) {
50 data_read = Wire.read();
51 }
52 data_read = data_read | 0x01; //enable
53 Wire.beginTransmission(GPIO_ADDRESS);
54 Wire.write(GPIO_WRITE_PORT0);
55 Wire.write(data_read);
56 Wire.endTransmission();
57}
58
59void MMA8491Q::disable(){
60
61 Wire.beginTransmission(GPIO_ADDRESS);
62 Wire.write(GPIO_WRITE_PORT0);
63 Wire.write(data_read & 0xFE); //disable
64 Wire.endTransmission();
65
66}
67
68float MMA8491Q::value(int val){
69
70 if (val < 0x2000)
71 return float(val)/1024; // read value is between 0 and 8191 (0g 8g)
72 else
73 return float((0x1FFF & val) - 0x2000)/1024;
74
75}
76
77void MMA8491Q::readRegister(int reg)
78{
79 enable();
80 delay(5);
81 Wire.beginTransmission(address);
82 Wire.write(reg);
83 Wire.endTransmission();
84 Wire.requestFrom(address, (byte)7);
85 state = Wire.read();
86 cx = Wire.read(); cx <<= 8; cx |= Wire.read(); cx >>= 2;
87 cy = Wire.read(); cy <<= 8; cy |= Wire.read(); cy >>= 2;
88 cz = Wire.read(); cz <<= 8; cz |= Wire.read(); cz >>= 2;
89 x_ = value(cx);
90 y_ = value(cy);
91 z_ = value(cz);
92 disable();
93
94}
95
96MMA8491Q mma8491q;
Note: See TracBrowser for help on using the repository browser.