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

Committer:
MikkoZ
Date:
Thu Oct 06 10:23:48 2016 +0000
Revision:
2:62891556d47b
Parent:
1:f328083fb80b
Child:
3:4fd5361ed180
Added many features for setting up and reading the sensor registers via driver functions.

Who changed what in which revision?

UserRevisionLine numberNew 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 #ifndef KX123_H
MikkoZ 0:a3f43eb92f86 16 #define KX123_H
MikkoZ 0:a3f43eb92f86 17
MikkoZ 0:a3f43eb92f86 18 #include "RegisterWriter/RegisterWriter/rohm_hal2.h"
MikkoZ 0:a3f43eb92f86 19 #include "RegisterWriter/RegisterWriter/RegisterWriter.h"
MikkoZ 0:a3f43eb92f86 20
MikkoZ 1:f328083fb80b 21 #include "kx123_registers.h"
MikkoZ 0:a3f43eb92f86 22
MikkoZ 0:a3f43eb92f86 23 /**
MikkoZ 1:f328083fb80b 24 * Kionix KX123 accelerometer i2c driver. For some extend can be used also with
MikkoZ 1:f328083fb80b 25 * kx012, kx022, kx023, kx23h, kx112, kx122, kx124, kx222 and kx224. Driver uses
MikkoZ 1:f328083fb80b 26 * RegisterWriter -class as (i2c) hardware abstraction layer.
MikkoZ 2:62891556d47b 27 * Note that when doing setup, sensor has to be in setup mode, not in operating mode.
MikkoZ 0:a3f43eb92f86 28 */
MikkoZ 0:a3f43eb92f86 29 class KX123
MikkoZ 0:a3f43eb92f86 30 {
MikkoZ 0:a3f43eb92f86 31 public:
MikkoZ 1:f328083fb80b 32
MikkoZ 0:a3f43eb92f86 33 KX123(RegisterWriter &i2c_obj, uint8_t sad = KX123_DEFAULT_SLAVE_ADDRESS, uint8_t wai = KX123_WHO_AM_I_WAI_ID);
MikkoZ 0:a3f43eb92f86 34 ~KX123();
MikkoZ 0:a3f43eb92f86 35
MikkoZ 2:62891556d47b 36 bool start_setup_mode(void);
MikkoZ 2:62891556d47b 37 bool start_measurement_mode(void);
MikkoZ 0:a3f43eb92f86 38 bool set_defaults(void);
MikkoZ 2:62891556d47b 39 bool getresults_highpass(uint16_t* buf);
MikkoZ 0:a3f43eb92f86 40 bool getresults_raw(int16_t* buf);
MikkoZ 0:a3f43eb92f86 41 bool getresults_g(float* buf);
MikkoZ 0:a3f43eb92f86 42
MikkoZ 2:62891556d47b 43
MikkoZ 2:62891556d47b 44 bool get_tilt(enum e_axis* current_previous);
MikkoZ 2:62891556d47b 45 bool get_tap_interrupt_axis(enum e_axis* axis);
MikkoZ 2:62891556d47b 46 bool get_detected_motion_axis(enum e_axis* axis);
MikkoZ 2:62891556d47b 47 bool set_tilt_axis_mask(uint8_t cnltl2_tilt_mask);
MikkoZ 2:62891556d47b 48
MikkoZ 2:62891556d47b 49 bool get_interrupt_reason(enum e_interrupt_reason* int_reason);
MikkoZ 2:62891556d47b 50
MikkoZ 2:62891556d47b 51 bool has_interrupt_occured();
MikkoZ 2:62891556d47b 52 void clear_interrupt();
MikkoZ 2:62891556d47b 53 void soft_reset();
MikkoZ 2:62891556d47b 54 bool self_test();
MikkoZ 2:62891556d47b 55
MikkoZ 2:62891556d47b 56 bool set_cntl3_odrs(uint8_t tilt_position_odr, uint8_t directional_tap_odr, uint8_t motion_wuf_odr); //0xff for DONT_SET
MikkoZ 2:62891556d47b 57 bool set_odcntl(bool iir_filter_off, uint8_t lowpass_filter_freq_half, uint8_t odr);
MikkoZ 2:62891556d47b 58 bool int1_setup(uint8_t pwsel,
MikkoZ 2:62891556d47b 59 bool physical_int_pin_enabled,
MikkoZ 2:62891556d47b 60 bool physical_int_pin_active_high,
MikkoZ 2:62891556d47b 61 bool physical_int_pin_latch_disabled,
MikkoZ 2:62891556d47b 62 bool self_test_polarity_positive,
MikkoZ 2:62891556d47b 63 bool spi3wire_enabled);
MikkoZ 2:62891556d47b 64 bool int2_setup(uint8_t pwsel,
MikkoZ 2:62891556d47b 65 bool physical_int_pin_enabled,
MikkoZ 2:62891556d47b 66 bool physical_int_pin_active_high,
MikkoZ 2:62891556d47b 67 bool physical_int_pin_latch_disabled,
MikkoZ 2:62891556d47b 68 bool aclr2_enabled,
MikkoZ 2:62891556d47b 69 bool aclr1_enabled);
MikkoZ 2:62891556d47b 70 bool set_int1_interrupt_reason(uint8_t interrupt_reason);
MikkoZ 2:62891556d47b 71 bool set_int2_interrupt_reason(uint8_t interrupt_reason);
MikkoZ 2:62891556d47b 72
MikkoZ 2:62891556d47b 73 bool set_motion_detect_axis(uint8_t xxyyzz, bool axis_and_combination_enabled = false);
MikkoZ 2:62891556d47b 74 bool set_tap_axis(uint8_t xxyyzz);
MikkoZ 2:62891556d47b 75 /**
MikkoZ 2:62891556d47b 76 * Note that not all sensor setup registers are exposed with this driver yet.
MikkoZ 2:62891556d47b 77 * There are also setup registers for motion detect, tap/double tap detect, free
MikkoZ 2:62891556d47b 78 * fall detect, tilt hysteresis, low power and FIFO.
MikkoZ 2:62891556d47b 79 */
MikkoZ 2:62891556d47b 80
MikkoZ 0:a3f43eb92f86 81 private:
MikkoZ 2:62891556d47b 82 bool setup_mode_on;
MikkoZ 0:a3f43eb92f86 83 void set_tilt_position_defaults();
MikkoZ 0:a3f43eb92f86 84
MikkoZ 0:a3f43eb92f86 85 RegisterWriter i2c_rw;
MikkoZ 0:a3f43eb92f86 86 uint16_t resolution_divider;
MikkoZ 1:f328083fb80b 87 uint8_t _sad;
MikkoZ 1:f328083fb80b 88 uint8_t _wai;
MikkoZ 0:a3f43eb92f86 89 };
MikkoZ 0:a3f43eb92f86 90
MikkoZ 0:a3f43eb92f86 91 #endif