Skip to content

Commit 4eb84cc

Browse files
committed
in progress changes from review; changed get<Prop>() name scheme to <prop>() - more modern; Added is to DataAvailable(); formatted file; tweak some constants; added a method to get all parameter values via a passed in struct; STILL a work in progress - not tested or compiled
1 parent 9979377 commit 4eb84cc

File tree

2 files changed

+84
-47
lines changed

2 files changed

+84
-47
lines changed

src/sfeBmv080.cpp

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@
2121
#include "bmv080.h"
2222
#include "bmv080_defs.h"
2323

24-
#define SPI_CLK_FREQ ((uint32_t)(1e6))
24+
#define SPI_CLK_FREQ ((uint32_t)(1e6))
2525

2626
// Some communication functions used with the system. These are from the original code from
2727
// Bosch - so keeping them the same. It is unclear if the library they provide depends on these
2828
// specific values - it probably does - so leaving as is.
2929

3030
#define E_COMBRIDGE_OK ((int8_t)0)
3131
/*! -1: Status codes returned when memory allocation fails */
32-
#define E_COMBRIDGE_ERROR_MEMORY_ALLOCATION ((int8_t) - 1)
32+
#define E_COMBRIDGE_ERROR_MEMORY_ALLOCATION ((int8_t)-1)
3333
/*! -2: Status codes returned when the read operation fails */
34-
#define E_COMBRIDGE_ERROR_READ ((int8_t) - 2)
34+
#define E_COMBRIDGE_ERROR_READ ((int8_t)-2)
3535
/*! -3: Status codes returned when the write operation fails */
36-
#define E_COMBRIDGE_ERROR_WRITE ((int8_t) - 3)
36+
#define E_COMBRIDGE_ERROR_WRITE ((int8_t)-3)
3737
/*! -4: Status codes returned when writing the header fails */
38-
#define E_COMBRIDGE_ERROR_WRITE_HEADER ((int8_t) - 4)
38+
#define E_COMBRIDGE_ERROR_WRITE_HEADER ((int8_t)-4)
3939
/*! -5: Status codes returned when a reference is null */
40-
#define E_COMBRIDGE_ERROR_NULLPTR ((int8_t) - 5)
40+
#define E_COMBRIDGE_ERROR_NULLPTR ((int8_t)-5)
4141

4242
// C function used in this library only - so static
4343

@@ -55,21 +55,21 @@ extern "C"
5555
uint16_t payload_length)
5656
{
5757
if (handle == nullptr)
58-
return E_COMBRIDGE_ERROR_NULLPTR;
58+
return E_COMBRIDGE_ERROR_NULLPTR;
5959

6060
// Our output var.
6161
size_t nRead = 0;
6262

6363
// Get our sparkfun toolkit bus object/interface
6464
sfeTkIBus *theBus = (sfeTkIBus *)handle;
6565

66-
if(theBus->type() == kBusTypeI2C) // I2C specific shift
67-
header = header << 1;
66+
if (theBus->type() == kBusTypeI2C) // I2C specific shift
67+
header = header << 1;
6868

6969
sfeTkError_t rc = theBus->readRegister(header, payload, payload_length, nRead);
7070

7171
if (rc != kSTkErrOk || nRead != payload_length)
72-
return E_COMBRIDGE_ERROR_READ;
72+
return E_COMBRIDGE_ERROR_READ;
7373

7474
return E_COMBRIDGE_OK;
7575
}
@@ -85,7 +85,7 @@ extern "C"
8585

8686
sfeTkIBus *theBus = (sfeTkIBus *)handle;
8787

88-
if(theBus->type() == kBusTypeI2C) // I2C specific shift
88+
if (theBus->type() == kBusTypeI2C) // I2C specific shift
8989
header = header << 1;
9090

9191
sfeTkError_t rc = theBus->writeRegister(header, payload, payload_length);
@@ -104,6 +104,7 @@ extern "C"
104104
return E_COMBRIDGE_OK;
105105
}
106106

107+
//---------------------------------------------------------------------
107108
// This function is just used in this file, so declaring it static
108109

109110
/* Custom function for consuming sensor readings */
@@ -112,13 +113,15 @@ extern "C"
112113
((sfeBmv080 *)callback_parameters)->setSensorValue(bmv080_output);
113114
}
114115

