Kionix KX123 accelerometer C++ driver. Can be used for some extend also with kx022, kx023, kx122, etc. when used features are present in sensor.
Fork of kionix-kx123-driver by
kx123.cpp@1:f328083fb80b, 2016-09-30 (annotated)
- Committer:
- MikkoZ
- Date:
- Fri Sep 30 11:52:29 2016 +0000
- Revision:
- 1:f328083fb80b
- Parent:
- 0:a3f43eb92f86
- Child:
- 2:62891556d47b
Added doxygen documentation for library.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikkoZ | 0:a3f43eb92f86 | 1 | /* Copyright 2016 Rohm Semiconductor |
MikkoZ | 0:a3f43eb92f86 | 2 | |
MikkoZ | 0:a3f43eb92f86 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); |
MikkoZ | 0:a3f43eb92f86 | 4 | you may not use this file except in compliance with the License. |
MikkoZ | 0:a3f43eb92f86 | 5 | You may obtain a copy of the License at |
MikkoZ | 0:a3f43eb92f86 | 6 | |
MikkoZ | 0:a3f43eb92f86 | 7 | http://www.apache.org/licenses/LICENSE-2.0 |
MikkoZ | 0:a3f43eb92f86 | 8 | |
MikkoZ | 0:a3f43eb92f86 | 9 | Unless required by applicable law or agreed to in writing, software |
MikkoZ | 0:a3f43eb92f86 | 10 | distributed under the License is distributed on an "AS IS" BASIS, |
MikkoZ | 0:a3f43eb92f86 | 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
MikkoZ | 0:a3f43eb92f86 | 12 | See the License for the specific language governing permissions and |
MikkoZ | 0:a3f43eb92f86 | 13 | limitations under the License. |
MikkoZ | 0:a3f43eb92f86 | 14 | */ |
MikkoZ | 0:a3f43eb92f86 | 15 | #include "RegisterWriter/RegisterWriter/rohm_hal2.h" |
MikkoZ | 0:a3f43eb92f86 | 16 | #include "RegisterWriter/RegisterWriter/RegisterWriter.h" |
MikkoZ | 0:a3f43eb92f86 | 17 | |
MikkoZ | 0:a3f43eb92f86 | 18 | #include "kx123.h" |
MikkoZ | 0:a3f43eb92f86 | 19 | |
MikkoZ | 0:a3f43eb92f86 | 20 | /** |
MikkoZ | 1:f328083fb80b | 21 | * Create a KX123 instance for communication through pre-instantiated |
MikkoZ | 1:f328083fb80b | 22 | * RegisterWriter (I2C) -object. |
MikkoZ | 0:a3f43eb92f86 | 23 | * |
MikkoZ | 1:f328083fb80b | 24 | * @param i2c_obj pre-instantiated RegisterWriter (I2C) -object reference |
MikkoZ | 0:a3f43eb92f86 | 25 | * @param sad slave address of sensor. |
MikkoZ | 0:a3f43eb92f86 | 26 | * @param wai who_am_i value (i.e. sensor type/model) |
MikkoZ | 0:a3f43eb92f86 | 27 | */ |
MikkoZ | 1:f328083fb80b | 28 | KX123::KX123(RegisterWriter &i2c_obj, uint8_t sad, uint8_t wai) : i2c_rw(i2c_obj) { |
MikkoZ | 1:f328083fb80b | 29 | _sad = sad; |
MikkoZ | 1:f328083fb80b | 30 | _wai = wai; |
MikkoZ | 0:a3f43eb92f86 | 31 | } |
MikkoZ | 0:a3f43eb92f86 | 32 | |
MikkoZ | 0:a3f43eb92f86 | 33 | KX123::~KX123(){ |
MikkoZ | 0:a3f43eb92f86 | 34 | } |
MikkoZ | 0:a3f43eb92f86 | 35 | |
MikkoZ | 0:a3f43eb92f86 | 36 | /** |
MikkoZ | 0:a3f43eb92f86 | 37 | * Check if sensor is connected, setup defaults to sensor and start measuring. |
MikkoZ | 0:a3f43eb92f86 | 38 | * @return true on error, false on ok |
MikkoZ | 0:a3f43eb92f86 | 39 | */ |
MikkoZ | 0:a3f43eb92f86 | 40 | bool KX123::set_defaults() |
MikkoZ | 0:a3f43eb92f86 | 41 | { |
MikkoZ | 0:a3f43eb92f86 | 42 | unsigned char buf; |
MikkoZ | 0:a3f43eb92f86 | 43 | |
MikkoZ | 0:a3f43eb92f86 | 44 | DEBUG_print("\n\r"); |
MikkoZ | 0:a3f43eb92f86 | 45 | DEBUG_print("KX123 init started\n\r"); |
MikkoZ | 1:f328083fb80b | 46 | i2c_rw.read_register(_sad, KX122_WHO_AM_I, &buf, 1); |
MikkoZ | 0:a3f43eb92f86 | 47 | if (buf == KX123_WHO_AM_I_WAI_ID) { |
MikkoZ | 0:a3f43eb92f86 | 48 | DEBUG_print("KX123 found. (WAI %d) ", buf); |
MikkoZ | 0:a3f43eb92f86 | 49 | } else { |
MikkoZ | 0:a3f43eb92f86 | 50 | DEBUG_print("KX123 not found (WAI %d, not %d). ", buf, KX123_WHO_AM_I_WAI_ID); |
MikkoZ | 0:a3f43eb92f86 | 51 | switch(buf){ |
MikkoZ | 0:a3f43eb92f86 | 52 | case KX012_WHO_AM_I_WAI_ID: |
MikkoZ | 0:a3f43eb92f86 | 53 | DEBUG_print("Found KX012"); |
MikkoZ | 0:a3f43eb92f86 | 54 | break; |
MikkoZ | 0:a3f43eb92f86 | 55 | case KX022_WHO_AM_I_WAI_ID: |
MikkoZ | 0:a3f43eb92f86 | 56 | DEBUG_print("Found KX022"); |
MikkoZ | 0:a3f43eb92f86 | 57 | break; |
MikkoZ | 0:a3f43eb92f86 | 58 | case KX023_WHO_AM_I_WAI_ID: |
MikkoZ | 0:a3f43eb92f86 | 59 | DEBUG_print("Found KX023"); |
MikkoZ | 0:a3f43eb92f86 | 60 | break; |
MikkoZ | 0:a3f43eb92f86 | 61 | case KX23H_WHO_AM_I_WAI_ID: |
MikkoZ | 0:a3f43eb92f86 | 62 | DEBUG_print("Found KX23H"); |
MikkoZ | 0:a3f43eb92f86 | 63 | break; |
MikkoZ | 0:a3f43eb92f86 | 64 | case KX112_WHO_AM_I_WAI_ID: |
MikkoZ | 0:a3f43eb92f86 | 65 | DEBUG_print("Found KX112"); |
MikkoZ | 0:a3f43eb92f86 | 66 | break; |
MikkoZ | 0:a3f43eb92f86 | 67 | case KX122_WHO_AM_I_WAI_ID: |
MikkoZ | 0:a3f43eb92f86 | 68 | DEBUG_print("Found KX122"); |
MikkoZ | 0:a3f43eb92f86 | 69 | break; |
MikkoZ | 0:a3f43eb92f86 | 70 | case KX124_WHO_AM_I_WAI_ID: |
MikkoZ | 0:a3f43eb92f86 | 71 | DEBUG_print("Found KX124"); |
MikkoZ | 0:a3f43eb92f86 | 72 | break; |
MikkoZ | 0:a3f43eb92f86 | 73 | case KX222_WHO_AM_I_WAI_ID: |
MikkoZ | 0:a3f43eb92f86 | 74 | DEBUG_print("Found KX222"); |
MikkoZ | 0:a3f43eb92f86 | 75 | break; |
MikkoZ | 0:a3f43eb92f86 | 76 | case KX224_WHO_AM_I_WAI_ID: |
MikkoZ | 0:a3f43eb92f86 | 77 | DEBUG_print("Found KX224"); |
MikkoZ | 0:a3f43eb92f86 | 78 | break; |
MikkoZ | 0:a3f43eb92f86 | 79 | default: |
MikkoZ | 0:a3f43eb92f86 | 80 | DEBUG_print("Not even other sensor found from same family.\n\r"); |
MikkoZ | 0:a3f43eb92f86 | 81 | return true; |
MikkoZ | 0:a3f43eb92f86 | 82 | } |
MikkoZ | 0:a3f43eb92f86 | 83 | DEBUG_print(" though, trying to use that.\n\r"); |
MikkoZ | 0:a3f43eb92f86 | 84 | } |
MikkoZ | 0:a3f43eb92f86 | 85 | |
MikkoZ | 0:a3f43eb92f86 | 86 | //First set CNTL1 PC1-bit to stand-by mode, after that setup can be made |
MikkoZ | 1:f328083fb80b | 87 | i2c_rw.write_register(_sad, KX122_CNTL1, 0 ); |
MikkoZ | 0:a3f43eb92f86 | 88 | |
MikkoZ | 0:a3f43eb92f86 | 89 | //set_tilt_position_defaults(); |
MikkoZ | 0:a3f43eb92f86 | 90 | |
MikkoZ | 0:a3f43eb92f86 | 91 | //ODCNTL: Output Data Rate control (ODR) |
MikkoZ | 1:f328083fb80b | 92 | i2c_rw.write_register(_sad, KX122_ODCNTL, KX122_ODCNTL_OSA_25600); |
MikkoZ | 0:a3f43eb92f86 | 93 | //Setup G-range and 8/16-bit resolution + set CNTL1 PC1-bit to operating mode (also WUF_EN, TP_EN and DT_EN) |
MikkoZ | 1:f328083fb80b | 94 | i2c_rw.write_register(_sad, KX122_CNTL1, ( KX122_CNTL1_PC1 | KX122_CNTL1_GSEL_8G | KX122_CNTL1_RES ) ); |
MikkoZ | 0:a3f43eb92f86 | 95 | |
MikkoZ | 0:a3f43eb92f86 | 96 | //resolution_divider = 32768/2; //KX122_CNTL1_GSEL_2G |
MikkoZ | 0:a3f43eb92f86 | 97 | //resolution_divider = 32768/4; //KX122_CNTL1_GSEL_4G |
MikkoZ | 0:a3f43eb92f86 | 98 | resolution_divider = 32768/8; //KX122_CNTL1_GSEL_8G |
MikkoZ | 0:a3f43eb92f86 | 99 | |
MikkoZ | 0:a3f43eb92f86 | 100 | return false; |
MikkoZ | 0:a3f43eb92f86 | 101 | } |
MikkoZ | 0:a3f43eb92f86 | 102 | |
MikkoZ | 0:a3f43eb92f86 | 103 | |
MikkoZ | 0:a3f43eb92f86 | 104 | /** |
MikkoZ | 1:f328083fb80b | 105 | * CNTL PC1-bit has to be set to stand-by before this command. |
MikkoZ | 0:a3f43eb92f86 | 106 | **/ |
MikkoZ | 0:a3f43eb92f86 | 107 | void KX123::set_tilt_position_defaults(){ |
MikkoZ | 0:a3f43eb92f86 | 108 | //CNTL3: Tilt position control, directional tap control and motion wakeup control |
MikkoZ | 1:f328083fb80b | 109 | i2c_rw.write_register(_sad, KX122_CNTL3, ( KX122_CNTL3_OTP_50 | KX122_CNTL3_OTDT_400 ) ); |
MikkoZ | 0:a3f43eb92f86 | 110 | //TILT_TIMER: Setup tilt position timer (~=filter) |
MikkoZ | 1:f328083fb80b | 111 | i2c_rw.write_register(_sad, KX122_TILT_TIMER, 0x01); |
MikkoZ | 0:a3f43eb92f86 | 112 | return; |
MikkoZ | 0:a3f43eb92f86 | 113 | } |
MikkoZ | 0:a3f43eb92f86 | 114 | |
MikkoZ | 0:a3f43eb92f86 | 115 | |
MikkoZ | 0:a3f43eb92f86 | 116 | /** |
MikkoZ | 1:f328083fb80b | 117 | * Get raw uint16_t XYZ-values from sensor |
MikkoZ | 1:f328083fb80b | 118 | * @param *buf to uint16_t[3] for results |
MikkoZ | 1:f328083fb80b | 119 | * @return true on error, false on read ok. |
MikkoZ | 0:a3f43eb92f86 | 120 | **/ |
MikkoZ | 0:a3f43eb92f86 | 121 | bool KX123::getresults_raw(int16_t* buf){ |
MikkoZ | 0:a3f43eb92f86 | 122 | #define RESULTS_LEN 6 |
MikkoZ | 0:a3f43eb92f86 | 123 | uint8_t tmp[RESULTS_LEN]; //XYZ (lhlhlh) |
MikkoZ | 0:a3f43eb92f86 | 124 | uint8_t read_bytes; |
MikkoZ | 0:a3f43eb92f86 | 125 | |
MikkoZ | 1:f328083fb80b | 126 | read_bytes = i2c_rw.read_register(_sad, KX122_XOUT_L, &tmp[0], sizeof(tmp)); |
MikkoZ | 0:a3f43eb92f86 | 127 | if (read_bytes != RESULTS_LEN){ |
MikkoZ | 0:a3f43eb92f86 | 128 | return true; |
MikkoZ | 0:a3f43eb92f86 | 129 | } |
MikkoZ | 0:a3f43eb92f86 | 130 | buf[0] = ( tmp[1] << 8 ) | tmp[0]; //X |
MikkoZ | 0:a3f43eb92f86 | 131 | buf[1] = ( tmp[3] << 8 ) | tmp[2]; //Y |
MikkoZ | 0:a3f43eb92f86 | 132 | buf[2] = ( tmp[5] << 8 ) | tmp[4]; //Z |
MikkoZ | 0:a3f43eb92f86 | 133 | return false; |
MikkoZ | 0:a3f43eb92f86 | 134 | } |
MikkoZ | 0:a3f43eb92f86 | 135 | |
MikkoZ | 0:a3f43eb92f86 | 136 | |
MikkoZ | 1:f328083fb80b | 137 | /** |
MikkoZ | 1:f328083fb80b | 138 | * Get gravity scaled float XYZ-values from sensor |
MikkoZ | 1:f328083fb80b | 139 | * @param *buf to float[3] for results |
MikkoZ | 1:f328083fb80b | 140 | * @return true on error, false on read ok. |
MikkoZ | 1:f328083fb80b | 141 | **/ |
MikkoZ | 0:a3f43eb92f86 | 142 | bool KX123::getresults_g(float* buf){ |
MikkoZ | 0:a3f43eb92f86 | 143 | int16_t raw[3]; |
MikkoZ | 0:a3f43eb92f86 | 144 | int read_error; |
MikkoZ | 0:a3f43eb92f86 | 145 | |
MikkoZ | 0:a3f43eb92f86 | 146 | read_error = getresults_raw(&raw[0]); |
MikkoZ | 0:a3f43eb92f86 | 147 | if (read_error){ |
MikkoZ | 0:a3f43eb92f86 | 148 | return read_error; |
MikkoZ | 0:a3f43eb92f86 | 149 | } |
MikkoZ | 0:a3f43eb92f86 | 150 | |
MikkoZ | 0:a3f43eb92f86 | 151 | //Scale raw values to G-scale |
MikkoZ | 0:a3f43eb92f86 | 152 | buf[0] = ((float)raw[0]) / resolution_divider; |
MikkoZ | 0:a3f43eb92f86 | 153 | buf[1] = ((float)raw[1]) / resolution_divider; |
MikkoZ | 0:a3f43eb92f86 | 154 | buf[2] = ((float)raw[2]) / resolution_divider; |
MikkoZ | 0:a3f43eb92f86 | 155 | return false; |
MikkoZ | 0:a3f43eb92f86 | 156 | } |
MikkoZ | 0:a3f43eb92f86 | 157 | |
MikkoZ | 0:a3f43eb92f86 | 158 |