├── README.md
├── examples
├── X_NUCLEO_IKS01A1_6DOrientation
│ └── X_NUCLEO_IKS01A1_6DOrientation.ino
├── X_NUCLEO_IKS01A1_DoubleTap
│ └── X_NUCLEO_IKS01A1_DoubleTap.ino
├── X_NUCLEO_IKS01A1_FreeFall
│ └── X_NUCLEO_IKS01A1_FreeFall.ino
├── X_NUCLEO_IKS01A1_LSM6DS3_DataLog_Terminal
│ └── X_NUCLEO_IKS01A1_LSM6DS3_DataLog_Terminal.ino
├── X_NUCLEO_IKS01A1_MultiEvent
│ └── X_NUCLEO_IKS01A1_MultiEvent.ino
├── X_NUCLEO_IKS01A1_Pedometer
│ └── X_NUCLEO_IKS01A1_Pedometer.ino
├── X_NUCLEO_IKS01A1_Tap
│ └── X_NUCLEO_IKS01A1_Tap.ino
├── X_NUCLEO_IKS01A1_Tilt
│ └── X_NUCLEO_IKS01A1_Tilt.ino
└── X_NUCLEO_IKS01A1_WakeUp
│ └── X_NUCLEO_IKS01A1_WakeUp.ino
├── keywords.txt
├── library.properties
└── src
├── LSM6DS3Sensor.cpp
├── LSM6DS3Sensor.h
├── LSM6DS3_ACC_GYRO_Driver.c
└── LSM6DS3_ACC_GYRO_Driver.h
/README.md:
--------------------------------------------------------------------------------
1 | # LSM6DS3
2 | Arduino library to support the LSM6DS3 3D accelerometer and 3D gyroscope
3 |
4 | ## API
5 |
6 | This sensor uses I2C or SPI to communicate.
7 | For I2C it is then required to create a TwoWire interface before accessing to the sensors:
8 |
9 | TwoWire dev_i2c(I2C_SDA, I2C_SCL);
10 | dev_i2c.begin();
11 |
12 | For SPI it is then required to create a SPI interface before accessing to the sensors:
13 |
14 | SPIClass dev_spi(SPI_MOSI, SPI_MISO, SPI_SCK);
15 | dev_spi.begin();
16 |
17 | An instance can be created and enabled when the I2C bus is used following the procedure below:
18 |
19 | LSM6DS3Sensor AccGyr(&dev_i2c);
20 | AccGyr.begin();
21 | AccGyr.Enable_X();
22 | AccGyr.Enable_G();
23 |
24 | An instance can be created and enabled when the SPI bus is used following the procedure below:
25 |
26 | LSM6DS3Sensor AccGyr(&dev_spi, CS_PIN);
27 | AccGyr.begin();
28 | AccGyr.Enable_X();
29 | AccGyr.Enable_G();
30 |
31 | The access to the sensor values is done as explained below:
32 |
33 | Read accelerometer and gyroscope.
34 |
35 | int32_t accelerometer[3];
36 | int32_t gyroscope[3];
37 | AccGyr.Get_X_Axes(accelerometer);
38 | AccGyr.Get_G_Axes(gyroscope);
39 |
40 | ## Documentation
41 |
42 | You can find the source files at
43 | https://github.com/stm32duino/LSM6DS3
44 |
45 | The LSM6DS3 datasheet is available at
46 | http://www.st.com/content/st_com/en/products/mems-and-sensors/inemo-inertial-modules/lsm6ds3.html
47 |
--------------------------------------------------------------------------------
/examples/X_NUCLEO_IKS01A1_6DOrientation/X_NUCLEO_IKS01A1_6DOrientation.ino:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file X_NUCLEO_IKS01A1_6DOrientation.ino
4 | * @author AST
5 | * @version V1.0.0
6 | * @date 7 September 2017
7 | * @brief Arduino test application for the STMicrolectronics X-NUCLEO-IKS01A1
8 | * MEMS Inertial and Environmental sensor expansion board.
9 | * This application detects 6D Orientation event through the LSM6DS3 sensor.
10 | * This application makes use of C++ classes obtained from the C
11 | * components' drivers.
12 | ******************************************************************************
13 | * @attention
14 | *
15 | *
© COPYRIGHT(c) 2017 STMicroelectronics
16 | *
17 | * Redistribution and use in source and binary forms, with or without modification,
18 | * are permitted provided that the following conditions are met:
19 | * 1. Redistributions of source code must retain the above copyright notice,
20 | * this list of conditions and the following disclaimer.
21 | * 2. Redistributions in binary form must reproduce the above copyright notice,
22 | * this list of conditions and the following disclaimer in the documentation
23 | * and/or other materials provided with the distribution.
24 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
25 | * may be used to endorse or promote products derived from this software
26 | * without specific prior written permission.
27 | *
28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 | *
39 | ******************************************************************************
40 | */
41 |
42 | /** NOTE
43 | In order to use this sketch on X-NUCLEO-IKS01A1 board you need the STEVAL-MKI160V1
44 | board attached to X-NUCLEO-IKS01A1 board via DIL24 interface.
45 | */
46 |
47 | // Includes.
48 | #include
49 |
50 | #if defined(ARDUINO_SAM_DUE)
51 | #define DEV_I2C Wire1 //Define which I2C bus is used. Wire1 for the Arduino Due
52 | #define SerialPort Serial
53 | #else
54 | #define DEV_I2C Wire //Or Wire
55 | #define SerialPort Serial
56 | #endif
57 |
58 | // Components.
59 | LSM6DS3Sensor AccGyr(&DEV_I2C, LSM6DS3_ACC_GYRO_I2C_ADDRESS_LOW);
60 |
61 | //Interrupts.
62 | volatile int mems_event = 0;
63 |
64 | char report[256];
65 |
66 | void INT1Event_cb();
67 | void sendOrientation();
68 |
69 | void setup() {
70 | // Led.
71 | pinMode(13, OUTPUT);
72 |
73 | // Initialize serial for output.
74 | SerialPort.begin(9600);
75 |
76 | // Initialize I2C bus.
77 | DEV_I2C.begin();
78 |
79 | //Interrupts.
80 | attachInterrupt(A2, INT1Event_cb, RISING);
81 |
82 | // Initialize Components.
83 | AccGyr.begin();
84 | AccGyr.Enable_X();
85 |
86 | // Enable 6D Orientation.
87 | AccGyr.Enable_6D_Orientation();
88 | }
89 |
90 | void loop() {
91 | if (mems_event)
92 | {
93 | mems_event = 0;
94 | LSM6DS3_Event_Status_t status;
95 | AccGyr.Get_Event_Status(&status);
96 | if (status.D6DOrientationStatus)
97 | {
98 | // Send 6D Orientation
99 | sendOrientation();
100 |
101 | // Led blinking.
102 | digitalWrite(13, HIGH);
103 | delay(100);
104 | digitalWrite(13, LOW);
105 | }
106 | }
107 | }
108 |
109 | void INT1Event_cb()
110 | {
111 | mems_event = 1;
112 | }
113 |
114 | void sendOrientation()
115 | {
116 | uint8_t xl = 0;
117 | uint8_t xh = 0;
118 | uint8_t yl = 0;
119 | uint8_t yh = 0;
120 | uint8_t zl = 0;
121 | uint8_t zh = 0;
122 |
123 | AccGyr.Get_6D_Orientation_XL(&xl);
124 | AccGyr.Get_6D_Orientation_XH(&xh);
125 | AccGyr.Get_6D_Orientation_YL(&yl);
126 | AccGyr.Get_6D_Orientation_YH(&yh);
127 | AccGyr.Get_6D_Orientation_ZL(&zl);
128 | AccGyr.Get_6D_Orientation_ZH(&zh);
129 |
130 | if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 1 && zh == 0 )
131 | {
132 | sprintf( report, "\r\n ________________ " \
133 | "\r\n | | " \
134 | "\r\n | * | " \
135 | "\r\n | | " \
136 | "\r\n | | " \
137 | "\r\n | | " \
138 | "\r\n | | " \
139 | "\r\n |________________| \r\n" );
140 | }
141 |
142 | else if ( xl == 1 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 0 )
143 | {
144 | sprintf( report, "\r\n ________________ " \
145 | "\r\n | | " \
146 | "\r\n | * | " \
147 | "\r\n | | " \
148 | "\r\n | | " \
149 | "\r\n | | " \
150 | "\r\n | | " \
151 | "\r\n |________________| \r\n" );
152 | }
153 |
154 | else if ( xl == 0 && yl == 0 && zl == 0 && xh == 1 && yh == 0 && zh == 0 )
155 | {
156 | sprintf( report, "\r\n ________________ " \
157 | "\r\n | | " \
158 | "\r\n | | " \
159 | "\r\n | | " \
160 | "\r\n | | " \
161 | "\r\n | | " \
162 | "\r\n | * | " \
163 | "\r\n |________________| \r\n" );
164 | }
165 |
166 | else if ( xl == 0 && yl == 1 && zl == 0 && xh == 0 && yh == 0 && zh == 0 )
167 | {
168 | sprintf( report, "\r\n ________________ " \
169 | "\r\n | | " \
170 | "\r\n | | " \
171 | "\r\n | | " \
172 | "\r\n | | " \
173 | "\r\n | | " \
174 | "\r\n | * | " \
175 | "\r\n |________________| \r\n" );
176 | }
177 |
178 | else if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 1 )
179 | {
180 | sprintf( report, "\r\n __*_____________ " \
181 | "\r\n |________________| \r\n" );
182 | }
183 |
184 | else if ( xl == 0 && yl == 0 && zl == 1 && xh == 0 && yh == 0 && zh == 0 )
185 | {
186 | sprintf( report, "\r\n ________________ " \
187 | "\r\n |________________| " \
188 | "\r\n * \r\n" );
189 | }
190 |
191 | else
192 | {
193 | sprintf( report, "None of the 6D orientation axes is set in LSM6DSL - accelerometer.\r\n" );
194 | }
195 |
196 | SerialPort.print(report);
197 | }
198 |
--------------------------------------------------------------------------------
/examples/X_NUCLEO_IKS01A1_DoubleTap/X_NUCLEO_IKS01A1_DoubleTap.ino:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file X_NUCLEO_IKS01A1_DoubleTap.ino
4 | * @author AST
5 | * @version V1.0.0
6 | * @date 7 September 2017
7 | * @brief Arduino test application for the STMicrolectronics X-NUCLEO-IKS01A1
8 | * MEMS Inertial and Environmental sensor expansion board.
9 | * This application detects double tap event through the LSM6DS3 sensor.
10 | * This application makes use of C++ classes obtained from the C
11 | * components' drivers.
12 | ******************************************************************************
13 | * @attention
14 | *
15 | * © COPYRIGHT(c) 2017 STMicroelectronics
16 | *
17 | * Redistribution and use in source and binary forms, with or without modification,
18 | * are permitted provided that the following conditions are met:
19 | * 1. Redistributions of source code must retain the above copyright notice,
20 | * this list of conditions and the following disclaimer.
21 | * 2. Redistributions in binary form must reproduce the above copyright notice,
22 | * this list of conditions and the following disclaimer in the documentation
23 | * and/or other materials provided with the distribution.
24 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
25 | * may be used to endorse or promote products derived from this software
26 | * without specific prior written permission.
27 | *
28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 | *
39 | ******************************************************************************
40 | */
41 |
42 | /** NOTE
43 | In order to use this sketch on X-NUCLEO-IKS01A1 board you need the STEVAL-MKI160V1
44 | board attached to X-NUCLEO-IKS01A1 board via DIL24 interface.
45 | */
46 |
47 | // Includes.
48 | #include
49 |
50 | #if defined(ARDUINO_SAM_DUE)
51 | #define DEV_I2C Wire1 //Define which I2C bus is used. Wire1 for the Arduino Due
52 | #define SerialPort Serial
53 | #else
54 | #define DEV_I2C Wire //Or Wire
55 | #define SerialPort Serial
56 | #endif
57 |
58 | // Components.
59 | LSM6DS3Sensor AccGyr(&DEV_I2C, LSM6DS3_ACC_GYRO_I2C_ADDRESS_LOW);
60 |
61 | //Interrupts.
62 | volatile int mems_event = 0;
63 |
64 | void INT1Event_cb();
65 |
66 | void setup() {
67 | // Led.
68 | pinMode(13, OUTPUT);
69 |
70 | // Initialize serial for output.
71 | SerialPort.begin(9600);
72 |
73 | // Initialize I2C bus.
74 | DEV_I2C.begin();
75 |
76 | //Interrupts.
77 | attachInterrupt(A2, INT1Event_cb, RISING);
78 |
79 | // Initialize Components.
80 | AccGyr.begin();
81 | AccGyr.Enable_X();
82 |
83 | // Enable Double Tap Detection.
84 | AccGyr.Enable_Double_Tap_Detection();
85 | }
86 |
87 | void loop() {
88 | if (mems_event) {
89 | mems_event = 0;
90 | LSM6DS3_Event_Status_t status;
91 | AccGyr.Get_Event_Status(&status);
92 | if (status.DoubleTapStatus)
93 | {
94 | // Led blinking.
95 | digitalWrite(13, HIGH);
96 | delay(100);
97 | digitalWrite(13, LOW);
98 | delay(100);
99 | digitalWrite(13, HIGH);
100 | delay(100);
101 | digitalWrite(13, LOW);
102 |
103 | // Output data.
104 | SerialPort.println("Double Tap Detected!");
105 | }
106 | }
107 | }
108 |
109 | void INT1Event_cb()
110 | {
111 | mems_event = 1;
112 | }
113 |
--------------------------------------------------------------------------------
/examples/X_NUCLEO_IKS01A1_FreeFall/X_NUCLEO_IKS01A1_FreeFall.ino:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file X_NUCLEO_IKS01A1_FreeFall.ino
4 | * @author AST
5 | * @version V1.0.0
6 | * @date 7 September 2017
7 | * @brief Arduino test application for the STMicrolectronics X-NUCLEO-IKS01A1
8 | * MEMS Inertial and Environmental sensor expansion board.
9 | * This application detects free fall through the LSM6DS3 sensor.
10 | * This application makes use of C++ classes obtained from the C
11 | * components' drivers.
12 | ******************************************************************************
13 | * @attention
14 | *
15 | * © COPYRIGHT(c) 2017 STMicroelectronics
16 | *
17 | * Redistribution and use in source and binary forms, with or without modification,
18 | * are permitted provided that the following conditions are met:
19 | * 1. Redistributions of source code must retain the above copyright notice,
20 | * this list of conditions and the following disclaimer.
21 | * 2. Redistributions in binary form must reproduce the above copyright notice,
22 | * this list of conditions and the following disclaimer in the documentation
23 | * and/or other materials provided with the distribution.
24 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
25 | * may be used to endorse or promote products derived from this software
26 | * without specific prior written permission.
27 | *
28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 | *
39 | ******************************************************************************
40 | */
41 |
42 | /** NOTE
43 | In order to use this sketch on X-NUCLEO-IKS01A1 board you need the STEVAL-MKI160V1
44 | board attached to X-NUCLEO-IKS01A1 board via DIL24 interface.
45 | */
46 |
47 | // Includes.
48 | #include
49 |
50 | #if defined(ARDUINO_SAM_DUE)
51 | #define DEV_I2C Wire1 //Define which I2C bus is used. Wire1 for the Arduino Due
52 | #define SerialPort Serial
53 | #else
54 | #define DEV_I2C Wire //Or Wire
55 | #define SerialPort Serial
56 | #endif
57 |
58 | // Components.
59 | LSM6DS3Sensor AccGyr(&DEV_I2C, LSM6DS3_ACC_GYRO_I2C_ADDRESS_LOW);
60 |
61 | //Interrupts.
62 | volatile int mems_event = 0;
63 |
64 | void INT1Event_cb();
65 |
66 | void setup() {
67 | // Led.
68 | pinMode(13, OUTPUT);
69 |
70 | // Initialize serial for output.
71 | SerialPort.begin(9600);
72 |
73 | // Initialize I2C bus.
74 | DEV_I2C.begin();
75 |
76 | //Interrupts.
77 | attachInterrupt(A2, INT1Event_cb, RISING);
78 |
79 | // Initialize Components.
80 | AccGyr.begin();
81 | AccGyr.Enable_X();
82 |
83 | // Enable Free Fall Detection.
84 | AccGyr.Enable_Free_Fall_Detection();
85 | }
86 |
87 | void loop() {
88 | if (mems_event) {
89 | mems_event = 0;
90 | LSM6DS3_Event_Status_t status;
91 | AccGyr.Get_Event_Status(&status);
92 | if (status.FreeFallStatus)
93 | {
94 | // Led blinking.
95 | digitalWrite(13, HIGH);
96 | delay(200);
97 | digitalWrite(13, LOW);
98 |
99 | // Output data.
100 | SerialPort.println("Free Fall Detected!");
101 | }
102 | }
103 | }
104 |
105 | void INT1Event_cb()
106 | {
107 | mems_event = 1;
108 | }
109 |
--------------------------------------------------------------------------------
/examples/X_NUCLEO_IKS01A1_LSM6DS3_DataLog_Terminal/X_NUCLEO_IKS01A1_LSM6DS3_DataLog_Terminal.ino:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file X_NUCLEO_IKS01A1_LSM6DS3_DataLog_Terminal.ino
4 | * @author AST
5 | * @version V1.0.0
6 | * @date 7 September 2017
7 | * @brief Arduino test application for the STMicrolectronics X-NUCLEO-IKS01A1
8 | * MEMS Inertial and Environmental sensor expansion board.
9 | * This application makes use of C++ classes obtained from the C
10 | * components' drivers.
11 | ******************************************************************************
12 | * @attention
13 | *
14 | * © COPYRIGHT(c) 2017 STMicroelectronics
15 | *
16 | * Redistribution and use in source and binary forms, with or without modification,
17 | * are permitted provided that the following conditions are met:
18 | * 1. Redistributions of source code must retain the above copyright notice,
19 | * this list of conditions and the following disclaimer.
20 | * 2. Redistributions in binary form must reproduce the above copyright notice,
21 | * this list of conditions and the following disclaimer in the documentation
22 | * and/or other materials provided with the distribution.
23 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
24 | * may be used to endorse or promote products derived from this software
25 | * without specific prior written permission.
26 | *
27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 | *
38 | ******************************************************************************
39 | */
40 |
41 | /** NOTE
42 | In order to use this sketch on X-NUCLEO-IKS01A1 board you need the STEVAL-MKI160V1
43 | board attached to X-NUCLEO-IKS01A1 board via DIL24 interface.
44 | */
45 |
46 | // Includes.
47 | #include
48 |
49 | #if defined(ARDUINO_SAM_DUE)
50 | #define DEV_I2C Wire1 //Define which I2C bus is used. Wire1 for the Arduino Due
51 | #define SerialPort Serial
52 | #else
53 | #define DEV_I2C Wire //Or Wire
54 | #define SerialPort Serial
55 | #endif
56 |
57 | // Components.
58 | LSM6DS3Sensor AccGyr(&DEV_I2C, LSM6DS3_ACC_GYRO_I2C_ADDRESS_LOW);
59 |
60 | void setup() {
61 | // Led.
62 | pinMode(13, OUTPUT);
63 |
64 | // Initialize serial for output.
65 | SerialPort.begin(9600);
66 |
67 | // Initialize I2C bus.
68 | DEV_I2C.begin();
69 |
70 | // Initialize components.
71 | AccGyr.begin();
72 | AccGyr.Enable_X();
73 | AccGyr.Enable_G();
74 | }
75 |
76 | void loop() {
77 | // Led blinking.
78 | digitalWrite(13, HIGH);
79 | delay(250);
80 | digitalWrite(13, LOW);
81 | delay(250);
82 |
83 | // Read accelerometer and gyroscope.
84 | int32_t accelerometer[3];
85 | int32_t gyroscope[3];
86 | AccGyr.Get_X_Axes(accelerometer);
87 | AccGyr.Get_G_Axes(gyroscope);
88 |
89 | // Output data.
90 | SerialPort.print("| Acc[mg]: ");
91 | SerialPort.print(accelerometer[0]);
92 | SerialPort.print(" ");
93 | SerialPort.print(accelerometer[1]);
94 | SerialPort.print(" ");
95 | SerialPort.print(accelerometer[2]);
96 | SerialPort.print(" | Gyr[mdps]: ");
97 | SerialPort.print(gyroscope[0]);
98 | SerialPort.print(" ");
99 | SerialPort.print(gyroscope[1]);
100 | SerialPort.print(" ");
101 | SerialPort.print(gyroscope[2]);
102 | SerialPort.println(" |");
103 | }
104 |
--------------------------------------------------------------------------------
/examples/X_NUCLEO_IKS01A1_MultiEvent/X_NUCLEO_IKS01A1_MultiEvent.ino:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file X_NUCLEO_IKS01A1_MultiEvent.ino
4 | * @author AST
5 | * @version V1.0.0
6 | * @date 7 September 2017
7 | * @brief Arduino test application for the STMicrolectronics X-NUCLEO-IKS01A1
8 | * MEMS Inertial and Environmental sensor expansion board.
9 | * This application detects free fall, tap, double tap, tilt, wake up,
10 | * 6D Orientation and step events through the LSM6DS3 sensor.
11 | * This application makes use of C++ classes obtained from the C
12 | * components' drivers.
13 | ******************************************************************************
14 | * @attention
15 | *
16 | * © COPYRIGHT(c) 2017 STMicroelectronics
17 | *
18 | * Redistribution and use in source and binary forms, with or without modification,
19 | * are permitted provided that the following conditions are met:
20 | * 1. Redistributions of source code must retain the above copyright notice,
21 | * this list of conditions and the following disclaimer.
22 | * 2. Redistributions in binary form must reproduce the above copyright notice,
23 | * this list of conditions and the following disclaimer in the documentation
24 | * and/or other materials provided with the distribution.
25 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
26 | * may be used to endorse or promote products derived from this software
27 | * without specific prior written permission.
28 | *
29 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
30 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
33 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 | *
40 | ******************************************************************************
41 | */
42 |
43 | /** NOTE
44 | In order to use this sketch on X-NUCLEO-IKS01A1 board you need the STEVAL-MKI160V1
45 | board attached to X-NUCLEO-IKS01A1 board via DIL24 interface.
46 | */
47 |
48 | // Includes.
49 | #include
50 |
51 | #if defined(ARDUINO_SAM_DUE)
52 | #define DEV_I2C Wire1 //Define which I2C bus is used. Wire1 for the Arduino Due
53 | #define SerialPort Serial
54 | #else
55 | #define DEV_I2C Wire //Or Wire
56 | #define SerialPort Serial
57 | #endif
58 |
59 | // Components.
60 | LSM6DS3Sensor AccGyr(&DEV_I2C, LSM6DS3_ACC_GYRO_I2C_ADDRESS_LOW);
61 |
62 | //Interrupts.
63 | volatile int mems_event = 0;
64 |
65 | uint16_t step_count = 0;
66 | char report[256];
67 |
68 | void INT1Event_cb();
69 | void INT2Event_cb();
70 | void sendOrientation();
71 |
72 | void setup() {
73 | // Led.
74 | pinMode(13, OUTPUT);
75 |
76 | // Initialize serial for output.
77 | SerialPort.begin(9600);
78 |
79 | // Initialize I2C bus.
80 | DEV_I2C.begin();
81 |
82 | //Interrupts.
83 | attachInterrupt(A2, INT1Event_cb, RISING);
84 | attachInterrupt(A3, INT2Event_cb, RISING);
85 |
86 | // Initialize Components.
87 | AccGyr.begin();
88 | AccGyr.Enable_X();
89 |
90 | // Enable all HW events.
91 | AccGyr.Enable_Pedometer();
92 | AccGyr.Enable_Tilt_Detection();
93 | AccGyr.Enable_Free_Fall_Detection();
94 | AccGyr.Enable_Single_Tap_Detection();
95 | AccGyr.Enable_Double_Tap_Detection();
96 | AccGyr.Enable_6D_Orientation();
97 | AccGyr.Enable_Wake_Up_Detection();
98 | }
99 |
100 | void loop() {
101 | if (mems_event)
102 | {
103 | mems_event = 0;
104 | LSM6DS3_Event_Status_t status;
105 | AccGyr.Get_Event_Status(&status);
106 |
107 | if (status.StepStatus)
108 | {
109 | // New step detected, so print the step counter
110 | AccGyr.Get_Step_Counter(&step_count);
111 | snprintf(report, sizeof(report), "Step counter: %d", step_count);
112 | SerialPort.println(report);
113 | }
114 |
115 | if (status.FreeFallStatus)
116 | {
117 | // Output data.
118 | SerialPort.println("Free Fall Detected!");
119 | }
120 |
121 | if (status.TapStatus)
122 | {
123 | // Output data.
124 | SerialPort.println("Single Tap Detected!");
125 | }
126 |
127 | if (status.DoubleTapStatus)
128 | {
129 | // Output data.
130 | SerialPort.println("Double Tap Detected!");
131 | }
132 |
133 | if (status.TiltStatus)
134 | {
135 | // Output data.
136 | SerialPort.println("Tilt Detected!");
137 | }
138 |
139 | if (status.D6DOrientationStatus)
140 | {
141 | // Send 6D Orientation
142 | sendOrientation();
143 | }
144 |
145 | if (status.WakeUpStatus)
146 | {
147 | // Output data.
148 | SerialPort.println("Wake up Detected!");
149 | }
150 | }
151 | }
152 |
153 | void INT1Event_cb()
154 | {
155 | mems_event = 1;
156 | }
157 |
158 | void INT2Event_cb()
159 | {
160 | mems_event = 1;
161 | }
162 |
163 | void sendOrientation()
164 | {
165 | uint8_t xl = 0;
166 | uint8_t xh = 0;
167 | uint8_t yl = 0;
168 | uint8_t yh = 0;
169 | uint8_t zl = 0;
170 | uint8_t zh = 0;
171 |
172 | AccGyr.Get_6D_Orientation_XL(&xl);
173 | AccGyr.Get_6D_Orientation_XH(&xh);
174 | AccGyr.Get_6D_Orientation_YL(&yl);
175 | AccGyr.Get_6D_Orientation_YH(&yh);
176 | AccGyr.Get_6D_Orientation_ZL(&zl);
177 | AccGyr.Get_6D_Orientation_ZH(&zh);
178 |
179 | if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 1 && zh == 0 )
180 | {
181 | sprintf( report, "\r\n ________________ " \
182 | "\r\n | | " \
183 | "\r\n | * | " \
184 | "\r\n | | " \
185 | "\r\n | | " \
186 | "\r\n | | " \
187 | "\r\n | | " \
188 | "\r\n |________________| \r\n" );
189 | }
190 |
191 | else if ( xl == 1 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 0 )
192 | {
193 | sprintf( report, "\r\n ________________ " \
194 | "\r\n | | " \
195 | "\r\n | * | " \
196 | "\r\n | | " \
197 | "\r\n | | " \
198 | "\r\n | | " \
199 | "\r\n | | " \
200 | "\r\n |________________| \r\n" );
201 | }
202 |
203 | else if ( xl == 0 && yl == 0 && zl == 0 && xh == 1 && yh == 0 && zh == 0 )
204 | {
205 | sprintf( report, "\r\n ________________ " \
206 | "\r\n | | " \
207 | "\r\n | | " \
208 | "\r\n | | " \
209 | "\r\n | | " \
210 | "\r\n | | " \
211 | "\r\n | * | " \
212 | "\r\n |________________| \r\n" );
213 | }
214 |
215 | else if ( xl == 0 && yl == 1 && zl == 0 && xh == 0 && yh == 0 && zh == 0 )
216 | {
217 | sprintf( report, "\r\n ________________ " \
218 | "\r\n | | " \
219 | "\r\n | | " \
220 | "\r\n | | " \
221 | "\r\n | | " \
222 | "\r\n | | " \
223 | "\r\n | * | " \
224 | "\r\n |________________| \r\n" );
225 | }
226 |
227 | else if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 1 )
228 | {
229 | sprintf( report, "\r\n __*_____________ " \
230 | "\r\n |________________| \r\n" );
231 | }
232 |
233 | else if ( xl == 0 && yl == 0 && zl == 1 && xh == 0 && yh == 0 && zh == 0 )
234 | {
235 | sprintf( report, "\r\n ________________ " \
236 | "\r\n |________________| " \
237 | "\r\n * \r\n" );
238 | }
239 |
240 | else
241 | {
242 | sprintf( report, "None of the 6D orientation axes is set in LSM6DSL - accelerometer.\r\n" );
243 | }
244 |
245 | SerialPort.print(report);
246 | }
247 |
--------------------------------------------------------------------------------
/examples/X_NUCLEO_IKS01A1_Pedometer/X_NUCLEO_IKS01A1_Pedometer.ino:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file X_NUCLEO_IKS01A1_Pedometer.ino
4 | * @author AST
5 | * @version V1.0.0
6 | * @date 7 September 2017
7 | * @brief Arduino test application for the STMicrolectronics X-NUCLEO-IKS01A1
8 | * MEMS Inertial and Environmental sensor expansion board.
9 | * This application detects step event through the LSM6DS3 sensor.
10 | * This application makes use of C++ classes obtained from the C
11 | * components' drivers.
12 | ******************************************************************************
13 | * @attention
14 | *
15 | * © COPYRIGHT(c) 2017 STMicroelectronics
16 | *
17 | * Redistribution and use in source and binary forms, with or without modification,
18 | * are permitted provided that the following conditions are met:
19 | * 1. Redistributions of source code must retain the above copyright notice,
20 | * this list of conditions and the following disclaimer.
21 | * 2. Redistributions in binary form must reproduce the above copyright notice,
22 | * this list of conditions and the following disclaimer in the documentation
23 | * and/or other materials provided with the distribution.
24 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
25 | * may be used to endorse or promote products derived from this software
26 | * without specific prior written permission.
27 | *
28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 | *
39 | ******************************************************************************
40 | */
41 |
42 | /** NOTE
43 | In order to use this sketch on X-NUCLEO-IKS01A1 board you need the STEVAL-MKI160V1
44 | board attached to X-NUCLEO-IKS01A1 board via DIL24 interface.
45 | */
46 |
47 | // Includes.
48 | #include
49 |
50 | #if defined(ARDUINO_SAM_DUE)
51 | #define DEV_I2C Wire1 //Define which I2C bus is used. Wire1 for the Arduino Due
52 | #define SerialPort Serial
53 | #else
54 | #define DEV_I2C Wire //Or Wire
55 | #define SerialPort Serial
56 | #endif
57 |
58 | // Components.
59 | LSM6DS3Sensor AccGyr(&DEV_I2C, LSM6DS3_ACC_GYRO_I2C_ADDRESS_LOW);
60 |
61 | //Interrupts.
62 | volatile int mems_event = 0;
63 |
64 | uint32_t previous_tick = 0;
65 | uint32_t current_tick = 0;
66 | uint16_t step_count = 0;
67 | char report[256];
68 |
69 | void INT1Event_cb();
70 |
71 | void setup() {
72 | // Led.
73 | pinMode(13, OUTPUT);
74 |
75 | // Initialize serial for output.
76 | SerialPort.begin(9600);
77 |
78 | // Initialize I2C bus.
79 | DEV_I2C.begin();
80 |
81 | //Interrupts.
82 | attachInterrupt(A2, INT1Event_cb, RISING);
83 |
84 | // Initialize Components.
85 | AccGyr.begin();
86 | AccGyr.Enable_X();
87 |
88 | // Enable Pedometer.
89 | AccGyr.Enable_Pedometer();
90 |
91 | previous_tick = millis();
92 | }
93 |
94 | void loop() {
95 | if (mems_event)
96 | {
97 | mems_event = 0;
98 | LSM6DS3_Event_Status_t status;
99 | AccGyr.Get_Event_Status(&status);
100 | if (status.StepStatus)
101 | {
102 | // New step detected, so print the step counter
103 | AccGyr.Get_Step_Counter(&step_count);
104 | snprintf(report, sizeof(report), "Step counter: %d", step_count);
105 | SerialPort.println(report);
106 |
107 | // Led blinking.
108 | digitalWrite(13, HIGH);
109 | delay(100);
110 | digitalWrite(13, LOW);
111 | }
112 | }
113 |
114 | // Print the step counter in any case every 3000 ms
115 | current_tick = millis();
116 | if((current_tick - previous_tick) >= 3000)
117 | {
118 | AccGyr.Get_Step_Counter(&step_count);
119 | snprintf(report, sizeof(report), "Step counter: %d", step_count);
120 | SerialPort.println(report);
121 | previous_tick = millis();
122 | }
123 | }
124 |
125 | void INT1Event_cb()
126 | {
127 | mems_event = 1;
128 | }
129 |
--------------------------------------------------------------------------------
/examples/X_NUCLEO_IKS01A1_Tap/X_NUCLEO_IKS01A1_Tap.ino:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file X_NUCLEO_IKS01A1_Tap.ino
4 | * @author AST
5 | * @version V1.0.0
6 | * @date 7 September 2017
7 | * @brief Arduino test application for the STMicrolectronics X-NUCLEO-IKS01A1
8 | * MEMS Inertial and Environmental sensor expansion board.
9 | * This application detects tap event through the LSM6DS3 sensor.
10 | * This application makes use of C++ classes obtained from the C
11 | * components' drivers.
12 | ******************************************************************************
13 | * @attention
14 | *
15 | * © COPYRIGHT(c) 2017 STMicroelectronics
16 | *
17 | * Redistribution and use in source and binary forms, with or without modification,
18 | * are permitted provided that the following conditions are met:
19 | * 1. Redistributions of source code must retain the above copyright notice,
20 | * this list of conditions and the following disclaimer.
21 | * 2. Redistributions in binary form must reproduce the above copyright notice,
22 | * this list of conditions and the following disclaimer in the documentation
23 | * and/or other materials provided with the distribution.
24 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
25 | * may be used to endorse or promote products derived from this software
26 | * without specific prior written permission.
27 | *
28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 | *
39 | ******************************************************************************
40 | */
41 |
42 | /** NOTE
43 | In order to use this sketch on X-NUCLEO-IKS01A1 board you need the STEVAL-MKI160V1
44 | board attached to X-NUCLEO-IKS01A1 board via DIL24 interface.
45 | */
46 |
47 | // Includes.
48 | #include
49 |
50 | #if defined(ARDUINO_SAM_DUE)
51 | #define DEV_I2C Wire1 //Define which I2C bus is used. Wire1 for the Arduino Due
52 | #define SerialPort Serial
53 | #else
54 | #define DEV_I2C Wire //Or Wire
55 | #define SerialPort Serial
56 | #endif
57 |
58 | // Components.
59 | LSM6DS3Sensor AccGyr(&DEV_I2C, LSM6DS3_ACC_GYRO_I2C_ADDRESS_LOW);
60 |
61 | //Interrupts.
62 | volatile int mems_event = 0;
63 |
64 | void INT1Event_cb();
65 |
66 | void setup() {
67 | // Led.
68 | pinMode(13, OUTPUT);
69 |
70 | // Initialize serial for output.
71 | SerialPort.begin(9600);
72 |
73 | // Initialize I2C bus.
74 | DEV_I2C.begin();
75 |
76 | //Interrupts.
77 | attachInterrupt(A2, INT1Event_cb, RISING);
78 |
79 | // Initialize Components.
80 | AccGyr.begin();
81 | AccGyr.Enable_X();
82 |
83 | // Enable Single Tap Detection.
84 | AccGyr.Enable_Single_Tap_Detection();
85 | }
86 |
87 | void loop() {
88 | if (mems_event) {
89 | mems_event = 0;
90 | LSM6DS3_Event_Status_t status;
91 | AccGyr.Get_Event_Status(&status);
92 | if (status.TapStatus)
93 | {
94 | // Led blinking.
95 | digitalWrite(13, HIGH);
96 | delay(100);
97 | digitalWrite(13, LOW);
98 |
99 | // Output data.
100 | SerialPort.println("Single Tap Detected!");
101 | }
102 | }
103 | }
104 |
105 | void INT1Event_cb()
106 | {
107 | mems_event = 1;
108 | }
109 |
--------------------------------------------------------------------------------
/examples/X_NUCLEO_IKS01A1_Tilt/X_NUCLEO_IKS01A1_Tilt.ino:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file X_NUCLEO_IKS01A1_Tilt.ino
4 | * @author AST
5 | * @version V1.0.0
6 | * @date 7 September 2017
7 | * @brief Arduino test application for the STMicrolectronics X-NUCLEO-IKS01A1
8 | * MEMS Inertial and Environmental sensor expansion board.
9 | * This application detects tilt event through the LSM6DS3 sensor.
10 | * This application makes use of C++ classes obtained from the C
11 | * components' drivers.
12 | ******************************************************************************
13 | * @attention
14 | *
15 | * © COPYRIGHT(c) 2017 STMicroelectronics
16 | *
17 | * Redistribution and use in source and binary forms, with or without modification,
18 | * are permitted provided that the following conditions are met:
19 | * 1. Redistributions of source code must retain the above copyright notice,
20 | * this list of conditions and the following disclaimer.
21 | * 2. Redistributions in binary form must reproduce the above copyright notice,
22 | * this list of conditions and the following disclaimer in the documentation
23 | * and/or other materials provided with the distribution.
24 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
25 | * may be used to endorse or promote products derived from this software
26 | * without specific prior written permission.
27 | *
28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 | *
39 | ******************************************************************************
40 | */
41 |
42 | /** NOTE
43 | In order to use this sketch on X-NUCLEO-IKS01A1 board you need the STEVAL-MKI160V1
44 | board attached to X-NUCLEO-IKS01A1 board via DIL24 interface.
45 | */
46 |
47 | // Includes.
48 | #include
49 |
50 | #if defined(ARDUINO_SAM_DUE)
51 | #define DEV_I2C Wire1 //Define which I2C bus is used. Wire1 for the Arduino Due
52 | #define SerialPort Serial
53 | #else
54 | #define DEV_I2C Wire //Or Wire
55 | #define SerialPort Serial
56 | #endif
57 |
58 | // Components.
59 | LSM6DS3Sensor AccGyr(&DEV_I2C, LSM6DS3_ACC_GYRO_I2C_ADDRESS_LOW);
60 |
61 | //Interrupts.
62 | volatile int mems_event = 0;
63 |
64 | void INT1Event_cb();
65 |
66 | void setup() {
67 | // Led.
68 | pinMode(13, OUTPUT);
69 |
70 | // Initialize serial for output.
71 | SerialPort.begin(9600);
72 |
73 | // Initialize I2C bus.
74 | DEV_I2C.begin();
75 |
76 | //Interrupts.
77 | attachInterrupt(A2, INT1Event_cb, RISING);
78 |
79 | // Initialize Components.
80 | AccGyr.begin();
81 | AccGyr.Enable_X();
82 |
83 | // Enable Tilt Detection.
84 | AccGyr.Enable_Tilt_Detection();
85 | }
86 |
87 | void loop() {
88 | if (mems_event) {
89 | mems_event = 0;
90 | LSM6DS3_Event_Status_t status;
91 | AccGyr.Get_Event_Status(&status);
92 | if (status.TiltStatus)
93 | {
94 | // Led blinking.
95 | digitalWrite(13, HIGH);
96 | delay(100);
97 | digitalWrite(13, LOW);
98 |
99 | // Output data.
100 | SerialPort.println("Tilt Detected!");
101 | }
102 | }
103 | }
104 |
105 | void INT1Event_cb()
106 | {
107 | mems_event = 1;
108 | }
109 |
--------------------------------------------------------------------------------
/examples/X_NUCLEO_IKS01A1_WakeUp/X_NUCLEO_IKS01A1_WakeUp.ino:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file X_NUCLEO_IKS01A1_WakeUp.ino
4 | * @author AST
5 | * @version V1.0.0
6 | * @date 7 September 2017
7 | * @brief Arduino test application for the STMicrolectronics X-NUCLEO-IKS01A1
8 | * MEMS Inertial and Environmental sensor expansion board.
9 | * This application detects wake up event through the LSM6DS3 sensor.
10 | * This application makes use of C++ classes obtained from the C
11 | * components' drivers.
12 | ******************************************************************************
13 | * @attention
14 | *
15 | * © COPYRIGHT(c) 2017 STMicroelectronics
16 | *
17 | * Redistribution and use in source and binary forms, with or without modification,
18 | * are permitted provided that the following conditions are met:
19 | * 1. Redistributions of source code must retain the above copyright notice,
20 | * this list of conditions and the following disclaimer.
21 | * 2. Redistributions in binary form must reproduce the above copyright notice,
22 | * this list of conditions and the following disclaimer in the documentation
23 | * and/or other materials provided with the distribution.
24 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
25 | * may be used to endorse or promote products derived from this software
26 | * without specific prior written permission.
27 | *
28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 | *
39 | ******************************************************************************
40 | */
41 |
42 | /** NOTE
43 | In order to use this sketch on X-NUCLEO-IKS01A1 board you need the STEVAL-MKI160V1
44 | board attached to X-NUCLEO-IKS01A1 board via DIL24 interface.
45 | */
46 |
47 | // Includes.
48 | #include
49 |
50 | #if defined(ARDUINO_SAM_DUE)
51 | #define DEV_I2C Wire1 //Define which I2C bus is used. Wire1 for the Arduino Due
52 | #define SerialPort Serial
53 | #else
54 | #define DEV_I2C Wire //Or Wire
55 | #define SerialPort Serial
56 | #endif
57 |
58 | // Components.
59 | LSM6DS3Sensor AccGyr(&DEV_I2C, LSM6DS3_ACC_GYRO_I2C_ADDRESS_LOW);
60 |
61 | //Interrupts.
62 | volatile int mems_event = 0;
63 |
64 | void INT2Event_cb();
65 |
66 | void setup() {
67 | // Led.
68 | pinMode(13, OUTPUT);
69 |
70 | // Initialize serial for output.
71 | SerialPort.begin(9600);
72 |
73 | // Initialize I2C bus.
74 | DEV_I2C.begin();
75 |
76 | //Interrupts.
77 | attachInterrupt(A3, INT2Event_cb, RISING);
78 |
79 | // Initialize Components.
80 | AccGyr.begin();
81 | AccGyr.Enable_X();
82 |
83 | // Enable Wake Up Detection.
84 | AccGyr.Enable_Wake_Up_Detection();
85 | }
86 |
87 | void loop() {
88 | if (mems_event) {
89 | mems_event = 0;
90 | LSM6DS3_Event_Status_t status;
91 | AccGyr.Get_Event_Status(&status);
92 | if (status.WakeUpStatus)
93 | {
94 | // Led blinking.
95 | digitalWrite(13, HIGH);
96 | delay(100);
97 | digitalWrite(13, LOW);
98 |
99 | // Output data.
100 | SerialPort.println("Wake up Detected!");
101 | }
102 | }
103 | }
104 |
105 | void INT2Event_cb()
106 | {
107 | mems_event = 1;
108 | }
109 |
--------------------------------------------------------------------------------
/keywords.txt:
--------------------------------------------------------------------------------
1 | #######################################
2 | # Syntax Coloring Map For LSM6DS3
3 | #######################################
4 |
5 | #######################################
6 | # Datatypes (KEYWORD1)
7 | #######################################
8 |
9 | LSM6DS3Sensor KEYWORD1
10 |
11 | #######################################
12 | # Methods and Functions (KEYWORD2)
13 | #######################################
14 |
15 | begin KEYWORD2
16 | end KEYWORD2
17 | Enable_X KEYWORD2
18 | Enable_G KEYWORD2
19 | Disable_X KEYWORD2
20 | Disable_G KEYWORD2
21 | ReadID KEYWORD2
22 | Get_X_Axes KEYWORD2
23 | Get_G_Axes KEYWORD2
24 | Get_X_Sensitivity KEYWORD2
25 | Get_G_Sensitivity KEYWORD2
26 | Get_X_AxesRaw KEYWORD2
27 | Get_G_AxesRaw KEYWORD2
28 | Get_X_ODR KEYWORD2
29 | Get_G_ODR KEYWORD2
30 | Set_X_ODR KEYWORD2
31 | Set_G_ODR KEYWORD2
32 | Get_X_FS KEYWORD2
33 | Get_G_FS KEYWORD2
34 | Set_X_FS KEYWORD2
35 | Set_G_FS KEYWORD2
36 | Enable_Free_Fall_Detection KEYWORD2
37 | Disable_Free_Fall_Detection KEYWORD2
38 | Set_Free_Fall_Threshold KEYWORD2
39 | Enable_Pedometer KEYWORD2
40 | Disable_Pedometer KEYWORD2
41 | Get_Step_Counter KEYWORD2
42 | Reset_Step_Counter KEYWORD2
43 | Set_Pedometer_Threshold KEYWORD2
44 | Enable_Tilt_Detection KEYWORD2
45 | Disable_Tilt_Detection KEYWORD2
46 | Enable_Wake_Up_Detection KEYWORD2
47 | Disable_Wake_Up_Detection KEYWORD2
48 | Set_Wake_Up_Threshold KEYWORD2
49 | Enable_Single_Tap_Detection KEYWORD2
50 | Disable_Single_Tap_Detection KEYWORD2
51 | Enable_Double_Tap_Detection KEYWORD2
52 | Disable_Double_Tap_Detection KEYWORD2
53 | Set_Tap_Threshold KEYWORD2
54 | Set_Tap_Shock_Time KEYWORD2
55 | Set_Tap_Quiet_Time KEYWORD2
56 | Set_Tap_Duration_Time KEYWORD2
57 | Enable_6D_Orientation KEYWORD2
58 | Disable_6D_Orientation KEYWORD2
59 | Get_6D_Orientation_XL KEYWORD2
60 | Get_6D_Orientation_XH KEYWORD2
61 | Get_6D_Orientation_YL KEYWORD2
62 | Get_6D_Orientation_YH KEYWORD2
63 | Get_6D_Orientation_ZL KEYWORD2
64 | Get_6D_Orientation_ZH KEYWORD2
65 | Get_Event_Status KEYWORD2
66 | ReadReg KEYWORD2
67 | WriteReg KEYWORD2
68 |
69 | #######################################
70 | # Constants (LITERAL1)
71 | #######################################
72 |
73 | LSM6DS3_ACC_SENSITIVITY_FOR_FS_2G LITERAL1
74 | LSM6DS3_ACC_SENSITIVITY_FOR_FS_4G LITERAL1
75 | LSM6DS3_ACC_SENSITIVITY_FOR_FS_8G LITERAL1
76 | LSM6DS3_ACC_SENSITIVITY_FOR_FS_16G LITERAL1
77 | LSM6DS3_GYRO_SENSITIVITY_FOR_FS_125DPS LITERAL1
78 | LSM6DS3_GYRO_SENSITIVITY_FOR_FS_245DPS LITERAL1
79 | LSM6DS3_GYRO_SENSITIVITY_FOR_FS_500DPS LITERAL1
80 | LSM6DS3_GYRO_SENSITIVITY_FOR_FS_1000DPS LITERAL1
81 | LSM6DS3_GYRO_SENSITIVITY_FOR_FS_2000DPS LITERAL1
82 | LSM6DS3_PEDOMETER_THRESHOLD_LOW LITERAL1
83 | LSM6DS3_PEDOMETER_THRESHOLD_MID_LOW LITERAL1
84 | LSM6DS3_PEDOMETER_THRESHOLD_MID LITERAL1
85 | LSM6DS3_PEDOMETER_THRESHOLD_MID_HIGH LITERAL1
86 | LSM6DS3_PEDOMETER_THRESHOLD_HIGH LITERAL1
87 | LSM6DS3_WAKE_UP_THRESHOLD_LOW LITERAL1
88 | LSM6DS3_WAKE_UP_THRESHOLD_MID_LOW LITERAL1
89 | LSM6DS3_WAKE_UP_THRESHOLD_MID LITERAL1
90 | LSM6DS3_WAKE_UP_THRESHOLD_MID_HIGH LITERAL1
91 | LSM6DS3_WAKE_UP_THRESHOLD_HIGH LITERAL1
92 | LSM6DS3_TAP_THRESHOLD_LOW LITERAL1
93 | LSM6DS3_TAP_THRESHOLD_MID_LOW LITERAL1
94 | LSM6DS3_TAP_THRESHOLD_MID LITERAL1
95 | LSM6DS3_TAP_THRESHOLD_MID_HIGH LITERAL1
96 | LSM6DS3_TAP_THRESHOLD_HIGH LITERAL1
97 | LSM6DS3_TAP_SHOCK_TIME_LOW LITERAL1
98 | LSM6DS3_TAP_SHOCK_TIME_MID_LOW LITERAL1
99 | LSM6DS3_TAP_SHOCK_TIME_MID_HIGH LITERAL1
100 | LSM6DS3_TAP_SHOCK_TIME_HIGH LITERAL1
101 | LSM6DS3_TAP_QUIET_TIME_LOW LITERAL1
102 | LSM6DS3_TAP_QUIET_TIME_MID_LOW LITERAL1
103 | LSM6DS3_TAP_QUIET_TIME_MID_HIGH LITERAL1
104 | LSM6DS3_TAP_QUIET_TIME_HIGH LITERAL1
105 | LSM6DS3_TAP_DURATION_LOW LITERAL1
106 | LSM6DS3_TAP_DURATION_MID_LOW LITERAL1
107 | LSM6DS3_TAP_DURATION_MID LITERAL1
108 | LSM6DS3_TAP_DURATION_MID_HIGH LITERAL1
109 | LSM6DS3_TAP_DURATION_HIGH LITERAL1
110 |
--------------------------------------------------------------------------------
/library.properties:
--------------------------------------------------------------------------------
1 | name=STM32duino LSM6DS3
2 | version=2.0.0
3 | author=AST
4 | maintainer=stm32duino
5 | sentence=3D accelerometer and 3D gyroscope.
6 | paragraph=This library provides Arduino support for the 3D accelerometer and 3D gyroscope LSM6DS3 for STM32 boards.
7 | category=Sensors
8 | url=https://github.com/stm32duino/LSM6DS3
9 | architectures=stm32, avr, sam
10 |
--------------------------------------------------------------------------------
/src/LSM6DS3Sensor.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file LSM6DS3Sensor.cpp
4 | * @author AST
5 | * @version V1.0.0
6 | * @date 7 September 2017
7 | * @brief Implementation of an LSM6DS3 Inertial Measurement Unit (IMU) 6 axes
8 | * sensor.
9 | ******************************************************************************
10 | * @attention
11 | *
12 | * © COPYRIGHT(c) 2017 STMicroelectronics
13 | *
14 | * Redistribution and use in source and binary forms, with or without modification,
15 | * are permitted provided that the following conditions are met:
16 | * 1. Redistributions of source code must retain the above copyright notice,
17 | * this list of conditions and the following disclaimer.
18 | * 2. Redistributions in binary form must reproduce the above copyright notice,
19 | * this list of conditions and the following disclaimer in the documentation
20 | * and/or other materials provided with the distribution.
21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
22 | * may be used to endorse or promote products derived from this software
23 | * without specific prior written permission.
24 | *
25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 | *
36 | ******************************************************************************
37 | */
38 |
39 |
40 | /* Includes ------------------------------------------------------------------*/
41 |
42 | #include "Arduino.h"
43 | #include "LSM6DS3Sensor.h"
44 |
45 |
46 | /* Class Implementation ------------------------------------------------------*/
47 | /** Constructor
48 | * @param i2c object of an helper class which handles the I2C peripheral
49 | * @param address the address of the component's instance
50 | */
51 | LSM6DS3Sensor::LSM6DS3Sensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), address(address)
52 | {
53 | dev_spi = NULL;
54 | X_isEnabled = 0;
55 | G_isEnabled = 0;
56 | }
57 |
58 | /** Constructor
59 | * @param spi object of an helper class which handles the SPI peripheral
60 | * @param cs_pin the chip select pin
61 | * @param spi_speed the SPI speed
62 | */
63 | LSM6DS3Sensor::LSM6DS3Sensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : dev_spi(spi), cs_pin(cs_pin), spi_speed(spi_speed)
64 | {
65 | dev_i2c = NULL;
66 | address = 0;
67 | X_isEnabled = 0U;
68 | G_isEnabled = 0U;
69 | }
70 |
71 | /**
72 | * @brief Configure the sensor in order to be used
73 | * @retval 0 in case of success, an error code otherwise
74 | */
75 | LSM6DS3StatusTypeDef LSM6DS3Sensor::begin()
76 | {
77 | if(dev_spi)
78 | {
79 | // Configure CS pin
80 | pinMode(cs_pin, OUTPUT);
81 | digitalWrite(cs_pin, HIGH);
82 | }
83 |
84 | /* Enable register address automatically incremented during a multiple byte
85 | access with a serial interface. */
86 | if ( LSM6DS3_ACC_GYRO_W_IF_Addr_Incr( (void *)this, LSM6DS3_ACC_GYRO_IF_INC_ENABLED ) == MEMS_ERROR )
87 | {
88 | return LSM6DS3_STATUS_ERROR;
89 | }
90 |
91 | /* Enable BDU */
92 | if ( LSM6DS3_ACC_GYRO_W_BDU( (void *)this, LSM6DS3_ACC_GYRO_BDU_BLOCK_UPDATE ) == MEMS_ERROR )
93 | {
94 | return LSM6DS3_STATUS_ERROR;
95 | }
96 |
97 | /* FIFO mode selection */
98 | if ( LSM6DS3_ACC_GYRO_W_FIFO_MODE( (void *)this, LSM6DS3_ACC_GYRO_FIFO_MODE_BYPASS ) == MEMS_ERROR )
99 | {
100 | return LSM6DS3_STATUS_ERROR;
101 | }
102 |
103 | /* Output data rate selection - power down. */
104 | if ( LSM6DS3_ACC_GYRO_W_ODR_XL( (void *)this, LSM6DS3_ACC_GYRO_ODR_XL_POWER_DOWN ) == MEMS_ERROR )
105 | {
106 | return LSM6DS3_STATUS_ERROR;
107 | }
108 |
109 | /* Full scale selection. */
110 | if ( Set_X_FS( 2.0f ) == LSM6DS3_STATUS_ERROR )
111 | {
112 | return LSM6DS3_STATUS_ERROR;
113 | }
114 |
115 | /* Enable axes. */
116 | if ( LSM6DS3_ACC_GYRO_W_XEN_XL( (void *)this, LSM6DS3_ACC_GYRO_XEN_XL_ENABLED ) == MEMS_ERROR )
117 | {
118 | return LSM6DS3_STATUS_ERROR;
119 | }
120 |
121 | if ( LSM6DS3_ACC_GYRO_W_YEN_XL( (void *)this, LSM6DS3_ACC_GYRO_YEN_XL_ENABLED ) == MEMS_ERROR )
122 | {
123 | return LSM6DS3_STATUS_ERROR;
124 | }
125 |
126 | if ( LSM6DS3_ACC_GYRO_W_ZEN_XL( (void *)this, LSM6DS3_ACC_GYRO_ZEN_XL_ENABLED ) == MEMS_ERROR )
127 | {
128 | return LSM6DS3_STATUS_ERROR;
129 | }
130 |
131 | /* Output data rate selection - power down */
132 | if ( LSM6DS3_ACC_GYRO_W_ODR_G( (void *)this, LSM6DS3_ACC_GYRO_ODR_G_POWER_DOWN ) == MEMS_ERROR )
133 | {
134 | return LSM6DS3_STATUS_ERROR;
135 | }
136 |
137 | /* Full scale selection. */
138 | if ( Set_G_FS( 2000.0f ) == LSM6DS3_STATUS_ERROR )
139 | {
140 | return LSM6DS3_STATUS_ERROR;
141 | }
142 |
143 | if ( LSM6DS3_ACC_GYRO_W_XEN_G( (void *)this, LSM6DS3_ACC_GYRO_XEN_G_ENABLED ) == MEMS_ERROR )
144 | {
145 | return LSM6DS3_STATUS_ERROR;
146 | }
147 |
148 | if ( LSM6DS3_ACC_GYRO_W_YEN_G( (void *)this, LSM6DS3_ACC_GYRO_YEN_G_ENABLED ) == MEMS_ERROR )
149 | {
150 | return LSM6DS3_STATUS_ERROR;
151 | }
152 |
153 | if ( LSM6DS3_ACC_GYRO_W_ZEN_G( (void *)this, LSM6DS3_ACC_GYRO_ZEN_G_ENABLED ) == MEMS_ERROR )
154 | {
155 | return LSM6DS3_STATUS_ERROR;
156 | }
157 |
158 | X_Last_ODR = 104.0f;
159 |
160 | X_isEnabled = 0;
161 |
162 | G_Last_ODR = 104.0f;
163 |
164 | G_isEnabled = 0;
165 |
166 | return LSM6DS3_STATUS_OK;
167 | }
168 |
169 | /**
170 | * @brief Disable the sensor and relative resources
171 | * @retval 0 in case of success, an error code otherwise
172 | */
173 | LSM6DS3StatusTypeDef LSM6DS3Sensor::end()
174 | {
175 | /* Disable both acc and gyro */
176 | if (Disable_X() != LSM6DS3_STATUS_OK)
177 | {
178 | return LSM6DS3_STATUS_ERROR;
179 | }
180 |
181 | if (Disable_G() != LSM6DS3_STATUS_OK)
182 | {
183 | return LSM6DS3_STATUS_ERROR;
184 | }
185 |
186 | /* Reset CS configuration */
187 | if(dev_spi)
188 | {
189 | // Configure CS pin
190 | pinMode(cs_pin, INPUT);
191 | }
192 |
193 | return LSM6DS3_STATUS_OK;
194 | }
195 |
196 | /**
197 | * @brief Enable LSM6DS3 Accelerator
198 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
199 | */
200 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Enable_X(void)
201 | {
202 | /* Check if the component is already enabled */
203 | if ( X_isEnabled == 1 )
204 | {
205 | return LSM6DS3_STATUS_OK;
206 | }
207 |
208 | /* Output data rate selection. */
209 | if ( Set_X_ODR_When_Enabled( X_Last_ODR ) == LSM6DS3_STATUS_ERROR )
210 | {
211 | return LSM6DS3_STATUS_ERROR;
212 | }
213 |
214 | X_isEnabled = 1;
215 |
216 | return LSM6DS3_STATUS_OK;
217 | }
218 |
219 | /**
220 | * @brief Enable LSM6DS3 Gyroscope
221 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
222 | */
223 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Enable_G(void)
224 | {
225 | /* Check if the component is already enabled */
226 | if ( G_isEnabled == 1 )
227 | {
228 | return LSM6DS3_STATUS_OK;
229 | }
230 |
231 | /* Output data rate selection. */
232 | if ( Set_G_ODR_When_Enabled( G_Last_ODR ) == LSM6DS3_STATUS_ERROR )
233 | {
234 | return LSM6DS3_STATUS_ERROR;
235 | }
236 |
237 | G_isEnabled = 1;
238 |
239 | return LSM6DS3_STATUS_OK;
240 | }
241 |
242 | /**
243 | * @brief Disable LSM6DS3 Accelerator
244 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
245 | */
246 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Disable_X(void)
247 | {
248 | /* Check if the component is already disabled */
249 | if ( X_isEnabled == 0 )
250 | {
251 | return LSM6DS3_STATUS_OK;
252 | }
253 |
254 | /* Store actual output data rate. */
255 | if ( Get_X_ODR( &X_Last_ODR ) == LSM6DS3_STATUS_ERROR )
256 | {
257 | return LSM6DS3_STATUS_ERROR;
258 | }
259 |
260 | /* Output data rate selection - power down. */
261 | if ( LSM6DS3_ACC_GYRO_W_ODR_XL( (void *)this, LSM6DS3_ACC_GYRO_ODR_XL_POWER_DOWN ) == MEMS_ERROR )
262 | {
263 | return LSM6DS3_STATUS_ERROR;
264 | }
265 |
266 | X_isEnabled = 0;
267 |
268 | return LSM6DS3_STATUS_OK;
269 | }
270 |
271 | /**
272 | * @brief Disable LSM6DS3 Gyroscope
273 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
274 | */
275 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Disable_G(void)
276 | {
277 | /* Check if the component is already disabled */
278 | if ( G_isEnabled == 0 )
279 | {
280 | return LSM6DS3_STATUS_OK;
281 | }
282 |
283 | /* Store actual output data rate. */
284 | if ( Get_G_ODR( &G_Last_ODR ) == LSM6DS3_STATUS_ERROR )
285 | {
286 | return LSM6DS3_STATUS_ERROR;
287 | }
288 |
289 | /* Output data rate selection - power down */
290 | if ( LSM6DS3_ACC_GYRO_W_ODR_G( (void *)this, LSM6DS3_ACC_GYRO_ODR_G_POWER_DOWN ) == MEMS_ERROR )
291 | {
292 | return LSM6DS3_STATUS_ERROR;
293 | }
294 |
295 | G_isEnabled = 0;
296 |
297 | return LSM6DS3_STATUS_OK;
298 | }
299 |
300 | /**
301 | * @brief Read ID of LSM6DS3 Accelerometer and Gyroscope
302 | * @param p_id the pointer where the ID of the device is stored
303 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
304 | */
305 | LSM6DS3StatusTypeDef LSM6DS3Sensor::ReadID(uint8_t *p_id)
306 | {
307 | if(!p_id)
308 | {
309 | return LSM6DS3_STATUS_ERROR;
310 | }
311 |
312 | /* Read WHO AM I register */
313 | if ( LSM6DS3_ACC_GYRO_R_WHO_AM_I( (void *)this, p_id ) == MEMS_ERROR )
314 | {
315 | return LSM6DS3_STATUS_ERROR;
316 | }
317 |
318 | return LSM6DS3_STATUS_OK;
319 | }
320 |
321 | /**
322 | * @brief Read data from LSM6DS3 Accelerometer
323 | * @param pData the pointer where the accelerometer data are stored
324 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
325 | */
326 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Get_X_Axes(int32_t *pData)
327 | {
328 | int16_t dataRaw[3];
329 | float sensitivity = 0;
330 |
331 | /* Read raw data from LSM6DS3 output register. */
332 | if ( Get_X_AxesRaw( dataRaw ) == LSM6DS3_STATUS_ERROR )
333 | {
334 | return LSM6DS3_STATUS_ERROR;
335 | }
336 |
337 | /* Get LSM6DS3 actual sensitivity. */
338 | if ( Get_X_Sensitivity( &sensitivity ) == LSM6DS3_STATUS_ERROR )
339 | {
340 | return LSM6DS3_STATUS_ERROR;
341 | }
342 |
343 | /* Calculate the data. */
344 | pData[0] = ( int32_t )( dataRaw[0] * sensitivity );
345 | pData[1] = ( int32_t )( dataRaw[1] * sensitivity );
346 | pData[2] = ( int32_t )( dataRaw[2] * sensitivity );
347 |
348 | return LSM6DS3_STATUS_OK;
349 | }
350 |
351 | /**
352 | * @brief Read data from LSM6DS3 Gyroscope
353 | * @param pData the pointer where the gyroscope data are stored
354 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
355 | */
356 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Get_G_Axes(int32_t *pData)
357 | {
358 | int16_t dataRaw[3];
359 | float sensitivity = 0;
360 |
361 | /* Read raw data from LSM6DS3 output register. */
362 | if ( Get_G_AxesRaw( dataRaw ) == LSM6DS3_STATUS_ERROR )
363 | {
364 | return LSM6DS3_STATUS_ERROR;
365 | }
366 |
367 | /* Get LSM6DS3 actual sensitivity. */
368 | if ( Get_G_Sensitivity( &sensitivity ) == LSM6DS3_STATUS_ERROR )
369 | {
370 | return LSM6DS3_STATUS_ERROR;
371 | }
372 |
373 | /* Calculate the data. */
374 | pData[0] = ( int32_t )( dataRaw[0] * sensitivity );
375 | pData[1] = ( int32_t )( dataRaw[1] * sensitivity );
376 | pData[2] = ( int32_t )( dataRaw[2] * sensitivity );
377 |
378 | return LSM6DS3_STATUS_OK;
379 | }
380 |
381 | /**
382 | * @brief Read Accelerometer Sensitivity
383 | * @param pfData the pointer where the accelerometer sensitivity is stored
384 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
385 | */
386 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Get_X_Sensitivity(float *pfData)
387 | {
388 | LSM6DS3_ACC_GYRO_FS_XL_t fullScale;
389 |
390 | /* Read actual full scale selection from sensor. */
391 | if ( LSM6DS3_ACC_GYRO_R_FS_XL( (void *)this, &fullScale ) == MEMS_ERROR )
392 | {
393 | return LSM6DS3_STATUS_ERROR;
394 | }
395 |
396 | /* Store the sensitivity based on actual full scale. */
397 | switch( fullScale )
398 | {
399 | case LSM6DS3_ACC_GYRO_FS_XL_2g:
400 | *pfData = ( float )LSM6DS3_ACC_SENSITIVITY_FOR_FS_2G;
401 | break;
402 | case LSM6DS3_ACC_GYRO_FS_XL_4g:
403 | *pfData = ( float )LSM6DS3_ACC_SENSITIVITY_FOR_FS_4G;
404 | break;
405 | case LSM6DS3_ACC_GYRO_FS_XL_8g:
406 | *pfData = ( float )LSM6DS3_ACC_SENSITIVITY_FOR_FS_8G;
407 | break;
408 | case LSM6DS3_ACC_GYRO_FS_XL_16g:
409 | *pfData = ( float )LSM6DS3_ACC_SENSITIVITY_FOR_FS_16G;
410 | break;
411 | default:
412 | *pfData = -1.0f;
413 | return LSM6DS3_STATUS_ERROR;
414 | }
415 |
416 | return LSM6DS3_STATUS_OK;
417 | }
418 |
419 | /**
420 | * @brief Read Gyroscope Sensitivity
421 | * @param pfData the pointer where the gyroscope sensitivity is stored
422 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
423 | */
424 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Get_G_Sensitivity(float *pfData)
425 | {
426 | LSM6DS3_ACC_GYRO_FS_125_t fullScale125;
427 | LSM6DS3_ACC_GYRO_FS_G_t fullScale;
428 |
429 | /* Read full scale 125 selection from sensor. */
430 | if ( LSM6DS3_ACC_GYRO_R_FS_125( (void *)this, &fullScale125 ) == MEMS_ERROR )
431 | {
432 | return LSM6DS3_STATUS_ERROR;
433 | }
434 |
435 | if ( fullScale125 == LSM6DS3_ACC_GYRO_FS_125_ENABLED )
436 | {
437 | *pfData = ( float )LSM6DS3_GYRO_SENSITIVITY_FOR_FS_125DPS;
438 | }
439 |
440 | else
441 | {
442 |
443 | /* Read actual full scale selection from sensor. */
444 | if ( LSM6DS3_ACC_GYRO_R_FS_G( (void *)this, &fullScale ) == MEMS_ERROR )
445 | {
446 | return LSM6DS3_STATUS_ERROR;
447 | }
448 |
449 | /* Store the sensitivity based on actual full scale. */
450 | switch( fullScale )
451 | {
452 | case LSM6DS3_ACC_GYRO_FS_G_245dps:
453 | *pfData = ( float )LSM6DS3_GYRO_SENSITIVITY_FOR_FS_245DPS;
454 | break;
455 | case LSM6DS3_ACC_GYRO_FS_G_500dps:
456 | *pfData = ( float )LSM6DS3_GYRO_SENSITIVITY_FOR_FS_500DPS;
457 | break;
458 | case LSM6DS3_ACC_GYRO_FS_G_1000dps:
459 | *pfData = ( float )LSM6DS3_GYRO_SENSITIVITY_FOR_FS_1000DPS;
460 | break;
461 | case LSM6DS3_ACC_GYRO_FS_G_2000dps:
462 | *pfData = ( float )LSM6DS3_GYRO_SENSITIVITY_FOR_FS_2000DPS;
463 | break;
464 | default:
465 | *pfData = -1.0f;
466 | return LSM6DS3_STATUS_ERROR;
467 | }
468 | }
469 |
470 | return LSM6DS3_STATUS_OK;
471 | }
472 |
473 | /**
474 | * @brief Read raw data from LSM6DS3 Accelerometer
475 | * @param pData the pointer where the accelerometer raw data are stored
476 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
477 | */
478 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Get_X_AxesRaw(int16_t *pData)
479 | {
480 | uint8_t regValue[6] = {0, 0, 0, 0, 0, 0};
481 |
482 | /* Read output registers from LSM6DS3_ACC_GYRO_OUTX_L_XL to LSM6DS3_ACC_GYRO_OUTZ_H_XL. */
483 | if ( LSM6DS3_ACC_GYRO_GetRawAccData( (void *)this, regValue ) == MEMS_ERROR )
484 | {
485 | return LSM6DS3_STATUS_ERROR;
486 | }
487 |
488 | /* Format the data. */
489 | pData[0] = ( ( ( ( int16_t )regValue[1] ) << 8 ) + ( int16_t )regValue[0] );
490 | pData[1] = ( ( ( ( int16_t )regValue[3] ) << 8 ) + ( int16_t )regValue[2] );
491 | pData[2] = ( ( ( ( int16_t )regValue[5] ) << 8 ) + ( int16_t )regValue[4] );
492 |
493 | return LSM6DS3_STATUS_OK;
494 | }
495 |
496 | /**
497 | * @brief Read raw data from LSM6DS3 Gyroscope
498 | * @param pData the pointer where the gyroscope raw data are stored
499 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
500 | */
501 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Get_G_AxesRaw(int16_t *pData)
502 | {
503 | uint8_t regValue[6] = {0, 0, 0, 0, 0, 0};
504 |
505 | /* Read output registers from LSM6DS3_ACC_GYRO_OUTX_L_G to LSM6DS3_ACC_GYRO_OUTZ_H_G. */
506 | if ( LSM6DS3_ACC_GYRO_GetRawGyroData( (void *)this, regValue ) == MEMS_ERROR )
507 | {
508 | return LSM6DS3_STATUS_ERROR;
509 | }
510 |
511 | /* Format the data. */
512 | pData[0] = ( ( ( ( int16_t )regValue[1] ) << 8 ) + ( int16_t )regValue[0] );
513 | pData[1] = ( ( ( ( int16_t )regValue[3] ) << 8 ) + ( int16_t )regValue[2] );
514 | pData[2] = ( ( ( ( int16_t )regValue[5] ) << 8 ) + ( int16_t )regValue[4] );
515 |
516 | return LSM6DS3_STATUS_OK;
517 | }
518 |
519 | /**
520 | * @brief Read LSM6DS3 Accelerometer output data rate
521 | * @param odr the pointer to the output data rate
522 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
523 | */
524 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Get_X_ODR(float* odr)
525 | {
526 | LSM6DS3_ACC_GYRO_ODR_XL_t odr_low_level;
527 |
528 | if ( LSM6DS3_ACC_GYRO_R_ODR_XL( (void *)this, &odr_low_level ) == MEMS_ERROR )
529 | {
530 | return LSM6DS3_STATUS_ERROR;
531 | }
532 |
533 | switch( odr_low_level )
534 | {
535 | case LSM6DS3_ACC_GYRO_ODR_XL_POWER_DOWN:
536 | *odr = 0.0f;
537 | break;
538 | case LSM6DS3_ACC_GYRO_ODR_XL_13Hz:
539 | *odr = 13.0f;
540 | break;
541 | case LSM6DS3_ACC_GYRO_ODR_XL_26Hz:
542 | *odr = 26.0f;
543 | break;
544 | case LSM6DS3_ACC_GYRO_ODR_XL_52Hz:
545 | *odr = 52.0f;
546 | break;
547 | case LSM6DS3_ACC_GYRO_ODR_XL_104Hz:
548 | *odr = 104.0f;
549 | break;
550 | case LSM6DS3_ACC_GYRO_ODR_XL_208Hz:
551 | *odr = 208.0f;
552 | break;
553 | case LSM6DS3_ACC_GYRO_ODR_XL_416Hz:
554 | *odr = 416.0f;
555 | break;
556 | case LSM6DS3_ACC_GYRO_ODR_XL_833Hz:
557 | *odr = 833.0f;
558 | break;
559 | case LSM6DS3_ACC_GYRO_ODR_XL_1660Hz:
560 | *odr = 1660.0f;
561 | break;
562 | case LSM6DS3_ACC_GYRO_ODR_XL_3330Hz:
563 | *odr = 3330.0f;
564 | break;
565 | case LSM6DS3_ACC_GYRO_ODR_XL_6660Hz:
566 | *odr = 6660.0f;
567 | break;
568 | default:
569 | *odr = -1.0f;
570 | return LSM6DS3_STATUS_ERROR;
571 | }
572 |
573 | return LSM6DS3_STATUS_OK;
574 | }
575 |
576 | /**
577 | * @brief Read LSM6DS3 Gyroscope output data rate
578 | * @param odr the pointer to the output data rate
579 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
580 | */
581 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Get_G_ODR(float* odr)
582 | {
583 | LSM6DS3_ACC_GYRO_ODR_G_t odr_low_level;
584 |
585 | if ( LSM6DS3_ACC_GYRO_R_ODR_G( (void *)this, &odr_low_level ) == MEMS_ERROR )
586 | {
587 | return LSM6DS3_STATUS_ERROR;
588 | }
589 |
590 | switch( odr_low_level )
591 | {
592 | case LSM6DS3_ACC_GYRO_ODR_G_POWER_DOWN:
593 | *odr = 0.0f;
594 | break;
595 | case LSM6DS3_ACC_GYRO_ODR_G_13Hz:
596 | *odr = 13.0f;
597 | break;
598 | case LSM6DS3_ACC_GYRO_ODR_G_26Hz:
599 | *odr = 26.0f;
600 | break;
601 | case LSM6DS3_ACC_GYRO_ODR_G_52Hz:
602 | *odr = 52.0f;
603 | break;
604 | case LSM6DS3_ACC_GYRO_ODR_G_104Hz:
605 | *odr = 104.0f;
606 | break;
607 | case LSM6DS3_ACC_GYRO_ODR_G_208Hz:
608 | *odr = 208.0f;
609 | break;
610 | case LSM6DS3_ACC_GYRO_ODR_G_416Hz:
611 | *odr = 416.0f;
612 | break;
613 | case LSM6DS3_ACC_GYRO_ODR_G_833Hz:
614 | *odr = 833.0f;
615 | break;
616 | case LSM6DS3_ACC_GYRO_ODR_G_1660Hz:
617 | *odr = 1660.0f;
618 | break;
619 | default:
620 | *odr = -1.0f;
621 | return LSM6DS3_STATUS_ERROR;
622 | }
623 |
624 | return LSM6DS3_STATUS_OK;
625 | }
626 |
627 | /**
628 | * @brief Set LSM6DS3 Accelerometer output data rate
629 | * @param odr the output data rate to be set
630 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
631 | */
632 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Set_X_ODR(float odr)
633 | {
634 | if(X_isEnabled == 1)
635 | {
636 | if(Set_X_ODR_When_Enabled(odr) == LSM6DS3_STATUS_ERROR)
637 | {
638 | return LSM6DS3_STATUS_ERROR;
639 | }
640 | }
641 | else
642 | {
643 | if(Set_X_ODR_When_Disabled(odr) == LSM6DS3_STATUS_ERROR)
644 | {
645 | return LSM6DS3_STATUS_ERROR;
646 | }
647 | }
648 |
649 | return LSM6DS3_STATUS_OK;
650 | }
651 |
652 | /**
653 | * @brief Set LSM6DS3 Accelerometer output data rate when enabled
654 | * @param odr the output data rate to be set
655 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
656 | */
657 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Set_X_ODR_When_Enabled(float odr)
658 | {
659 | LSM6DS3_ACC_GYRO_ODR_XL_t new_odr;
660 |
661 | new_odr = ( odr <= 13.0f ) ? LSM6DS3_ACC_GYRO_ODR_XL_13Hz
662 | : ( odr <= 26.0f ) ? LSM6DS3_ACC_GYRO_ODR_XL_26Hz
663 | : ( odr <= 52.0f ) ? LSM6DS3_ACC_GYRO_ODR_XL_52Hz
664 | : ( odr <= 104.0f ) ? LSM6DS3_ACC_GYRO_ODR_XL_104Hz
665 | : ( odr <= 208.0f ) ? LSM6DS3_ACC_GYRO_ODR_XL_208Hz
666 | : ( odr <= 416.0f ) ? LSM6DS3_ACC_GYRO_ODR_XL_416Hz
667 | : ( odr <= 833.0f ) ? LSM6DS3_ACC_GYRO_ODR_XL_833Hz
668 | : ( odr <= 1660.0f ) ? LSM6DS3_ACC_GYRO_ODR_XL_1660Hz
669 | : ( odr <= 3330.0f ) ? LSM6DS3_ACC_GYRO_ODR_XL_3330Hz
670 | : LSM6DS3_ACC_GYRO_ODR_XL_6660Hz;
671 |
672 | if ( LSM6DS3_ACC_GYRO_W_ODR_XL( (void *)this, new_odr ) == MEMS_ERROR )
673 | {
674 | return LSM6DS3_STATUS_ERROR;
675 | }
676 |
677 | return LSM6DS3_STATUS_OK;
678 | }
679 |
680 | /**
681 | * @brief Set LSM6DS3 Accelerometer output data rate when disabled
682 | * @param odr the output data rate to be set
683 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
684 | */
685 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Set_X_ODR_When_Disabled(float odr)
686 | {
687 | X_Last_ODR = ( odr <= 13.0f ) ? 13.0f
688 | : ( odr <= 26.0f ) ? 26.0f
689 | : ( odr <= 52.0f ) ? 52.0f
690 | : ( odr <= 104.0f ) ? 104.0f
691 | : ( odr <= 208.0f ) ? 208.0f
692 | : ( odr <= 416.0f ) ? 416.0f
693 | : ( odr <= 833.0f ) ? 833.0f
694 | : ( odr <= 1660.0f ) ? 1660.0f
695 | : ( odr <= 3330.0f ) ? 3330.0f
696 | : 6660.0f;
697 |
698 | return LSM6DS3_STATUS_OK;
699 | }
700 |
701 | /**
702 | * @brief Set LSM6DS3 Gyroscope output data rate
703 | * @param odr the output data rate to be set
704 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
705 | */
706 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Set_G_ODR(float odr)
707 | {
708 | if(G_isEnabled == 1)
709 | {
710 | if(Set_G_ODR_When_Enabled(odr) == LSM6DS3_STATUS_ERROR)
711 | {
712 | return LSM6DS3_STATUS_ERROR;
713 | }
714 | }
715 | else
716 | {
717 | if(Set_G_ODR_When_Disabled(odr) == LSM6DS3_STATUS_ERROR)
718 | {
719 | return LSM6DS3_STATUS_ERROR;
720 | }
721 | }
722 |
723 | return LSM6DS3_STATUS_OK;
724 | }
725 |
726 | /**
727 | * @brief Set LSM6DS3 Gyroscope output data rate when enabled
728 | * @param odr the output data rate to be set
729 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
730 | */
731 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Set_G_ODR_When_Enabled(float odr)
732 | {
733 | LSM6DS3_ACC_GYRO_ODR_G_t new_odr;
734 |
735 | new_odr = ( odr <= 13.0f ) ? LSM6DS3_ACC_GYRO_ODR_G_13Hz
736 | : ( odr <= 26.0f ) ? LSM6DS3_ACC_GYRO_ODR_G_26Hz
737 | : ( odr <= 52.0f ) ? LSM6DS3_ACC_GYRO_ODR_G_52Hz
738 | : ( odr <= 104.0f ) ? LSM6DS3_ACC_GYRO_ODR_G_104Hz
739 | : ( odr <= 208.0f ) ? LSM6DS3_ACC_GYRO_ODR_G_208Hz
740 | : ( odr <= 416.0f ) ? LSM6DS3_ACC_GYRO_ODR_G_416Hz
741 | : ( odr <= 833.0f ) ? LSM6DS3_ACC_GYRO_ODR_G_833Hz
742 | : LSM6DS3_ACC_GYRO_ODR_G_1660Hz;
743 |
744 | if ( LSM6DS3_ACC_GYRO_W_ODR_G( (void *)this, new_odr ) == MEMS_ERROR )
745 | {
746 | return LSM6DS3_STATUS_ERROR;
747 | }
748 |
749 | return LSM6DS3_STATUS_OK;
750 | }
751 |
752 | /**
753 | * @brief Set LSM6DS3 Gyroscope output data rate when disabled
754 | * @param odr the output data rate to be set
755 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
756 | */
757 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Set_G_ODR_When_Disabled(float odr)
758 | {
759 | G_Last_ODR = ( odr <= 13.0f ) ? 13.0f
760 | : ( odr <= 26.0f ) ? 26.0f
761 | : ( odr <= 52.0f ) ? 52.0f
762 | : ( odr <= 104.0f ) ? 104.0f
763 | : ( odr <= 208.0f ) ? 208.0f
764 | : ( odr <= 416.0f ) ? 416.0f
765 | : ( odr <= 833.0f ) ? 833.0f
766 | : 1660.0f;
767 |
768 | return LSM6DS3_STATUS_OK;
769 | }
770 |
771 | /**
772 | * @brief Read LSM6DS3 Accelerometer full scale
773 | * @param fullScale the pointer to the full scale
774 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
775 | */
776 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Get_X_FS(float* fullScale)
777 | {
778 | LSM6DS3_ACC_GYRO_FS_XL_t fs_low_level;
779 |
780 | if ( LSM6DS3_ACC_GYRO_R_FS_XL( (void *)this, &fs_low_level ) == MEMS_ERROR )
781 | {
782 | return LSM6DS3_STATUS_ERROR;
783 | }
784 |
785 | switch( fs_low_level )
786 | {
787 | case LSM6DS3_ACC_GYRO_FS_XL_2g:
788 | *fullScale = 2.0f;
789 | break;
790 | case LSM6DS3_ACC_GYRO_FS_XL_4g:
791 | *fullScale = 4.0f;
792 | break;
793 | case LSM6DS3_ACC_GYRO_FS_XL_8g:
794 | *fullScale = 8.0f;
795 | break;
796 | case LSM6DS3_ACC_GYRO_FS_XL_16g:
797 | *fullScale = 16.0f;
798 | break;
799 | default:
800 | *fullScale = -1.0f;
801 | return LSM6DS3_STATUS_ERROR;
802 | }
803 |
804 | return LSM6DS3_STATUS_OK;
805 | }
806 |
807 | /**
808 | * @brief Read LSM6DS3 Gyroscope full scale
809 | * @param fullScale the pointer to the full scale
810 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
811 | */
812 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Get_G_FS(float* fullScale)
813 | {
814 | LSM6DS3_ACC_GYRO_FS_G_t fs_low_level;
815 | LSM6DS3_ACC_GYRO_FS_125_t fs_125;
816 |
817 | if ( LSM6DS3_ACC_GYRO_R_FS_125( (void *)this, &fs_125 ) == MEMS_ERROR )
818 | {
819 | return LSM6DS3_STATUS_ERROR;
820 | }
821 | if ( LSM6DS3_ACC_GYRO_R_FS_G( (void *)this, &fs_low_level ) == MEMS_ERROR )
822 | {
823 | return LSM6DS3_STATUS_ERROR;
824 | }
825 |
826 | if ( fs_125 == LSM6DS3_ACC_GYRO_FS_125_ENABLED )
827 | {
828 | *fullScale = 125.0f;
829 | }
830 |
831 | else
832 | {
833 | switch( fs_low_level )
834 | {
835 | case LSM6DS3_ACC_GYRO_FS_G_245dps:
836 | *fullScale = 245.0f;
837 | break;
838 | case LSM6DS3_ACC_GYRO_FS_G_500dps:
839 | *fullScale = 500.0f;
840 | break;
841 | case LSM6DS3_ACC_GYRO_FS_G_1000dps:
842 | *fullScale = 1000.0f;
843 | break;
844 | case LSM6DS3_ACC_GYRO_FS_G_2000dps:
845 | *fullScale = 2000.0f;
846 | break;
847 | default:
848 | *fullScale = -1.0f;
849 | return LSM6DS3_STATUS_ERROR;
850 | }
851 | }
852 |
853 | return LSM6DS3_STATUS_OK;
854 | }
855 |
856 | /**
857 | * @brief Set LSM6DS3 Accelerometer full scale
858 | * @param fullScale the full scale to be set
859 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
860 | */
861 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Set_X_FS(float fullScale)
862 | {
863 | LSM6DS3_ACC_GYRO_FS_XL_t new_fs;
864 |
865 | new_fs = ( fullScale <= 2.0f ) ? LSM6DS3_ACC_GYRO_FS_XL_2g
866 | : ( fullScale <= 4.0f ) ? LSM6DS3_ACC_GYRO_FS_XL_4g
867 | : ( fullScale <= 8.0f ) ? LSM6DS3_ACC_GYRO_FS_XL_8g
868 | : LSM6DS3_ACC_GYRO_FS_XL_16g;
869 |
870 | if ( LSM6DS3_ACC_GYRO_W_FS_XL( (void *)this, new_fs ) == MEMS_ERROR )
871 | {
872 | return LSM6DS3_STATUS_ERROR;
873 | }
874 |
875 | return LSM6DS3_STATUS_OK;
876 | }
877 |
878 | /**
879 | * @brief Set LSM6DS3 Gyroscope full scale
880 | * @param fullScale the full scale to be set
881 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
882 | */
883 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Set_G_FS(float fullScale)
884 | {
885 | LSM6DS3_ACC_GYRO_FS_G_t new_fs;
886 |
887 | if ( fullScale <= 125.0f )
888 | {
889 | if ( LSM6DS3_ACC_GYRO_W_FS_125( (void *)this, LSM6DS3_ACC_GYRO_FS_125_ENABLED ) == MEMS_ERROR )
890 | {
891 | return LSM6DS3_STATUS_ERROR;
892 | }
893 | }
894 | else
895 | {
896 | new_fs = ( fullScale <= 245.0f ) ? LSM6DS3_ACC_GYRO_FS_G_245dps
897 | : ( fullScale <= 500.0f ) ? LSM6DS3_ACC_GYRO_FS_G_500dps
898 | : ( fullScale <= 1000.0f ) ? LSM6DS3_ACC_GYRO_FS_G_1000dps
899 | : LSM6DS3_ACC_GYRO_FS_G_2000dps;
900 |
901 | if ( LSM6DS3_ACC_GYRO_W_FS_125( (void *)this, LSM6DS3_ACC_GYRO_FS_125_DISABLED ) == MEMS_ERROR )
902 | {
903 | return LSM6DS3_STATUS_ERROR;
904 | }
905 | if ( LSM6DS3_ACC_GYRO_W_FS_G( (void *)this, new_fs ) == MEMS_ERROR )
906 | {
907 | return LSM6DS3_STATUS_ERROR;
908 | }
909 | }
910 |
911 | return LSM6DS3_STATUS_OK;
912 | }
913 |
914 | /**
915 | * @brief Enable free fall detection
916 | * @note This function sets the LSM6DS3 accelerometer ODR to 416Hz and the LSM6DS3 accelerometer full scale to 2g
917 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
918 | */
919 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Enable_Free_Fall_Detection(void)
920 | {
921 | return Enable_Free_Fall_Detection(LSM6DS3_INT1_PIN);
922 | }
923 |
924 | /**
925 | * @brief Enable free fall detection
926 | * @param int_pin the interrupt pin to be used
927 | * @note This function sets the LSM6DS3 accelerometer ODR to 416Hz and the LSM6DS3 accelerometer full scale to 2g
928 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
929 | */
930 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Enable_Free_Fall_Detection(LSM6DS3_Interrupt_Pin_t int_pin)
931 | {
932 | /* Output Data Rate selection */
933 | if(Set_X_ODR(416.0f) == LSM6DS3_STATUS_ERROR)
934 | {
935 | return LSM6DS3_STATUS_ERROR;
936 | }
937 |
938 | /* Full scale selection */
939 | if ( LSM6DS3_ACC_GYRO_W_FS_XL( (void *)this, LSM6DS3_ACC_GYRO_FS_XL_2g ) == MEMS_ERROR )
940 | {
941 | return LSM6DS3_STATUS_ERROR;
942 | }
943 |
944 | /* FF_DUR setting */
945 | if ( LSM6DS3_ACC_GYRO_W_FF_Duration( (void *)this, 0x06 ) == MEMS_ERROR )
946 | {
947 | return LSM6DS3_STATUS_ERROR;
948 | }
949 |
950 | /* WAKE_DUR setting */
951 | if ( LSM6DS3_ACC_GYRO_W_WAKE_DUR( (void *)this, 0x00 ) == MEMS_ERROR )
952 | {
953 | return LSM6DS3_STATUS_ERROR;
954 | }
955 |
956 | /* TIMER_HR setting */
957 | if ( LSM6DS3_ACC_GYRO_W_TIMER_HR( (void *)this, LSM6DS3_ACC_GYRO_TIMER_HR_6_4ms ) == MEMS_ERROR )
958 | {
959 | return LSM6DS3_STATUS_ERROR;
960 | }
961 |
962 | /* SLEEP_DUR setting */
963 | if ( LSM6DS3_ACC_GYRO_W_SLEEP_DUR( (void *)this, 0x00 ) == MEMS_ERROR )
964 | {
965 | return LSM6DS3_STATUS_ERROR;
966 | }
967 |
968 | /* FF_THS setting */
969 | if ( LSM6DS3_ACC_GYRO_W_FF_THS( (void *)this, LSM6DS3_ACC_GYRO_FF_THS_10 ) == MEMS_ERROR )
970 | {
971 | return LSM6DS3_STATUS_ERROR;
972 | }
973 |
974 | /* Enable free fall event on either INT1 or INT2 pin */
975 | switch (int_pin)
976 | {
977 | case LSM6DS3_INT1_PIN:
978 | if ( LSM6DS3_ACC_GYRO_W_FFEvOnInt1( (void *)this, LSM6DS3_ACC_GYRO_INT1_FF_ENABLED ) == MEMS_ERROR )
979 | {
980 | return LSM6DS3_STATUS_ERROR;
981 | }
982 | break;
983 |
984 | case LSM6DS3_INT2_PIN:
985 | if ( LSM6DS3_ACC_GYRO_W_FFEvOnInt2( (void *)this, LSM6DS3_ACC_GYRO_INT2_FF_ENABLED ) == MEMS_ERROR )
986 | {
987 | return LSM6DS3_STATUS_ERROR;
988 | }
989 | break;
990 |
991 | default:
992 | return LSM6DS3_STATUS_ERROR;
993 | }
994 |
995 | return LSM6DS3_STATUS_OK;
996 | }
997 |
998 | /**
999 | * @brief Disable free fall detection
1000 | * @param None
1001 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1002 | */
1003 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Disable_Free_Fall_Detection(void)
1004 | {
1005 | /* Disable free fall event on INT1 pin */
1006 | if ( LSM6DS3_ACC_GYRO_W_FFEvOnInt1( (void *)this, LSM6DS3_ACC_GYRO_INT1_FF_DISABLED ) == MEMS_ERROR )
1007 | {
1008 | return LSM6DS3_STATUS_ERROR;
1009 | }
1010 |
1011 | /* Disable free fall event on INT2 pin */
1012 | if ( LSM6DS3_ACC_GYRO_W_FFEvOnInt2( (void *)this, LSM6DS3_ACC_GYRO_INT2_FF_DISABLED ) == MEMS_ERROR )
1013 | {
1014 | return LSM6DS3_STATUS_ERROR;
1015 | }
1016 |
1017 | /* FF_DUR setting */
1018 | if ( LSM6DS3_ACC_GYRO_W_FF_Duration( (void *)this, 0x00 ) == MEMS_ERROR )
1019 | {
1020 | return LSM6DS3_STATUS_ERROR;
1021 | }
1022 |
1023 | /* FF_THS setting */
1024 | if ( LSM6DS3_ACC_GYRO_W_FF_THS( (void *)this, LSM6DS3_ACC_GYRO_FF_THS_5 ) == MEMS_ERROR )
1025 | {
1026 | return LSM6DS3_STATUS_ERROR;
1027 | }
1028 |
1029 | return LSM6DS3_STATUS_OK;
1030 | }
1031 |
1032 | /**
1033 | * @brief Set the free fall detection threshold for LSM6DS3 accelerometer sensor
1034 | * @param thr the threshold to be set
1035 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1036 | */
1037 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Set_Free_Fall_Threshold(uint8_t thr)
1038 | {
1039 |
1040 | if ( LSM6DS3_ACC_GYRO_W_FF_THS( (void *)this, (LSM6DS3_ACC_GYRO_FF_THS_t)thr ) == MEMS_ERROR )
1041 | {
1042 | return LSM6DS3_STATUS_ERROR;
1043 | }
1044 |
1045 | return LSM6DS3_STATUS_OK;
1046 | }
1047 |
1048 | /**
1049 | * @brief Enable the pedometer feature for LSM6DS3 accelerometer sensor
1050 | * @note This function sets the LSM6DS3 accelerometer ODR to 26Hz and the LSM6DS3 accelerometer full scale to 2g
1051 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1052 | */
1053 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Enable_Pedometer(void)
1054 | {
1055 | /* Output Data Rate selection */
1056 | if( Set_X_ODR(26.0f) == LSM6DS3_STATUS_ERROR )
1057 | {
1058 | return LSM6DS3_STATUS_ERROR;
1059 | }
1060 |
1061 | /* Full scale selection. */
1062 | if( Set_X_FS(2.0f) == LSM6DS3_STATUS_ERROR )
1063 | {
1064 | return LSM6DS3_STATUS_ERROR;
1065 | }
1066 |
1067 | /* Set pedometer threshold. */
1068 | if ( Set_Pedometer_Threshold(LSM6DS3_PEDOMETER_THRESHOLD_MID_HIGH) == LSM6DS3_STATUS_ERROR )
1069 | {
1070 | return LSM6DS3_STATUS_ERROR;
1071 | }
1072 |
1073 | /* Enable embedded functionalities. */
1074 | if ( LSM6DS3_ACC_GYRO_W_FUNC_EN( (void *)this, LSM6DS3_ACC_GYRO_FUNC_EN_ENABLED ) == MEMS_ERROR )
1075 | {
1076 | return LSM6DS3_STATUS_ERROR;
1077 | }
1078 |
1079 | /* Enable pedometer algorithm. */
1080 | if ( LSM6DS3_ACC_GYRO_W_PEDO_EN( (void *)this, LSM6DS3_ACC_GYRO_PEDO_EN_ENABLED ) == MEMS_ERROR )
1081 | {
1082 | return LSM6DS3_STATUS_ERROR;
1083 | }
1084 |
1085 | /* Enable pedometer on INT1. */
1086 | if ( LSM6DS3_ACC_GYRO_W_PEDO_STEP_on_INT1( (void *)this, LSM6DS3_ACC_GYRO_INT1_PEDO_ENABLED ) == MEMS_ERROR )
1087 | {
1088 | return LSM6DS3_STATUS_ERROR;
1089 | }
1090 |
1091 | return LSM6DS3_STATUS_OK;
1092 | }
1093 |
1094 | /**
1095 | * @brief Disable the pedometer feature for LSM6DS3 accelerometer sensor
1096 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1097 | */
1098 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Disable_Pedometer(void)
1099 | {
1100 | /* Disable pedometer on INT1. */
1101 | if ( LSM6DS3_ACC_GYRO_W_PEDO_STEP_on_INT1( (void *)this, LSM6DS3_ACC_GYRO_INT1_PEDO_DISABLED ) == MEMS_ERROR )
1102 | {
1103 | return LSM6DS3_STATUS_ERROR;
1104 | }
1105 |
1106 | /* Disable pedometer algorithm. */
1107 | if ( LSM6DS3_ACC_GYRO_W_PEDO_EN( (void *)this, LSM6DS3_ACC_GYRO_PEDO_EN_DISABLED ) == MEMS_ERROR )
1108 | {
1109 | return LSM6DS3_STATUS_ERROR;
1110 | }
1111 |
1112 | /* Disable embedded functionalities. */
1113 | if ( LSM6DS3_ACC_GYRO_W_FUNC_EN( (void *)this, LSM6DS3_ACC_GYRO_FUNC_EN_DISABLED ) == MEMS_ERROR )
1114 | {
1115 | return LSM6DS3_STATUS_ERROR;
1116 | }
1117 |
1118 | /* Reset pedometer threshold. */
1119 | if ( Set_Pedometer_Threshold(0x0) == LSM6DS3_STATUS_ERROR )
1120 | {
1121 | return LSM6DS3_STATUS_ERROR;
1122 | }
1123 |
1124 | return LSM6DS3_STATUS_OK;
1125 | }
1126 |
1127 | /**
1128 | * @brief Get the step counter for LSM6DS3 accelerometer sensor
1129 | * @param step_count the pointer to the step counter
1130 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1131 | */
1132 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Get_Step_Counter(uint16_t *step_count)
1133 | {
1134 | if ( LSM6DS3_ACC_GYRO_Get_GetStepCounter( (void *)this, ( uint8_t* )step_count ) == MEMS_ERROR )
1135 | {
1136 | return LSM6DS3_STATUS_ERROR;
1137 | }
1138 |
1139 | return LSM6DS3_STATUS_OK;
1140 | }
1141 |
1142 | /**
1143 | * @brief Reset of the step counter for LSM6DS3 accelerometer sensor
1144 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1145 | */
1146 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Reset_Step_Counter(void)
1147 | {
1148 | if ( LSM6DS3_ACC_GYRO_W_PedoStepReset( (void *)this, LSM6DS3_ACC_GYRO_PEDO_RST_STEP_ENABLED ) == MEMS_ERROR )
1149 | {
1150 | return LSM6DS3_STATUS_ERROR;
1151 | }
1152 |
1153 | delay(10);
1154 |
1155 | if ( LSM6DS3_ACC_GYRO_W_PedoStepReset( (void *)this, LSM6DS3_ACC_GYRO_PEDO_RST_STEP_DISABLED ) == MEMS_ERROR )
1156 | {
1157 | return LSM6DS3_STATUS_ERROR;
1158 | }
1159 |
1160 | return LSM6DS3_STATUS_OK;
1161 | }
1162 |
1163 | /**
1164 | * @brief Set the pedometer threshold for LSM6DS3 accelerometer sensor
1165 | * @param thr the threshold to be set
1166 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1167 | */
1168 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Set_Pedometer_Threshold(uint8_t thr)
1169 | {
1170 | if ( LSM6DS3_ACC_GYRO_W_PedoThreshold( (void *)this, thr ) == MEMS_ERROR )
1171 | {
1172 | return LSM6DS3_STATUS_ERROR;
1173 | }
1174 |
1175 | return LSM6DS3_STATUS_OK;
1176 | }
1177 |
1178 | /**
1179 | * @brief Enable the tilt detection for LSM6DS3 accelerometer sensor
1180 | * @note This function sets the LSM6DS3 accelerometer ODR to 26Hz and the LSM6DS3 accelerometer full scale to 2g
1181 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1182 | */
1183 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Enable_Tilt_Detection(void)
1184 | {
1185 | return Enable_Tilt_Detection(LSM6DS3_INT1_PIN);
1186 | }
1187 |
1188 | /**
1189 | * @brief Enable the tilt detection for LSM6DS3 accelerometer sensor
1190 | * @param int_pin the interrupt pin to be used
1191 | * @note This function sets the LSM6DS3 accelerometer ODR to 26Hz and the LSM6DS3 accelerometer full scale to 2g
1192 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1193 | */
1194 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Enable_Tilt_Detection(LSM6DS3_Interrupt_Pin_t int_pin)
1195 | {
1196 | /* Output Data Rate selection */
1197 | if( Set_X_ODR(26.0f) == LSM6DS3_STATUS_ERROR )
1198 | {
1199 | return LSM6DS3_STATUS_ERROR;
1200 | }
1201 |
1202 | /* Full scale selection. */
1203 | if( Set_X_FS(2.0f) == LSM6DS3_STATUS_ERROR )
1204 | {
1205 | return LSM6DS3_STATUS_ERROR;
1206 | }
1207 |
1208 | /* Enable embedded functionalities */
1209 | if ( LSM6DS3_ACC_GYRO_W_FUNC_EN( (void *)this, LSM6DS3_ACC_GYRO_FUNC_EN_ENABLED ) == MEMS_ERROR )
1210 | {
1211 | return LSM6DS3_STATUS_ERROR;
1212 | }
1213 |
1214 | /* Enable tilt calculation. */
1215 | if ( LSM6DS3_ACC_GYRO_W_TILT_EN( (void *)this, LSM6DS3_ACC_GYRO_TILT_EN_ENABLED ) == MEMS_ERROR )
1216 | {
1217 | return LSM6DS3_STATUS_ERROR;
1218 | }
1219 |
1220 | /* Enable tilt detection on either INT1 or INT2 pin */
1221 | switch (int_pin)
1222 | {
1223 | case LSM6DS3_INT1_PIN:
1224 | if ( LSM6DS3_ACC_GYRO_W_TiltEvOnInt1( (void *)this, LSM6DS3_ACC_GYRO_INT1_TILT_ENABLED ) == MEMS_ERROR )
1225 | {
1226 | return LSM6DS3_STATUS_ERROR;
1227 | }
1228 | break;
1229 |
1230 | case LSM6DS3_INT2_PIN:
1231 | if ( LSM6DS3_ACC_GYRO_W_TiltEvOnInt2( (void *)this, LSM6DS3_ACC_GYRO_INT2_TILT_ENABLED ) == MEMS_ERROR )
1232 | {
1233 | return LSM6DS3_STATUS_ERROR;
1234 | }
1235 | break;
1236 |
1237 | default:
1238 | return LSM6DS3_STATUS_ERROR;
1239 | }
1240 |
1241 | return LSM6DS3_STATUS_OK;
1242 | }
1243 |
1244 | /**
1245 | * @brief Disable the tilt detection for LSM6DS3 accelerometer sensor
1246 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1247 | */
1248 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Disable_Tilt_Detection(void)
1249 | {
1250 | /* Disable tilt event on INT1. */
1251 | if ( LSM6DS3_ACC_GYRO_W_TiltEvOnInt1( (void *)this, LSM6DS3_ACC_GYRO_INT1_TILT_DISABLED ) == MEMS_ERROR )
1252 | {
1253 | return LSM6DS3_STATUS_ERROR;
1254 | }
1255 |
1256 | /* Disable tilt event on INT2. */
1257 | if ( LSM6DS3_ACC_GYRO_W_TiltEvOnInt2( (void *)this, LSM6DS3_ACC_GYRO_INT2_TILT_DISABLED ) == MEMS_ERROR )
1258 | {
1259 | return LSM6DS3_STATUS_ERROR;
1260 | }
1261 |
1262 | /* Disable tilt calculation. */
1263 | if ( LSM6DS3_ACC_GYRO_W_TILT_EN( (void *)this, LSM6DS3_ACC_GYRO_TILT_EN_DISABLED ) == MEMS_ERROR )
1264 | {
1265 | return LSM6DS3_STATUS_ERROR;
1266 | }
1267 |
1268 | /* Disable embedded functionalities */
1269 | if ( LSM6DS3_ACC_GYRO_W_FUNC_EN( (void *)this, LSM6DS3_ACC_GYRO_FUNC_EN_DISABLED ) == MEMS_ERROR )
1270 | {
1271 | return LSM6DS3_STATUS_ERROR;
1272 | }
1273 |
1274 | return LSM6DS3_STATUS_OK;
1275 | }
1276 |
1277 | /**
1278 | * @brief Enable the wake up detection for LSM6DS3 accelerometer sensor
1279 | * @note This function sets the LSM6DS3 accelerometer ODR to 416Hz and the LSM6DS3 accelerometer full scale to 2g
1280 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1281 | */
1282 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Enable_Wake_Up_Detection(void)
1283 | {
1284 | return Enable_Wake_Up_Detection(LSM6DS3_INT2_PIN);
1285 | }
1286 |
1287 | /**
1288 | * @brief Enable the wake up detection for LSM6DS3 accelerometer sensor
1289 | * @param int_pin the interrupt pin to be used
1290 | * @note This function sets the LSM6DS3 accelerometer ODR to 416Hz and the LSM6DS3 accelerometer full scale to 2g
1291 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1292 | */
1293 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Enable_Wake_Up_Detection(LSM6DS3_Interrupt_Pin_t int_pin)
1294 | {
1295 | /* Output Data Rate selection */
1296 | if( Set_X_ODR(416.0f) == LSM6DS3_STATUS_ERROR )
1297 | {
1298 | return LSM6DS3_STATUS_ERROR;
1299 | }
1300 |
1301 | /* Full scale selection. */
1302 | if( Set_X_FS(2.0f) == LSM6DS3_STATUS_ERROR )
1303 | {
1304 | return LSM6DS3_STATUS_ERROR;
1305 | }
1306 |
1307 | /* WAKE_DUR setting */
1308 | if ( LSM6DS3_ACC_GYRO_W_WAKE_DUR( (void *)this, 0x00 ) == MEMS_ERROR )
1309 | {
1310 | return LSM6DS3_STATUS_ERROR;
1311 | }
1312 |
1313 | /* Set wake up threshold. */
1314 | if ( LSM6DS3_ACC_GYRO_W_WK_THS( (void *)this, 0x02 ) == MEMS_ERROR )
1315 | {
1316 | return LSM6DS3_STATUS_ERROR;
1317 | }
1318 |
1319 | /* Enable wake up detection on either INT1 or INT2 pin */
1320 | switch (int_pin)
1321 | {
1322 | case LSM6DS3_INT1_PIN:
1323 | if ( LSM6DS3_ACC_GYRO_W_WUEvOnInt1( (void *)this, LSM6DS3_ACC_GYRO_INT1_WU_ENABLED ) == MEMS_ERROR )
1324 | {
1325 | return LSM6DS3_STATUS_ERROR;
1326 | }
1327 | break;
1328 |
1329 | case LSM6DS3_INT2_PIN:
1330 | if ( LSM6DS3_ACC_GYRO_W_WUEvOnInt2( (void *)this, LSM6DS3_ACC_GYRO_INT2_WU_ENABLED ) == MEMS_ERROR )
1331 | {
1332 | return LSM6DS3_STATUS_ERROR;
1333 | }
1334 | break;
1335 |
1336 | default:
1337 | return LSM6DS3_STATUS_ERROR;
1338 | }
1339 |
1340 | return LSM6DS3_STATUS_OK;
1341 | }
1342 |
1343 | /**
1344 | * @brief Disable the wake up detection for LSM6DS3 accelerometer sensor
1345 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1346 | */
1347 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Disable_Wake_Up_Detection(void)
1348 | {
1349 | /* Disable wake up event on INT1 */
1350 | if ( LSM6DS3_ACC_GYRO_W_WUEvOnInt1( (void *)this, LSM6DS3_ACC_GYRO_INT1_WU_DISABLED ) == MEMS_ERROR )
1351 | {
1352 | return LSM6DS3_STATUS_ERROR;
1353 | }
1354 |
1355 | /* Disable wake up event on INT2 */
1356 | if ( LSM6DS3_ACC_GYRO_W_WUEvOnInt2( (void *)this, LSM6DS3_ACC_GYRO_INT2_WU_DISABLED ) == MEMS_ERROR )
1357 | {
1358 | return LSM6DS3_STATUS_ERROR;
1359 | }
1360 |
1361 | /* WU_DUR setting */
1362 | if ( LSM6DS3_ACC_GYRO_W_WAKE_DUR( (void *)this, 0x00 ) == MEMS_ERROR )
1363 | {
1364 | return LSM6DS3_STATUS_ERROR;
1365 | }
1366 |
1367 | /* WU_THS setting */
1368 | if ( LSM6DS3_ACC_GYRO_W_WK_THS( (void *)this, 0x00 ) == MEMS_ERROR )
1369 | {
1370 | return LSM6DS3_STATUS_ERROR;
1371 | }
1372 |
1373 | return LSM6DS3_STATUS_OK;
1374 | }
1375 |
1376 | /**
1377 | * @brief Set the wake up threshold for LSM6DS3 accelerometer sensor
1378 | * @param thr the threshold to be set
1379 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1380 | */
1381 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Set_Wake_Up_Threshold(uint8_t thr)
1382 | {
1383 | if ( LSM6DS3_ACC_GYRO_W_WK_THS( (void *)this, thr ) == MEMS_ERROR )
1384 | {
1385 | return LSM6DS3_STATUS_ERROR;
1386 | }
1387 |
1388 | return LSM6DS3_STATUS_OK;
1389 | }
1390 |
1391 | /**
1392 | * @brief Enable the single tap detection for LSM6DS3 accelerometer sensor
1393 | * @note This function sets the LSM6DS3 accelerometer ODR to 416Hz and the LSM6DS3 accelerometer full scale to 2g
1394 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1395 | */
1396 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Enable_Single_Tap_Detection(void)
1397 | {
1398 | return Enable_Single_Tap_Detection(LSM6DS3_INT1_PIN);
1399 | }
1400 |
1401 | /**
1402 | * @brief Enable the single tap detection for LSM6DS3 accelerometer sensor
1403 | * @param int_pin the interrupt pin to be used
1404 | * @note This function sets the LSM6DS3 accelerometer ODR to 416Hz and the LSM6DS3 accelerometer full scale to 2g
1405 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1406 | */
1407 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Enable_Single_Tap_Detection(LSM6DS3_Interrupt_Pin_t int_pin)
1408 | {
1409 | /* Output Data Rate selection */
1410 | if( Set_X_ODR(416.0f) == LSM6DS3_STATUS_ERROR )
1411 | {
1412 | return LSM6DS3_STATUS_ERROR;
1413 | }
1414 |
1415 | /* Full scale selection. */
1416 | if( Set_X_FS(2.0f) == LSM6DS3_STATUS_ERROR )
1417 | {
1418 | return LSM6DS3_STATUS_ERROR;
1419 | }
1420 |
1421 | /* Enable X direction in tap recognition. */
1422 | if ( LSM6DS3_ACC_GYRO_W_TAP_X_EN( (void *)this, LSM6DS3_ACC_GYRO_TAP_X_EN_ENABLED ) == MEMS_ERROR )
1423 | {
1424 | return LSM6DS3_STATUS_ERROR;
1425 | }
1426 |
1427 | /* Enable Y direction in tap recognition. */
1428 | if ( LSM6DS3_ACC_GYRO_W_TAP_Y_EN( (void *)this, LSM6DS3_ACC_GYRO_TAP_Y_EN_ENABLED ) == MEMS_ERROR )
1429 | {
1430 | return LSM6DS3_STATUS_ERROR;
1431 | }
1432 |
1433 | /* Enable Z direction in tap recognition. */
1434 | if ( LSM6DS3_ACC_GYRO_W_TAP_Z_EN( (void *)this, LSM6DS3_ACC_GYRO_TAP_Z_EN_ENABLED ) == MEMS_ERROR )
1435 | {
1436 | return LSM6DS3_STATUS_ERROR;
1437 | }
1438 |
1439 | /* Set tap threshold. */
1440 | if ( Set_Tap_Threshold( LSM6DS3_TAP_THRESHOLD_MID_LOW ) == LSM6DS3_STATUS_ERROR )
1441 | {
1442 | return LSM6DS3_STATUS_ERROR;
1443 | }
1444 |
1445 | /* Set tap shock time window. */
1446 | if ( Set_Tap_Shock_Time( LSM6DS3_TAP_SHOCK_TIME_MID_HIGH ) == LSM6DS3_STATUS_ERROR )
1447 | {
1448 | return LSM6DS3_STATUS_ERROR;
1449 | }
1450 |
1451 | /* Set tap quiet time window. */
1452 | if ( Set_Tap_Quiet_Time( LSM6DS3_TAP_QUIET_TIME_MID_LOW ) == LSM6DS3_STATUS_ERROR )
1453 | {
1454 | return LSM6DS3_STATUS_ERROR;
1455 | }
1456 |
1457 | /* _NOTE_: Tap duration time window - don't care for single tap. */
1458 |
1459 | /* _NOTE_: Single/Double Tap event - don't care of this flag for single tap. */
1460 |
1461 | /* Enable single tap on either INT1 or INT2 pin */
1462 | switch (int_pin)
1463 | {
1464 | case LSM6DS3_INT1_PIN:
1465 | if ( LSM6DS3_ACC_GYRO_W_SingleTapOnInt1( (void *)this, LSM6DS3_ACC_GYRO_INT1_SINGLE_TAP_ENABLED ) == MEMS_ERROR )
1466 | {
1467 | return LSM6DS3_STATUS_ERROR;
1468 | }
1469 | break;
1470 |
1471 | case LSM6DS3_INT2_PIN:
1472 | if ( LSM6DS3_ACC_GYRO_W_SingleTapOnInt2( (void *)this, LSM6DS3_ACC_GYRO_INT2_SINGLE_TAP_ENABLED ) == MEMS_ERROR )
1473 | {
1474 | return LSM6DS3_STATUS_ERROR;
1475 | }
1476 | break;
1477 |
1478 | default:
1479 | return LSM6DS3_STATUS_ERROR;
1480 | }
1481 |
1482 | return LSM6DS3_STATUS_OK;
1483 | }
1484 |
1485 | /**
1486 | * @brief Disable the single tap detection for LSM6DS3 accelerometer sensor
1487 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1488 | */
1489 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Disable_Single_Tap_Detection(void)
1490 | {
1491 | /* Disable single tap interrupt on INT1 pin. */
1492 | if ( LSM6DS3_ACC_GYRO_W_SingleTapOnInt1( (void *)this, LSM6DS3_ACC_GYRO_INT1_SINGLE_TAP_DISABLED ) == MEMS_ERROR )
1493 | {
1494 | return LSM6DS3_STATUS_ERROR;
1495 | }
1496 |
1497 | /* Disable single tap interrupt on INT2 pin. */
1498 | if ( LSM6DS3_ACC_GYRO_W_SingleTapOnInt2( (void *)this, LSM6DS3_ACC_GYRO_INT2_SINGLE_TAP_DISABLED ) == MEMS_ERROR )
1499 | {
1500 | return LSM6DS3_STATUS_ERROR;
1501 | }
1502 |
1503 | /* Reset tap threshold. */
1504 | if ( Set_Tap_Threshold( 0x0 ) == LSM6DS3_STATUS_ERROR )
1505 | {
1506 | return LSM6DS3_STATUS_ERROR;
1507 | }
1508 |
1509 | /* Reset tap shock time window. */
1510 | if ( Set_Tap_Shock_Time( 0x0 ) == LSM6DS3_STATUS_ERROR )
1511 | {
1512 | return LSM6DS3_STATUS_ERROR;
1513 | }
1514 |
1515 | /* Reset tap quiet time window. */
1516 | if ( Set_Tap_Quiet_Time( 0x0 ) == LSM6DS3_STATUS_ERROR )
1517 | {
1518 | return LSM6DS3_STATUS_ERROR;
1519 | }
1520 |
1521 | /* _NOTE_: Tap duration time window - don't care for single tap. */
1522 |
1523 | /* _NOTE_: Single/Double Tap event - don't care of this flag for single tap. */
1524 |
1525 | /* Disable Z direction in tap recognition. */
1526 | if ( LSM6DS3_ACC_GYRO_W_TAP_Z_EN( (void *)this, LSM6DS3_ACC_GYRO_TAP_Z_EN_DISABLED ) == MEMS_ERROR )
1527 | {
1528 | return LSM6DS3_STATUS_ERROR;
1529 | }
1530 |
1531 | /* Disable Y direction in tap recognition. */
1532 | if ( LSM6DS3_ACC_GYRO_W_TAP_Y_EN( (void *)this, LSM6DS3_ACC_GYRO_TAP_Y_EN_DISABLED ) == MEMS_ERROR )
1533 | {
1534 | return LSM6DS3_STATUS_ERROR;
1535 | }
1536 |
1537 | /* Disable X direction in tap recognition. */
1538 | if ( LSM6DS3_ACC_GYRO_W_TAP_X_EN( (void *)this, LSM6DS3_ACC_GYRO_TAP_X_EN_DISABLED ) == MEMS_ERROR )
1539 | {
1540 | return LSM6DS3_STATUS_ERROR;
1541 | }
1542 |
1543 | return LSM6DS3_STATUS_OK;
1544 | }
1545 |
1546 | /**
1547 | * @brief Enable the double tap detection for LSM6DS3 accelerometer sensor
1548 | * @note This function sets the LSM6DS3 accelerometer ODR to 416Hz and the LSM6DS3 accelerometer full scale to 2g
1549 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1550 | */
1551 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Enable_Double_Tap_Detection(void)
1552 | {
1553 | return Enable_Double_Tap_Detection(LSM6DS3_INT1_PIN);
1554 | }
1555 |
1556 | /**
1557 | * @brief Enable the double tap detection for LSM6DS3 accelerometer sensor
1558 | * @param int_pin the interrupt pin to be used
1559 | * @note This function sets the LSM6DS3 accelerometer ODR to 416Hz and the LSM6DS3 accelerometer full scale to 2g
1560 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1561 | */
1562 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Enable_Double_Tap_Detection(LSM6DS3_Interrupt_Pin_t int_pin)
1563 | {
1564 | /* Output Data Rate selection */
1565 | if( Set_X_ODR(416.0f) == LSM6DS3_STATUS_ERROR )
1566 | {
1567 | return LSM6DS3_STATUS_ERROR;
1568 | }
1569 |
1570 | /* Full scale selection. */
1571 | if( Set_X_FS(2.0f) == LSM6DS3_STATUS_ERROR )
1572 | {
1573 | return LSM6DS3_STATUS_ERROR;
1574 | }
1575 |
1576 | /* Enable X direction in tap recognition. */
1577 | if ( LSM6DS3_ACC_GYRO_W_TAP_X_EN( (void *)this, LSM6DS3_ACC_GYRO_TAP_X_EN_ENABLED ) == MEMS_ERROR )
1578 | {
1579 | return LSM6DS3_STATUS_ERROR;
1580 | }
1581 |
1582 | /* Enable Y direction in tap recognition. */
1583 | if ( LSM6DS3_ACC_GYRO_W_TAP_Y_EN( (void *)this, LSM6DS3_ACC_GYRO_TAP_Y_EN_ENABLED ) == MEMS_ERROR )
1584 | {
1585 | return LSM6DS3_STATUS_ERROR;
1586 | }
1587 |
1588 | /* Enable Z direction in tap recognition. */
1589 | if ( LSM6DS3_ACC_GYRO_W_TAP_Z_EN( (void *)this, LSM6DS3_ACC_GYRO_TAP_Z_EN_ENABLED ) == MEMS_ERROR )
1590 | {
1591 | return LSM6DS3_STATUS_ERROR;
1592 | }
1593 |
1594 | /* Set tap threshold. */
1595 | if ( Set_Tap_Threshold( LSM6DS3_TAP_THRESHOLD_MID_LOW ) == LSM6DS3_STATUS_ERROR )
1596 | {
1597 | return LSM6DS3_STATUS_ERROR;
1598 | }
1599 |
1600 | /* Set tap shock time window. */
1601 | if ( Set_Tap_Shock_Time( LSM6DS3_TAP_SHOCK_TIME_HIGH ) == LSM6DS3_STATUS_ERROR )
1602 | {
1603 | return LSM6DS3_STATUS_ERROR;
1604 | }
1605 |
1606 | /* Set tap quiet time window. */
1607 | if ( Set_Tap_Quiet_Time( LSM6DS3_TAP_QUIET_TIME_HIGH ) == LSM6DS3_STATUS_ERROR )
1608 | {
1609 | return LSM6DS3_STATUS_ERROR;
1610 | }
1611 |
1612 | /* Set tap duration time window. */
1613 | if ( Set_Tap_Duration_Time( LSM6DS3_TAP_DURATION_TIME_MID ) == LSM6DS3_STATUS_ERROR )
1614 | {
1615 | return LSM6DS3_STATUS_ERROR;
1616 | }
1617 |
1618 | /* Single and double tap enabled. */
1619 | if ( LSM6DS3_ACC_GYRO_W_SINGLE_DOUBLE_TAP_EV( (void *)this, LSM6DS3_ACC_GYRO_SINGLE_DOUBLE_TAP_DOUBLE_TAP ) == MEMS_ERROR )
1620 | {
1621 | return LSM6DS3_STATUS_ERROR;
1622 | }
1623 |
1624 | /* Enable double tap on either INT1 or INT2 pin */
1625 | switch (int_pin)
1626 | {
1627 | case LSM6DS3_INT1_PIN:
1628 | if ( LSM6DS3_ACC_GYRO_W_TapEvOnInt1( (void *)this, LSM6DS3_ACC_GYRO_INT1_TAP_ENABLED ) == MEMS_ERROR )
1629 | {
1630 | return LSM6DS3_STATUS_ERROR;
1631 | }
1632 | break;
1633 |
1634 | case LSM6DS3_INT2_PIN:
1635 | if ( LSM6DS3_ACC_GYRO_W_TapEvOnInt2( (void *)this, LSM6DS3_ACC_GYRO_INT2_TAP_ENABLED ) == MEMS_ERROR )
1636 | {
1637 | return LSM6DS3_STATUS_ERROR;
1638 | }
1639 | break;
1640 |
1641 | default:
1642 | return LSM6DS3_STATUS_ERROR;
1643 | }
1644 |
1645 | return LSM6DS3_STATUS_OK;
1646 | }
1647 |
1648 | /**
1649 | * @brief Disable the double tap detection for LSM6DS3 accelerometer sensor
1650 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1651 | */
1652 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Disable_Double_Tap_Detection(void)
1653 | {
1654 | /* Disable double tap interrupt on INT1 pin. */
1655 | if ( LSM6DS3_ACC_GYRO_W_TapEvOnInt1( (void *)this, LSM6DS3_ACC_GYRO_INT1_TAP_DISABLED ) == MEMS_ERROR )
1656 | {
1657 | return LSM6DS3_STATUS_ERROR;
1658 | }
1659 |
1660 | /* Disable double tap interrupt on INT2 pin. */
1661 | if ( LSM6DS3_ACC_GYRO_W_TapEvOnInt2( (void *)this, LSM6DS3_ACC_GYRO_INT2_TAP_DISABLED ) == MEMS_ERROR )
1662 | {
1663 | return LSM6DS3_STATUS_ERROR;
1664 | }
1665 |
1666 | /* Reset tap threshold. */
1667 | if ( Set_Tap_Threshold( 0x0 ) == LSM6DS3_STATUS_ERROR )
1668 | {
1669 | return LSM6DS3_STATUS_ERROR;
1670 | }
1671 |
1672 | /* Reset tap shock time window. */
1673 | if ( Set_Tap_Shock_Time( 0x0 ) == LSM6DS3_STATUS_ERROR )
1674 | {
1675 | return LSM6DS3_STATUS_ERROR;
1676 | }
1677 |
1678 | /* Reset tap quiet time window. */
1679 | if ( Set_Tap_Quiet_Time( 0x0 ) == LSM6DS3_STATUS_ERROR )
1680 | {
1681 | return LSM6DS3_STATUS_ERROR;
1682 | }
1683 |
1684 | /* Reset tap duration time window. */
1685 | if ( Set_Tap_Duration_Time( 0x0 ) == LSM6DS3_STATUS_ERROR )
1686 | {
1687 | return LSM6DS3_STATUS_ERROR;
1688 | }
1689 |
1690 | /* Only single tap enabled. */
1691 | if ( LSM6DS3_ACC_GYRO_W_SINGLE_DOUBLE_TAP_EV( (void *)this, LSM6DS3_ACC_GYRO_SINGLE_DOUBLE_TAP_SINGLE_TAP ) == MEMS_ERROR )
1692 | {
1693 | return LSM6DS3_STATUS_ERROR;
1694 | }
1695 |
1696 | /* Disable Z direction in tap recognition. */
1697 | if ( LSM6DS3_ACC_GYRO_W_TAP_Z_EN( (void *)this, LSM6DS3_ACC_GYRO_TAP_Z_EN_DISABLED ) == MEMS_ERROR )
1698 | {
1699 | return LSM6DS3_STATUS_ERROR;
1700 | }
1701 |
1702 | /* Disable Y direction in tap recognition. */
1703 | if ( LSM6DS3_ACC_GYRO_W_TAP_Y_EN( (void *)this, LSM6DS3_ACC_GYRO_TAP_Y_EN_DISABLED ) == MEMS_ERROR )
1704 | {
1705 | return LSM6DS3_STATUS_ERROR;
1706 | }
1707 |
1708 | /* Disable X direction in tap recognition. */
1709 | if ( LSM6DS3_ACC_GYRO_W_TAP_X_EN( (void *)this, LSM6DS3_ACC_GYRO_TAP_X_EN_DISABLED ) == MEMS_ERROR )
1710 | {
1711 | return LSM6DS3_STATUS_ERROR;
1712 | }
1713 |
1714 | return LSM6DS3_STATUS_OK;
1715 | }
1716 |
1717 | /**
1718 | * @brief Set the tap threshold for LSM6DS3 accelerometer sensor
1719 | * @param thr the threshold to be set
1720 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1721 | */
1722 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Set_Tap_Threshold(uint8_t thr)
1723 | {
1724 | if ( LSM6DS3_ACC_GYRO_W_TAP_THS( (void *)this, thr ) == MEMS_ERROR )
1725 | {
1726 | return LSM6DS3_STATUS_ERROR;
1727 | }
1728 |
1729 | return LSM6DS3_STATUS_OK;
1730 | }
1731 |
1732 | /**
1733 | * @brief Set the tap shock time window for LSM6DS3 accelerometer sensor
1734 | * @param time the shock time window to be set
1735 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1736 | */
1737 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Set_Tap_Shock_Time(uint8_t time)
1738 | {
1739 | if ( LSM6DS3_ACC_GYRO_W_SHOCK_Duration( (void *)this, time ) == MEMS_ERROR )
1740 | {
1741 | return LSM6DS3_STATUS_ERROR;
1742 | }
1743 |
1744 | return LSM6DS3_STATUS_OK;
1745 | }
1746 |
1747 | /**
1748 | * @brief Set the tap quiet time window for LSM6DS3 accelerometer sensor
1749 | * @param time the quiet time window to be set
1750 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1751 | */
1752 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Set_Tap_Quiet_Time(uint8_t time)
1753 | {
1754 | if ( LSM6DS3_ACC_GYRO_W_QUIET_Duration( (void *)this, time ) == MEMS_ERROR )
1755 | {
1756 | return LSM6DS3_STATUS_ERROR;
1757 | }
1758 |
1759 | return LSM6DS3_STATUS_OK;
1760 | }
1761 |
1762 | /**
1763 | * @brief Set the tap duration of the time window for LSM6DS3 accelerometer sensor
1764 | * @param time the duration of the time window to be set
1765 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1766 | */
1767 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Set_Tap_Duration_Time(uint8_t time)
1768 | {
1769 | if ( LSM6DS3_ACC_GYRO_W_DUR( (void *)this, time ) == MEMS_ERROR )
1770 | {
1771 | return LSM6DS3_STATUS_ERROR;
1772 | }
1773 |
1774 | return LSM6DS3_STATUS_OK;
1775 | }
1776 |
1777 | /**
1778 | * @brief Enable the 6D orientation detection for LSM6DS3 accelerometer sensor
1779 | * @note This function sets the LSM6DS3 accelerometer ODR to 416Hz and the LSM6DS3 accelerometer full scale to 2g
1780 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1781 | */
1782 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Enable_6D_Orientation(void)
1783 | {
1784 | return Enable_6D_Orientation(LSM6DS3_INT1_PIN);
1785 | }
1786 |
1787 | /**
1788 | * @brief Enable the 6D orientation detection for LSM6DS3 accelerometer sensor
1789 | * @param int_pin the interrupt pin to be used
1790 | * @note This function sets the LSM6DS3 accelerometer ODR to 416Hz and the LSM6DS3 accelerometer full scale to 2g
1791 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1792 | */
1793 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Enable_6D_Orientation(LSM6DS3_Interrupt_Pin_t int_pin)
1794 | {
1795 | /* Output Data Rate selection */
1796 | if( Set_X_ODR(416.0f) == LSM6DS3_STATUS_ERROR )
1797 | {
1798 | return LSM6DS3_STATUS_ERROR;
1799 | }
1800 |
1801 | /* Full scale selection. */
1802 | if( Set_X_FS(2.0f) == LSM6DS3_STATUS_ERROR )
1803 | {
1804 | return LSM6DS3_STATUS_ERROR;
1805 | }
1806 |
1807 | /* Set 6D threshold. */
1808 | if ( LSM6DS3_ACC_GYRO_W_SIXD_THS( (void *)this, LSM6DS3_ACC_GYRO_SIXD_THS_60_degree ) == MEMS_ERROR )
1809 | {
1810 | return LSM6DS3_STATUS_ERROR;
1811 | }
1812 |
1813 | /* Enable 6D orientation on either INT1 or INT2 pin */
1814 | switch (int_pin)
1815 | {
1816 | case LSM6DS3_INT1_PIN:
1817 | if ( LSM6DS3_ACC_GYRO_W_6DEvOnInt1( (void *)this, LSM6DS3_ACC_GYRO_INT1_6D_ENABLED ) == MEMS_ERROR )
1818 | {
1819 | return LSM6DS3_STATUS_ERROR;
1820 | }
1821 | break;
1822 |
1823 | case LSM6DS3_INT2_PIN:
1824 | if ( LSM6DS3_ACC_GYRO_W_6DEvOnInt2( (void *)this, LSM6DS3_ACC_GYRO_INT2_6D_ENABLED ) == MEMS_ERROR )
1825 | {
1826 | return LSM6DS3_STATUS_ERROR;
1827 | }
1828 | break;
1829 |
1830 | default:
1831 | return LSM6DS3_STATUS_ERROR;
1832 | }
1833 |
1834 | return LSM6DS3_STATUS_OK;
1835 | }
1836 |
1837 | /**
1838 | * @brief Disable the 6D orientation detection for LSM6DS3 accelerometer sensor
1839 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1840 | */
1841 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Disable_6D_Orientation(void)
1842 | {
1843 | /* Disable 6D orientation interrupt on INT1 pin. */
1844 | if ( LSM6DS3_ACC_GYRO_W_6DEvOnInt1( (void *)this, LSM6DS3_ACC_GYRO_INT1_6D_DISABLED ) == MEMS_ERROR )
1845 | {
1846 | return LSM6DS3_STATUS_ERROR;
1847 | }
1848 |
1849 | /* Disable 6D orientation interrupt on INT2 pin. */
1850 | if ( LSM6DS3_ACC_GYRO_W_6DEvOnInt2( (void *)this, LSM6DS3_ACC_GYRO_INT2_6D_DISABLED ) == MEMS_ERROR )
1851 | {
1852 | return LSM6DS3_STATUS_ERROR;
1853 | }
1854 |
1855 | /* Reset 6D threshold. */
1856 | if ( LSM6DS3_ACC_GYRO_W_SIXD_THS( (void *)this, LSM6DS3_ACC_GYRO_SIXD_THS_80_degree ) == MEMS_ERROR )
1857 | {
1858 | return LSM6DS3_STATUS_ERROR;
1859 | }
1860 |
1861 | return LSM6DS3_STATUS_OK;
1862 | }
1863 |
1864 | /**
1865 | * @brief Get the 6D orientation XL axis for LSM6DS3 accelerometer sensor
1866 | * @param xl the pointer to the 6D orientation XL axis
1867 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1868 | */
1869 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Get_6D_Orientation_XL(uint8_t *xl)
1870 | {
1871 | LSM6DS3_ACC_GYRO_DSD_XL_t xl_raw;
1872 |
1873 | if ( LSM6DS3_ACC_GYRO_R_DSD_XL( (void *)this, &xl_raw ) == MEMS_ERROR )
1874 | {
1875 | return LSM6DS3_STATUS_ERROR;
1876 | }
1877 |
1878 | switch( xl_raw )
1879 | {
1880 | case LSM6DS3_ACC_GYRO_DSD_XL_DETECTED:
1881 | *xl = 1;
1882 | break;
1883 | case LSM6DS3_ACC_GYRO_DSD_XL_NOT_DETECTED:
1884 | *xl = 0;
1885 | break;
1886 | default:
1887 | return LSM6DS3_STATUS_ERROR;
1888 | }
1889 |
1890 | return LSM6DS3_STATUS_OK;
1891 | }
1892 |
1893 | /**
1894 | * @brief Get the 6D orientation XH axis for LSM6DS3 accelerometer sensor
1895 | * @param xh the pointer to the 6D orientation XH axis
1896 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1897 | */
1898 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Get_6D_Orientation_XH(uint8_t *xh)
1899 | {
1900 | LSM6DS3_ACC_GYRO_DSD_XH_t xh_raw;
1901 |
1902 | if ( LSM6DS3_ACC_GYRO_R_DSD_XH( (void *)this, &xh_raw ) == MEMS_ERROR )
1903 | {
1904 | return LSM6DS3_STATUS_ERROR;
1905 | }
1906 |
1907 | switch( xh_raw )
1908 | {
1909 | case LSM6DS3_ACC_GYRO_DSD_XH_DETECTED:
1910 | *xh = 1;
1911 | break;
1912 | case LSM6DS3_ACC_GYRO_DSD_XH_NOT_DETECTED:
1913 | *xh = 0;
1914 | break;
1915 | default:
1916 | return LSM6DS3_STATUS_ERROR;
1917 | }
1918 |
1919 | return LSM6DS3_STATUS_OK;
1920 | }
1921 |
1922 | /**
1923 | * @brief Get the 6D orientation YL axis for LSM6DS3 accelerometer sensor
1924 | * @param yl the pointer to the 6D orientation YL axis
1925 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1926 | */
1927 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Get_6D_Orientation_YL(uint8_t *yl)
1928 | {
1929 | LSM6DS3_ACC_GYRO_DSD_YL_t yl_raw;
1930 |
1931 | if ( LSM6DS3_ACC_GYRO_R_DSD_YL( (void *)this, &yl_raw ) == MEMS_ERROR )
1932 | {
1933 | return LSM6DS3_STATUS_ERROR;
1934 | }
1935 |
1936 | switch( yl_raw )
1937 | {
1938 | case LSM6DS3_ACC_GYRO_DSD_YL_DETECTED:
1939 | *yl = 1;
1940 | break;
1941 | case LSM6DS3_ACC_GYRO_DSD_YL_NOT_DETECTED:
1942 | *yl = 0;
1943 | break;
1944 | default:
1945 | return LSM6DS3_STATUS_ERROR;
1946 | }
1947 |
1948 | return LSM6DS3_STATUS_OK;
1949 | }
1950 |
1951 | /**
1952 | * @brief Get the 6D orientation YH axis for LSM6DS3 accelerometer sensor
1953 | * @param yh the pointer to the 6D orientation YH axis
1954 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1955 | */
1956 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Get_6D_Orientation_YH(uint8_t *yh)
1957 | {
1958 | LSM6DS3_ACC_GYRO_DSD_YH_t yh_raw;
1959 |
1960 | if ( LSM6DS3_ACC_GYRO_R_DSD_YH( (void *)this, &yh_raw ) == MEMS_ERROR )
1961 | {
1962 | return LSM6DS3_STATUS_ERROR;
1963 | }
1964 |
1965 | switch( yh_raw )
1966 | {
1967 | case LSM6DS3_ACC_GYRO_DSD_YH_DETECTED:
1968 | *yh = 1;
1969 | break;
1970 | case LSM6DS3_ACC_GYRO_DSD_YH_NOT_DETECTED:
1971 | *yh = 0;
1972 | break;
1973 | default:
1974 | return LSM6DS3_STATUS_ERROR;
1975 | }
1976 |
1977 | return LSM6DS3_STATUS_OK;
1978 | }
1979 |
1980 | /**
1981 | * @brief Get the 6D orientation ZL axis for LSM6DS3 accelerometer sensor
1982 | * @param zl the pointer to the 6D orientation ZL axis
1983 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
1984 | */
1985 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Get_6D_Orientation_ZL(uint8_t *zl)
1986 | {
1987 | LSM6DS3_ACC_GYRO_DSD_ZL_t zl_raw;
1988 |
1989 | if ( LSM6DS3_ACC_GYRO_R_DSD_ZL( (void *)this, &zl_raw ) == MEMS_ERROR )
1990 | {
1991 | return LSM6DS3_STATUS_ERROR;
1992 | }
1993 |
1994 | switch( zl_raw )
1995 | {
1996 | case LSM6DS3_ACC_GYRO_DSD_ZL_DETECTED:
1997 | *zl = 1;
1998 | break;
1999 | case LSM6DS3_ACC_GYRO_DSD_ZL_NOT_DETECTED:
2000 | *zl = 0;
2001 | break;
2002 | default:
2003 | return LSM6DS3_STATUS_ERROR;
2004 | }
2005 |
2006 | return LSM6DS3_STATUS_OK;
2007 | }
2008 |
2009 | /**
2010 | * @brief Get the 6D orientation ZH axis for LSM6DS3 accelerometer sensor
2011 | * @param zh the pointer to the 6D orientation ZH axis
2012 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
2013 | */
2014 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Get_6D_Orientation_ZH(uint8_t *zh)
2015 | {
2016 | LSM6DS3_ACC_GYRO_DSD_ZH_t zh_raw;
2017 |
2018 | if ( LSM6DS3_ACC_GYRO_R_DSD_ZH( (void *)this, &zh_raw ) == MEMS_ERROR )
2019 | {
2020 | return LSM6DS3_STATUS_ERROR;
2021 | }
2022 |
2023 | switch( zh_raw )
2024 | {
2025 | case LSM6DS3_ACC_GYRO_DSD_ZH_DETECTED:
2026 | *zh = 1;
2027 | break;
2028 | case LSM6DS3_ACC_GYRO_DSD_ZH_NOT_DETECTED:
2029 | *zh = 0;
2030 | break;
2031 | default:
2032 | return LSM6DS3_STATUS_ERROR;
2033 | }
2034 |
2035 | return LSM6DS3_STATUS_OK;
2036 | }
2037 |
2038 | /**
2039 | * @brief Get the status of all hardware events for LSM6DS3 accelerometer sensor
2040 | * @param status the pointer to the status of all hardware events
2041 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
2042 | */
2043 | LSM6DS3StatusTypeDef LSM6DS3Sensor::Get_Event_Status( LSM6DS3_Event_Status_t *status )
2044 | {
2045 | uint8_t Wake_Up_Src = 0, Tap_Src = 0, D6D_Src = 0, Func_Src = 0, Md1_Cfg = 0, Md2_Cfg = 0, Int1_Ctrl = 0;
2046 |
2047 | memset((void *)status, 0x0, sizeof(LSM6DS3_Event_Status_t));
2048 |
2049 | if(ReadReg(LSM6DS3_ACC_GYRO_WAKE_UP_SRC, &Wake_Up_Src ) == LSM6DS3_STATUS_ERROR )
2050 | {
2051 | return LSM6DS3_STATUS_ERROR;
2052 | }
2053 |
2054 | if(ReadReg(LSM6DS3_ACC_GYRO_TAP_SRC, &Tap_Src ) == LSM6DS3_STATUS_ERROR )
2055 | {
2056 | return LSM6DS3_STATUS_ERROR;
2057 | }
2058 |
2059 | if(ReadReg(LSM6DS3_ACC_GYRO_D6D_SRC, &D6D_Src ) == LSM6DS3_STATUS_ERROR )
2060 | {
2061 | return LSM6DS3_STATUS_ERROR;
2062 | }
2063 |
2064 | if(ReadReg(LSM6DS3_ACC_GYRO_FUNC_SRC, &Func_Src ) == LSM6DS3_STATUS_ERROR )
2065 | {
2066 | return LSM6DS3_STATUS_ERROR;
2067 | }
2068 |
2069 | if(ReadReg(LSM6DS3_ACC_GYRO_MD1_CFG, &Md1_Cfg ) == LSM6DS3_STATUS_ERROR )
2070 | {
2071 | return LSM6DS3_STATUS_ERROR;
2072 | }
2073 |
2074 | if(ReadReg(LSM6DS3_ACC_GYRO_MD2_CFG, &Md2_Cfg ) == LSM6DS3_STATUS_ERROR )
2075 | {
2076 | return LSM6DS3_STATUS_ERROR;
2077 | }
2078 |
2079 | if(ReadReg(LSM6DS3_ACC_GYRO_INT1_CTRL, &Int1_Ctrl ) == LSM6DS3_STATUS_ERROR )
2080 | {
2081 | return LSM6DS3_STATUS_ERROR;
2082 | }
2083 |
2084 | if((Md1_Cfg & LSM6DS3_ACC_GYRO_INT1_FF_MASK) || (Md2_Cfg & LSM6DS3_ACC_GYRO_INT2_FF_MASK))
2085 | {
2086 | if((Wake_Up_Src & LSM6DS3_ACC_GYRO_FF_EV_STATUS_MASK))
2087 | {
2088 | status->FreeFallStatus = 1;
2089 | }
2090 | }
2091 |
2092 | if((Md1_Cfg & LSM6DS3_ACC_GYRO_INT1_WU_MASK) || (Md2_Cfg & LSM6DS3_ACC_GYRO_INT2_WU_MASK))
2093 | {
2094 | if((Wake_Up_Src & LSM6DS3_ACC_GYRO_WU_EV_STATUS_MASK))
2095 | {
2096 | status->WakeUpStatus = 1;
2097 | }
2098 | }
2099 |
2100 | if((Md1_Cfg & LSM6DS3_ACC_GYRO_INT1_SINGLE_TAP_MASK) || (Md2_Cfg & LSM6DS3_ACC_GYRO_INT2_SINGLE_TAP_MASK))
2101 | {
2102 | if((Tap_Src & LSM6DS3_ACC_GYRO_SINGLE_TAP_EV_STATUS_MASK))
2103 | {
2104 | status->TapStatus = 1;
2105 | }
2106 | }
2107 |
2108 | if((Md1_Cfg & LSM6DS3_ACC_GYRO_INT1_TAP_MASK) || (Md2_Cfg & LSM6DS3_ACC_GYRO_INT2_TAP_MASK))
2109 | {
2110 | if((Tap_Src & LSM6DS3_ACC_GYRO_DOUBLE_TAP_EV_STATUS_MASK))
2111 | {
2112 | status->DoubleTapStatus = 1;
2113 | }
2114 | }
2115 |
2116 | if((Md1_Cfg & LSM6DS3_ACC_GYRO_INT1_6D_MASK) || (Md2_Cfg & LSM6DS3_ACC_GYRO_INT2_6D_MASK))
2117 | {
2118 | if((D6D_Src & LSM6DS3_ACC_GYRO_D6D_EV_STATUS_MASK))
2119 | {
2120 | status->D6DOrientationStatus = 1;
2121 | }
2122 | }
2123 |
2124 | if((Int1_Ctrl & LSM6DS3_ACC_GYRO_INT1_PEDO_MASK))
2125 | {
2126 | if((Func_Src & LSM6DS3_ACC_GYRO_PEDO_EV_STATUS_MASK))
2127 | {
2128 | status->StepStatus = 1;
2129 | }
2130 | }
2131 |
2132 | if((Md1_Cfg & LSM6DS3_ACC_GYRO_INT1_TILT_MASK) || (Md2_Cfg & LSM6DS3_ACC_GYRO_INT2_TILT_MASK))
2133 | {
2134 | if((Func_Src & LSM6DS3_ACC_GYRO_TILT_EV_STATUS_MASK))
2135 | {
2136 | status->TiltStatus = 1;
2137 | }
2138 | }
2139 |
2140 | return LSM6DS3_STATUS_OK;
2141 | }
2142 |
2143 | /**
2144 | * @brief Read the data from register
2145 | * @param reg register address
2146 | * @param data register data
2147 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
2148 | */
2149 | LSM6DS3StatusTypeDef LSM6DS3Sensor::ReadReg( uint8_t reg, uint8_t *data )
2150 | {
2151 |
2152 | if ( LSM6DS3_ACC_GYRO_ReadReg( (void *)this, reg, data, 1 ) == MEMS_ERROR )
2153 | {
2154 | return LSM6DS3_STATUS_ERROR;
2155 | }
2156 |
2157 | return LSM6DS3_STATUS_OK;
2158 | }
2159 |
2160 | /**
2161 | * @brief Write the data to register
2162 | * @param reg register address
2163 | * @param data register data
2164 | * @retval LSM6DS3_STATUS_OK in case of success, an error code otherwise
2165 | */
2166 | LSM6DS3StatusTypeDef LSM6DS3Sensor::WriteReg( uint8_t reg, uint8_t data )
2167 | {
2168 |
2169 | if ( LSM6DS3_ACC_GYRO_WriteReg( (void *)this, reg, &data, 1 ) == MEMS_ERROR )
2170 | {
2171 | return LSM6DS3_STATUS_ERROR;
2172 | }
2173 |
2174 | return LSM6DS3_STATUS_OK;
2175 | }
2176 |
2177 |
2178 | uint8_t LSM6DS3_IO_Write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite )
2179 | {
2180 | return ((LSM6DS3Sensor *)handle)->IO_Write(pBuffer, WriteAddr, nBytesToWrite);
2181 | }
2182 |
2183 | uint8_t LSM6DS3_IO_Read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead )
2184 | {
2185 | return ((LSM6DS3Sensor *)handle)->IO_Read(pBuffer, ReadAddr, nBytesToRead);
2186 | }
2187 |
--------------------------------------------------------------------------------
/src/LSM6DS3Sensor.h:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file LSM6DS3Sensor.h
4 | * @author AST
5 | * @version V1.0.0
6 | * @date 7 September 2017
7 | * @brief Abstract Class of an LSM6DS3 Inertial Measurement Unit (IMU) 6 axes
8 | * sensor.
9 | ******************************************************************************
10 | * @attention
11 | *
12 | * © COPYRIGHT(c) 2017 STMicroelectronics
13 | *
14 | * Redistribution and use in source and binary forms, with or without modification,
15 | * are permitted provided that the following conditions are met:
16 | * 1. Redistributions of source code must retain the above copyright notice,
17 | * this list of conditions and the following disclaimer.
18 | * 2. Redistributions in binary form must reproduce the above copyright notice,
19 | * this list of conditions and the following disclaimer in the documentation
20 | * and/or other materials provided with the distribution.
21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
22 | * may be used to endorse or promote products derived from this software
23 | * without specific prior written permission.
24 | *
25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 | *
36 | ******************************************************************************
37 | */
38 |
39 |
40 | /* Prevent recursive inclusion -----------------------------------------------*/
41 |
42 | #ifndef __LSM6DS3Sensor_H__
43 | #define __LSM6DS3Sensor_H__
44 |
45 |
46 | /* Includes ------------------------------------------------------------------*/
47 |
48 | #include "Wire.h"
49 | #include "SPI.h"
50 | #include "LSM6DS3_ACC_GYRO_Driver.h"
51 |
52 | /* Defines -------------------------------------------------------------------*/
53 |
54 | #define LSM6DS3_ACC_SENSITIVITY_FOR_FS_2G 0.061 /**< Sensitivity value for 2 g full scale [mg/LSB] */
55 | #define LSM6DS3_ACC_SENSITIVITY_FOR_FS_4G 0.122 /**< Sensitivity value for 4 g full scale [mg/LSB] */
56 | #define LSM6DS3_ACC_SENSITIVITY_FOR_FS_8G 0.244 /**< Sensitivity value for 8 g full scale [mg/LSB] */
57 | #define LSM6DS3_ACC_SENSITIVITY_FOR_FS_16G 0.488 /**< Sensitivity value for 16 g full scale [mg/LSB] */
58 |
59 | #define LSM6DS3_GYRO_SENSITIVITY_FOR_FS_125DPS 04.375 /**< Sensitivity value for 125 dps full scale [mdps/LSB] */
60 | #define LSM6DS3_GYRO_SENSITIVITY_FOR_FS_245DPS 08.750 /**< Sensitivity value for 245 dps full scale [mdps/LSB] */
61 | #define LSM6DS3_GYRO_SENSITIVITY_FOR_FS_500DPS 17.500 /**< Sensitivity value for 500 dps full scale [mdps/LSB] */
62 | #define LSM6DS3_GYRO_SENSITIVITY_FOR_FS_1000DPS 35.000 /**< Sensitivity value for 1000 dps full scale [mdps/LSB] */
63 | #define LSM6DS3_GYRO_SENSITIVITY_FOR_FS_2000DPS 70.000 /**< Sensitivity value for 2000 dps full scale [mdps/LSB] */
64 |
65 | #define LSM6DS3_PEDOMETER_THRESHOLD_LOW 0x00 /**< Lowest value of pedometer threshold */
66 | #define LSM6DS3_PEDOMETER_THRESHOLD_MID_LOW 0x07
67 | #define LSM6DS3_PEDOMETER_THRESHOLD_MID 0x0F
68 | #define LSM6DS3_PEDOMETER_THRESHOLD_MID_HIGH 0x17
69 | #define LSM6DS3_PEDOMETER_THRESHOLD_HIGH 0x1F /**< Highest value of pedometer threshold */
70 |
71 | #define LSM6DS3_WAKE_UP_THRESHOLD_LOW 0x01 /**< Lowest value of wake up threshold */
72 | #define LSM6DS3_WAKE_UP_THRESHOLD_MID_LOW 0x0F
73 | #define LSM6DS3_WAKE_UP_THRESHOLD_MID 0x1F
74 | #define LSM6DS3_WAKE_UP_THRESHOLD_MID_HIGH 0x2F
75 | #define LSM6DS3_WAKE_UP_THRESHOLD_HIGH 0x3F /**< Highest value of wake up threshold */
76 |
77 | #define LSM6DS3_TAP_THRESHOLD_LOW 0x01 /**< Lowest value of wake up threshold */
78 | #define LSM6DS3_TAP_THRESHOLD_MID_LOW 0x08
79 | #define LSM6DS3_TAP_THRESHOLD_MID 0x10
80 | #define LSM6DS3_TAP_THRESHOLD_MID_HIGH 0x18
81 | #define LSM6DS3_TAP_THRESHOLD_HIGH 0x1F /**< Highest value of wake up threshold */
82 |
83 | #define LSM6DS3_TAP_SHOCK_TIME_LOW 0x00 /**< Lowest value of wake up threshold */
84 | #define LSM6DS3_TAP_SHOCK_TIME_MID_LOW 0x01
85 | #define LSM6DS3_TAP_SHOCK_TIME_MID_HIGH 0x02
86 | #define LSM6DS3_TAP_SHOCK_TIME_HIGH 0x03 /**< Highest value of wake up threshold */
87 |
88 | #define LSM6DS3_TAP_QUIET_TIME_LOW 0x00 /**< Lowest value of wake up threshold */
89 | #define LSM6DS3_TAP_QUIET_TIME_MID_LOW 0x01
90 | #define LSM6DS3_TAP_QUIET_TIME_MID_HIGH 0x02
91 | #define LSM6DS3_TAP_QUIET_TIME_HIGH 0x03 /**< Highest value of wake up threshold */
92 |
93 | #define LSM6DS3_TAP_DURATION_TIME_LOW 0x00 /**< Lowest value of wake up threshold */
94 | #define LSM6DS3_TAP_DURATION_TIME_MID_LOW 0x04
95 | #define LSM6DS3_TAP_DURATION_TIME_MID 0x08
96 | #define LSM6DS3_TAP_DURATION_TIME_MID_HIGH 0x0C
97 | #define LSM6DS3_TAP_DURATION_TIME_HIGH 0x0F /**< Highest value of wake up threshold */
98 |
99 | /* Typedefs ------------------------------------------------------------------*/
100 | typedef enum
101 | {
102 | LSM6DS3_STATUS_OK = 0,
103 | LSM6DS3_STATUS_ERROR,
104 | LSM6DS3_STATUS_TIMEOUT,
105 | LSM6DS3_STATUS_NOT_IMPLEMENTED
106 | } LSM6DS3StatusTypeDef;
107 |
108 | typedef enum
109 | {
110 | LSM6DS3_INT1_PIN,
111 | LSM6DS3_INT2_PIN
112 | } LSM6DS3_Interrupt_Pin_t;
113 |
114 | typedef struct
115 | {
116 | unsigned int FreeFallStatus : 1;
117 | unsigned int TapStatus : 1;
118 | unsigned int DoubleTapStatus : 1;
119 | unsigned int WakeUpStatus : 1;
120 | unsigned int StepStatus : 1;
121 | unsigned int TiltStatus : 1;
122 | unsigned int D6DOrientationStatus : 1;
123 | } LSM6DS3_Event_Status_t;
124 |
125 | /* Class Declaration ---------------------------------------------------------*/
126 |
127 | /**
128 | * Abstract class of an LSM6DS3 Inertial Measurement Unit (IMU) 6 axes
129 | * sensor.
130 | */
131 | class LSM6DS3Sensor
132 | {
133 | public:
134 | LSM6DS3Sensor (TwoWire *i2c, uint8_t address=LSM6DS3_ACC_GYRO_I2C_ADDRESS_HIGH);
135 | LSM6DS3Sensor (SPIClass *spi, int cs_pin, uint32_t spi_speed=2000000);
136 | LSM6DS3StatusTypeDef begin (void);
137 | LSM6DS3StatusTypeDef end (void);
138 | LSM6DS3StatusTypeDef Enable_X (void);
139 | LSM6DS3StatusTypeDef Enable_G (void);
140 | LSM6DS3StatusTypeDef Disable_X (void);
141 | LSM6DS3StatusTypeDef Disable_G (void);
142 | LSM6DS3StatusTypeDef ReadID (uint8_t *p_id);
143 | LSM6DS3StatusTypeDef Get_X_Axes (int32_t *pData);
144 | LSM6DS3StatusTypeDef Get_G_Axes (int32_t *pData);
145 | LSM6DS3StatusTypeDef Get_X_Sensitivity (float *pfData);
146 | LSM6DS3StatusTypeDef Get_G_Sensitivity (float *pfData);
147 | LSM6DS3StatusTypeDef Get_X_AxesRaw (int16_t *pData);
148 | LSM6DS3StatusTypeDef Get_G_AxesRaw (int16_t *pData);
149 | LSM6DS3StatusTypeDef Get_X_ODR (float *odr);
150 | LSM6DS3StatusTypeDef Get_G_ODR (float *odr);
151 | LSM6DS3StatusTypeDef Set_X_ODR (float odr);
152 | LSM6DS3StatusTypeDef Set_G_ODR (float odr);
153 | LSM6DS3StatusTypeDef Get_X_FS (float *fullScale);
154 | LSM6DS3StatusTypeDef Get_G_FS (float *fullScale);
155 | LSM6DS3StatusTypeDef Set_X_FS (float fullScale);
156 | LSM6DS3StatusTypeDef Set_G_FS (float fullScale);
157 | LSM6DS3StatusTypeDef Enable_Free_Fall_Detection (void);
158 | LSM6DS3StatusTypeDef Enable_Free_Fall_Detection (LSM6DS3_Interrupt_Pin_t int_pin);
159 | LSM6DS3StatusTypeDef Disable_Free_Fall_Detection (void);
160 | LSM6DS3StatusTypeDef Set_Free_Fall_Threshold (uint8_t thr);
161 | LSM6DS3StatusTypeDef Enable_Pedometer (void);
162 | LSM6DS3StatusTypeDef Disable_Pedometer (void);
163 | LSM6DS3StatusTypeDef Get_Step_Counter (uint16_t *step_count);
164 | LSM6DS3StatusTypeDef Reset_Step_Counter (void);
165 | LSM6DS3StatusTypeDef Set_Pedometer_Threshold (uint8_t thr);
166 | LSM6DS3StatusTypeDef Enable_Tilt_Detection (void);
167 | LSM6DS3StatusTypeDef Enable_Tilt_Detection (LSM6DS3_Interrupt_Pin_t int_pin);
168 | LSM6DS3StatusTypeDef Disable_Tilt_Detection (void);
169 | LSM6DS3StatusTypeDef Enable_Wake_Up_Detection (void);
170 | LSM6DS3StatusTypeDef Enable_Wake_Up_Detection (LSM6DS3_Interrupt_Pin_t int_pin);
171 | LSM6DS3StatusTypeDef Disable_Wake_Up_Detection (void);
172 | LSM6DS3StatusTypeDef Set_Wake_Up_Threshold (uint8_t thr);
173 | LSM6DS3StatusTypeDef Enable_Single_Tap_Detection (void);
174 | LSM6DS3StatusTypeDef Enable_Single_Tap_Detection (LSM6DS3_Interrupt_Pin_t int_pin);
175 | LSM6DS3StatusTypeDef Disable_Single_Tap_Detection (void);
176 | LSM6DS3StatusTypeDef Enable_Double_Tap_Detection (void);
177 | LSM6DS3StatusTypeDef Enable_Double_Tap_Detection (LSM6DS3_Interrupt_Pin_t int_pin);
178 | LSM6DS3StatusTypeDef Disable_Double_Tap_Detection (void);
179 | LSM6DS3StatusTypeDef Set_Tap_Threshold (uint8_t thr);
180 | LSM6DS3StatusTypeDef Set_Tap_Shock_Time (uint8_t time);
181 | LSM6DS3StatusTypeDef Set_Tap_Quiet_Time (uint8_t time);
182 | LSM6DS3StatusTypeDef Set_Tap_Duration_Time (uint8_t time);
183 | LSM6DS3StatusTypeDef Enable_6D_Orientation (void);
184 | LSM6DS3StatusTypeDef Enable_6D_Orientation (LSM6DS3_Interrupt_Pin_t int_pin);
185 | LSM6DS3StatusTypeDef Disable_6D_Orientation (void);
186 | LSM6DS3StatusTypeDef Get_6D_Orientation_XL (uint8_t *xl);
187 | LSM6DS3StatusTypeDef Get_6D_Orientation_XH (uint8_t *xh);
188 | LSM6DS3StatusTypeDef Get_6D_Orientation_YL (uint8_t *yl);
189 | LSM6DS3StatusTypeDef Get_6D_Orientation_YH (uint8_t *yh);
190 | LSM6DS3StatusTypeDef Get_6D_Orientation_ZL (uint8_t *zl);
191 | LSM6DS3StatusTypeDef Get_6D_Orientation_ZH (uint8_t *zh);
192 | LSM6DS3StatusTypeDef Get_Event_Status (LSM6DS3_Event_Status_t *status);
193 | LSM6DS3StatusTypeDef ReadReg (uint8_t reg, uint8_t *data);
194 | LSM6DS3StatusTypeDef WriteReg (uint8_t reg, uint8_t data);
195 |
196 | /**
197 | * @brief Utility function to read data.
198 | * @param pBuffer: pointer to data to be read.
199 | * @param RegisterAddr: specifies internal address register to be read.
200 | * @param NumByteToRead: number of bytes to be read.
201 | * @retval 0 if ok, an error code otherwise.
202 | */
203 | uint8_t IO_Read(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead)
204 | {
205 | if (dev_spi) {
206 | dev_spi->beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE3));
207 |
208 | digitalWrite(cs_pin, LOW);
209 |
210 | /* Write Reg Address */
211 | dev_spi->transfer(RegisterAddr | 0x80);
212 | /* Read the data */
213 | for (uint16_t i=0; itransfer(0x00);
215 | }
216 |
217 | digitalWrite(cs_pin, HIGH);
218 |
219 | dev_spi->endTransaction();
220 |
221 | return 0;
222 | }
223 |
224 | if (dev_i2c) {
225 | dev_i2c->beginTransmission(((uint8_t)(((address) >> 1) & 0x7F)));
226 | dev_i2c->write(RegisterAddr);
227 | dev_i2c->endTransmission(false);
228 |
229 | dev_i2c->requestFrom(((uint8_t)(((address) >> 1) & 0x7F)), (byte) NumByteToRead);
230 |
231 | int i=0;
232 | while (dev_i2c->available())
233 | {
234 | pBuffer[i] = dev_i2c->read();
235 | i++;
236 | }
237 |
238 | return 0;
239 | }
240 |
241 | return 1;
242 | }
243 |
244 | /**
245 | * @brief Utility function to write data.
246 | * @param pBuffer: pointer to data to be written.
247 | * @param RegisterAddr: specifies internal address register to be written.
248 | * @param NumByteToWrite: number of bytes to write.
249 | * @retval 0 if ok, an error code otherwise.
250 | */
251 | uint8_t IO_Write(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite)
252 | {
253 | if (dev_spi) {
254 | dev_spi->beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE3));
255 |
256 | digitalWrite(cs_pin, LOW);
257 |
258 | /* Write Reg Address */
259 | dev_spi->transfer(RegisterAddr);
260 | /* Write the data */
261 | for (uint16_t i=0; itransfer(pBuffer[i]);
263 | }
264 |
265 | digitalWrite(cs_pin, HIGH);
266 |
267 | dev_spi->endTransaction();
268 |
269 | return 0;
270 | }
271 |
272 | if (dev_i2c) {
273 | dev_i2c->beginTransmission(((uint8_t)(((address) >> 1) & 0x7F)));
274 |
275 | dev_i2c->write(RegisterAddr);
276 | for (int i = 0 ; i < NumByteToWrite ; i++)
277 | dev_i2c->write(pBuffer[i]);
278 |
279 | dev_i2c->endTransmission(true);
280 |
281 | return 0;
282 | }
283 |
284 | return 1;
285 | }
286 |
287 | private:
288 | LSM6DS3StatusTypeDef Set_X_ODR_When_Enabled(float odr);
289 | LSM6DS3StatusTypeDef Set_G_ODR_When_Enabled(float odr);
290 | LSM6DS3StatusTypeDef Set_X_ODR_When_Disabled(float odr);
291 | LSM6DS3StatusTypeDef Set_G_ODR_When_Disabled(float odr);
292 |
293 | /* Helper classes. */
294 | TwoWire *dev_i2c;
295 | SPIClass *dev_spi;
296 |
297 | /* Configuration */
298 | uint8_t address;
299 | int cs_pin;
300 | uint32_t spi_speed;
301 |
302 | uint8_t X_isEnabled;
303 | float X_Last_ODR;
304 | uint8_t G_isEnabled;
305 | float G_Last_ODR;
306 | };
307 |
308 | #ifdef __cplusplus
309 | extern "C" {
310 | #endif
311 | uint8_t LSM6DS3_IO_Write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite );
312 | uint8_t LSM6DS3_IO_Read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead );
313 | #ifdef __cplusplus
314 | }
315 | #endif
316 |
317 | #endif
--------------------------------------------------------------------------------