INSAT Mini Project

Dependencies:   ST_INTERFACES X_NUCLEO_COMMON

Fork of X_NUCLEO_6180XA1 by ST

Revision:
26:db0cdc5ecc0a
Parent:
24:025afc3fe30f
Child:
28:7c9031e96c22
--- a/Components/VL6180X/vl6180x_class.cpp	Mon Nov 02 13:49:54 2015 +0000
+++ b/Components/VL6180X/vl6180x_class.cpp	Wed Nov 04 09:11:52 2015 +0100
@@ -528,7 +528,7 @@
         if( status ) break;
         status = VL6180x_AlsSetAnalogueGain(dev,  0);
         if( status ) break;
-        status = VL6180x_AlsSetThresholds(dev, 0, 0xFFFF);
+        status = VL6180x_AlsSetThresholds(dev, 0, 1800);
         if( status ) break;
         /* set Als InterruptMode to new sample */
         status=VL6180x_AlsConfigInterrupt(dev, CONFIG_GPIO_INTERRUPT_DISABLED);
@@ -725,10 +725,12 @@
  
  
  
-int VL6180X::VL6180x_AlsSetThresholds(VL6180xDev_t dev, uint16_t low, uint16_t high) { 
+int VL6180X::VL6180x_AlsSetThresholds(VL6180xDev_t dev, lux_t low, lux_t high) { 
     int status;
     uint32_t AlsAnGain, IntPeriod, AlsScaler, GainFix, RawAlsHigh, RawAlsLow;
+    uint16_t RawThreshLow, RawThreshHigh;
     const uint32_t LuxResxIntIme =(uint32_t)(0.56f* DEF_INT_PEFRIOD *(1<<LUXRES_FIX_PREC));
+    void *ptr;
  
     LOG_FUNCTION_START("%d %d", (int )low, (int)high);
     AlsAnGain=VL6180xDevDataGet(dev, AlsGainCode);
@@ -738,16 +740,18 @@
     IntPeriod++;
     RawAlsLow=low*AlsScaler*GainFix;
     RawAlsLow=RawAlsLow*IntPeriod;
-    RawAlsLow=RawAlsLow/LuxResxIntIme;
-    
+    RawAlsLow=RawAlsLow/LuxResxIntIme;  
     RawAlsHigh=high*(AlsScaler*GainFix);
     RawAlsHigh=RawAlsHigh*IntPeriod;
     RawAlsHigh=RawAlsHigh/LuxResxIntIme;
-    status = VL6180x_WrByte(dev, SYSALS_THRESH_LOW, (uint16_t)RawAlsLow); 
+    ptr=&RawAlsLow;
+    RawThreshLow=*(uint16_t*)ptr;
+    status = VL6180x_WrWord(dev, SYSALS_THRESH_LOW, RawThreshLow); 
     if(!status ){
-        status = VL6180x_WrByte(dev, SYSALS_THRESH_HIGH, (uint16_t)RawAlsHigh);
+        ptr=&RawAlsHigh;
+        RawThreshHigh=*(uint16_t*)ptr;
+        status = VL6180x_WrWord(dev, SYSALS_THRESH_HIGH, RawThreshHigh);
     }
- 
     LOG_FUNCTION_END(status) ;
     return status;
 }
@@ -2896,7 +2900,7 @@
         else
            return (r_status|l_status);                  
             
-      case(range_continuous_interrrupt_high_threshold):   
+      case(range_continuous_interrupt_high_threshold):   
         status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_LEVEL_HIGH);
         status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED);
         if((!r_status)&&(!l_status))
