Simple Ranging Example, using Expansion Board Sensor, driven via Interrupts.
Dependencies: mbed X_NUCLEO_53L1A1_mbed
Diff: main.cpp
- Revision:
- 19:92972e54d45c
- Parent:
- 17:e7a6fd6d3c97
--- a/main.cpp Fri May 17 09:49:47 2019 +0000 +++ b/main.cpp Fri May 17 10:05:56 2019 +0000 @@ -1,6 +1,7 @@ /* - * This VL53L1X Expansion board test application performs range measurements - * using the onboard embedded centre sensor. + * This VL53L1X Expansion board sample application performs range measurements + * with interrupts enabled to generate a hardware interrupt each time a new + * measurement is ready to be read. * * Measured ranges are ouput on the Serial Port, running at 115200 baud. * @@ -36,31 +37,54 @@ #define VL53L1_I2C_SCL D15 static XNucleo53L1A1 *board=NULL; - Serial pc(SERIAL_TX, SERIAL_RX); -/*=================================== Main ================================== -=============================================================================*/ -int main() +/* flags that handle interrupt request for sensor and user blue button*/ +volatile bool int_sensor = false; +volatile bool int_stop = false; + +/* ISR callback function of the centre sensor */ +void sensor_irq(void) { + int_sensor = true; + board->sensor_centre->disable_interrupt_measure_detection_irq(); +} +/* Start the sensor ranging */ +int init_sensor() +{ int status = 0; - uint8_t ready_centre = 0; - uint8_t ready_left = 0; - uint8_t ready_right = 0; - uint16_t distance_centre = 0; - uint16_t distance_left = 0; - uint16_t distance_right = 0; + /* start the measure on the center sensor */ + if (NULL != board->sensor_centre) { + status = board->sensor_centre->stop_measurement(); + if (status != 0) { + return status; + } - printf("Hello world!\r\n"); + status = board->sensor_centre->start_measurement(&sensor_irq); + if (status != 0) { + return status; + } + } + return status; +} - vl53L1X_DevI2C *dev_I2C = new vl53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL); +/* ISR callback function of the user blue button to switch measuring sensor. */ +void measuring_stop_irq(void) +{ + int_stop = true; +} - printf("I2C device created!\r\n"); +/* + * Main ranging function + */ +int range_measure(vl53L1X_DevI2C *device_i2c) +{ + int status = 0; + uint16_t distance = 0; /* creates the 53L1A1 expansion board singleton obj */ - board = XNucleo53L1A1::instance(dev_I2C, A2, D9, D2); - printf("board created!\r\n"); + board = XNucleo53L1A1::instance(device_i2c, A2, D9, D2); /* init the 53L1A1 expansion board with default values */ status = board->init_board(); @@ -69,55 +93,40 @@ return status; } - printf("board initiated! - %d\r\n", status); - - /* Start ranging on the centre sensor */ - status = board->sensor_centre->VL53L1X_StartRanging(); + /* init an array with chars to id the sensors */ + status = init_sensor(); if (status != 0) { - printf("Centre sensor failed to start ranging!\r\n"); - return status; - } - - /* Start ranging on the left sensor */ - status = board->sensor_left->VL53L1X_StartRanging(); - if (status != 0) { - printf("Left sensor failed to start ranging!\r\n"); - return status; - } - - /* Start ranging on the right sensor */ - status = board->sensor_right->VL53L1X_StartRanging(); - if (status != 0) { - printf("Right sensor failed to start ranging!\r\n"); + printf("Failed to init sensors!\r\n"); return status; } - /* Ranging loop - * Checks each sensor for data ready - */ - while (1) - { - board->sensor_centre->VL53L1X_CheckForDataReady(&ready_centre); - board->sensor_left->VL53L1X_CheckForDataReady(&ready_left); - board->sensor_right->VL53L1X_CheckForDataReady(&ready_right); - - if (ready_centre) { - board->sensor_centre->VL53L1X_GetRangeStatus(&ready_centre); - board->sensor_centre->VL53L1X_GetDistance(&distance_centre); - } - if (ready_left) { - board->sensor_centre->VL53L1X_GetRangeStatus(&ready_left); - board->sensor_centre->VL53L1X_GetDistance(&distance_left); - } - if (ready_right) { - board->sensor_centre->VL53L1X_GetRangeStatus(&ready_right); - board->sensor_centre->VL53L1X_GetDistance(&distance_right); + printf("Entering loop mode\r\n"); + /* Main ranging interrupt loop */ + while (true) { + if (int_sensor) { + int_sensor = false; + status = board->sensor_centre->handle_irq(&distance); + printf("distance: %d\r\n", distance); } - printf("%d\t", distance_centre); - printf("%d\t", distance_left); - printf("%d\t", distance_right); + if (int_stop) { + printf("\r\nEnding loop mode \r\n"); + break; + } } return status; + } + +/*=================================== Main ================================== +=============================================================================*/ +int main() +{ +#if USER_BUTTON==PC_13 // we are cross compiling for Nucleo-f401 + InterruptIn stop_button(USER_BUTTON); + stop_button.rise(&measuring_stop_irq); +#endif + vl53L1X_DevI2C *device_i2c = new vl53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL); + range_measure(device_i2c); // start continuous measures +}