INSAT Mini Project
Dependencies: ST_INTERFACES X_NUCLEO_COMMON
Fork of X_NUCLEO_6180XA1 by
Diff: Components/VL6180X/vl6180x_class.h
- Revision:
- 12:71d589e6fd2c
- Parent:
- 11:88541229403e
- Child:
- 14:0effa0bbf192
--- a/Components/VL6180X/vl6180x_class.h Tue Oct 06 09:43:25 2015 +0200 +++ b/Components/VL6180X/vl6180x_class.h Tue Oct 13 14:22:45 2015 +0200 @@ -50,47 +50,75 @@ //#include "vl6180x_appcfg.h" #include "STMPE1600_class.h" +/* data struct containing range measure, light measure and type of error provided to the user */ +typedef struct MeasureData +{ + int32_t range_mm; + uint32_t lux; + uint32_t range_error; + uint32_t als_error; + uint32_t int_error; +}MeasureData_t; + +/* sensor operating modes */ +typedef enum +{ + range_single_shot_polling=1, + als_single_shot_polling, + range_continuous_polling, + als_continuous_polling, + range_continuous_interrupt, + als_continuous_interrupt, + range_continuous_als_single_shot, + range_single_shot_als_continuous, + interleaved_mode, +}OperatingMode; + +/* type of sensor measurement */ +typedef enum +{ + range_measure=1, + light_measure, + light_range_measure, +}MeasureType; + + /** default device address */ #define DEFAULT_DEVICE_ADDRESS 0x29 /* Classes -------------------------------------------------------------------*/ /** Class representing a VL6180X sensor component */ - //FIXME verficare se impostare le funzioni come virtual come nel caso IKS class VL6180X : public RangeSensor, public LightSensor { public: /** Constructor * @param[in] i2c device I2C to be used for communication * @param[in] digital out pin/STMPE1600DigiOut pin to be used for GPIO expander communication - * @param[in] device address, 0x29 by default - * @param[in] device id, 0 by default + * @param[in] gpio1 pin + * @param[in] device address, 0x29 by default */ - VL6180X(DevI2C &i2c, DigitalOut &pin, /*PinName pin_gpio1,*/ uint8_t DevAddr=DEFAULT_DEVICE_ADDRESS) : RangeSensor(), LightSensor(), dev_i2c(i2c), gpio0(&pin) + VL6180X(DevI2C &i2c, DigitalOut &pin, PinName pin_gpio1, uint8_t DevAddr=DEFAULT_DEVICE_ADDRESS) : RangeSensor(), LightSensor(), dev_i2c(i2c), gpio0(&pin), interrupt_measure(pin_gpio1) { MyDevice.I2cAddr=DevAddr; MyDevice.Present=0; MyDevice.Ready=0; Device=&MyDevice;; expgpio0=NULL; - //free_fall=new InterruptIn(pin_gpio1); } - VL6180X(DevI2C &i2c, STMPE1600DigiOut &pin, /*PinName pin_gpio1,*/ uint8_t DevAddr=DEFAULT_DEVICE_ADDRESS) : RangeSensor(), LightSensor(), dev_i2c(i2c), expgpio0(&pin) + VL6180X(DevI2C &i2c, STMPE1600DigiOut &pin, PinName pin_gpio1, uint8_t DevAddr=DEFAULT_DEVICE_ADDRESS) : RangeSensor(), LightSensor(), dev_i2c(i2c), expgpio0(&pin), interrupt_measure(pin_gpio1) { MyDevice.I2cAddr=DevAddr; MyDevice.Present=0; MyDevice.Ready=0; Device=&MyDevice; - gpio0=NULL; - //free_fall=new InterruptIn(pin_gpio1); - } - + gpio0=NULL; + } /** Destructor */ - //~VL6180X(){} - + //~VL6180X(){} void VL6180x_On(void) { if (gpio0) *gpio0=1; @@ -101,54 +129,91 @@ if (gpio0) *gpio0=0; else if (expgpio0) *expgpio0=0; } - - int InitSensor(uint8_t NewAddr) - { - int status; - - VL6180x_Off(); - VL6180x_On(); - status=IsPresent(); - if(!status) - { - Device->Present=1; - status=Init(); - if(status) - error("Failed to init VL6180X sensor!\n"); - status=Prepare(); - if(status) - error("Failed to prepare VL6180X!\n"); - status=SetI2CAddress(NewAddr); - if(status) - error("Failed to change I2C address!\n"); - Device->Ready=1; - } - return status; - } - - int Init() - { - return VL6180x_InitData(Device); // ritorna 0 se corretto - } - + int IsPresent() { int status; status=ReadID(); - if(!status) - error("Failed to read ID device! Device not present!\n"); + if(status) + printf("Failed to read ID device! Device not present!\n"); //FIXME da sistemare la funzione di stampa errore ErrLog da platform.h return status; } - void SetPresent() + int InitSensor(uint8_t NewAddr); + + int StartMeasurement(OperatingMode operating_mode, void (*fptr)(void), MeasureData_t *Data); + + int GetRangeError(MeasureData_t *Data, VL6180x_RangeData_t RangeData); + + int GetAlsError(MeasureData_t *Data, VL6180x_AlsData_t AlsData); + + int RangeMeasPollSingleShot(MeasureData_t *Data); + + int AlsMeasPollSingleShot(MeasureData_t *Data); + + int AlsStartContinuousMode() { - Device->Present=1; + return VL6180x_AlsSetSystemMode(Device, MODE_START_STOP|MODE_CONTINUOUS); } - void SetReady() + int RangeMeasPollContinuousMode(MeasureData_t *Data); + + int AlsMeasPollContinuousMode(MeasureData_t *Data); + + int GetRangeMeasContinuousMode(MeasureData_t *Data); + + int GetAlsMeasContinuousMode(MeasureData_t *Data); + + int RangeMeasIntContinuousMode(void (*fptr)(void)); + + int AlsMeasIntContinuousMode(void (*fptr)(void)); + + int InterleavedMode(void (*fptr)(void)); + + int StartInterleavedMode(); + + /* handling functions of the interrupt_measure */ + + /** Attach a function to call when an interrupt is detected, i.e. measurement is ready + * + * @param[in] fptr A pointer to a void function, or 0 to set as none + */ + void AttachInterruptMeasureDetectionIRQ(void (*fptr)(void)) { - Device->Ready=1; + interrupt_measure.rise(fptr); + } + + /** Enable interrupt measure IRQ + */ + void EnableInterruptMeasureDetectionIRQ(void) + { + interrupt_measure.enable_irq(); + } + + /** Disable interrupt measure IRQ + */ + void DisableInterruptMeasureDetectionIRQ(void) + { + interrupt_measure.disable_irq(); + } + + void HandleIRQ(MeasureType measure_type, MeasureData_t *Data); // FIXME sistemare la sequenza delle funzioni completandola con i print degli errori + + /* Wrapper functions */ + int WaitDeviceBooted() + { + return VL6180x_WaitDeviceBooted(Device); + } + + int Init() + { + return VL6180x_InitData(Device); + } + + int SetupGPIO1(uint8_t InitFunction, int ActiveHigh) + { + return VL6180x_SetupGPIO1(Device, InitFunction, ActiveHigh); } int Prepare() @@ -156,6 +221,201 @@ return VL6180x_Prepare(Device); } + int RangeStartContinuousMode() + { + return VL6180x_RangeStartContinuousMode(Device); + } + + int RangeStartSingleShot() + { + return VL6180x_RangeStartSingleShot(Device); + } + + int RangeSetMaxConvergenceTime(uint8_t MaxConTime_msec) + { + return VL6180x_RangeSetMaxConvergenceTime(Device, MaxConTime_msec); + } + + int RangePollMeasurement(VL6180x_RangeData_t *pRangeData) + { + return VL6180x_RangePollMeasurement(Device, pRangeData); + } + + int RangeGetMeasurementIfReady(VL6180x_RangeData_t *pRangeData) + { + return VL6180x_RangeGetMeasurementIfReady(Device, pRangeData); + } + + int RangeGetMeasurement(VL6180x_RangeData_t *pRangeData) + { + return VL6180x_RangeGetMeasurement(Device, pRangeData); + } + + int GetRange(int32_t *piData) + { + return VL6180x_RangeGetResult(Device, piData); + } + + int RangeConfigInterrupt(uint8_t ConfigGpioInt) + { + return VL6180x_RangeConfigInterrupt(Device, ConfigGpioInt); + } + + int RangeGetInterruptStatus(uint8_t *pIntStatus) + { + return VL6180x_RangeGetInterruptStatus(Device, pIntStatus); + } + + int AlsPollMeasurement(VL6180x_AlsData_t *pAlsData) + { + return VL6180x_AlsPollMeasurement(Device, pAlsData); + } + + int AlsGetMeasurement(VL6180x_AlsData_t *pAlsData) + { + return VL6180x_AlsGetMeasurement(Device, pAlsData); + } + + int AlsConfigInterrupt(uint8_t ConfigGpioInt) + { + return VL6180x_AlsConfigInterrupt(Device, ConfigGpioInt); + } + + int AlsSetIntegrationPeriod(uint16_t period_ms) + { + return VL6180x_AlsSetIntegrationPeriod(Device, period_ms); + } + + int AlsSetInterMeasurementPeriod(uint16_t intermeasurement_period_ms) + { + return VL6180x_AlsSetInterMeasurementPeriod(Device, intermeasurement_period_ms); + } + + int AlsSetAnalogueGain(uint8_t gain) + { + return VL6180x_AlsSetAnalogueGain(Device, gain); + } + + int AlsSetThresholds(uint8_t low, uint8_t high) + { + return VL6180x_AlsSetThresholds(Device, low, high); + } + + int AlsGetInterruptStatus(uint8_t *pIntStatus) + { + return VL6180x_AlsGetInterruptStatus(Device, pIntStatus); + } + + int StaticInit() + { + return VL6180x_StaticInit(Device); + } + + int RangeWaitDeviceReady(int MaxLoop ) + { + return VL6180x_RangeWaitDeviceReady(Device, MaxLoop); + } + + int RangeSetInterMeasPeriod(uint32_t InterMeasTime_msec) + { + return VL6180x_RangeSetInterMeasPeriod(Device, InterMeasTime_msec); + } + + int UpscaleSetScaling(uint8_t scaling) + { + return VL6180x_UpscaleSetScaling(Device, scaling); + } + + int UpscaleGetScaling() + { + return VL6180x_UpscaleGetScaling(Device); + } + + uint16_t GetUpperLimit() + { + return VL6180x_GetUpperLimit(Device); + } + + int RangeSetThresholds(uint16_t low, uint16_t high, int SafeHold) + { + return VL6180x_RangeSetThresholds(Device, low, high, SafeHold); + } + + int RangeGetThresholds(uint16_t *low, uint16_t *high) + { + return VL6180x_RangeGetThresholds(Device, low, high); + } + + int RangeSetRawThresholds(uint8_t low, uint8_t high) + { + return VL6180x_RangeSetRawThresholds(Device, low, high); + } + + int RangeSetEceFactor(uint16_t FactorM, uint16_t FactorD) + { + return VL6180x_RangeSetEceFactor(Device, FactorM, FactorD); + } + + int RangeSetEceState(int enable) + { + return VL6180x_RangeSetEceState(Device, enable); + } + + int FilterSetState(int state) + { + return VL6180x_FilterSetState(Device, state); + } + + int FilterGetState() + { + return VL6180x_FilterGetState(Device); + } + + int DMaxSetState(int state) + { + return VL6180x_DMaxSetState(Device, state); + } + + int DMaxGetState() + { + return VL6180x_DMaxGetState(Device); + } + + int RangeSetSystemMode(uint8_t mode) + { + return VL6180x_RangeSetSystemMode(Device, mode); + } + + int8_t GetOffsetCalibrationData() + { + return VL6180x_GetOffsetCalibrationData(Device); + } + + void SetOffsetCalibrationData(int8_t offset) + { + return VL6180x_SetOffsetCalibrationData(Device, offset); + } + + int SetXTalkCompensationRate(FixPoint97_t Rate) + { + return VL6180x_SetXTalkCompensationRate(Device, Rate); + } + + int AlsWaitDeviceReady(int MaxLoop) + { + return VL6180x_AlsWaitDeviceReady(Device, MaxLoop); + } + + int AlsSetSystemMode(uint8_t mode) + { + return VL6180x_AlsSetSystemMode(Device, mode); + } + + int SetGroupParamHold(int Hold) + { + return VL6180x_SetGroupParamHold(Device, Hold); + } + int SetI2CAddress(int NewAddr) { int status; @@ -166,46 +426,43 @@ return status; } - int StartMeasurement(int operating_mode) + int SetupGPIOx(int pin, uint8_t IntFunction, int ActiveHigh) { - int status; - - switch(operating_mode) - { - case(1): - return VL6180x_RangePollMeasurement(Device, &RangeData); - case(2): - return VL6180x_AlsPollMeasurement(Device, &AlsData); - case(3): break; - case(4): break; - case(5): break; - case(6): break; - case(7): break; - default: - return INVALID_PARAMS; - } - } - /* - int GetRange(int *piData) { - return VL6180X_RangeGetMeasurement(piData); - } - - int GetLight(int *piData) { - return VL6180X_AlsGetMeasurement(piData); - }*/ - - int RangePollMeasurement() - { - return VL6180x_RangePollMeasurement(Device, &RangeData); + return VL6180x_SetupGPIOx(Device, pin, IntFunction, ActiveHigh); } - int AlsPollMeasurement() + int SetGPIOxPolarity(int pin, int active_high) + { + return VL6180x_SetGPIOxPolarity(Device, pin, active_high); + } + + int SetGPIOxFunctionality(int pin, uint8_t functionality) { - return VL6180x_AlsPollMeasurement(Device, &AlsData); + return VL6180x_SetGPIOxFunctionality(Device, pin, functionality); + } + + int DisableGPIOxOut(int pin) + { + return VL6180x_DisableGPIOxOut(Device, pin); } - - /* api.c functions */ + int GetInterruptStatus(uint8_t *status) + { + return VL6180x_GetInterruptStatus(Device, status); + } + + int ClearInterrupt(uint8_t IntClear) + { + return VL6180x_ClearInterrupt(Device, IntClear ); + } + + int GetLight(uint32_t *piData) + { + return VL6180x_AlsGetLux(Device, piData); + } + + + /* api.h functions */ int VL6180x_WaitDeviceBooted(VL6180xDev_t dev); int VL6180x_InitData(VL6180xDev_t dev ); int VL6180x_SetupGPIO1(VL6180xDev_t dev, uint8_t IntFunction, int ActiveHigh); @@ -256,6 +513,8 @@ int VL6180x_DisableGPIOxOut(VL6180xDev_t dev, int pin); int VL6180x_GetInterruptStatus(VL6180xDev_t dev, uint8_t *status); int VL6180x_ClearInterrupt(VL6180xDev_t dev, uint8_t IntClear ); + + /* Other functions defined in api.c */ int VL6180x_RangeStaticInit(VL6180xDev_t dev); int VL6180x_UpscaleRegInit(VL6180xDev_t dev); int VL6180x_UpscaleStaticInit(VL6180xDev_t dev); @@ -292,15 +551,11 @@ DigitalOut *gpio0; /* GPIO expander */ STMPE1600DigiOut *expgpio0; - /* Input interrupt */ - //InterruptIn *free_fall; + /* Measure detection IRQ */ + InterruptIn interrupt_measure; /* Device data */ MyVL6180Dev_t MyDevice; VL6180xDev_t Device; - /* Device range data */ - VL6180x_RangeData_t RangeData; - /* Device als data */ - VL6180x_AlsData_t AlsData; }; #endif // __VL6180X_CLASS_H