116+
//---------------------------------------------------------------------
115117
static void bmv080_service_routine(const bmv080_handle_t handle, void *callback_parameters)
116118
{
117119
/* The interrupt is served by the BMV080 sensor driver */
118120
bmv080_status_code_t bmv080_current_status =
119121
bmv080_serve_interrupt(handle, (bmv080_callback_data_ready_t)use_sensor_output, callback_parameters);
120122
if (bmv080_current_status != E_BMV080_OK)
121123
{
124+
// TODO: libraries should not output text by default, need to add a debug mode/flag to library?
122125
printf("Fetching measurement data failed with BMV080 status %d\r\n", (int32_t)bmv080_current_status);
123126
}
124127
}
@@ -127,6 +130,7 @@ extern "C"
127130
}
128131
#endif
129132

133+
//---------------------------------------------------------------------
130134
sfeTkError_t sfeBmv080::begin(sfeTkIBus *theBus)
131135
{
132136
// Nullptr check
@@ -139,31 +143,53 @@ sfeTkError_t sfeBmv080::begin(sfeTkIBus *theBus)
139143
return kSTkErrOk;
140144
}
141145

142-
float sfeBmv080::getPM25()
146+
//---------------------------------------------------------------------
147+
float sfeBmv080::PM25()
143148
{
144149
return _sensorValue.pm2_5_mass_concentration;
145150
}
146151

147-
float sfeBmv080::getPM1()
152+
//---------------------------------------------------------------------
153+
float sfeBmv080::PM1()
148154
{
149155
return _sensorValue.pm1_mass_concentration;
150156
}
151157

152-
bool sfeBmv080::getIsObstructed()
158+
//---------------------------------------------------------------------
159+
bool sfeBmv080::isObstructed()
153160
{
154161
return _sensorValue.is_obstructed;
155162
}
156163

164+
//---------------------------------------------------------------------
157165
void sfeBmv080::setSensorValue(bmv080_output_t bmv080_output)
158166
{
167+
// TODO: should here be a mode where the library user can set register a callback function to handle the data?
168+
// This way the end user can get all the sensor data at once - possible issue is stack/re-entrancy
159169
_dataAvailable = true;
160-
_sensorValue.pm2_5_mass_concentration = bmv080_output.pm2_5_mass_concentration;
161-
_sensorValue.pm1_mass_concentration = bmv080_output.pm1_mass_concentration;
162-
_sensorValue.runtime_in_sec = bmv080_output.runtime_in_sec;
163-
_sensorValue.is_obstructed = bmv080_output.is_obstructed;
164-
_sensorValue.is_outside_measurement_range = bmv080_output.is_outside_measurement_range;
170+
171+
// cache the latest sensor values - copy output to our class variable
172+
_sensorValue = bmv080_output;
173+
}
174+
175+
//---------------------------------------------------------------------
176+
bool sfeBmv080::sensorValue(bmv080_output_t *bmv080_output, bool update_data /* default is true*/)
177+
{
178+
if (!bmv080_output)
179+
return false;
180+
181+
// Get the latest sensor data ...
182+
if (update_data)
183+
{
184+
_dataAvailable = false;
185+
bmv080_service_routine(bmv080_handle_class, this);
186+
}
187+
if (_dataAvailable)
188+
*bmv080_output = _sensorValue;
189+
return _dataAvailable;
165190
}
166191

192+
//---------------------------------------------------------------------
167193
bool sfeBmv080::setMode(uint8_t mode)
168194
{
169195
bmv080_status_code_t bmv080_current_status; // return status from the Bosch API function
@@ -190,9 +216,11 @@ bool sfeBmv080::setMode(uint8_t mode)
190216
}
191217
}
192218

193-
bool sfeBmv080::dataAvailable()
219+
//---------------------------------------------------------------------
220+
bool sfeBmv080::isDataAvailable()
194221
{
195222
bmv080_service_routine(bmv080_handle_class, this);
223+
// TODO: What is the logic here? The expectation is that a user calls this before accessing any data?
196224
if (_dataAvailable == true)
197225
{
198226
_dataAvailable = false;
@@ -202,6 +230,7 @@ bool sfeBmv080::dataAvailable()
202230
return false;
203231
}
204232

233+
//---------------------------------------------------------------------
205234
// Our init method
206235
bool sfeBmv080::init()
207236
{
@@ -215,8 +244,7 @@ bool sfeBmv080::init()
215244
return true;
216245
}
217246

218-
219-
247+
//---------------------------------------------------------------------
220248
bool sfeBmv080::open()
221249
{
222250
if (_theBus == nullptr)
@@ -257,7 +285,7 @@ bool sfeBmv080::reset()
257285
}
258286
}
259287

