What is the proper way of implementing and handling status return values from the VL53L1X ultralight driver (STSW-IMG009)?
From Pololu's Arduino adaptation of the "heavy" driver =) (STSW-IMG007) I gather that the functions in vl53l1_platform.c should return 0 if they succeed and 1 if they fail, so that's what I've done in the ultralight driver. However, some of the other functions in the ultralight driver don't appear to be handling the status return values correctly. For instance, the function VL53L1X_SensorInit in VL53L1X_api.c mostly ignores the status return until the final call to WL53L1_WrByte, which it then returns as the status for VL53L1X_SensorInit.
VL53L1X_ERROR VL53L1X_SensorInit(uint16_t dev)
{
VL53L1X_ERROR status = 0;
uint8_t Addr = 0x00, tmp;
for (Addr = 0x2D; Addr <= 0x87; Addr++){
status = VL53L1_WrByte(dev, Addr, VL51L1X_DEFAULT_CONFIGURATION[Addr - 0x2D]);
}
status = VL53L1X_StartRanging(dev);
tmp = 0;
while(tmp==0){
status = VL53L1X_CheckForDataReady(dev, &tmp);
}
status = VL53L1X_ClearInterrupt(dev);
status = VL53L1X_StopRanging(dev);
status = VL53L1_WrByte(dev, VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x09); /* two bounds VHV */
status = VL53L1_WrByte(dev, 0x0B, 0); /* start VHV from the previous temperature */
return status;
}If the first call to VL53L1_WrByte had failed, that fact would have been overwritten by subsequent assignments to status and might not be reflected in the status return value for VL53L1X_SensorInit.
I'm thinking that any function containing multiple API function calls that return status should examine that status after each call and return it immediately if it equals 1. My only concern is that that strategy might leave the sensor in an unpredictable state, in which case I'd instead change all the status return value assignments to status return value ORs, e.g.
status |= VL53L1X_ClearInterrupt(dev);
status |= VL53L1X_StopRanging(dev);
status |= VL53L1_WrByte(dev, VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x09); /* two bounds VHV */
status |= VL53L1_WrByte(dev, 0x0B, 0); /* start VHV from the previous temperature */
return status;Do you agree with the premise of my question? If so, which status handling convention would you recommend?
