/// <summary> /// Initializes a new instance of the <see cref="ProximitySensor"/> class /// </summary> /// <param name="proximitySensorType"> /// </param> /// <param name="analogInput"> /// The analog input to which the proximity sensor is attached /// </param> /// <exception cref="ArgumentNullException"> /// Thrown when the given <see cref="AnalogInput"/> is null /// </exception> /// <remarks> /// This sensor is only provides valid results for objects farther than 3" away or objects closer than /// <see cref="MaximumReadableDistance"/> from the sensor. Passing a trigger which attempts to obtain /// distances outside of this range will result in a large number of false positives. /// </remarks> public ProximitySensor(ProximitySensorType proximitySensorType, AnalogInput analogInput) { if (analogInput == null) { throw new ArgumentNullException("analogInput"); } this.proximitySensorType = proximitySensorType; MaximumReadableDistance = GetMaximumReadableDistance(analogInput); new Thread(() => { while (true) { if (IsEnabled && ObjectDetectionTrigger != null) { var distance = new Distance(analogInput.ReadRaw(), proximitySensorType); if (ObjectDetectionTrigger(MaximumReadableDistance, distance)) { if (ObjectDetected != null) { ObjectDetected(this, new ObjectDetectedEventArgs(distance, DateTime.UtcNow)); } } } Thread.Sleep(10); } }).Start(); }
/// <summary> /// Initializes a new instance of the <see cref="ObjectDetectedEventArgs"/> class /// </summary> /// <param name="distance"> /// The distance an object lies from the proximity sensor /// </param> /// <param name="dateTime"> /// The date and time of the event /// </param> public ObjectDetectedEventArgs(Distance distance, DateTime dateTime) { Distance = distance; DateTime = dateTime; }
/// <summary> /// Determines whether the specified <see cref="Distance"/> is equal to the current <see cref="Distance"/> /// </summary> /// <param name="distance"> /// The <see cref="Distance"/> to compare with the current <see cref="Distance"/> /// </param> /// <returns> /// <value> /// true /// </value> /// if the specified <see cref="Distance"/> is equal to the current <see cref="Distance"/>; otherwise, /// <value> /// false /// </value> /// </returns> public bool Equals(Distance distance) { return Equals(distance as object); }