@@ -35,72 +35,59 @@ Bmv080 bmv080;
35
35
36
36
SET_LOOP_TASK_STACK_SIZE (60 * 1024 ); // 60KB
37
37
38
- int hi2c ;
38
+ extern " C " bmv080_handle_t bmv080_handle = NULL ;
39
39
40
40
/* A unique handle is used to address a BMV080 sensor unit */
41
- static bmv080_handle_t bmv080_handle = NULL ;
41
+ // static bmv080_handle_t bmv080_handle = NULL;
42
42
43
43
/* handle for print function to be used in interrupt service routine */
44
- static print_function_t print_handle = NULL ;
44
+ // static print_function_t print_handle = NULL;
45
45
46
- volatile uint32_t data_ready_callback_count = 0 ;
46
+ // volatile uint32_t data_ready_callback_count = 0;
47
47
48
- void print_to_serial (const char *format, ...);
48
+ // void print_to_serial(const char *format, ...);
49
49
50
- /* Private variables ---------------------------------------------------------*/
51
- spi_device_t spi_device = {};
52
50
i2c_device_t i2c_device = {};
53
51
54
- bmv080_status_code_t bmv080_current_status = E_BMV080_OK;
55
-
56
- volatile bmv080_output_t bmv080_output;
57
-
58
52
#define IRQ_Pin 14
59
53
60
54
bool int_flag = false ;
61
55
62
56
void setup ()
63
57
{
64
- // Start serial
65
- Serial.begin (115200 );
58
+ // // Start serial
59
+ // Serial.begin(115200);
66
60
67
- while (!Serial) delay (10 ); // Wait for Serial to become available.
68
- // Necessary for boards with native USB (like the SAMD51 Thing+).
69
- // For a final version of a project that does not need serial debug (or a USB cable plugged in),
70
- // Comment out this while loop, or it will prevent the remaining code from running.
61
+ // while(!Serial) delay(10); // Wait for Serial to become available.
62
+ // // Necessary for boards with native USB (like the SAMD51 Thing+).
63
+ // // For a final version of a project that does not need serial debug (or a USB cable plugged in),
64
+ // // Comment out this while loop, or it will prevent the remaining code from running.
71
65
72
- Serial.println ();
73
- Serial.println (" BMV080 Example 1 - Basic Readings" );
66
+ // Serial.println();
67
+ // Serial.println("BMV080 Example 1 - Basic Readings");
74
68
75
- Wire.begin ();
69
+ // Wire.begin();
76
70
77
- if (bmv080.begin (BMV080_ADDR, Wire, BMV080_IRQ) == false ) {
78
- Serial.println (" BMV080 not detected at default I2C address. Check your jumpers and the hookup guide. Freezing..." );
79
- while (1 )
80
- ;
81
- }
82
- Serial.println (" BMV080 found!" );
71
+ // if (bmv080.begin(BMV080_ADDR, Wire, BMV080_IRQ) == false) {
72
+ // Serial.println("BMV080 not detected at default I2C address. Check your jumpers and the hookup guide. Freezing...");
73
+ // while (1)
74
+ // ;
75
+ // }
76
+ // Serial.println("BMV080 found!");
83
77
84
78
// Wire.setClock(400000); //Increase I2C data rate to 400kHz
85
-
86
- // TODO: Add other setup code if needed. Most setup should be done in begin()
87
-
88
79
89
80
Serial.begin (115200 );
90
81
Serial.println (" Starting BMV080 example..." );
91
82
92
- /* Communication interface initialization */
93
- spi_init (&spi_device);
83
+ /* Communication interface initialization */
84
+
94
85
i2c_init (&i2c_device);
95
86
96
87
setup_sensor ();
97
88
98
- print_handle = (const print_function_t )print_to_serial;
99
-
100
- enable_external_interrupt ((bool )true ); // Use of hardware interrupt of the BMV080 sensor unit can be used as trigger.
101
-
102
89
/* Start particle measurement in continuous mode */
103
- bmv080_current_status = bmv080_start_continuous_measurement (bmv080_handle);
90
+ bmv080_status_code_t bmv080_current_status = bmv080_start_continuous_measurement (bmv080_handle);
104
91
105
92
if (bmv080_current_status != E_BMV080_OK)
106
93
{
@@ -110,88 +97,25 @@ void setup()
110
97
{
111
98
printf (" BMV080 continuous measurement started successfully\n " );
112
99
}
113
-
114
- uint32_t sensor_measurement_duration_seconds = 60 ;
115
- data_ready_callback_count = 0 ;
116
-
117
- printf (" Particle measurement started in continuous mode for %d seconds \r\n " , sensor_measurement_duration_seconds);
118
100
}
119
101
120
102
void loop ()
121
103
{
122
- delay (100 );
123
- if (int_flag == true )
104
+ if (bmv080.dataAvailable ())
124
105
{
125
- int_flag = false ;
126
- do {
127
- bmv080_service_routine ();
128
- } while (digitalRead (IRQ_Pin) == 0 );
129
- }
130
- }
131
-
132
- /* Private functions ---------------------------------------------------------*/
133
- static void enable_external_interrupt (bool enable)
134
- {
135
- int checkPin = digitalPinToInterrupt (IRQ_Pin);
136
-
137
- if (checkPin == -1 ) {
138
- Serial.println (" Not a valid interrupt pin!" );
139
- } else {
140
- Serial.println (" Valid interrupt pin." );
141
- }
142
-
143
- if (enable)
144
- { /* Enabel external interrupt */
145
- attachInterrupt (digitalPinToInterrupt (IRQ_Pin), gpio_isr_handler, FALLING );
146
- }else
147
- { /* Disable external interrupt */
148
- detachInterrupt (digitalPinToInterrupt (IRQ_Pin) );
149
- }
150
- }
106
+ float pm25 = bmv080.getPM25 ();
151
107
152
- void gpio_isr_handler (void )
153
- {
154
- int_flag = true ;
155
- }
156
-
157
-
158
- /* Custom function for consuming sensor readings */
159
- void use_sensor_output (bmv080_output_t bmv080_output, void * callback_parameters)
160
- {
161
- data_ready_callback_count += 1 ;
162
- print_function_t print = (print_function_t )callback_parameters;
163
-
164
- print (" Runtime: %.2f s, PM2.5: %.0f ug/m^3, obstructed: %s, outside detection limits: %s\r\n " ,
165
- bmv080_output.runtime_in_sec , bmv080_output.pm2_5 , (bmv080_output.is_obstructed ? " yes" : " no" ), (bmv080_output.is_outside_detection_limits ? " yes" : " no" ));
166
- }
167
-
168
- void print_to_serial (const char *format, ...)
169
- {
170
- char print_buffer[1024 ];
171
- va_list args;
172
- va_start (args, format);
173
- vsnprintf (print_buffer, sizeof (print_buffer), format, args);
174
- va_end (args);
175
- Serial.print (print_buffer);
176
- }
108
+ Serial.print (pm25);
177
109
178
- void bmv080_service_routine (void )
179
- {
180
- if ( (bmv080_handle != NULL ) && (print_handle != NULL ))
110
+ if (bmv080.getIsObstructed () == true )
181
111
{
182
- // Serial.println("s");
183
- /* The interrupt is served by the BMV080 sensor driver */
184
- bmv080_status_code_t bmv080_current_status = bmv080_serve_interrupt (bmv080_handle, (bmv080_callback_data_ready_t )use_sensor_output, (void *)print_handle);
185
- if (bmv080_current_status != E_BMV080_OK)
186
- {
187
- printf (" Fetching measurement data failed with BMV080 status %d\r\n " , (int32_t )bmv080_current_status);
188
- }
112
+ Serial.print (" \t Obstructed" );
189
113
}
190
- }
191
-
192
-
193
-
194
114
115
+ Serial.println ();
116
+ }
117
+ delay (100 );
118
+ }
195
119
196
120
void setup_sensor (void )
197
121
{
@@ -202,7 +126,7 @@ void setup_sensor(void)
202
126
char git_hash[12 ];
203
127
int32_t commits_ahead = 0 ;
204
128
205
- bmv080_current_status = bmv080_get_driver_version (&major, &minor, &patch, git_hash, &commits_ahead);
129
+ bmv080_status_code_t bmv080_current_status = bmv080_get_driver_version (&major, &minor, &patch, git_hash, &commits_ahead);
206
130
207
131
if (bmv080_current_status != E_BMV080_OK)
208
132
{
@@ -228,6 +152,8 @@ void setup_sensor(void)
228
152
printf (" BMV080 handle opened successfully\n " );
229
153
}
230
154
155
+ bmv080.setHandle (bmv080_handle);
156
+
231
157
/* Reset the BMV080 sensor unit */
232
158
bmv080_current_status = bmv080_reset (bmv080_handle);
233
159
0 commit comments