260-
bool sfeBmv080::getDriverVersion()
288+
bool sfeBmv080::driverVersion()
261289
{
262290
uint16_t major = 0;
263291
uint16_t minor = 0;
@@ -278,7 +306,7 @@ bool sfeBmv080::getDriverVersion()
278306
return true;
279307
}
280308

281-
bool sfeBmv080::getID()
309+
bool sfeBmv080::ID()
282310
{
283311
char id[13];
284312
memset(id, 0x00, 13);
@@ -296,7 +324,7 @@ bool sfeBmv080::getID()
296324
}
297325
}
298326

299-
uint16_t sfeBmv080::getDutyCyclingPeriod()
327+
uint16_t sfeBmv080::dutyCyclingPeriod()
300328
{
301329
uint16_t duty_cycling_period = 0;
302330
bmv080_status_code_t bmv080_current_status =
@@ -329,13 +357,14 @@ bool sfeBmv080::setDutyCyclingPeriod(uint16_t duty_cycling_period)
329357
}
330358
}
331359

332-
float sfeBmv080::getVolumetricMassDensity()
360+
float sfeBmv080::volumetricMassDensity()
333361
{
334362
float volumetric_mass_density = 0.0;
335363
bmv080_status_code_t bmv080_current_status =
336364
bmv080_get_parameter(bmv080_handle_class, "volumetric_mass_density", (void *)&volumetric_mass_density);
337365
if (bmv080_current_status != E_BMV080_OK)
338366
{
367+
// TODO: libraries should not output text by default, need to add a debug mode/flag to library?
339368
printf("Error getting BMV080 Volumetric Mass Density: %d\n", bmv080_current_status);
340369
return 0.0;
341370
}
@@ -351,6 +380,7 @@ bool sfeBmv080::setVolumetricMassDensity(float volumetric_mass_density)
351380
bmv080_set_parameter(bmv080_handle_class, "volumetric_mass_density", (void *)&volumetric_mass_density);
352381
if (bmv080_current_status != E_BMV080_OK)
353382
{
383+
// TODO: libraries should not output text by default, need to add a debug mode/flag to library?
354384
printf("Error setting BMV080 Volumetric Mass Density: %d\n", bmv080_current_status);
355385
return false;
356386
}
@@ -360,13 +390,13 @@ bool sfeBmv080::setVolumetricMassDensity(float volumetric_mass_density)
360390
}
361391
}
362392

363-
float sfeBmv080::getIntegrationTime()
393+
float sfeBmv080::integrationTime()
364394
{
365395
float integration_time = 0.0;
366396
bmv080_status_code_t bmv080_current_status =
367397
bmv080_get_parameter(bmv080_handle_class, "integration_time", (void *)&integration_time);
368398
if (bmv080_current_status != E_BMV080_OK)
369-
{
399+
{ // todo -- no printf in library
370400
printf("Error getting BMV080 Integration Time: %d\n", bmv080_current_status);
371401
return 0.0;
372402
}
@@ -391,7 +421,7 @@ bool sfeBmv080::setIntegrationTime(float integration_time)
391421
}
392422
}
393423

394-
uint32_t sfeBmv080::getDistributionId()
424+
uint32_t sfeBmv080::distributionId()
395425
{
396426
uint32_t distribution_id = 0;
397427
bmv080_status_code_t bmv080_current_status =
@@ -422,7 +452,7 @@ bool sfeBmv080::setDistributionId(uint32_t distribution_id)
422452
}
423453
}
424454

425-
bool sfeBmv080::getDoObstructionDetection()
455+
bool sfeBmv080::doObstructionDetection()
426456
{
427457
bool do_obstruction_detection = false;
428458
bmv080_status_code_t bmv080_current_status =
@@ -453,7 +483,7 @@ bool sfeBmv080::setDoObstructionDetection(bool do_obstruction_detection)
453483
}
454484
}
455485

456-
bool sfeBmv080::getDoVibrationFiltering()
486+
bool sfeBmv080::doVibrationFiltering()
457487
{
458488
bool do_vibration_filtering = false;
459489
bmv080_status_code_t bmv080_current_status =
@@ -484,7 +514,7 @@ bool sfeBmv080::setDoVibrationFiltering(bool do_vibration_filtering)
484514
}
485515
}
486516

487-
uint8_t sfeBmv080::getMeasurementAlgorithm()
517+
uint8_t sfeBmv080::measurementAlgorithm()
488518
{
489519
bmv080_measurement_algorithm_t measurement_algorithm;
490520
bmv080_status_code_t bmv080_current_status =

0 commit comments

Comments
 (0)