Kionix KX123 accelerometer C++ driver. Can be used for some extend also with kx022, kx023, kx122, etc. when used features are present in sensor.

Dependents:   kionix-kx123-hello

Revision:
1:f328083fb80b
Parent:
0:a3f43eb92f86
Child:
2:62891556d47b
--- a/kx123.cpp	Thu Sep 29 15:05:08 2016 +0000
+++ b/kx123.cpp	Fri Sep 30 11:52:29 2016 +0000
@@ -15,26 +15,21 @@
 #include "RegisterWriter/RegisterWriter/rohm_hal2.h"
 #include "RegisterWriter/RegisterWriter/RegisterWriter.h"
 
-#include "kx123_registers.h"
 #include "kx123.h"
 
 /**
-* KX123 accelerometer driver
-*/
-/**
-* Create a KX123 instance which is connected to pre-instantiated I2C object.
+* Create a KX123 instance for communication through pre-instantiated
+* RegisterWriter (I2C) -object.
 *
+* @param i2c_obj pre-instantiated RegisterWriter (I2C) -object reference
 * @param sad slave address of sensor.
 * @param wai who_am_i value (i.e. sensor type/model)
 */
-KX123::KX123(RegisterWriter &i2c_obj, uint8_t newsad, uint8_t newwai) : i2c_rw(i2c_obj) {
-    sad = newsad;
-    wai = newwai;        
+KX123::KX123(RegisterWriter &i2c_obj, uint8_t sad, uint8_t wai) : i2c_rw(i2c_obj) {
+    _sad = sad;
+    _wai = wai;        
 }
 
-/**
-* KX123 destructor
-*/
 KX123::~KX123(){
 }
 
@@ -48,7 +43,7 @@
 
     DEBUG_print("\n\r");
     DEBUG_print("KX123 init started\n\r");
-    i2c_rw.read_register(sad, KX122_WHO_AM_I, &buf, 1);
+    i2c_rw.read_register(_sad, KX122_WHO_AM_I, &buf, 1);
     if (buf == KX123_WHO_AM_I_WAI_ID) {
         DEBUG_print("KX123 found. (WAI %d) ", buf);
     } else {
@@ -89,14 +84,14 @@
     }
 
     //First set CNTL1 PC1-bit to stand-by mode, after that setup can be made
-    i2c_rw.write_register(sad, KX122_CNTL1, 0 );
+    i2c_rw.write_register(_sad, KX122_CNTL1, 0 );
 
     //set_tilt_position_defaults();
 
     //ODCNTL: Output Data Rate control (ODR) 
-    i2c_rw.write_register(sad, KX122_ODCNTL, KX122_ODCNTL_OSA_25600);
+    i2c_rw.write_register(_sad, KX122_ODCNTL, KX122_ODCNTL_OSA_25600);
     //Setup G-range and 8/16-bit resolution + set CNTL1 PC1-bit to operating mode (also WUF_EN, TP_EN and DT_EN)
-    i2c_rw.write_register(sad, KX122_CNTL1, ( KX122_CNTL1_PC1 | KX122_CNTL1_GSEL_8G | KX122_CNTL1_RES ) );
+    i2c_rw.write_register(_sad, KX122_CNTL1, ( KX122_CNTL1_PC1 | KX122_CNTL1_GSEL_8G | KX122_CNTL1_RES ) );
 
     //resolution_divider = 32768/2;  //KX122_CNTL1_GSEL_2G
     //resolution_divider = 32768/4;  //KX122_CNTL1_GSEL_4G
@@ -107,27 +102,28 @@
 
 
 /**
-CNTL PC1-bit has to be set to stand-by before this command.
-return true on error
+* CNTL PC1-bit has to be set to stand-by before this command.
 **/
 void KX123::set_tilt_position_defaults(){
     //CNTL3: Tilt position control, directional tap control and motion wakeup control
-    i2c_rw.write_register(sad, KX122_CNTL3, ( KX122_CNTL3_OTP_50 | KX122_CNTL3_OTDT_400 ) ); 
+    i2c_rw.write_register(_sad, KX122_CNTL3, ( KX122_CNTL3_OTP_50 | KX122_CNTL3_OTDT_400 ) ); 
     //TILT_TIMER: Setup tilt position timer (~=filter)
-    i2c_rw.write_register(sad, KX122_TILT_TIMER, 0x01);
+    i2c_rw.write_register(_sad, KX122_TILT_TIMER, 0x01);
     return;
 }
 
 
 /**
-return read_error, true on error, false on read ok.
+* Get raw uint16_t XYZ-values from sensor
+* @param *buf to uint16_t[3] for results
+* @return true on error, false on read ok.
 **/
 bool KX123::getresults_raw(int16_t* buf){
     #define RESULTS_LEN 6
     uint8_t tmp[RESULTS_LEN];     //XYZ (lhlhlh)
     uint8_t read_bytes;
 
-    read_bytes = i2c_rw.read_register(sad, KX122_XOUT_L, &tmp[0], sizeof(tmp));
+    read_bytes = i2c_rw.read_register(_sad, KX122_XOUT_L, &tmp[0], sizeof(tmp));
     if (read_bytes != RESULTS_LEN){
         return true;
         }
@@ -138,6 +134,11 @@
 }
 
 
+/**
+* Get gravity scaled float XYZ-values from sensor
+* @param *buf to float[3] for results
+* @return true on error, false on read ok.
+**/
 bool KX123::getresults_g(float* buf){
     int16_t raw[3];
     int read_error;