2 * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 MMA7455::MMA7455 (int bus, int devAddr) {
37 unsigned char data = 0;
42 m_controlAddr = devAddr;
45 m_i2ControlCtx = mraa_i2c_init(m_bus);
47 mraa_result_t error = mraa_i2c_address(m_i2ControlCtx, m_controlAddr);
48 if (error != MRAA_SUCCESS) {
49 fprintf(stderr, "Messed up i2c bus\n");
53 // setting GLVL 0x1 (64LSB/g) and MODE 0x1 (Measurement Mode)
54 data = (BIT (MMA7455_GLVL0) | BIT (MMA7455_MODE0));
55 error = ic2WriteReg (MMA7455_MCTL, &data, 0x1);
56 if (error != MRAA_SUCCESS) {
57 std::cout << "ERROR :: MMA7455 instance wan not created (Mode)" << std::endl;
61 if (MRAA_SUCCESS != calibrate ()) {
62 std::cout << "ERROR :: MMA7455 instance wan not created (Calibrate)" << std::endl;
68 mraa_i2c_stop(m_i2ControlCtx);
72 MMA7455::calibrate () {
73 mraa_result_t error = MRAA_SUCCESS;
77 xyz.value.x = xyz.value.y = xyz.value.z = 0;
80 error = readData (&xyz.value.x, &xyz.value.y, &xyz.value.z);
81 if (MRAA_SUCCESS != error) {
85 xyz.value.x += 2 * -xyz.value.x;
86 xyz.value.y += 2 * -xyz.value.y;
87 xyz.value.z += 2 * -(xyz.value.z - 64);
89 error = ic2WriteReg (MMA7455_XOFFL, (unsigned char *) &xyz, 0x6);
90 if (error != MRAA_SUCCESS) {
100 MMA7455::readData (short * ptrX, short * ptrY, short * ptrZ) {
102 unsigned char data = 0;
106 nBytes = ic2ReadReg (MMA7455_STATUS, &data, 0x1);
107 } while ( !(data & MMA7455_DRDY) && nBytes == MRAA_SUCCESS);
109 if (nBytes == MRAA_SUCCESS) {
110 std::cout << "NO_GDB :: 1" << std::endl;
114 nBytes = ic2ReadReg (MMA7455_XOUTL, (unsigned char *) &xyz, 0x6);
116 std::cout << "NO_GDB :: 2" << std::endl;
117 return MRAA_ERROR_UNSPECIFIED;
120 if (xyz.reg.x_msb & 0x02) {
121 xyz.reg.x_msb |= 0xFC;
124 if (xyz.reg.y_msb & 0x02) {
125 xyz.reg.y_msb |= 0xFC;
128 if (xyz.reg.z_msb & 0x02) {
129 xyz.reg.z_msb |= 0xFC;
132 // The result is the g-force in units of 64 per 'g'.
141 MMA7455::ic2ReadReg (unsigned char reg, unsigned char * buf, unsigned char size) {
142 if (MRAA_SUCCESS != mraa_i2c_address(m_i2ControlCtx, m_controlAddr)) {
146 if (MRAA_SUCCESS != mraa_i2c_write_byte(m_i2ControlCtx, reg)) {
150 if (MRAA_SUCCESS != mraa_i2c_address(m_i2ControlCtx, m_controlAddr)) {
154 return (int) mraa_i2c_read(m_i2ControlCtx, buf, size);
158 MMA7455::ic2WriteReg (unsigned char reg, unsigned char * buf, unsigned char size) {
159 mraa_result_t error = MRAA_SUCCESS;
161 uint8_t data[size + 1];
163 memcpy(&data[1], buf, size);
165 error = mraa_i2c_address (m_i2ControlCtx, m_controlAddr);
166 if (error != MRAA_SUCCESS) {
169 error = mraa_i2c_write (m_i2ControlCtx, data, size + 1);
170 if (error != MRAA_SUCCESS) {