KX022 Accelerometer library

Dependencies:   mbed

Dependents:   SP_BEUNBOX_code

Fork of KX022 by Rohm

KXTJ3.h

Committer:
deldering95
Date:
2019-02-05
Revision:
5:9519f53d9965
Parent:
4:4038b02c530d

File content as of revision 5:9519f53d9965:

/* Copyright (c) 2015 ARM Ltd., 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.
*
*
*  KXTJ3 Accelerometer library
*
*  @author  Toyomasa Watarai
*  @version 1.0
*  @date    30-December-2015
*
*  Library for "KXTJ3 Accelerometer library" from Kionix a Rohm group
*    http://www.kionix.com/product/KXTJ3-1020
*
*/

#ifndef KXTJ3_H
#define KXTJ3_H

#include "mbed.h"

#define KXTJ3_DEFAULT_SLAVE_ADDRESS (0x0f << 1)
#define KXTJ3_WAI_VAL         (0x35)

#define KXTJ3_XOUT_L          (0x06)
#define KXTJ3_XOUT_H          (0x07)
#define KXTJ3_YOUT_L          (0x08)
#define KXTJ3_YOUT_H          (0x09)
#define KXTJ3_ZOUT_L          (0x0A)
#define KXTJ3_ZOUT_H          (0x0B)
#define KXTJ3_WHO_AM_I        (0x0F)
#define KXTJ3_CNTL1           (0x1B)
#define KXTJ3_CNTL3           (0x1A)
#define KXTJ3_ODCNTL          (0x21)
#define KXTJ3_TILT_TIMER      (0x22)

#define _DEBUG
#ifdef _DEBUG
extern Serial pc;
#define DEBUG_PRINT(...) pc.printf(__VA_ARGS__)
#else
#define DEBUG_PRINT(...)
#endif

/**
* KXTJ3 accelerometer example
*
* @code
* #include "mbed.h"
* #include "KXTJ3.h"
*
* int main(void) {
*
* KXTJ3 acc(I2C_SDA, I2C_SCL);
* PwmOut rled(LED_RED);
* PwmOut gled(LED_GREEN);
* PwmOut bled(LED_BLUE);
*
*     while (true) {
*         rled = 1.0 - abs(acc.getAccX());
*         gled = 1.0 - abs(acc.getAccY());
*         bled = 1.0 - abs(acc.getAccZ());
*         wait(0.1);
*     }
* }
* @endcode
*/
class KXTJ3
{
public:
    /**
    * KXTJ3 constructor
    *
    * @param sda SDA pin
    * @param sdl SCL pin
    * @param addr slave address of the I2C peripheral (default: 0x3C)
    */
//    KXTJ3(PinName sda, PinName scl, int addr = KXTJ3_DEFAULT_SLAVE_ADDRESS);

    /**
     * Create a KXTJ3 instance which is connected to specified I2C pins
     * with specified address
     *
     * @param i2c_obj I2C object (instance)
     * @param addr slave address of the I2C-bus peripheral (default: 0x3C)
     */
    KXTJ3(I2C* i2c_obj, int addr = KXTJ3_DEFAULT_SLAVE_ADDRESS);

    /**
    * KXTJ3 destructor
    */
    ~KXTJ3();

    /** Initializa KXTJ3 sensor
     *
     *  Configure sensor setting
     *
     */
    void initialize(void);

    /**
     * Get X axis acceleration
     *
     * @returns X axis acceleration
     */
    float getAccX();

    /**
     * Get Y axis acceleration
     *
     * @returns Y axis acceleration
     */
    float getAccY();

    /**
     * Get Z axis acceleration
     *
     * @returns Z axis acceleration
     */
    float getAccZ();

    /**
     * Get XYZ axis acceleration
     *
     * @param res array where acceleration data will be stored
     */
    void getAccAllAxis(float * res);

private:
    I2C* m_i2c;
    int m_addr;
    void readRegs(int addr, uint8_t * data, int len);
    void writeRegs(uint8_t * data, int len);
    int16_t getAccAxis(uint8_t addr);

};

#endif