From 0a338569c4b540ab2e32c2c21ee715f33d41fa65 Mon Sep 17 00:00:00 2001 From: Carlo Parata Date: Thu, 28 Jan 2021 14:49:15 +0100 Subject: [PATCH] Add begin and end APIs and improve library --- .../X_NUCLEO_53L0A1_Gesture_DirSwipe.ino | 73 ++++--- .../X_NUCLEO_53L0A1_Gesture_Swipe1.ino | 53 +++-- .../X_NUCLEO_53L0A1_Gesture_Tap1.ino | 53 +++-- .../X_NUCLEO_53L0A1_HelloWorld.ino | 45 ++-- keywords.txt | 4 +- library.properties | 2 +- src/stmpe1600_class.h | 195 ++++++++++-------- src/vl53l0x_x_nucleo_53l0a1_class.h | 12 +- 8 files changed, 226 insertions(+), 211 deletions(-) diff --git a/examples/X_NUCLEO_53L0A1_Gesture_DirSwipe/X_NUCLEO_53L0A1_Gesture_DirSwipe.ino b/examples/X_NUCLEO_53L0A1_Gesture_DirSwipe/X_NUCLEO_53L0A1_Gesture_DirSwipe.ino index a158ff7..4e35208 100644 --- a/examples/X_NUCLEO_53L0A1_Gesture_DirSwipe/X_NUCLEO_53L0A1_Gesture_DirSwipe.ino +++ b/examples/X_NUCLEO_53L0A1_Gesture_DirSwipe/X_NUCLEO_53L0A1_Gesture_DirSwipe.ino @@ -72,12 +72,12 @@ #endif // Components. -STMPE1600DigiOut *xshutdown_top; -STMPE1600DigiOut *xshutdown_left; -STMPE1600DigiOut *xshutdown_right; -VL53L0X_X_NUCLEO_53L0A1 *sensor_vl53l0x_top; -VL53L0X_X_NUCLEO_53L0A1 *sensor_vl53l0x_left; -VL53L0X_X_NUCLEO_53L0A1 *sensor_vl53l0x_right; +STMPE1600DigiOut xshutdown_top(&DEV_I2C, GPIO_15, (0x42 * 2)); +STMPE1600DigiOut xshutdown_left(&DEV_I2C, GPIO_14, (0x43 * 2)); +STMPE1600DigiOut xshutdown_right(&DEV_I2C, GPIO_15, (0x43 * 2)); +VL53L0X_X_NUCLEO_53L0A1 sensor_vl53l0x_top(&DEV_I2C, &xshutdown_top); +VL53L0X_X_NUCLEO_53L0A1 sensor_vl53l0x_left(&DEV_I2C, &xshutdown_left); +VL53L0X_X_NUCLEO_53L0A1 sensor_vl53l0x_right(&DEV_I2C, &xshutdown_right); // Gesture structure. Gesture_DIRSWIPE_1_Data_t gestureDirSwipeData; @@ -95,52 +95,52 @@ void SetupSingleShot(void){ uint32_t refSpadCount; uint8_t isApertureSpads; - status = sensor_vl53l0x_left->StaticInit(); + status = sensor_vl53l0x_left.StaticInit(); if( status ){ SerialPort.println("StaticInit left sensor failed"); } - status = sensor_vl53l0x_left->PerformRefCalibration(&VhvSettings, &PhaseCal); + status = sensor_vl53l0x_left.PerformRefCalibration(&VhvSettings, &PhaseCal); if( status ){ SerialPort.println("PerformRefCalibration left sensor failed"); } - status = sensor_vl53l0x_left->PerformRefSpadManagement(&refSpadCount, &isApertureSpads); + status = sensor_vl53l0x_left.PerformRefSpadManagement(&refSpadCount, &isApertureSpads); if( status ){ SerialPort.println("PerformRefSpadManagement left sensor failed"); } - status = sensor_vl53l0x_left->SetDeviceMode(VL53L0X_DEVICEMODE_SINGLE_RANGING); // Setup in single ranging mode + status = sensor_vl53l0x_left.SetDeviceMode(VL53L0X_DEVICEMODE_SINGLE_RANGING); // Setup in single ranging mode if( status ){ SerialPort.println("SetDeviceMode left sensor failed"); } - status = sensor_vl53l0x_left->SetMeasurementTimingBudgetMicroSeconds(20*1000); + status = sensor_vl53l0x_left.SetMeasurementTimingBudgetMicroSeconds(20*1000); if( status ){ SerialPort.println("SetMeasurementTimingBudgetMicroSeconds left sensor failed"); } - status = sensor_vl53l0x_right->StaticInit(); + status = sensor_vl53l0x_right.StaticInit(); if( status ){ SerialPort.println("StaticInit right sensor failed"); } - status = sensor_vl53l0x_right->PerformRefCalibration(&VhvSettings, &PhaseCal); + status = sensor_vl53l0x_right.PerformRefCalibration(&VhvSettings, &PhaseCal); if( status ){ SerialPort.println("PerformRefCalibration right sensor failed"); } - status = sensor_vl53l0x_right->PerformRefSpadManagement(&refSpadCount, &isApertureSpads); + status = sensor_vl53l0x_right.PerformRefSpadManagement(&refSpadCount, &isApertureSpads); if( status ){ SerialPort.println("PerformRefSpadManagement right sensor failed"); } - status = sensor_vl53l0x_right->SetDeviceMode(VL53L0X_DEVICEMODE_SINGLE_RANGING); // Setup in single ranging mode + status = sensor_vl53l0x_right.SetDeviceMode(VL53L0X_DEVICEMODE_SINGLE_RANGING); // Setup in single ranging mode if( status ){ SerialPort.println("SetDeviceMode right sensor failed"); } - status = sensor_vl53l0x_right->SetMeasurementTimingBudgetMicroSeconds(20*1000); + status = sensor_vl53l0x_right.SetMeasurementTimingBudgetMicroSeconds(20*1000); if( status ){ SerialPort.println("SetMeasurementTimingBudgetMicroSeconds right sensor failed"); } @@ -180,36 +180,33 @@ void setup() { DEV_I2C.begin(); DEV_I2C.setClock(400000); - // Create VL53L0X top component. - xshutdown_top = new STMPE1600DigiOut(&DEV_I2C, GPIO_15, (0x42 * 2)); - sensor_vl53l0x_top = new VL53L0X_X_NUCLEO_53L0A1(&DEV_I2C, xshutdown_top, A2); + // Configure VL53L0X top component. + sensor_vl53l0x_top.begin(); // Switch off VL53L0X top component. - sensor_vl53l0x_top->VL53L0X_Off(); + sensor_vl53l0x_top.VL53L0X_Off(); - // Create (if present) VL53L0X left component. - xshutdown_left = new STMPE1600DigiOut(&DEV_I2C, GPIO_14, (0x43 * 2)); - sensor_vl53l0x_left = new VL53L0X_X_NUCLEO_53L0A1(&DEV_I2C, xshutdown_left, D8); + // Configure (if present) VL53L0X left component. + sensor_vl53l0x_left.begin(); // Switch off (if present) VL53L0X left component. - sensor_vl53l0x_left->VL53L0X_Off(); + sensor_vl53l0x_left.VL53L0X_Off(); - // Create (if present) VL53L0X right component. - xshutdown_right = new STMPE1600DigiOut(&DEV_I2C, GPIO_15, (0x43 * 2)); - sensor_vl53l0x_right = new VL53L0X_X_NUCLEO_53L0A1(&DEV_I2C, xshutdown_right, D2); + // Configure (if present) VL53L0X right component. + sensor_vl53l0x_right.begin(); // Switch off (if present) VL53L0X right component. - sensor_vl53l0x_right->VL53L0X_Off(); + sensor_vl53l0x_right.VL53L0X_Off(); // Initialize VL53L0X left component. - status = sensor_vl53l0x_left->InitSensor(0x12); + status = sensor_vl53l0x_left.InitSensor(0x12); if(status) { SerialPort.println("Init sensor_vl53l0x_left failed..."); } // Initialize VL53L0X right component. - status = sensor_vl53l0x_right->InitSensor(0x14); + status = sensor_vl53l0x_right.InitSensor(0x14); if(status) { SerialPort.println("Init sensor_vl53l0x_right failed..."); @@ -227,8 +224,8 @@ void setup() { void loop() { int gesture_code; - sensor_vl53l0x_left->StartMeasurement(); - sensor_vl53l0x_right->StartMeasurement(); + sensor_vl53l0x_left.StartMeasurement(); + sensor_vl53l0x_right.StartMeasurement(); int left_done = 0; int right_done = 0; @@ -240,7 +237,7 @@ void loop() { if(left_done == 0) { NewDataReady = 0; - int status = sensor_vl53l0x_left->GetMeasurementDataReady(&NewDataReady); + int status = sensor_vl53l0x_left.GetMeasurementDataReady(&NewDataReady); if( status ){ SerialPort.println("GetMeasurementDataReady left sensor failed"); @@ -248,12 +245,12 @@ void loop() { if(NewDataReady) { - status = sensor_vl53l0x_left->ClearInterruptMask(0); + status = sensor_vl53l0x_left.ClearInterruptMask(0); if( status ){ SerialPort.println("ClearInterruptMask left sensor failed"); } - status = sensor_vl53l0x_left->GetRangingMeasurementData(&pRangingMeasurementData); + status = sensor_vl53l0x_left.GetRangingMeasurementData(&pRangingMeasurementData); if( status ){ SerialPort.println("GetRangingMeasurementData left sensor failed"); } @@ -272,7 +269,7 @@ void loop() { if(right_done == 0) { NewDataReady = 0; - int status = sensor_vl53l0x_right->GetMeasurementDataReady(&NewDataReady); + int status = sensor_vl53l0x_right.GetMeasurementDataReady(&NewDataReady); if( status ){ SerialPort.println("GetMeasurementDataReady right sensor failed"); @@ -280,12 +277,12 @@ void loop() { if(NewDataReady) { - status = sensor_vl53l0x_right->ClearInterruptMask(0); + status = sensor_vl53l0x_right.ClearInterruptMask(0); if( status ){ SerialPort.println("ClearInterruptMask right sensor failed"); } - status = sensor_vl53l0x_right->GetRangingMeasurementData(&pRangingMeasurementData); + status = sensor_vl53l0x_right.GetRangingMeasurementData(&pRangingMeasurementData); if( status ){ SerialPort.println("GetRangingMeasurementData right sensor failed"); } diff --git a/examples/X_NUCLEO_53L0A1_Gesture_Swipe1/X_NUCLEO_53L0A1_Gesture_Swipe1.ino b/examples/X_NUCLEO_53L0A1_Gesture_Swipe1/X_NUCLEO_53L0A1_Gesture_Swipe1.ino index 1b98b14..687d7e4 100644 --- a/examples/X_NUCLEO_53L0A1_Gesture_Swipe1/X_NUCLEO_53L0A1_Gesture_Swipe1.ino +++ b/examples/X_NUCLEO_53L0A1_Gesture_Swipe1/X_NUCLEO_53L0A1_Gesture_Swipe1.ino @@ -72,12 +72,12 @@ #endif // Components. -STMPE1600DigiOut *xshutdown_top; -STMPE1600DigiOut *xshutdown_left; -STMPE1600DigiOut *xshutdown_right; -VL53L0X_X_NUCLEO_53L0A1 *sensor_vl53l0x_top; -VL53L0X_X_NUCLEO_53L0A1 *sensor_vl53l0x_left; -VL53L0X_X_NUCLEO_53L0A1 *sensor_vl53l0x_right; +STMPE1600DigiOut xshutdown_top(&DEV_I2C, GPIO_15, (0x42 * 2)); +STMPE1600DigiOut xshutdown_left(&DEV_I2C, GPIO_14, (0x43 * 2)); +STMPE1600DigiOut xshutdown_right(&DEV_I2C, GPIO_15, (0x43 * 2)); +VL53L0X_X_NUCLEO_53L0A1 sensor_vl53l0x_top(&DEV_I2C, &xshutdown_top); +VL53L0X_X_NUCLEO_53L0A1 sensor_vl53l0x_left(&DEV_I2C, &xshutdown_left); +VL53L0X_X_NUCLEO_53L0A1 sensor_vl53l0x_right(&DEV_I2C, &xshutdown_right); // Gesture structure. Gesture_SWIPE_1_Data_t gestureSwipeData; @@ -95,27 +95,27 @@ void SetupSingleShot(void){ uint32_t refSpadCount; uint8_t isApertureSpads; - status = sensor_vl53l0x_top->StaticInit(); + status = sensor_vl53l0x_top.StaticInit(); if( status ){ SerialPort.println("StaticInit top sensor failed"); } - status = sensor_vl53l0x_top->PerformRefCalibration(&VhvSettings, &PhaseCal); + status = sensor_vl53l0x_top.PerformRefCalibration(&VhvSettings, &PhaseCal); if( status ){ SerialPort.println("PerformRefCalibration top sensor failed"); } - status = sensor_vl53l0x_top->PerformRefSpadManagement(&refSpadCount, &isApertureSpads); + status = sensor_vl53l0x_top.PerformRefSpadManagement(&refSpadCount, &isApertureSpads); if( status ){ SerialPort.println("PerformRefSpadManagement top sensor failed"); } - status = sensor_vl53l0x_top->SetDeviceMode(VL53L0X_DEVICEMODE_SINGLE_RANGING); // Setup in single ranging mode + status = sensor_vl53l0x_top.SetDeviceMode(VL53L0X_DEVICEMODE_SINGLE_RANGING); // Setup in single ranging mode if( status ){ SerialPort.println("SetDeviceMode top sensor failed"); } - status = sensor_vl53l0x_top->SetMeasurementTimingBudgetMicroSeconds(20*1000); + status = sensor_vl53l0x_top.SetMeasurementTimingBudgetMicroSeconds(20*1000); if( status ){ SerialPort.println("SetMeasurementTimingBudgetMicroSeconds top sensor failed"); } @@ -154,29 +154,26 @@ void setup() { // Initialize I2C bus. DEV_I2C.begin(); - // Create VL53L0X top component. - xshutdown_top = new STMPE1600DigiOut(&DEV_I2C, GPIO_15, (0x42 * 2)); - sensor_vl53l0x_top = new VL53L0X_X_NUCLEO_53L0A1(&DEV_I2C, xshutdown_top, A2); + // Configure VL53L0X top component. + sensor_vl53l0x_top.begin(); // Switch off VL53L0X top component. - sensor_vl53l0x_top->VL53L0X_Off(); + sensor_vl53l0x_top.VL53L0X_Off(); - // Create (if present) VL53L0X left component. - xshutdown_left = new STMPE1600DigiOut(&DEV_I2C, GPIO_14, (0x43 * 2)); - sensor_vl53l0x_left = new VL53L0X_X_NUCLEO_53L0A1(&DEV_I2C, xshutdown_left, D8); + // Configure (if present) VL53L0X left component. + sensor_vl53l0x_left.begin(); // Switch off (if present) VL53L0X left component. - sensor_vl53l0x_left->VL53L0X_Off(); + sensor_vl53l0x_left.VL53L0X_Off(); - // Create (if present) VL53L0X right component. - xshutdown_right = new STMPE1600DigiOut(&DEV_I2C, GPIO_15, (0x43 * 2)); - sensor_vl53l0x_right = new VL53L0X_X_NUCLEO_53L0A1(&DEV_I2C, xshutdown_right, D2); + // Configure (if present) VL53L0X right component. + sensor_vl53l0x_right.begin(); // Switch off (if present) VL53L0X right component. - sensor_vl53l0x_right->VL53L0X_Off(); + sensor_vl53l0x_right.VL53L0X_Off(); // Initialize VL53L0X top component. - status = sensor_vl53l0x_top->InitSensor(0x10); + status = sensor_vl53l0x_top.InitSensor(0x10); if(status) { SerialPort.println("Init sensor_vl53l0x_top failed..."); @@ -194,7 +191,7 @@ void setup() { void loop() { int gesture_code; - sensor_vl53l0x_top->StartMeasurement(); + sensor_vl53l0x_top.StartMeasurement(); int top_done = 0; uint8_t NewDataReady=0; @@ -205,7 +202,7 @@ void loop() { if(top_done == 0) { NewDataReady = 0; - int status = sensor_vl53l0x_top->GetMeasurementDataReady(&NewDataReady); + int status = sensor_vl53l0x_top.GetMeasurementDataReady(&NewDataReady); if( status ){ SerialPort.println("GetMeasurementDataReady top sensor failed"); @@ -213,12 +210,12 @@ void loop() { if(NewDataReady) { - status = sensor_vl53l0x_top->ClearInterruptMask(0); + status = sensor_vl53l0x_top.ClearInterruptMask(0); if( status ){ SerialPort.println("ClearInterruptMask top sensor failed"); } - status = sensor_vl53l0x_top->GetRangingMeasurementData(&pRangingMeasurementData); + status = sensor_vl53l0x_top.GetRangingMeasurementData(&pRangingMeasurementData); if( status ){ SerialPort.println("GetRangingMeasurementData top sensor failed"); } diff --git a/examples/X_NUCLEO_53L0A1_Gesture_Tap1/X_NUCLEO_53L0A1_Gesture_Tap1.ino b/examples/X_NUCLEO_53L0A1_Gesture_Tap1/X_NUCLEO_53L0A1_Gesture_Tap1.ino index d438c6c..20c1cda 100644 --- a/examples/X_NUCLEO_53L0A1_Gesture_Tap1/X_NUCLEO_53L0A1_Gesture_Tap1.ino +++ b/examples/X_NUCLEO_53L0A1_Gesture_Tap1/X_NUCLEO_53L0A1_Gesture_Tap1.ino @@ -72,12 +72,12 @@ #endif // Components. -STMPE1600DigiOut *xshutdown_top; -STMPE1600DigiOut *xshutdown_left; -STMPE1600DigiOut *xshutdown_right; -VL53L0X_X_NUCLEO_53L0A1 *sensor_vl53l0x_top; -VL53L0X_X_NUCLEO_53L0A1 *sensor_vl53l0x_left; -VL53L0X_X_NUCLEO_53L0A1 *sensor_vl53l0x_right; +STMPE1600DigiOut xshutdown_top(&DEV_I2C, GPIO_15, (0x42 * 2)); +STMPE1600DigiOut xshutdown_left(&DEV_I2C, GPIO_14, (0x43 * 2)); +STMPE1600DigiOut xshutdown_right(&DEV_I2C, GPIO_15, (0x43 * 2)); +VL53L0X_X_NUCLEO_53L0A1 sensor_vl53l0x_top(&DEV_I2C, &xshutdown_top); +VL53L0X_X_NUCLEO_53L0A1 sensor_vl53l0x_left(&DEV_I2C, &xshutdown_left); +VL53L0X_X_NUCLEO_53L0A1 sensor_vl53l0x_right(&DEV_I2C, &xshutdown_right); // Gesture structure. Gesture_TAP_1_Data_t gestureTapData; @@ -95,27 +95,27 @@ void SetupSingleShot(void){ uint32_t refSpadCount; uint8_t isApertureSpads; - status = sensor_vl53l0x_top->StaticInit(); + status = sensor_vl53l0x_top.StaticInit(); if( status ){ SerialPort.println("StaticInit top sensor failed"); } - status = sensor_vl53l0x_top->PerformRefCalibration(&VhvSettings, &PhaseCal); + status = sensor_vl53l0x_top.PerformRefCalibration(&VhvSettings, &PhaseCal); if( status ){ SerialPort.println("PerformRefCalibration top sensor failed"); } - status = sensor_vl53l0x_top->PerformRefSpadManagement(&refSpadCount, &isApertureSpads); + status = sensor_vl53l0x_top.PerformRefSpadManagement(&refSpadCount, &isApertureSpads); if( status ){ SerialPort.println("PerformRefSpadManagement top sensor failed"); } - status = sensor_vl53l0x_top->SetDeviceMode(VL53L0X_DEVICEMODE_SINGLE_RANGING); // Setup in single ranging mode + status = sensor_vl53l0x_top.SetDeviceMode(VL53L0X_DEVICEMODE_SINGLE_RANGING); // Setup in single ranging mode if( status ){ SerialPort.println("SetDeviceMode top sensor failed"); } - status = sensor_vl53l0x_top->SetMeasurementTimingBudgetMicroSeconds(20*1000); + status = sensor_vl53l0x_top.SetMeasurementTimingBudgetMicroSeconds(20*1000); if( status ){ SerialPort.println("SetMeasurementTimingBudgetMicroSeconds top sensor failed"); } @@ -154,29 +154,26 @@ void setup() { // Initialize I2C bus. DEV_I2C.begin(); - // Create VL53L0X top component. - xshutdown_top = new STMPE1600DigiOut(&DEV_I2C, GPIO_15, (0x42 * 2)); - sensor_vl53l0x_top = new VL53L0X_X_NUCLEO_53L0A1(&DEV_I2C, xshutdown_top, A2); + // Configure VL53L0X top component. + sensor_vl53l0x_top.begin(); // Switch off VL53L0X top component. - sensor_vl53l0x_top->VL53L0X_Off(); + sensor_vl53l0x_top.VL53L0X_Off(); - // Create (if present) VL53L0X left component. - xshutdown_left = new STMPE1600DigiOut(&DEV_I2C, GPIO_14, (0x43 * 2)); - sensor_vl53l0x_left = new VL53L0X_X_NUCLEO_53L0A1(&DEV_I2C, xshutdown_left, D8); + // Configure (if present) VL53L0X left component. + sensor_vl53l0x_left.begin(); // Switch off (if present) VL53L0X left component. - sensor_vl53l0x_left->VL53L0X_Off(); + sensor_vl53l0x_left.VL53L0X_Off(); - // Create (if present) VL53L0X right component. - xshutdown_right = new STMPE1600DigiOut(&DEV_I2C, GPIO_15, (0x43 * 2)); - sensor_vl53l0x_right = new VL53L0X_X_NUCLEO_53L0A1(&DEV_I2C, xshutdown_right, D2); + // Configure (if present) VL53L0X right component. + sensor_vl53l0x_right.begin(); // Switch off (if present) VL53L0X right component. - sensor_vl53l0x_right->VL53L0X_Off(); + sensor_vl53l0x_right.VL53L0X_Off(); // Initialize VL53L0X top component. - status = sensor_vl53l0x_top->InitSensor(0x10); + status = sensor_vl53l0x_top.InitSensor(0x10); if(status) { SerialPort.println("Init sensor_vl53l0x_top failed..."); @@ -194,7 +191,7 @@ void setup() { void loop() { int gesture_code; - sensor_vl53l0x_top->StartMeasurement(); + sensor_vl53l0x_top.StartMeasurement(); int top_done = 0; uint8_t NewDataReady=0; @@ -205,7 +202,7 @@ void loop() { if(top_done == 0) { NewDataReady = 0; - int status = sensor_vl53l0x_top->GetMeasurementDataReady(&NewDataReady); + int status = sensor_vl53l0x_top.GetMeasurementDataReady(&NewDataReady); if( status ){ SerialPort.println("GetMeasurementDataReady top sensor failed"); @@ -213,12 +210,12 @@ void loop() { if(NewDataReady) { - status = sensor_vl53l0x_top->ClearInterruptMask(0); + status = sensor_vl53l0x_top.ClearInterruptMask(0); if( status ){ SerialPort.println("ClearInterruptMask top sensor failed"); } - status = sensor_vl53l0x_top->GetRangingMeasurementData(&pRangingMeasurementData); + status = sensor_vl53l0x_top.GetRangingMeasurementData(&pRangingMeasurementData); if( status ){ SerialPort.println("GetRangingMeasurementData top sensor failed"); } diff --git a/examples/X_NUCLEO_53L0A1_HelloWorld/X_NUCLEO_53L0A1_HelloWorld.ino b/examples/X_NUCLEO_53L0A1_HelloWorld/X_NUCLEO_53L0A1_HelloWorld.ino index a995aa4..e614900 100644 --- a/examples/X_NUCLEO_53L0A1_HelloWorld/X_NUCLEO_53L0A1_HelloWorld.ino +++ b/examples/X_NUCLEO_53L0A1_HelloWorld/X_NUCLEO_53L0A1_HelloWorld.ino @@ -70,12 +70,12 @@ #endif // Components. -STMPE1600DigiOut *xshutdown_top; -STMPE1600DigiOut *xshutdown_left; -STMPE1600DigiOut *xshutdown_right; -VL53L0X_X_NUCLEO_53L0A1 *sensor_vl53l0x_top; -VL53L0X_X_NUCLEO_53L0A1 *sensor_vl53l0x_left; -VL53L0X_X_NUCLEO_53L0A1 *sensor_vl53l0x_right; +STMPE1600DigiOut xshutdown_top(&DEV_I2C, GPIO_15, (0x42 * 2)); +STMPE1600DigiOut xshutdown_left(&DEV_I2C, GPIO_14, (0x43 * 2)); +STMPE1600DigiOut xshutdown_right(&DEV_I2C, GPIO_15, (0x43 * 2)); +VL53L0X_X_NUCLEO_53L0A1 sensor_vl53l0x_top(&DEV_I2C, &xshutdown_top); +VL53L0X_X_NUCLEO_53L0A1 sensor_vl53l0x_left(&DEV_I2C, &xshutdown_left); +VL53L0X_X_NUCLEO_53L0A1 sensor_vl53l0x_right(&DEV_I2C, &xshutdown_right); /* Setup ---------------------------------------------------------------------*/ @@ -110,43 +110,40 @@ void setup() { // Initialize I2C bus. DEV_I2C.begin(); - // Create VL53L0X top component. - xshutdown_top = new STMPE1600DigiOut(&DEV_I2C, GPIO_15, (0x42 * 2)); - sensor_vl53l0x_top = new VL53L0X_X_NUCLEO_53L0A1(&DEV_I2C, xshutdown_top, A2); + // Configure VL53L0X top component. + sensor_vl53l0x_top.begin(); // Switch off VL53L0X top component. - sensor_vl53l0x_top->VL53L0X_Off(); + sensor_vl53l0x_top.VL53L0X_Off(); - // Create (if present) VL53L0X left component. - xshutdown_left = new STMPE1600DigiOut(&DEV_I2C, GPIO_14, (0x43 * 2)); - sensor_vl53l0x_left = new VL53L0X_X_NUCLEO_53L0A1(&DEV_I2C, xshutdown_left, D8); + // Configure (if present) VL53L0X left component. + sensor_vl53l0x_left.begin(); // Switch off (if present) VL53L0X left component. - sensor_vl53l0x_left->VL53L0X_Off(); + sensor_vl53l0x_left.VL53L0X_Off(); - // Create (if present) VL53L0X right component. - xshutdown_right = new STMPE1600DigiOut(&DEV_I2C, GPIO_15, (0x43 * 2)); - sensor_vl53l0x_right = new VL53L0X_X_NUCLEO_53L0A1(&DEV_I2C, xshutdown_right, D2); + // Configure (if present) VL53L0X right component. + sensor_vl53l0x_right.begin(); // Switch off (if present) VL53L0X right component. - sensor_vl53l0x_right->VL53L0X_Off(); + sensor_vl53l0x_right.VL53L0X_Off(); // Initialize VL53L0X top component. - status = sensor_vl53l0x_top->InitSensor(0x10); + status = sensor_vl53l0x_top.InitSensor(0x10); if(status) { SerialPort.println("Init sensor_vl53l0x_top failed..."); } // Initialize VL53L0X left component. - status = sensor_vl53l0x_left->InitSensor(0x12); + status = sensor_vl53l0x_left.InitSensor(0x12); if(status) { SerialPort.println("Init sensor_vl53l0x_left failed..."); } // Initialize VL53L0X right component. - status = sensor_vl53l0x_right->InitSensor(0x14); + status = sensor_vl53l0x_right.InitSensor(0x14); if(status) { SerialPort.println("Init sensor_vl53l0x_right failed..."); @@ -165,7 +162,7 @@ void loop() { // Read Range. uint32_t distance; int status; - status = sensor_vl53l0x_top->GetDistance(&distance); + status = sensor_vl53l0x_top.GetDistance(&distance); if (status == VL53L0X_ERROR_NONE) { @@ -175,7 +172,7 @@ void loop() { SerialPort.println(report); } - status = sensor_vl53l0x_left->GetDistance(&distance); + status = sensor_vl53l0x_left.GetDistance(&distance); if (status == VL53L0X_ERROR_NONE) { @@ -185,7 +182,7 @@ void loop() { SerialPort.println(report); } - status = sensor_vl53l0x_right->GetDistance(&distance); + status = sensor_vl53l0x_right.GetDistance(&distance); if (status == VL53L0X_ERROR_NONE) { diff --git a/keywords.txt b/keywords.txt index 77c1597..c0137c8 100644 --- a/keywords.txt +++ b/keywords.txt @@ -1,5 +1,5 @@ ####################################### -# Syntax Coloring Map For x-nucleo-53l0a1 +# Syntax Coloring Map For X-NUCLEO-53L0A1 ####################################### ####################################### @@ -15,4 +15,6 @@ VL53L0X_X_NUCLEO_53L0A1 KEYWORD1 VL53L0X_Off KEYWORD2 VL53L0X_On KEYWORD2 +begin KEYWORD2 +end KEYWORD2 write KEYWORD2 diff --git a/library.properties b/library.properties index a70b4c9..5869f66 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=STM32duino X-NUCLEO-53L0A1 -version=1.0.3 +version=2.0.0 author=AST maintainer=stm32duino sentence=Allows controlling the VL53L0X sensors on board of X-NUCLEO-53L0A1 diff --git a/src/stmpe1600_class.h b/src/stmpe1600_class.h index c52b1e0..4da743c 100644 --- a/src/stmpe1600_class.h +++ b/src/stmpe1600_class.h @@ -49,7 +49,7 @@ #define SYS_CTRL (uint8_t)0x03 #define IEGPIOR_0_7 (uint8_t)0x08 #define IEGPIOR_8_15 (uint8_t)0x09 -#define ISGPIOR_0_7 (uint8_t)0x0A +#define ISGPIOR_0_7 (uint8_t)0x0A #define ISGPIOR_8_15 (uint8_t)0x0B #define GPMR_0_7 (uint8_t)0x10 #define GPMR_8_15 (uint8_t)0x11 @@ -58,121 +58,136 @@ #define GPDR_0_7 (uint8_t)0x14 #define GPDR_8_15 (uint8_t)0x15 #define GPIR_0_7 (uint8_t)0x16 -#define GPIR_8_15 (uint8_t)0x17 - - -typedef enum -{ - // GPIO Expander pin names - GPIO_0=0, - GPIO_1, - GPIO_2, - GPIO_3, - GPIO_4, - GPIO_5, - GPIO_6, - GPIO_7, - GPIO_8, - GPIO_9, - GPIO_10, - GPIO_11, - GPIO_12, - GPIO_13, - GPIO_14, - GPIO_15, - NOT_CON +#define GPIR_8_15 (uint8_t)0x17 + + +typedef enum { + // GPIO Expander pin names + GPIO_0 = 0, + GPIO_1, + GPIO_2, + GPIO_3, + GPIO_4, + GPIO_5, + GPIO_6, + GPIO_7, + GPIO_8, + GPIO_9, + GPIO_10, + GPIO_11, + GPIO_12, + GPIO_13, + GPIO_14, + GPIO_15, + NOT_CON } ExpGpioPinName; -typedef enum -{ - EXP_GPIO_INPUT = 0, - EXP_GPIO_OUTPUT, - EXP_GPIO_NOT_CONNECTED +typedef enum { + EXP_GPIO_INPUT = 0, + EXP_GPIO_OUTPUT, + EXP_GPIO_NOT_CONNECTED } ExpGpioPinDirection; /* Classes -------------------------------------------------------------------*/ /** Class representing a single stmpe1600 GPIO expander output pin */ -class STMPE1600DigiOut -{ - -public: - /** Constructor - * @param[in] &i2c device I2C to be used for communication - * @param[in] outpinname the desired out pin name to be created - * @param[in] DevAddr the stmpe1600 I2C device addres (deft STMPE1600_DEF_DEVICE_ADDRESS) - * @param[in] lvl the default ot pin level - */ - STMPE1600DigiOut (TwoWire *i2c, ExpGpioPinName outpinname, uint8_t DevAddr=STMPE1600_DEF_DEVICE_ADDRESS, bool lvl=STMPE1600_DEF_DIGIOUT_LVL): dev_i2c(i2c), expdevaddr(DevAddr), exppinname(outpinname) - { - bit16Tobit8_t data; - if (exppinname == NOT_CON) return; - /* set the exppinname as output */ - STMPE1600DigiOut_I2CRead(&data.u8bit[0], expdevaddr, GPDR_0_7, 1); - STMPE1600DigiOut_I2CRead(&data.u8bit[1], expdevaddr, GPDR_8_15, 1); - data.u16bit = data.u16bit | (1<<(uint16_t)exppinname); // set gpio as out - STMPE1600DigiOut_I2CWrite(&data.u8bit[0], expdevaddr, GPDR_0_7, 1); - STMPE1600DigiOut_I2CWrite(&data.u8bit[1], expdevaddr, GPDR_8_15, 1); - write(lvl); - } - - /** - * @brief Write on the out pin - * @param[in] lvl level to write - * @return 0 on Success - */ - void write (int lvl) - { - bit16Tobit8_t data; - if (exppinname == NOT_CON) return; - /* set the exppinname state to lvl */ - STMPE1600DigiOut_I2CRead(data.u8bit, expdevaddr, GPSR_0_7, 2); - data.u16bit = data.u16bit & (uint16_t)(~(1<<(uint16_t)exppinname)); // set pin mask - if (lvl) data.u16bit = data.u16bit | (uint16_t)(1<<(uint16_t)exppinname); - STMPE1600DigiOut_I2CWrite(data.u8bit, expdevaddr, GPSR_0_7, 2); - } - -private: - TwoWire *dev_i2c; - uint8_t expdevaddr; - ExpGpioPinName exppinname; - - int STMPE1600DigiOut_I2CWrite(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, uint16_t NumByteToWrite) - { +class STMPE1600DigiOut { + + public: + /** Constructor + * @param[in] &i2c device I2C to be used for communication + * @param[in] outpinname the desired out pin name to be created + * @param[in] DevAddr the stmpe1600 I2C device address (deft STMPE1600_DEF_DEVICE_ADDRESS) + * @param[in] lvl the default output pin level + */ + STMPE1600DigiOut(TwoWire *i2c, ExpGpioPinName outpinname, uint8_t DevAddr = STMPE1600_DEF_DEVICE_ADDRESS, bool lvl = STMPE1600_DEF_DIGIOUT_LVL): dev_i2c(i2c), expdevaddr(DevAddr), exppinname(outpinname) + { + if (exppinname == NOT_CON) { + return; + } + pinlvl = lvl; + } + + int begin() + { + bit16Tobit8_t data; + /* set the exppinname as output */ + STMPE1600DigiOut_I2CRead(&data.u8bit[0], expdevaddr, GPDR_0_7, 1); + STMPE1600DigiOut_I2CRead(&data.u8bit[1], expdevaddr, GPDR_8_15, 1); + data.u16bit = data.u16bit | (1 << (uint16_t)exppinname); // set gpio as out + STMPE1600DigiOut_I2CWrite(&data.u8bit[0], expdevaddr, GPDR_0_7, 1); + STMPE1600DigiOut_I2CWrite(&data.u8bit[1], expdevaddr, GPDR_8_15, 1); + write(pinlvl); + return 0; + } + + int end() + { + /* Do nothing */ + return 0; + } + + /** + * @brief Write on the out pin + * @param[in] lvl level to write + * @return 0 on Success + */ + void write(int lvl) + { + bit16Tobit8_t data; + if (exppinname == NOT_CON) { + return; + } + /* set the exppinname state to lvl */ + STMPE1600DigiOut_I2CRead(data.u8bit, expdevaddr, GPSR_0_7, 2); + data.u16bit = data.u16bit & (uint16_t)(~(1 << (uint16_t)exppinname)); // set pin mask + if (lvl) { + data.u16bit = data.u16bit | (uint16_t)(1 << (uint16_t)exppinname); + } + STMPE1600DigiOut_I2CWrite(data.u8bit, expdevaddr, GPSR_0_7, 2); + } + + private: + TwoWire *dev_i2c; + uint8_t expdevaddr; + ExpGpioPinName exppinname; + bool pinlvl; + + int STMPE1600DigiOut_I2CWrite(uint8_t *pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, uint16_t NumByteToWrite) + { dev_i2c->beginTransmission(((uint8_t)(((DeviceAddr) >> 1) & 0x7F))); dev_i2c->write(RegisterAddr); - for (uint16_t i = 0 ; i < NumByteToWrite ; i++) - dev_i2c->write(pBuffer[i]); + for (uint16_t i = 0 ; i < NumByteToWrite ; i++) { + dev_i2c->write(pBuffer[i]); + } dev_i2c->endTransmission(true); return 0; - } + } - int STMPE1600DigiOut_I2CRead(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, uint16_t NumByteToRead) - { + int STMPE1600DigiOut_I2CRead(uint8_t *pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, uint16_t NumByteToRead) + { dev_i2c->beginTransmission(((uint8_t)(((DeviceAddr) >> 1) & 0x7F))); dev_i2c->write(RegisterAddr); dev_i2c->endTransmission(false); dev_i2c->requestFrom(((uint8_t)(((DeviceAddr) >> 1) & 0x7F)), (byte) NumByteToRead); - int i=0; - while (dev_i2c->available()) - { - pBuffer[i] = dev_i2c->read(); - i++; + int i = 0; + while (dev_i2c->available()) { + pBuffer[i] = dev_i2c->read(); + i++; } return 0; - } - - typedef union - { + } + + typedef union { uint16_t u16bit; uint8_t u8bit[2]; - } bit16Tobit8_t; + } bit16Tobit8_t; }; diff --git a/src/vl53l0x_x_nucleo_53l0a1_class.h b/src/vl53l0x_x_nucleo_53l0a1_class.h index 4347569..fdf331d 100644 --- a/src/vl53l0x_x_nucleo_53l0a1_class.h +++ b/src/vl53l0x_x_nucleo_53l0a1_class.h @@ -57,7 +57,7 @@ class VL53L0X_X_NUCLEO_53L0A1 : public VL53L0X * @param[in] pin_gpio1 pin Mbed InterruptIn PinName to be used as component GPIO_1 INT * @param[in] device address, 0x29 by default */ - VL53L0X_X_NUCLEO_53L0A1(TwoWire *i2c, STMPE1600DigiOut *pin, int pin_gpio1, uint8_t DevAddr=VL53L0x_DEFAULT_DEVICE_ADDRESS) : VL53L0X(i2c, -1, pin_gpio1, DevAddr) + VL53L0X_X_NUCLEO_53L0A1(TwoWire *i2c, STMPE1600DigiOut *pin) : VL53L0X(i2c, -1) { expgpio0 = pin; } @@ -68,6 +68,16 @@ class VL53L0X_X_NUCLEO_53L0A1 : public VL53L0X /* warning: VL53L0X_X_NUCLEO_53L0A1 class inherits from GenericSensor, RangeSensor and LightSensor, that haven`t a destructor. The warning should request to introduce a virtual destructor to make sure to delete the object */ + int begin() + { + return expgpio0->begin(); + } + + int end() + { + return expgpio0->end(); + } + /*** Interface Methods ***/ /*** High level API ***/ /**