@@ -54,66 +54,8 @@ LSM6DSRSensor::LSM6DSRSensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), addr
5454 reg_ctx.write_reg = LSM6DSR_io_write;
5555 reg_ctx.read_reg = LSM6DSR_io_read;
5656 reg_ctx.handle = (void *)this ;
57-
58- /* Disable I3C */
59- if (lsm6dsr_i3c_disable_set (®_ctx, LSM6DSR_I3C_DISABLE) != LSM6DSR_OK)
60- {
61- return ;
62- }
63-
64- /* Enable register address automatically incremented during a multiple byte
65- access with a serial interface. */
66- if (lsm6dsr_auto_increment_set (®_ctx, PROPERTY_ENABLE) != LSM6DSR_OK)
67- {
68- return ;
69- }
70-
71- /* Enable BDU */
72- if (lsm6dsr_block_data_update_set (®_ctx, PROPERTY_ENABLE) != LSM6DSR_OK)
73- {
74- return ;
75- }
76-
77- /* FIFO mode selection */
78- if (lsm6dsr_fifo_mode_set (®_ctx, LSM6DSR_BYPASS_MODE) != LSM6DSR_OK)
79- {
80- return ;
81- }
82-
83- /* Select default output data rate. */
84- acc_odr = LSM6DSR_XL_ODR_104Hz;
85-
86- /* Output data rate selection - power down. */
87- if (lsm6dsr_xl_data_rate_set (®_ctx, LSM6DSR_XL_ODR_OFF) != LSM6DSR_OK)
88- {
89- return ;
90- }
91-
92- /* Full scale selection. */
93- if (lsm6dsr_xl_full_scale_set (®_ctx, LSM6DSR_2g) != LSM6DSR_OK)
94- {
95- return ;
96- }
97-
98- /* Select default output data rate. */
99- gyro_odr = LSM6DSR_GY_ODR_104Hz;
100-
101- /* Output data rate selection - power down. */
102- if (lsm6dsr_gy_data_rate_set (®_ctx, LSM6DSR_GY_ODR_OFF) != LSM6DSR_OK)
103- {
104- return ;
105- }
106-
107- /* Full scale selection. */
108- if (lsm6dsr_gy_full_scale_set (®_ctx, LSM6DSR_2000dps) != LSM6DSR_OK)
109- {
110- return ;
111- }
112-
113- acc_is_enabled = 0 ;
114- gyro_is_enabled = 0 ;
115-
116- return ;
57+ acc_is_enabled = 0U ;
58+ gyro_is_enabled = 0U ;
11759}
11860
11961/* * Constructor
@@ -126,36 +68,48 @@ LSM6DSRSensor::LSM6DSRSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : de
12668 reg_ctx.write_reg = LSM6DSR_io_write;
12769 reg_ctx.read_reg = LSM6DSR_io_read;
12870 reg_ctx.handle = (void *)this ;
129-
130- // Configure CS pin
131- pinMode (cs_pin, OUTPUT);
132- digitalWrite (cs_pin, HIGH);
13371 dev_i2c = NULL ;
134- address = 0 ;
72+ address = 0U ;
73+ acc_is_enabled = 0U ;
74+ gyro_is_enabled = 0U ;
75+ }
76+
77+ /* *
78+ * @brief Configure the sensor in order to be used
79+ * @retval 0 in case of success, an error code otherwise
80+ */
81+ LSM6DSRStatusTypeDef LSM6DSRSensor::begin ()
82+ {
83+ if (dev_spi)
84+ {
85+ // Configure CS pin
86+ pinMode (cs_pin, OUTPUT);
87+ digitalWrite (cs_pin, HIGH);
88+ }
13589
13690 /* Disable I3C */
13791 if (lsm6dsr_i3c_disable_set (®_ctx, LSM6DSR_I3C_DISABLE) != LSM6DSR_OK)
13892 {
139- return ;
93+ return LSM6DSR_ERROR ;
14094 }
14195
14296 /* Enable register address automatically incremented during a multiple byte
14397 access with a serial interface. */
14498 if (lsm6dsr_auto_increment_set (®_ctx, PROPERTY_ENABLE) != LSM6DSR_OK)
14599 {
146- return ;
100+ return LSM6DSR_ERROR ;
147101 }
148102
149103 /* Enable BDU */
150104 if (lsm6dsr_block_data_update_set (®_ctx, PROPERTY_ENABLE) != LSM6DSR_OK)
151105 {
152- return ;
106+ return LSM6DSR_ERROR ;
153107 }
154108
155109 /* FIFO mode selection */
156110 if (lsm6dsr_fifo_mode_set (®_ctx, LSM6DSR_BYPASS_MODE) != LSM6DSR_OK)
157111 {
158- return ;
112+ return LSM6DSR_ERROR ;
159113 }
160114
161115 /* Select default output data rate. */
@@ -164,13 +118,13 @@ LSM6DSRSensor::LSM6DSRSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : de
164118 /* Output data rate selection - power down. */
165119 if (lsm6dsr_xl_data_rate_set (®_ctx, LSM6DSR_XL_ODR_OFF) != LSM6DSR_OK)
166120 {
167- return ;
121+ return LSM6DSR_ERROR ;
168122 }
169123
170124 /* Full scale selection. */
171125 if (lsm6dsr_xl_full_scale_set (®_ctx, LSM6DSR_2g) != LSM6DSR_OK)
172126 {
173- return ;
127+ return LSM6DSR_ERROR ;
174128 }
175129
176130 /* Select default output data rate. */
@@ -179,21 +133,47 @@ LSM6DSRSensor::LSM6DSRSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : de
179133 /* Output data rate selection - power down. */
180134 if (lsm6dsr_gy_data_rate_set (®_ctx, LSM6DSR_GY_ODR_OFF) != LSM6DSR_OK)
181135 {
182- return ;
136+ return LSM6DSR_ERROR ;
183137 }
184138
185139 /* Full scale selection. */
186140 if (lsm6dsr_gy_full_scale_set (®_ctx, LSM6DSR_2000dps) != LSM6DSR_OK)
187141 {
188- return ;
142+ return LSM6DSR_ERROR ;
189143 }
190144
191145 acc_is_enabled = 0 ;
192146 gyro_is_enabled = 0 ;
193147
194- return ;
148+ return LSM6DSR_OK ;
195149}
196150
151+ /* *
152+ * @brief Disable the sensor and relative resources
153+ * @retval 0 in case of success, an error code otherwise
154+ */
155+ LSM6DSRStatusTypeDef LSM6DSRSensor::end ()
156+ {
157+ /* Disable both acc and gyro */
158+ if (Disable_X () != LSM6DSR_OK)
159+ {
160+ return LSM6DSR_ERROR;
161+ }
162+
163+ if (Disable_G () != LSM6DSR_OK)
164+ {
165+ return LSM6DSR_ERROR;
166+ }
167+
168+ /* Reset CS configuration */
169+ if (dev_spi)
170+ {
171+ // Configure CS pin
172+ pinMode (cs_pin, INPUT);
173+ }
174+
175+ return LSM6DSR_OK;
176+ }
197177
198178/* *
199179 * @brief Read component ID
0 commit comments