@@ -113,49 +113,125 @@ WipperSnapper_Component_I2C::scanAddresses() {
113
113
114
114
/* ******************************************************************************/
115
115
/* !
116
- @brief Initializes I2C device driver and attaches its object to the "bus"
116
+ @brief Initializes I2C device driver.
117
117
@param msgDeviceInitReq
118
118
A decoded I2CDevice initialization request message.
119
119
@returns True if I2C device is initialized and attached, False otherwise.
120
120
*/
121
121
/* ******************************************************************************/
122
- bool WipperSnapper_Component_I2C::attachI2CDevice (
122
+ bool WipperSnapper_Component_I2C::initI2CDevice (
123
123
wippersnapper_i2c_v1_I2CDeviceInitRequest *msgDeviceInitReq) {
124
- bool attachSuccess = false ;
125
- // Determine which sensor-specific callback to utilize
126
124
127
- // AHTX0 Sensor
125
+ uint16_t i2cAddress = (uint16_t )msgDeviceInitReq->i2c_address ;
126
+ // Determine which sensor-specific callback to utilize
128
127
if (msgDeviceInitReq->has_aht_init ) {
129
- // TODO: Implement handling in future release
130
- /* uint16_t addr = (uint16_t)msgDeviceInitReq->aht_init.address;
131
- WS_DEBUG_PRINTLN("Requesting to initialize AHTx sensor");
132
- WS_DEBUG_PRINT("\tSensor Addr: ");
133
- WS_DEBUG_PRINTLN(addr, HEX);
134
- WS_DEBUG_PRINT("\tTemperature sensor enabled? ");
135
- WS_DEBUG_PRINTLN(msgDeviceInitReq->aht_init.enable_temperature);
136
- WS_DEBUG_PRINT("\tHumidity sensor enabled? ");
137
- WS_DEBUG_PRINTLN(msgDeviceInitReq->aht_init.enable_humidity);
138
-
139
- // TODO: Create I2C Driver using the an AHT driver sub-class!
140
- I2C_Driver *aht = new I2C_Driver(addr, this->_i2c);
141
- // Attempt to initialize the sensor driver
142
- if (!aht->initAHTX0()) {
143
- attachSuccess = false;
144
- return attachSuccess;
145
- }
146
- // Initialize device-specific sensors
147
- if (msgDeviceInitReq->aht_init.enable_temperature == true) {
148
- aht->enableAHTX0Temperature();
149
- }
150
- if (msgDeviceInitReq->aht_init.enable_humidity == true) {
151
- aht->enableAHTX0Humidity();
152
- }
153
- // Push to vector for sensor drivers
154
- activeDrivers.push_back(aht); */
155
- attachSuccess = true ;
128
+ // Initialize new AHTX0 sensor
129
+ _ahtx0 = new WipperSnapper_I2C_Driver_AHTX0 (this ->_i2c , i2cAddress);
130
+
131
+ // Did we initialize successfully?
132
+ if (!_ahtx0->getInitialized ()) {
133
+ WS_DEBUG_PRINTLN (" ERROR: Failed to initialize AHTX0 chip!" );
134
+ return false ;
135
+ }
136
+ WS_DEBUG_PRINTLN (" AHTX0 Initialized Successfully!" );
137
+
138
+ // Configure AHTX0 sensor
139
+ if (msgDeviceInitReq->aht_init .enable_temperature ) {
140
+ _ahtx0->enableTemperatureSensor ();
141
+ _ahtx0->setTemperatureSensorPeriod (
142
+ msgDeviceInitReq->aht_init .period_temperature );
143
+ WS_DEBUG_PRINTLN (" Enabled AHTX0 Temperature Sensor, [Returns every: " );
144
+ WS_DEBUG_PRINT (msgDeviceInitReq->aht_init .period_temperature );
145
+ WS_DEBUG_PRINTLN (" seconds]" );
146
+ }
147
+ if (msgDeviceInitReq->aht_init .enable_humidity ) {
148
+ _ahtx0->enableHumiditySensor ();
149
+ _ahtx0->setHumiditySensorPeriod (
150
+ msgDeviceInitReq->aht_init .period_humidity );
151
+ WS_DEBUG_PRINTLN (" Enabled AHTX0 Humidity Sensor, [Returns every: " );
152
+ WS_DEBUG_PRINT (msgDeviceInitReq->aht_init .period_humidity );
153
+ WS_DEBUG_PRINTLN (" seconds]" );
154
+ }
155
+ drivers.push_back (_ahtx0);
156
156
} else {
157
157
WS_DEBUG_PRINTLN (" ERROR: Sensor not found" )
158
158
}
159
159
WS_DEBUG_PRINTLN (" Successfully initialized AHTX0 sensor!" );
160
- return attachSuccess;
160
+ return true ;
161
+ }
162
+
163
+ /* ******************************************************************************/
164
+ /* !
165
+ @brief Updates the properties of an I2C device driver.
166
+ @param msgDeviceUpdateReq
167
+ A decoded I2CDeviceUpdateRequest.
168
+ @returns True if I2C device is was successfully updated, False otherwise.
169
+ */
170
+ /* ******************************************************************************/
171
+ bool WipperSnapper_Component_I2C::updateI2CDevice (
172
+ wippersnapper_i2c_v1_I2CDeviceDeinitRequest *msgDeviceUpdateReq) {
173
+ // TODO!! //
174
+ }
175
+
176
+ /* ******************************************************************************/
177
+ /* !
178
+ @brief Deinitializes an I2C device driver.
179
+ @param msgDeviceDeinitReq
180
+ A decoded I2CDeviceDeinitRequest.
181
+ @returns True if I2C device is found and de-initialized, False otherwise.
182
+ */
183
+ /* ******************************************************************************/
184
+ bool WipperSnapper_Component_I2C::deinitI2CDevice (
185
+ wippersnapper_i2c_v1_I2CDeviceDeinitRequest *msgDeviceDeinitReq) {
186
+ uint16_t deviceAddr = (uint16_t )msgDeviceDeinitReq->i2c_address ;
187
+ // Loop thru vector of drivers
188
+ for (int i = 0 ; i < drivers.size (); i++) {
189
+ if (drivers[i]->getSensorAddress () == deviceAddr) {
190
+ // Check driver type
191
+ if (drivers[i]->getDriverType () == AHTX0) {
192
+ // Should we delete the driver entirely, or just update?
193
+ if ((msgDeviceDeinitReq->aht .disable_temperature &&
194
+ drivers[i]->getHumidSensorPeriod () == -1L ) ||
195
+ (msgDeviceDeinitReq->aht .disable_humidity &&
196
+ drivers[i]->getTempSensorPeriod () == -1L ) ||
197
+ (msgDeviceDeinitReq->aht .disable_temperature &&
198
+ msgDeviceDeinitReq->aht .disable_humidity )) {
199
+ // delete the driver and remove from list so we dont attempt to
200
+ // update() it
201
+ delete _ahtx0;
202
+ drivers.erase (drivers.begin () + i);
203
+ return true ;
204
+ WS_DEBUG_PRINTLN (" AHTX0 Deleted" );
205
+ }
206
+
207
+ // Disable the device's temperature sensor
208
+ else if (msgDeviceDeinitReq->aht .disable_temperature ) {
209
+ drivers[i]->disableTemperatureSensor ();
210
+ return true ;
211
+ WS_DEBUG_PRINTLN (" AHTX0 Temperature Sensor Disabled" );
212
+ }
213
+
214
+ // Disable the device's humidity sensor
215
+ else if (msgDeviceDeinitReq->aht .disable_humidity ) {
216
+ drivers[i]->disableHumiditySensor ();
217
+ WS_DEBUG_PRINTLN (" AHTX0 Humidity Sensor Disabled" );
218
+ return true ;
219
+ }
220
+ } else {
221
+ WS_DEBUG_PRINTLN (" ERROR: Driver type unspecified" );
222
+ }
223
+ }
224
+ }
225
+ // Driver was not erased or not found
226
+ return false ;
227
+ }
228
+
229
+ /* ******************************************************************************/
230
+ /* !
231
+ @brief Queries all I2C device drivers for new values. Fills and sends an
232
+ I2CSensorEvent with the sensor event data.
233
+ */
234
+ /* ******************************************************************************/
235
+ void WipperSnapper_Component_I2C::update () {
236
+ // TODO
161
237
}
0 commit comments