@@ -3229,9 +3233,9 @@
 int VL6180X::AlsSetLowThreshold(uint16_t threshold)
 {
    int status;
-   uint16_t low, high;
+   lux_t low, high;
      
-   status=VL6180x_AlsGetThresholds(Device, &low, &high);
+   status=AlsGetThresholds(Device, &low, &high);
    if(!status)
       status=VL6180x_AlsSetThresholds(Device, threshold, high);
    return status;
@@ -3241,31 +3245,47 @@
 int VL6180X::AlsSetHighThreshold(uint16_t threshold)
 {
    int status;
-   uint16_t low, high;
+   lux_t low, high;
      
-   status=VL6180x_AlsGetThresholds(Device, &low, &high);
+   status=AlsGetThresholds(Device, &low, &high);
    if(!status)
       status=VL6180x_AlsSetThresholds(Device, low, threshold);
    return status;
 }
  
     
-int VL6180X::VL6180x_AlsGetThresholds(VL6180xDev_t dev, uint16_t *low, uint16_t *high)  
+int VL6180X::AlsGetThresholds(VL6180xDev_t dev, lux_t *low, lux_t *high)  
 {
    int status;
+   uint16_t RawAlsLow, RawAlsHigh;
+   uint32_t luxLowValue, luxHighValue, IntPeriod, AlsAnGain, GainFix, AlsScaler;
+   const uint32_t LuxResxIntIme =(uint32_t)(0.56f* DEF_INT_PEFRIOD *(1<<LUXRES_FIX_PREC));
      
-   status=VL6180x_RdWord(dev, SYSALS_THRESH_LOW, low);
+   status=VL6180x_RdWord(dev, SYSALS_THRESH_LOW, &RawAlsLow);
    if(status)
    {
       VL6180x_ErrLog("rd SYSALS_THRESH_LOW fail");
       return status;
    }
-   status=VL6180x_RdWord(dev, SYSALS_THRESH_HIGH, high);
+   status=VL6180x_RdWord(dev, SYSALS_THRESH_HIGH, &RawAlsHigh);
    if(status)
    {
       VL6180x_ErrLog("rd SYSALS_THRESH_HIGH fail");
       return status;
    }
+   AlsAnGain=VL6180xDevDataGet(dev, AlsGainCode);
+   IntPeriod=VL6180xDevDataGet(dev, IntegrationPeriod);
+   AlsScaler=VL6180xDevDataGet(dev, AlsScaler);
+   GainFix=AlsGainLookUp[AlsAnGain];
+   IntPeriod++;
+   luxLowValue=(uint32_t)RawAlsLow*LuxResxIntIme;
+   luxLowValue=luxLowValue/IntPeriod;
+   luxLowValue=luxLowValue/(AlsScaler*GainFix);
+   luxHighValue=(uint32_t)RawAlsHigh*LuxResxIntIme;
+   luxHighValue=luxHighValue/IntPeriod;
+   luxHighValue=luxHighValue/(AlsScaler*GainFix);
+   *low=luxLowValue;
+   *high=luxHighValue;
    return status;
 }
     
@@ -3281,7 +3301,7 @@
       case(range_continuous_polling_high_threshold):
       case(range_continuous_polling_out_of_window):
       case(range_continuous_interrupt_low_threshold):
-      case(range_continuous_interrrupt_high_threshold):
+      case(range_continuous_interrupt_high_threshold):
       case(range_continuous_interrupt_out_of_window):
         return GetRangeMeas(operating_mode, Data);
          
@@ -3351,7 +3371,7 @@
       else
          return NOT_READY;
    }
-   else if((operating_mode==range_continuous_polling_high_threshold)||(operating_mode==range_continuous_interrrupt_high_threshold))
+   else if((operating_mode==range_continuous_polling_high_threshold)||(operating_mode==range_continuous_interrupt_high_threshold))
    {
       if(IntStatus.status.Range==RES_INT_STAT_GPIO_HIGH_LEVEL_THRESHOLD)
          status=VL6180x_RangeGetMeasurement(Device, &RangeData);
@@ -3509,7 +3529,7 @@
       case(range_continuous_polling_high_threshold):
       case(range_continuous_polling_out_of_window):
       case(range_continuous_interrupt_low_threshold):
-      case(range_continuous_interrrupt_high_threshold):
+      case(range_continuous_interrupt_high_threshold):
       case(range_continuous_interrupt_out_of_window):
         return StopRangeMeasurement(operating_mode);
          
@@ -3599,7 +3619,7 @@
       VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail");
       return status;
    }
-   status=VL6180x_AlsSetThresholds(Device, 0x0, 0xFFFF);
+   status=VL6180x_AlsSetThresholds(Device, 0x0, 1800);
    if(status)
       VL6180x_ErrLog("VL6180x_AlsSetThresholds fail");
    return status;