Ejemplo n.º 1
0
 void UpdateSensor_Tick(object sender, EventArgs e)
 {
     if (sensor == null)
     {
         return;
     }
     try
     {
         lock (sensor)
         {
             float lux = sensor.GetCurrentReading().IlluminanceInLux;
             if (lastEffectiveReadingTime < DateTime.Now - GraceTime)
             {
                 if (lux <= Dark && lastState == GlanceEventArgs.Light)
                 {
                     lastEffectiveReadingTime = DateTime.Now;
                     lastState = GlanceEventArgs.Dark;
                     RaiseGlanceEvent(GlanceEventArgs.Dark);
                 }
                 else if (lux > Dark && lastState == GlanceEventArgs.Dark)
                 {
                     lastEffectiveReadingTime = DateTime.Now;
                     lastState = GlanceEventArgs.Light;
                     RaiseGlanceEvent(GlanceEventArgs.Light);
                 }
             }
         }
     }
     catch { }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Ambient Light Sensor adjusts the brightness of the display. Less ambient light equates to a dimmer display.
        /// </summary>
        /// <param name="lightSensor"></param>
        /// <param name="e"></param>
        private async void LightSensor_ReadingChanged(LightSensor lightSensor, LightSensorReadingChangedEventArgs e)
        {
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                try
                {
                    LightSensorReading lightSensorReading = lightSensor.GetCurrentReading();

                    if (BrightnessOverrideSetting != null && BrightnessOverrideSetting.IsSupported)
                    {
                        const double maximumAllowedBrightness            = 0.15;
                        const double highestLuxValueBeforeFullBrightness = 25.0;

                        double brightness = Math.Min(lightSensorReading.IlluminanceInLux, highestLuxValueBeforeFullBrightness) / highestLuxValueBeforeFullBrightness *maximumAllowedBrightness;

                        if (PreviousDisplayBrightness != brightness)
                        {
                            BrightnessOverrideSetting.SetBrightnessLevel(brightness, DisplayBrightnessOverrideOptions.None);

                            PreviousDisplayBrightness = brightness;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Telemetry.TrackException(nameof(LightSensor_ReadingChanged), ex);
                }
            });
        }
Ejemplo n.º 3
0
        public SensorReading Get()
        {
            var reading = new SensorReading();

            if (lightSensor != null)
            {
                reading.Lux = lightSensor.GetCurrentReading().IlluminanceInLux;
            }
            if (gyroMeter != null)
            {
                var gyroReading = gyroMeter.GetCurrentReading();
                reading.GyroValue = new XYZValue
                {
                    X = gyroReading.AngularVelocityX,
                    Y = gyroReading.AngularVelocityY,
                    Z = gyroReading.AngularVelocityZ
                };
            }
            if (magnetometer != null)
            {
                var magReading = magnetometer.GetCurrentReading();
                reading.MagnetoValue = new XYZValue
                {
                    X = magReading.MagneticFieldX,
                    Y = magReading.MagneticFieldY,
                    Z = magReading.MagneticFieldZ
                };
            }
            Console.WriteLine("returning sensor reading: " + JsonConvert.SerializeObject(reading));

            return(reading);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Adjusts the exposure.
        /// </summary>
        /// <param name="average"></param>
        /// <returns>True, if successful.</returns>
        private async Task <bool> AdjustExposureAsync(double average)
        {
            bool success = false;

            if (IsExposureSupported)
            {
                double illuminance      = 0;
                double exposureAdjusted = 0;
                var    min = _videoDeviceController.ExposureControl.Min;

                double brightness = 0;
                _videoDeviceController.Brightness.TryGetValue(out brightness);

                if (average == 0 && _lightSensor != null)
                {
                    var reading = _lightSensor.GetCurrentReading();
                    illuminance = reading.IlluminanceInLux;
                }
                else
                {
                    illuminance = average;
                }

                /*
                 * 50, inside. light only from windows
                 * 100, inside. lighs on
                 * 500...1000, outside
                 */
                if (illuminance < 1000)
                {
                    exposureAdjusted = 25 - (illuminance / 1000) * 25;
                }
                else
                {
                    exposureAdjusted = 0;
                }

                await _videoDeviceController.ExposureControl.SetAutoAsync(false);

                await _videoDeviceController.ExposureControl.SetValueAsync(
                    min + TimeSpan.FromTicks((long)((double)min.Ticks *exposureAdjusted)));

                IsLuminanceShown = true;

                string luminanceAndExposure =
                    "Luminance: " + ((int)illuminance).ToString()
                    + "\nExposure: " + ((int)exposureAdjusted).ToString();

                if (ShowMessageRequest != null)
                {
                    ShowMessageRequest(this, luminanceAndExposure);
                }

                _lastLightMeasurement = exposureAdjusted;
                success = true;
            }

            return(success);
        }
 void timer_Tick(object sender, object e)
 {
     Dispatcher.BeginInvoke(new Action(() =>
     {
         var reading    = lightSensor.GetCurrentReading();
         TextLight.Text = string.Format(@"Light Sensor : {0}", reading.IlluminanceInLux.ToString());
     }));
 }
Ejemplo n.º 6
0
        /// <summary>
        /// This is the dispatcher callback.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void DisplayCurrentReading(object sender, object args)
        {
            LightSensorReading reading = _sensor.GetCurrentReading();

            if (reading != null)
            {
                ScenarioOutput_LUX.Text = String.Format("{0,5:0.00}", reading.IlluminanceInLux);
            }
        }
        public double lireMesureDuCapteur()
        {
            double valeurRetour = -1;

            LightSensorReading valeurLue = lightsensor.GetCurrentReading();

            valeurRetour = System.Convert.ToDouble(valeurLue.IlluminanceInLux);

            return(valeurRetour);
        }
Ejemplo n.º 8
0
 public Glance()
 {
     _updateSensor          = new DispatcherTimer();
     _updateSensor.Interval = TimeSpan.FromSeconds(0.25);
     _updateSensor.Tick    += UpdateSensor_Tick;
     sensor = LightSensor.GetDefault();
     if (sensor != null)
     {
         //sensor.ReportInterval = sensor.MinimumReportInterval;
         lastEffectiveReadingTime = DateTime.Now;
         lastState = sensor.GetCurrentReading().IlluminanceInLux <= Dark ? GlanceEventArgs.Dark : GlanceEventArgs.Light;
     }
 }
Ejemplo n.º 9
0
        public LightSensorReading GetLightSensorReading()
        {
            if (_lightSensor == null)
            {
                throw new InvalidOperationException("The light sensor is either not present or has not been initialized");
            }

            var reading = _lightSensor.GetCurrentReading();

            return(reading);
            // Available reading values include:
            // reading.IlluminanceInLux
            // reading.Timestamp
        }
Ejemplo n.º 10
0
        private void OnGetLight(object sender, RoutedEventArgs e)
        {
            LightSensor sensor = LightSensor.GetDefault();

            if (sensor != null)
            {
                LightSensorReading reading = sensor.GetCurrentReading();
                this.DefaultViewModel["LightResult"] = string.Format("Illuminance: {0} Lux", reading.IlluminanceInLux);
            }
            else
            {
                this.DefaultViewModel["LightResult"] = "No light sensor found";
            }
        }
Ejemplo n.º 11
0
        public void OnGetLight()
        {
            LightSensor sensor = LightSensor.GetDefault();

            if (sensor != null)
            {
                LightSensorReading reading = sensor.GetCurrentReading();
                Illuminance = $"Illuminance: {reading?.IlluminanceInLux}";
            }
            else
            {
                Illuminance = "Light sensor not found";
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Update Light Sensor readings event and update UI
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">The <see cref="object"/> instance containing the event data.</param>
        private void UpdateLightSensor(object sender, EventArgs e)
        {
            try
            {
                LightSensor light = LightSensor.GetDefault();
                if (light != null)
                {
                    SensorLabel.Text = LocRM.GetString("ALSReading") + ": " + light.GetCurrentReading().IlluminanceInLux.ToString();

                    int colorValue = (int)Math.Min(light.GetCurrentReading().IlluminanceInLux, 255);
                    labelLight.ForeColor = Color.FromArgb(colorValue, colorValue, 0);
                }
                else
                {
                    SensorLabel.Text = LocRM.GetString("NotFound");
                    _timer.Stop();
                }
            }
            catch (Exception ex)
            {
                Log.LogError(ex.ToString());
                _timer.Stop();
            }
        }
Ejemplo n.º 13
0
        public async void NewLight(LightSensor sender, LightSensorReadingChangedEventArgs args)
        {
            var reading = args == null?sender?.GetCurrentReading() : args.Reading;

            await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                this[LIGHTSENSOR] = reading == null
                    ? this[LIGHTSENSOR].New()
                    : this[LIGHTSENSOR].New(reading.IlluminanceInLux, 0, 0, 0);
                if (this[LIGHTSENSOR].IsChanged)
                {
                    OnPropertyChanged(new PropertyChangedEventArgs("ItemsList"));
                    OnSensorUpdated?.Invoke(this[LIGHTSENSOR]);
                }
            });

            if (SensorSwitches.P.HasValue && (SensorSwitches.P.Value == 1 || SensorSwitches.P.Value == 3))
            {
                SensorSwitches.P = 0;
            }
        }