@@ -35,72 +35,59 @@ Bmv080 bmv080;
3535
3636SET_LOOP_TASK_STACK_SIZE (60 * 1024 ); // 60KB
3737
38- int hi2c ;
38+ extern " C " bmv080_handle_t bmv080_handle = NULL ;
3939
4040/* 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;
4242
4343/* 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;
4545
46- volatile uint32_t data_ready_callback_count = 0 ;
46+ // volatile uint32_t data_ready_callback_count = 0;
4747
48- void print_to_serial (const char *format, ...);
48+ // void print_to_serial(const char *format, ...);
4949
50- /* Private variables ---------------------------------------------------------*/
51- spi_device_t spi_device = {};
5250i2c_device_t i2c_device = {};
5351
54- bmv080_status_code_t bmv080_current_status = E_BMV080_OK;
55-
56- volatile bmv080_output_t bmv080_output;
57-
5852#define IRQ_Pin 14
5953
6054bool int_flag = false ;
6155
6256void setup ()
6357{
64- // Start serial
65- Serial.begin (115200 );
58+ // // Start serial
59+ // Serial.begin(115200);
6660
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.
7165
72- Serial.println ();
73- Serial.println (" BMV080 Example 1 - Basic Readings" );
66+ // Serial.println();
67+ // Serial.println("BMV080 Example 1 - Basic Readings");
7468
75- Wire.begin ();
69+ // Wire.begin();
7670
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!");
8377
8478 // 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-
8879
8980 Serial.begin (115200 );
9081 Serial.println (" Starting BMV080 example..." );
9182
92- /* Communication interface initialization */
93- spi_init (&spi_device);
83+ /* Communication interface initialization */
84+
9485 i2c_init (&i2c_device);
9586
9687 setup_sensor ();
9788
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-
10289 /* 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);
10491
10592 if (bmv080_current_status != E_BMV080_OK)
10693 {
@@ -110,88 +97,25 @@ void setup()
11097 {
11198 printf (" BMV080 continuous measurement started successfully\n " );
11299 }
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);
118100}
119101
120102void loop ()
121103{
122- delay (100 );
123- if (int_flag == true )
104+ if (bmv080.dataAvailable ())
124105 {
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 ();
151107
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);
177109
178- void bmv080_service_routine (void )
179- {
180- if ( (bmv080_handle != NULL ) && (print_handle != NULL ))
110+ if (bmv080.getIsObstructed () == true )
181111 {
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" );
189113 }
190- }
191-
192-
193-
194114
115+ Serial.println ();
116+ }
117+ delay (100 );
118+ }
195119
196120void setup_sensor (void )
197121{
@@ -202,7 +126,7 @@ void setup_sensor(void)
202126 char git_hash[12 ];
203127 int32_t commits_ahead = 0 ;
204128
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);
206130
207131 if (bmv080_current_status != E_BMV080_OK)
208132 {
@@ -228,6 +152,8 @@ void setup_sensor(void)
228152 printf (" BMV080 handle opened successfully\n " );
229153 }
230154
155+ bmv080.setHandle (bmv080_handle);
156+
231157 /* Reset the BMV080 sensor unit */
232158 bmv080_current_status = bmv080_reset (bmv080_handle);
233159
0 commit comments