Library for the NucleoVl53L1 shield and the Vl53L1
Dependencies: VL53L1
Dependents: VL53L1CB_shield_sensor_polling VL53L1CB_MB2_NoShield_3sensors_interrupt_ranging VL53L1CB_MB5_NoShield_3sensors_interrupt_ranging VL53L1CB_MB5_NoShield_3sensors_interrupt_ranging
XNucleo53L1A1.cpp@2:4c77be1be0e3, 2020-11-08 (annotated)
- Committer:
- charlesmn
- Date:
- Sun Nov 08 13:51:43 2020 +0000
- Revision:
- 2:4c77be1be0e3
- Parent:
- 0:00a40f7a4114
- Child:
- 3:ac8e3765ece8
Cope with MBed 6.4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
charlesmn | 0:00a40f7a4114 | 1 | #include "XNucleo53L1A1.h" |
charlesmn | 0:00a40f7a4114 | 2 | |
charlesmn | 0:00a40f7a4114 | 3 | XNucleo53L1A1 *XNucleo53L1A1::_instance = NULL; |
charlesmn | 0:00a40f7a4114 | 4 | |
charlesmn | 0:00a40f7a4114 | 5 | |
charlesmn | 0:00a40f7a4114 | 6 | XNucleo53L1A1 *XNucleo53L1A1::instance(ToF_DevI2C *ext_i2c) |
charlesmn | 0:00a40f7a4114 | 7 | { |
charlesmn | 0:00a40f7a4114 | 8 | if (_instance == NULL) { |
charlesmn | 0:00a40f7a4114 | 9 | _instance = new XNucleo53L1A1(ext_i2c); |
charlesmn | 0:00a40f7a4114 | 10 | } else { |
charlesmn | 0:00a40f7a4114 | 11 | printf("Failed to create XNucleo53L1A1 instance\n\r\r\n"); |
charlesmn | 0:00a40f7a4114 | 12 | } |
charlesmn | 0:00a40f7a4114 | 13 | return _instance; |
charlesmn | 0:00a40f7a4114 | 14 | } |
charlesmn | 0:00a40f7a4114 | 15 | |
charlesmn | 0:00a40f7a4114 | 16 | XNucleo53L1A1 *XNucleo53L1A1::instance(ToF_DevI2C *ext_i2c, |
charlesmn | 0:00a40f7a4114 | 17 | PinName gpio1_centre, |
charlesmn | 0:00a40f7a4114 | 18 | PinName gpio1_left, PinName gpio1_right) |
charlesmn | 0:00a40f7a4114 | 19 | { |
charlesmn | 0:00a40f7a4114 | 20 | if (_instance == NULL) { |
charlesmn | 0:00a40f7a4114 | 21 | // printf("instance %d z\n",ext_i2c); |
charlesmn | 0:00a40f7a4114 | 22 | _instance = new XNucleo53L1A1(ext_i2c, gpio1_centre, gpio1_left, gpio1_right); |
charlesmn | 0:00a40f7a4114 | 23 | } else { |
charlesmn | 0:00a40f7a4114 | 24 | printf("Failed to create XNucleo53L0A1 instance\n\r"); |
charlesmn | 0:00a40f7a4114 | 25 | } |
charlesmn | 0:00a40f7a4114 | 26 | return _instance; |
charlesmn | 0:00a40f7a4114 | 27 | } |
charlesmn | 0:00a40f7a4114 | 28 | |
charlesmn | 0:00a40f7a4114 | 29 | |
charlesmn | 0:00a40f7a4114 | 30 | int XNucleo53L1A1::init_board() |
charlesmn | 0:00a40f7a4114 | 31 | { |
charlesmn | 0:00a40f7a4114 | 32 | int status, n_dev = 0; |
charlesmn | 0:00a40f7a4114 | 33 | |
charlesmn | 0:00a40f7a4114 | 34 | sensor_centre->VL53L1_Off(); |
charlesmn | 0:00a40f7a4114 | 35 | sensor_left->VL53L1_Off(); |
charlesmn | 0:00a40f7a4114 | 36 | sensor_right->VL53L1_Off(); |
charlesmn | 0:00a40f7a4114 | 37 | int i = 0; |
charlesmn | 0:00a40f7a4114 | 38 | |
charlesmn | 0:00a40f7a4114 | 39 | // On startup or on the shutdown pin going high the VL53l1 devices have a i2c address of 0x52. |
charlesmn | 0:00a40f7a4114 | 40 | // To initialise then we have to bring up one device at a time This involves raising the shutdown pin for that device, |
charlesmn | 0:00a40f7a4114 | 41 | // initialising then setting the i2c address to a unique value. Once that is done the shutdown pins don't need to be touched. |
charlesmn | 0:00a40f7a4114 | 42 | |
charlesmn | 0:00a40f7a4114 | 43 | // set the shutdown pins to be outputs ( they are inputs on power up) |
charlesmn | 0:00a40f7a4114 | 44 | stmpe1600_exp1->set_gpio_dir(GPIO_15,OUTPUT); |
charlesmn | 0:00a40f7a4114 | 45 | stmpe1600_exp0->set_gpio_dir(GPIO_14,OUTPUT); |
charlesmn | 0:00a40f7a4114 | 46 | stmpe1600_exp0->set_gpio_dir(GPIO_15,OUTPUT); |
charlesmn | 0:00a40f7a4114 | 47 | |
charlesmn | 0:00a40f7a4114 | 48 | // set the shutdown pins to low, this will reset the VL53l1s |
charlesmn | 0:00a40f7a4114 | 49 | stmpe1600_exp1->clear_gpio(GPIO_15); |
charlesmn | 0:00a40f7a4114 | 50 | stmpe1600_exp0->clear_gpio(GPIO_14); |
charlesmn | 0:00a40f7a4114 | 51 | stmpe1600_exp0->clear_gpio(GPIO_15); |
charlesmn | 0:00a40f7a4114 | 52 | |
charlesmn | 0:00a40f7a4114 | 53 | // select the first VL53l1. It will have i2c address of 0x52. Set the i2c address to |
charlesmn | 0:00a40f7a4114 | 54 | //NEW_SENSOR_CENTRE_ADDRESS |
charlesmn | 0:00a40f7a4114 | 55 | stmpe1600_exp1->set_gpio(GPIO_15); |
charlesmn | 0:00a40f7a4114 | 56 | status = sensor_centre->InitSensor(NEW_SENSOR_CENTRE_ADDRESS); |
charlesmn | 0:00a40f7a4114 | 57 | if (status) { |
charlesmn | 0:00a40f7a4114 | 58 | delete sensor_centre; |
charlesmn | 0:00a40f7a4114 | 59 | delete xshutdown_centre; |
charlesmn | 0:00a40f7a4114 | 60 | sensor_centre = NULL; |
charlesmn | 0:00a40f7a4114 | 61 | xshutdown_centre = NULL; |
charlesmn | 0:00a40f7a4114 | 62 | printf("Sensor centre not present\n\r"); |
charlesmn | 0:00a40f7a4114 | 63 | } else { |
charlesmn | 0:00a40f7a4114 | 64 | printf("Sensor centre present\n\r"); |
charlesmn | 0:00a40f7a4114 | 65 | n_dev++; |
charlesmn | 0:00a40f7a4114 | 66 | } |
charlesmn | 2:4c77be1be0e3 | 67 | #if (MBED_VERSION > 60100) |
charlesmn | 2:4c77be1be0e3 | 68 | thread_sleep_for(2); |
charlesmn | 2:4c77be1be0e3 | 69 | #else |
charlesmn | 2:4c77be1be0e3 | 70 | wait_ms(2); // NEEDS A DELAY BETWEEN SENSORS |
charlesmn | 2:4c77be1be0e3 | 71 | #endif |
charlesmn | 2:4c77be1be0e3 | 72 | |
charlesmn | 0:00a40f7a4114 | 73 | // select the second VL53l1. It will have i2c address of 0x52. Set the i2c address to |
charlesmn | 0:00a40f7a4114 | 74 | //NEW_SENSOR_LEFT_ADDRESS |
charlesmn | 0:00a40f7a4114 | 75 | |
charlesmn | 0:00a40f7a4114 | 76 | stmpe1600_exp0->set_gpio(GPIO_14); |
charlesmn | 0:00a40f7a4114 | 77 | |
charlesmn | 0:00a40f7a4114 | 78 | status = sensor_left->InitSensor(NEW_SENSOR_LEFT_ADDRESS); |
charlesmn | 0:00a40f7a4114 | 79 | if (status) { |
charlesmn | 0:00a40f7a4114 | 80 | delete sensor_left; |
charlesmn | 0:00a40f7a4114 | 81 | delete xshutdown_left; |
charlesmn | 0:00a40f7a4114 | 82 | sensor_left = NULL; |
charlesmn | 0:00a40f7a4114 | 83 | xshutdown_left = NULL; |
charlesmn | 0:00a40f7a4114 | 84 | printf("Sensor left not present\n\r"); |
charlesmn | 0:00a40f7a4114 | 85 | } else { |
charlesmn | 0:00a40f7a4114 | 86 | printf("Sensor left present\n\r"); |
charlesmn | 0:00a40f7a4114 | 87 | n_dev++; |
charlesmn | 0:00a40f7a4114 | 88 | } |
charlesmn | 2:4c77be1be0e3 | 89 | |
charlesmn | 2:4c77be1be0e3 | 90 | #if (MBED_VERSION > 60100) |
charlesmn | 2:4c77be1be0e3 | 91 | thread_sleep_for(50); |
charlesmn | 2:4c77be1be0e3 | 92 | #else |
charlesmn | 2:4c77be1be0e3 | 93 | wait_ms(50); // NEEDS A DELAY BETWEEN SENSORS |
charlesmn | 2:4c77be1be0e3 | 94 | #endif |
charlesmn | 0:00a40f7a4114 | 95 | // select the 3rd VL53l1. It will have i2c address of 0x52. Set the i2c address to |
charlesmn | 0:00a40f7a4114 | 96 | //NEW_SENSOR_RIGHT_ADDRESS |
charlesmn | 0:00a40f7a4114 | 97 | stmpe1600_exp0->set_gpio(GPIO_15); |
charlesmn | 0:00a40f7a4114 | 98 | status = sensor_right->InitSensor(NEW_SENSOR_RIGHT_ADDRESS); |
charlesmn | 0:00a40f7a4114 | 99 | if (status) { |
charlesmn | 0:00a40f7a4114 | 100 | delete sensor_right; |
charlesmn | 0:00a40f7a4114 | 101 | delete xshutdown_right; |
charlesmn | 0:00a40f7a4114 | 102 | sensor_right = NULL; |
charlesmn | 0:00a40f7a4114 | 103 | xshutdown_right = NULL; |
charlesmn | 0:00a40f7a4114 | 104 | printf("Sensor right not present\n\r"); |
charlesmn | 0:00a40f7a4114 | 105 | } else { |
charlesmn | 0:00a40f7a4114 | 106 | printf("Sensor right present\n\r"); |
charlesmn | 0:00a40f7a4114 | 107 | n_dev++; |
charlesmn | 0:00a40f7a4114 | 108 | } |
charlesmn | 2:4c77be1be0e3 | 109 | #if (MBED_VERSION > 60100) |
charlesmn | 2:4c77be1be0e3 | 110 | thread_sleep_for(50); |
charlesmn | 2:4c77be1be0e3 | 111 | #else |
charlesmn | 2:4c77be1be0e3 | 112 | wait_ms(50); // NEEDS A DELAY BETWEEN SENSORS |
charlesmn | 2:4c77be1be0e3 | 113 | #endif |
charlesmn | 0:00a40f7a4114 | 114 | if (n_dev == 0) { |
charlesmn | 0:00a40f7a4114 | 115 | return 1; |
charlesmn | 0:00a40f7a4114 | 116 | } else { |
charlesmn | 0:00a40f7a4114 | 117 | return 0; |
charlesmn | 0:00a40f7a4114 | 118 | } |
charlesmn | 0:00a40f7a4114 | 119 | } |