KX022 Accelerometer library

Dependents:   LazuriteGraph_Hello KX022_Hello GR-PEACH_IoT_Platform_HTTP_sample

Files at this revision

API Documentation at this revision

Comitter:
MikkoZ
Date:
Wed Sep 14 09:16:41 2016 +0000
Parent:
2:5ac8e9e5f63e
Commit message:
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.

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
diff -r 5ac8e9e5f63e -r e2d1659fe67e KX022.cpp
--- a/KX022.cpp	Wed Mar 23 03:32:14 2016 +0000
+++ b/KX022.cpp	Wed Sep 14 09:16:41 2016 +0000
@@ -49,10 +49,11 @@
     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. (%d)\n", buf);
-        return;
+        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;
@@ -110,9 +111,14 @@
 
 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);
-    m_i2c.read(m_addr, (char *)data, len);
+    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)
diff -r 5ac8e9e5f63e -r e2d1659fe67e KX022.h
--- a/KX022.h	Wed Mar 23 03:32:14 2016 +0000
+++ b/KX022.h	Wed Sep 14 09:16:41 2016 +0000
@@ -47,6 +47,7 @@
 #define KX022_ODCNTL          (0x1B)
 #define KX022_TILT_TIMER      (0x22)
 
+#define _DEBUG
 #ifdef _DEBUG
 extern Serial pc;
 #define DEBUG_PRINT(...) pc.printf(__VA_ARGS__)