@@ -54,54 +54,7 @@ LIS2DW12Sensor::LIS2DW12Sensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), ad
5454 reg_ctx.write_reg = LIS2DW12_io_write;
5555 reg_ctx.read_reg = LIS2DW12_io_read;
5656 reg_ctx.handle = (void *)this ;
57-
58- /* Enable register address automatically incremented during a multiple byte
59- access with a serial interface. */
60- if (lis2dw12_auto_increment_set (®_ctx, PROPERTY_ENABLE) != 0 )
61- {
62- return ;
63- }
64-
65- /* Enable BDU */
66- if (lis2dw12_block_data_update_set (®_ctx, PROPERTY_ENABLE) != 0 )
67- {
68- return ;
69- }
70-
71- /* FIFO mode selection */
72- if (lis2dw12_fifo_mode_set (®_ctx, LIS2DW12_BYPASS_MODE) != 0 )
73- {
74- return ;
75- }
76-
77- /* Power mode selection */
78- if (lis2dw12_power_mode_set (®_ctx, LIS2DW12_HIGH_PERFORMANCE) != 0 )
79- {
80- return ;
81- }
82-
83- /* Output data rate selection - power down. */
84- if (lis2dw12_data_rate_set (®_ctx, LIS2DW12_XL_ODR_OFF) != 0 )
85- {
86- return ;
87- }
88-
89- /* Full scale selection. */
90- if (lis2dw12_full_scale_set (®_ctx, LIS2DW12_2g) != 0 )
91- {
92- return ;
93- }
94-
95- /* Select default output data rate. */
96- X_Last_ODR = 100 .0f ;
97-
98- X_Last_Operating_Mode = LIS2DW12_HIGH_PERFORMANCE_MODE;
99-
100- X_Last_Noise = LIS2DW12_LOW_NOISE_DISABLE;
101-
10257 X_isEnabled = 0 ;
103-
104- return ;
10558}
10659
10760/* * Constructor
@@ -114,54 +67,59 @@ LIS2DW12Sensor::LIS2DW12Sensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) :
11467 reg_ctx.write_reg = LIS2DW12_io_write;
11568 reg_ctx.read_reg = LIS2DW12_io_read;
11669 reg_ctx.handle = (void *)this ;
117-
118- // Configure CS pin
119- pinMode (cs_pin, OUTPUT);
120- digitalWrite (cs_pin, HIGH);
12170 dev_i2c = NULL ;
12271 address = 0 ;
72+ X_isEnabled = 0 ;
73+ }
12374
124- /* Disable I2C on the component */
125- if (lis2dw12_i2c_interface_set (®_ctx, LIS2DW12_I2C_DISABLE) != 0 )
75+ /* *
76+ * @brief Configure the sensor in order to be used
77+ * @retval 0 in case of success, an error code otherwise
78+ */
79+ LIS2DW12StatusTypeDef LIS2DW12Sensor::begin ()
80+ {
81+ if (dev_spi)
12682 {
127- return ;
83+ // Configure CS pin
84+ pinMode (cs_pin, OUTPUT);
85+ digitalWrite (cs_pin, HIGH);
12886 }
12987
13088 /* Enable register address automatically incremented during a multiple byte
13189 access with a serial interface. */
13290 if (lis2dw12_auto_increment_set (®_ctx, PROPERTY_ENABLE) != 0 )
13391 {
134- return ;
92+ return LIS2DW12_STATUS_ERROR ;
13593 }
13694
13795 /* Enable BDU */
13896 if (lis2dw12_block_data_update_set (®_ctx, PROPERTY_ENABLE) != 0 )
13997 {
140- return ;
98+ return LIS2DW12_STATUS_ERROR ;
14199 }
142100
143101 /* FIFO mode selection */
144102 if (lis2dw12_fifo_mode_set (®_ctx, LIS2DW12_BYPASS_MODE) != 0 )
145103 {
146- return ;
104+ return LIS2DW12_STATUS_ERROR ;
147105 }
148106
149107 /* Power mode selection */
150108 if (lis2dw12_power_mode_set (®_ctx, LIS2DW12_HIGH_PERFORMANCE) != 0 )
151109 {
152- return ;
110+ return LIS2DW12_STATUS_ERROR ;
153111 }
154112
155113 /* Output data rate selection - power down. */
156114 if (lis2dw12_data_rate_set (®_ctx, LIS2DW12_XL_ODR_OFF) != 0 )
157115 {
158- return ;
116+ return LIS2DW12_STATUS_ERROR ;
159117 }
160118
161119 /* Full scale selection. */
162120 if (lis2dw12_full_scale_set (®_ctx, LIS2DW12_2g) != 0 )
163121 {
164- return ;
122+ return LIS2DW12_STATUS_ERROR ;
165123 }
166124
167125 /* Select default output data rate. */
@@ -173,7 +131,29 @@ LIS2DW12Sensor::LIS2DW12Sensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) :
173131
174132 X_isEnabled = 0 ;
175133
176- return ;
134+ return LIS2DW12_STATUS_OK;
135+ }
136+
137+ /* *
138+ * @brief Disable the sensor and relative resources
139+ * @retval 0 in case of success, an error code otherwise
140+ */
141+ LIS2DW12StatusTypeDef LIS2DW12Sensor::end ()
142+ {
143+ /* Disable acc */
144+ if (Disable_X () != LIS2DW12_STATUS_OK)
145+ {
146+ return LIS2DW12_STATUS_ERROR;
147+ }
148+
149+ /* Reset CS configuration */
150+ if (dev_spi)
151+ {
152+ // Configure CS pin
153+ pinMode (cs_pin, INPUT);
154+ }
155+
156+ return LIS2DW12_STATUS_OK;
177157}
178158
179159/* *
0 commit comments