KX022 Accelerometer library

Dependencies:   mbed

Dependents:   SP_BEUNBOX_code

Fork of KX022 by Rohm

Files at this revision

API Documentation at this revision

Comitter:
MACRUM
Date:
Wed Dec 30 16:16:32 2015 +0000
Child:
1:23bfc18affd9
Commit message:
Initial commit

Changed in this revision

KX022.cpp Show annotated file Show diff for this revision Revisions of this file
KX022.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/KX022.cpp	Wed Dec 30 16:16:32 2015 +0000
@@ -0,0 +1,121 @@
+/* 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.
+*
+*
+*  KX022 Accelerometer library
+*
+*  @author  Toyomasa Watarai
+*  @version 1.0
+*  @date    30-December-2015
+*
+*  Library for "KX022 Accelerometer library" from Kionix a Rohm group
+*    http://www.kionix.com/product/KX022-1020
+*
+*/
+
+#include "mbed.h"
+#include "KX022.h"
+
+KX022::KX022(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr)
+{
+    initialize();
+}
+
+KX022::KX022(I2C &i2c_obj, int addr) : m_i2c(i2c_obj), m_addr(addr)
+{
+    initialize();
+}
+
+KX022::~KX022()
+{
+}
+
+void KX022::initialize()
+{
+    unsigned char buf;
+    unsigned char reg[2];
+
+    readRegs(KX022_WHO_AM_I, &buf, sizeof(buf));
+    if (buf != KX022_WAI_VAL) {
+        DEBUG_PRINT("KX022 initialization error. (%d)\n", buf);
+        return;
+    }
+
+    reg[0] = KX022_CNTL1;
+    reg[1] = 0x41;
+    writeRegs(reg, 2);
+
+    reg[0] = KX022_ODCNTL;
+    reg[1] = 0x02;
+    writeRegs(reg, 2);
+
+    reg[0] = KX022_CNTL3;
+    reg[1] = 0xD8;
+    writeRegs(reg, 2);
+
+    reg[0] = KX022_TILT_TIMER;
+    reg[1] = 0x01;
+    writeRegs(reg, 2);
+
+    reg[0] = KX022_CNTL1;
+    reg[1] = 0xC1;
+    writeRegs(reg, 2);
+}
+    
+float KX022::getAccX()
+{
+    return (float(getAccAxis(KX022_XOUT_L))/16384);
+}
+
+float KX022::getAccY()
+{
+    return (float(getAccAxis(KX022_YOUT_L))/16384);
+}
+
+float KX022::getAccZ()
+{
+    return (float(getAccAxis(KX022_ZOUT_L))/16384);
+}
+
+void KX022::getAccAllAxis(float * res)
+{
+    res[0] = getAccX();
+    res[1] = getAccY();
+    res[2] = getAccZ();
+}
+
+int16_t KX022::getAccAxis(uint8_t addr)
+{
+    int16_t acc;
+    uint8_t res[2];
+
+    readRegs(addr, res, 2);
+    acc = ((res[1] << 8) | res[0]);
+    return acc;
+}
+
+void KX022::readRegs(int addr, uint8_t * data, int len)
+{
+    char t[1] = {addr};
+    m_i2c.write(m_addr, t, 1, true);
+    m_i2c.read(m_addr, (char *)data, len);
+}
+
+void KX022::writeRegs(uint8_t * data, int len)
+{
+    m_i2c.write(m_addr, (char *)data, len);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/KX022.h	Wed Dec 30 16:16:32 2015 +0000
@@ -0,0 +1,150 @@
+/* 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.
+*
+*
+*  KX022 Accelerometer library
+*
+*  @author  Toyomasa Watarai
+*  @version 1.0
+*  @date    30-December-2015
+*
+*  Library for "KX022 Accelerometer library" from Kionix a Rohm group
+*    http://www.kionix.com/product/KX022-1020
+*
+*/
+
+#ifndef KX022_H
+#define KX022_H
+
+#include "mbed.h"
+
+#define DEFAULT_SLAVE_ADDRESS (0x1E << 1)
+#define KX022_WAI_VAL         (0x14)
+
+#define KX022_XOUT_L          (0x06)
+#define KX022_XOUT_H          (0x07)
+#define KX022_YOUT_L          (0x08)
+#define KX022_YOUT_H          (0x09)
+#define KX022_ZOUT_L          (0x0A)
+#define KX022_ZOUT_H          (0x0B)
+#define KX022_WHO_AM_I        (0x0F)
+#define KX022_CNTL1           (0x18)
+#define KX022_CNTL3           (0x1A)
+#define KX022_ODCNTL          (0x1B)
+#define KX022_TILT_TIMER      (0x22)
+
+#ifdef _DEBUG
+extern Serial pc;
+#define DEBUG_PRINT(...) pc.printf(__VA_ARGS__)
+#else
+#define DEBUG_PRINT(...)
+#endif
+
+/**
+* KX022 accelerometer example
+*
+* @code
+* #include "mbed.h"
+* #include "KX022.h"
+*
+* int main(void) {
+*
+* KX022 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 KX022
+{
+public:
+    /**
+    * KX022 constructor
+    *
+    * @param sda SDA pin
+    * @param sdl SCL pin
+    * @param addr slave address of the I2C peripheral (default: 0x3C)
+    */
+    KX022(PinName sda, PinName scl, int addr = DEFAULT_SLAVE_ADDRESS);
+
+    /**
+     * Create a KX022 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)
+     */
+    KX022(I2C &i2c_obj, int addr = DEFAULT_SLAVE_ADDRESS);
+
+    /**
+    * KX022 destructor
+    */
+    ~KX022();
+
+    /** Initializa KX022 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