KX022 Accelerometer library
Fork of KX022 by
Revision 4:4038b02c530d, committed 2018-10-18
- Comitter:
- deldering95
- Date:
- Thu Oct 18 12:25:00 2018 +0000
- Parent:
- 3:e2d1659fe67e
- Child:
- 5:9519f53d9965
- Commit message:
- Accelero
Changed in this revision
--- a/KX022.cpp Wed Sep 14 09:16:41 2016 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,127 +0,0 @@
-/* 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];
-
- DEBUG_PRINT("KX022 init started\n\r");
- readRegs(KX022_WHO_AM_I, &buf, sizeof(buf));
- if (buf != KX022_WAI_VAL) {
- DEBUG_PRINT("KX022 initialization error. (WAI %d, not %d)\n\r", buf, KX022_WAI_VAL);
- DEBUG_PRINT("Trying to config anyway, in case there is some compatible sensor connected.\n\r");
- }
-
- 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)
-{
- int read_nok;
- char t[1] = {addr};
-
- m_i2c.write(m_addr, t, 1, true);
- read_nok = m_i2c.read(m_addr, (char *)data, len);
- if (read_nok){
- DEBUG_PRINT("Read fail\n\r");
- }
-}
-
-void KX022::writeRegs(uint8_t * data, int len)
-{
- m_i2c.write(m_addr, (char *)data, len);
-}
--- a/KX022.h Wed Sep 14 09:16:41 2016 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,151 +0,0 @@
-/* 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 KX022_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)
-
-#define _DEBUG
-#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 = KX022_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 = KX022_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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/KXTJ3.cpp Thu Oct 18 12:25:00 2018 +0000
@@ -0,0 +1,119 @@
+/* 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
+*
+*/
+
+#include "mbed.h"
+#include "KXTJ3.h"
+
+//KXTJ3::KXTJ3(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr)
+//{
+// initialize();
+//}
+
+KXTJ3::KXTJ3(I2C* i2c_obj, int addr) : m_i2c(i2c_obj), m_addr(addr)
+{
+ initialize();
+}
+
+KXTJ3::~KXTJ3()
+{
+}
+
+void KXTJ3::initialize()
+{
+// unsigned char buf;
+ unsigned char reg[2];
+/*
+ DEBUG_PRINT("KXTJ3 init started\n\r");
+ readRegs(KXTJ3_WHO_AM_I, &buf, 1);
+ if (buf != KXTJ3_WAI_VAL) {
+ DEBUG_PRINT("KXTJ3 initialization error. (WAI %d, not %d)\n\r", buf, KXTJ3_WAI_VAL);
+ DEBUG_PRINT("Trying to config anyway, in case there is some compatible sensor connected.\n\r");
+ }
+*/
+ reg[0] = KXTJ3_CNTL1;
+ reg[1] = 0x40;
+ writeRegs(reg, 2);
+
+ reg[0] = KXTJ3_ODCNTL;
+ reg[1] = 0x01;
+ writeRegs(reg, 2);
+
+ reg[0] = KXTJ3_CNTL1;
+ reg[1] = 0xC0;
+ writeRegs(reg, 2);
+}
+
+float KXTJ3::getAccX()
+{
+ return (float(getAccAxis(KXTJ3_XOUT_L))/16384);
+}
+
+float KXTJ3::getAccY()
+{
+ return (float(getAccAxis(KXTJ3_YOUT_L))/16384);
+}
+
+float KXTJ3::getAccZ()
+{
+ return (float(getAccAxis(KXTJ3_ZOUT_L))/16384);
+}
+
+void KXTJ3::getAccAllAxis(float * res)
+{
+ res[0] = getAccX();
+ res[1] = getAccY();
+ res[2] = getAccZ();
+}
+
+int16_t KXTJ3::getAccAxis(uint8_t addr)
+{
+ int16_t acc;
+ uint8_t res[2];
+
+ readRegs(addr, res, 2);
+ acc = ((res[1] << 8) | res[0]);
+ return acc;
+}
+
+void KXTJ3::readRegs(int addr, uint8_t * data, int len)
+{
+ int read_nok;
+ char t[1] = {addr};
+
+ m_i2c->write(m_addr, t, 1,true);
+ read_nok = m_i2c->read(m_addr, (char *)data, len);
+ if (read_nok){
+ DEBUG_PRINT("Read fail\n\r");
+ }
+}
+
+void KXTJ3::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/KXTJ3.h Thu Oct 18 12:25:00 2018 +0000
@@ -0,0 +1,151 @@
+/* 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Oct 18 12:25:00 2018 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/994bdf8177cb \ No newline at end of file
