KX022 Accelerometer library

Dependents:   LazuriteGraph_Hello KX022_Hello GR-PEACH_IoT_Platform_HTTP_sample

Committer:
MikkoZ
Date:
Wed Sep 14 09:16:41 2016 +0000
Revision:
3:e2d1659fe67e
Parent:
1:23bfc18affd9
Modified init routine to work with other WAI values too. Init tries to setup sensor anyway in hope to find regmap compatible sensor.; Turned on _DEBUG by default.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 0:e642dd732c4b 1 /* Copyright (c) 2015 ARM Ltd., MIT License
MACRUM 0:e642dd732c4b 2 *
MACRUM 0:e642dd732c4b 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
MACRUM 0:e642dd732c4b 4 * and associated documentation files (the "Software"), to deal in the Software without
MACRUM 0:e642dd732c4b 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
MACRUM 0:e642dd732c4b 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
MACRUM 0:e642dd732c4b 7 * Software is furnished to do so, subject to the following conditions:
MACRUM 0:e642dd732c4b 8 *
MACRUM 0:e642dd732c4b 9 * The above copyright notice and this permission notice shall be included in all copies or
MACRUM 0:e642dd732c4b 10 * substantial portions of the Software.
MACRUM 0:e642dd732c4b 11 *
MACRUM 0:e642dd732c4b 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
MACRUM 0:e642dd732c4b 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
MACRUM 0:e642dd732c4b 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
MACRUM 0:e642dd732c4b 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
MACRUM 0:e642dd732c4b 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
MACRUM 0:e642dd732c4b 17 *
MACRUM 0:e642dd732c4b 18 *
MACRUM 0:e642dd732c4b 19 * KX022 Accelerometer library
MACRUM 0:e642dd732c4b 20 *
MACRUM 0:e642dd732c4b 21 * @author Toyomasa Watarai
MACRUM 0:e642dd732c4b 22 * @version 1.0
MACRUM 0:e642dd732c4b 23 * @date 30-December-2015
MACRUM 0:e642dd732c4b 24 *
MACRUM 0:e642dd732c4b 25 * Library for "KX022 Accelerometer library" from Kionix a Rohm group
MACRUM 0:e642dd732c4b 26 * http://www.kionix.com/product/KX022-1020
MACRUM 0:e642dd732c4b 27 *
MACRUM 0:e642dd732c4b 28 */
MACRUM 0:e642dd732c4b 29
MACRUM 0:e642dd732c4b 30 #ifndef KX022_H
MACRUM 0:e642dd732c4b 31 #define KX022_H
MACRUM 0:e642dd732c4b 32
MACRUM 0:e642dd732c4b 33 #include "mbed.h"
MACRUM 0:e642dd732c4b 34
MACRUM 1:23bfc18affd9 35 #define KX022_DEFAULT_SLAVE_ADDRESS (0x1E << 1)
MACRUM 0:e642dd732c4b 36 #define KX022_WAI_VAL (0x14)
MACRUM 0:e642dd732c4b 37
MACRUM 0:e642dd732c4b 38 #define KX022_XOUT_L (0x06)
MACRUM 0:e642dd732c4b 39 #define KX022_XOUT_H (0x07)
MACRUM 0:e642dd732c4b 40 #define KX022_YOUT_L (0x08)
MACRUM 0:e642dd732c4b 41 #define KX022_YOUT_H (0x09)
MACRUM 0:e642dd732c4b 42 #define KX022_ZOUT_L (0x0A)
MACRUM 0:e642dd732c4b 43 #define KX022_ZOUT_H (0x0B)
MACRUM 0:e642dd732c4b 44 #define KX022_WHO_AM_I (0x0F)
MACRUM 0:e642dd732c4b 45 #define KX022_CNTL1 (0x18)
MACRUM 0:e642dd732c4b 46 #define KX022_CNTL3 (0x1A)
MACRUM 0:e642dd732c4b 47 #define KX022_ODCNTL (0x1B)
MACRUM 0:e642dd732c4b 48 #define KX022_TILT_TIMER (0x22)
MACRUM 0:e642dd732c4b 49
MikkoZ 3:e2d1659fe67e 50 #define _DEBUG
MACRUM 0:e642dd732c4b 51 #ifdef _DEBUG
MACRUM 0:e642dd732c4b 52 extern Serial pc;
MACRUM 0:e642dd732c4b 53 #define DEBUG_PRINT(...) pc.printf(__VA_ARGS__)
MACRUM 0:e642dd732c4b 54 #else
MACRUM 0:e642dd732c4b 55 #define DEBUG_PRINT(...)
MACRUM 0:e642dd732c4b 56 #endif
MACRUM 0:e642dd732c4b 57
MACRUM 0:e642dd732c4b 58 /**
MACRUM 0:e642dd732c4b 59 * KX022 accelerometer example
MACRUM 0:e642dd732c4b 60 *
MACRUM 0:e642dd732c4b 61 * @code
MACRUM 0:e642dd732c4b 62 * #include "mbed.h"
MACRUM 0:e642dd732c4b 63 * #include "KX022.h"
MACRUM 0:e642dd732c4b 64 *
MACRUM 0:e642dd732c4b 65 * int main(void) {
MACRUM 0:e642dd732c4b 66 *
MACRUM 0:e642dd732c4b 67 * KX022 acc(I2C_SDA, I2C_SCL);
MACRUM 0:e642dd732c4b 68 * PwmOut rled(LED_RED);
MACRUM 0:e642dd732c4b 69 * PwmOut gled(LED_GREEN);
MACRUM 0:e642dd732c4b 70 * PwmOut bled(LED_BLUE);
MACRUM 0:e642dd732c4b 71 *
MACRUM 0:e642dd732c4b 72 * while (true) {
MACRUM 0:e642dd732c4b 73 * rled = 1.0 - abs(acc.getAccX());
MACRUM 0:e642dd732c4b 74 * gled = 1.0 - abs(acc.getAccY());
MACRUM 0:e642dd732c4b 75 * bled = 1.0 - abs(acc.getAccZ());
MACRUM 0:e642dd732c4b 76 * wait(0.1);
MACRUM 0:e642dd732c4b 77 * }
MACRUM 0:e642dd732c4b 78 * }
MACRUM 0:e642dd732c4b 79 * @endcode
MACRUM 0:e642dd732c4b 80 */
MACRUM 0:e642dd732c4b 81 class KX022
MACRUM 0:e642dd732c4b 82 {
MACRUM 0:e642dd732c4b 83 public:
MACRUM 0:e642dd732c4b 84 /**
MACRUM 0:e642dd732c4b 85 * KX022 constructor
MACRUM 0:e642dd732c4b 86 *
MACRUM 0:e642dd732c4b 87 * @param sda SDA pin
MACRUM 0:e642dd732c4b 88 * @param sdl SCL pin
MACRUM 0:e642dd732c4b 89 * @param addr slave address of the I2C peripheral (default: 0x3C)
MACRUM 0:e642dd732c4b 90 */
MACRUM 1:23bfc18affd9 91 KX022(PinName sda, PinName scl, int addr = KX022_DEFAULT_SLAVE_ADDRESS);
MACRUM 0:e642dd732c4b 92
MACRUM 0:e642dd732c4b 93 /**
MACRUM 0:e642dd732c4b 94 * Create a KX022 instance which is connected to specified I2C pins
MACRUM 0:e642dd732c4b 95 * with specified address
MACRUM 0:e642dd732c4b 96 *
MACRUM 0:e642dd732c4b 97 * @param i2c_obj I2C object (instance)
MACRUM 0:e642dd732c4b 98 * @param addr slave address of the I2C-bus peripheral (default: 0x3C)
MACRUM 0:e642dd732c4b 99 */
MACRUM 1:23bfc18affd9 100 KX022(I2C &i2c_obj, int addr = KX022_DEFAULT_SLAVE_ADDRESS);
MACRUM 0:e642dd732c4b 101
MACRUM 0:e642dd732c4b 102 /**
MACRUM 0:e642dd732c4b 103 * KX022 destructor
MACRUM 0:e642dd732c4b 104 */
MACRUM 0:e642dd732c4b 105 ~KX022();
MACRUM 0:e642dd732c4b 106
MACRUM 0:e642dd732c4b 107 /** Initializa KX022 sensor
MACRUM 0:e642dd732c4b 108 *
MACRUM 0:e642dd732c4b 109 * Configure sensor setting
MACRUM 0:e642dd732c4b 110 *
MACRUM 0:e642dd732c4b 111 */
MACRUM 0:e642dd732c4b 112 void initialize(void);
MACRUM 0:e642dd732c4b 113
MACRUM 0:e642dd732c4b 114 /**
MACRUM 0:e642dd732c4b 115 * Get X axis acceleration
MACRUM 0:e642dd732c4b 116 *
MACRUM 0:e642dd732c4b 117 * @returns X axis acceleration
MACRUM 0:e642dd732c4b 118 */
MACRUM 0:e642dd732c4b 119 float getAccX();
MACRUM 0:e642dd732c4b 120
MACRUM 0:e642dd732c4b 121 /**
MACRUM 0:e642dd732c4b 122 * Get Y axis acceleration
MACRUM 0:e642dd732c4b 123 *
MACRUM 0:e642dd732c4b 124 * @returns Y axis acceleration
MACRUM 0:e642dd732c4b 125 */
MACRUM 0:e642dd732c4b 126 float getAccY();
MACRUM 0:e642dd732c4b 127
MACRUM 0:e642dd732c4b 128 /**
MACRUM 0:e642dd732c4b 129 * Get Z axis acceleration
MACRUM 0:e642dd732c4b 130 *
MACRUM 0:e642dd732c4b 131 * @returns Z axis acceleration
MACRUM 0:e642dd732c4b 132 */
MACRUM 0:e642dd732c4b 133 float getAccZ();
MACRUM 0:e642dd732c4b 134
MACRUM 0:e642dd732c4b 135 /**
MACRUM 0:e642dd732c4b 136 * Get XYZ axis acceleration
MACRUM 0:e642dd732c4b 137 *
MACRUM 0:e642dd732c4b 138 * @param res array where acceleration data will be stored
MACRUM 0:e642dd732c4b 139 */
MACRUM 0:e642dd732c4b 140 void getAccAllAxis(float * res);
MACRUM 0:e642dd732c4b 141
MACRUM 0:e642dd732c4b 142 private:
MACRUM 0:e642dd732c4b 143 I2C m_i2c;
MACRUM 0:e642dd732c4b 144 int m_addr;
MACRUM 0:e642dd732c4b 145 void readRegs(int addr, uint8_t * data, int len);
MACRUM 0:e642dd732c4b 146 void writeRegs(uint8_t * data, int len);
MACRUM 0:e642dd732c4b 147 int16_t getAccAxis(uint8_t addr);
MACRUM 0:e642dd732c4b 148
MACRUM 0:e642dd732c4b 149 };
MACRUM 0:e642dd732c4b 150
MACRUM 0:e642dd732c4b 151 #endif