@@ -54,67 +54,8 @@ LSM6DSOXSensor::LSM6DSOXSensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), ad
5454 reg_ctx.write_reg = LSM6DSOX_io_write;
5555 reg_ctx.read_reg = LSM6DSOX_io_read;
5656 reg_ctx.handle = (void *)this ;
57-
58- /* Disable I3C */
59- if (lsm6dsox_i3c_disable_set (®_ctx, LSM6DSOX_I3C_DISABLE) != LSM6DSOX_OK)
60- {
61- return ;
62- }
63-
64- /* Enable register address automatically incremented during a multiple byte
65- access with a serial interface. */
66- if (lsm6dsox_auto_increment_set (®_ctx, PROPERTY_ENABLE) != LSM6DSOX_OK)
67- {
68- return ;
69- }
70-
71- /* Enable BDU */
72- if (lsm6dsox_block_data_update_set (®_ctx, PROPERTY_ENABLE) != LSM6DSOX_OK)
73- {
74- return ;
75- }
76-
77- /* FIFO mode selection */
78- if (lsm6dsox_fifo_mode_set (®_ctx, LSM6DSOX_BYPASS_MODE) != LSM6DSOX_OK)
79- {
80- return ;
81- }
82-
83- /* Select default output data rate. */
84- acc_odr = LSM6DSOX_XL_ODR_104Hz;
85-
86- /* Output data rate selection - power down. */
87- if (lsm6dsox_xl_data_rate_set (®_ctx, LSM6DSOX_XL_ODR_OFF) != LSM6DSOX_OK)
88- {
89- return ;
90- }
91-
92- /* Full scale selection. */
93- if (lsm6dsox_xl_full_scale_set (®_ctx, LSM6DSOX_2g) != LSM6DSOX_OK)
94- {
95- return ;
96- }
97-
98- /* Select default output data rate. */
99- gyro_odr = LSM6DSOX_GY_ODR_104Hz;
100-
101- /* Output data rate selection - power down. */
102- if (lsm6dsox_gy_data_rate_set (®_ctx, LSM6DSOX_GY_ODR_OFF) != LSM6DSOX_OK)
103- {
104- return ;
105- }
106-
107- /* Full scale selection. */
108- if (lsm6dsox_gy_full_scale_set (®_ctx, LSM6DSOX_2000dps) != LSM6DSOX_OK)
109- {
110- return ;
111- }
112-
113- acc_is_enabled = 0 ;
114- gyro_is_enabled = 0 ;
115-
116-
117- return ;
57+ acc_is_enabled = 0U ;
58+ gyro_is_enabled = 0U ;
11859}
11960
12061/* * Constructor
@@ -127,36 +68,48 @@ LSM6DSOXSensor::LSM6DSOXSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) :
12768 reg_ctx.write_reg = LSM6DSOX_io_write;
12869 reg_ctx.read_reg = LSM6DSOX_io_read;
12970 reg_ctx.handle = (void *)this ;
130-
131- // Configure CS pin
132- pinMode (cs_pin, OUTPUT);
133- digitalWrite (cs_pin, HIGH);
13471 dev_i2c = NULL ;
135- address = 0 ;
72+ address = 0 ;
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+ LSM6DSOXStatusTypeDef LSM6DSOXSensor::begin ()
82+ {
83+ if (dev_spi)
84+ {
85+ // Configure CS pin
86+ pinMode (cs_pin, OUTPUT);
87+ digitalWrite (cs_pin, HIGH);
88+ }
13689
13790 /* Disable I3C */
13891 if (lsm6dsox_i3c_disable_set (®_ctx, LSM6DSOX_I3C_DISABLE) != LSM6DSOX_OK)
13992 {
140- return ;
93+ return LSM6DSOX_ERROR ;
14194 }
14295
14396 /* Enable register address automatically incremented during a multiple byte
14497 access with a serial interface. */
14598 if (lsm6dsox_auto_increment_set (®_ctx, PROPERTY_ENABLE) != LSM6DSOX_OK)
14699 {
147- return ;
100+ return LSM6DSOX_ERROR ;
148101 }
149102
150103 /* Enable BDU */
151104 if (lsm6dsox_block_data_update_set (®_ctx, PROPERTY_ENABLE) != LSM6DSOX_OK)
152105 {
153- return ;
106+ return LSM6DSOX_ERROR ;
154107 }
155108
156109 /* FIFO mode selection */
157110 if (lsm6dsox_fifo_mode_set (®_ctx, LSM6DSOX_BYPASS_MODE) != LSM6DSOX_OK)
158111 {
159- return ;
112+ return LSM6DSOX_ERROR ;
160113 }
161114
162115 /* Select default output data rate. */
@@ -165,13 +118,13 @@ LSM6DSOXSensor::LSM6DSOXSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) :
165118 /* Output data rate selection - power down. */
166119 if (lsm6dsox_xl_data_rate_set (®_ctx, LSM6DSOX_XL_ODR_OFF) != LSM6DSOX_OK)
167120 {
168- return ;
121+ return LSM6DSOX_ERROR ;
169122 }
170123
171124 /* Full scale selection. */
172125 if (lsm6dsox_xl_full_scale_set (®_ctx, LSM6DSOX_2g) != LSM6DSOX_OK)
173126 {
174- return ;
127+ return LSM6DSOX_ERROR ;
175128 }
176129
177130 /* Select default output data rate. */
@@ -180,21 +133,46 @@ LSM6DSOXSensor::LSM6DSOXSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) :
180133 /* Output data rate selection - power down. */
181134 if (lsm6dsox_gy_data_rate_set (®_ctx, LSM6DSOX_GY_ODR_OFF) != LSM6DSOX_OK)
182135 {
183- return ;
136+ return LSM6DSOX_ERROR ;
184137 }
185138
186139 /* Full scale selection. */
187140 if (lsm6dsox_gy_full_scale_set (®_ctx, LSM6DSOX_2000dps) != LSM6DSOX_OK)
188141 {
189- return ;
142+ return LSM6DSOX_ERROR ;
190143 }
191144
192145 acc_is_enabled = 0 ;
193146 gyro_is_enabled = 0 ;
194147
195-
196- return ;
197-
148+ return LSM6DSOX_OK;
149+ }
150+
151+ /* *
152+ * @brief Disable the sensor and relative resources
153+ * @retval 0 in case of success, an error code otherwise
154+ */
155+ LSM6DSOXStatusTypeDef LSM6DSOXSensor::end ()
156+ {
157+ /* Disable both acc and gyro */
158+ if (Disable_X () != LSM6DSOX_OK)
159+ {
160+ return LSM6DSOX_ERROR;
161+ }
162+
163+ if (Disable_G () != LSM6DSOX_OK)
164+ {
165+ return LSM6DSOX_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 LSM6DSOX_OK;
198176}
199177
200178
0 commit comments