示例#1
0
        private void GetValue()
        {
            try
            {
                var arduinoBytes = new byte[10];
                _device.Read(arduinoBytes);

                var sonarWidth = (ushort)(arduinoBytes[0] << 8 | arduinoBytes[1]);

                //var adcReading1 = (ushort)(arduinoBytes[2] << 8 | arduinoBytes[3]);
                //var adcReading2 = (ushort)(arduinoBytes[4] << 8 | arduinoBytes[5]);
                //var adcReading3 = (ushort)(arduinoBytes[6] << 8 | arduinoBytes[7]);
                //var adcReading4 = (ushort)(arduinoBytes[8] << 8 | arduinoBytes[9]);

                ProximityReceived?.Invoke(this, new ProximtyEventArgs
                {
                    RawValue  = sonarWidth,
                    Proximity = Math.Max(0, ((sonarWidth / 2d) / 2.91d) - 0.5d) //The sensors are about 5mm inside the casing of the device.
                });
            }
            catch (Exception ex)
            {
                SensorException?.Invoke(this, new ExceptionEventArgs
                {
                    Exception = ex,
                    Message   = "An error occurred reading values from Arduino Sensor Device"
                });
            }
        }
示例#2
0
        private void RequestSensorToStartCollectingData()
        {
            try
            {
                var command = (byte)(VCNL4000_Constants.VCNL4000_MEASUREPROXIMITY.GetHashCode() | VCNL4000_Constants.VCNL4000_MEASUREAMBIENT.GetHashCode());

                WriteCommandToCommandRegister(command);
            }
            catch (Exception ex)
            {
                SensorException?.Invoke(this, new ExceptionEventArgs
                {
                    Exception = ex,
                    Message   = "An Error Occured writing the request to read the proximity."
                });
            }
        }
示例#3
0
 private void SetPositionAndGetReading(int position)
 {
     try
     {
         _contoller.SetPwm(PwmChannel.C0, 0, position);
         Task.Delay(50).Wait();
         if (_haveReading())
         {
             PositionFound?.Invoke(this, new PositionalDistanceEventArgs {
                 Angle = Angle(position), Proximity = _lastReading.Proximity, RawValue = _lastReading.RawValue
             });
         }
     }
     catch (Exception ex)
     {
         SensorException?.Invoke(this, new ExceptionEventArgs {
             Exception = ex, Message = "An error occured setting Angle and getting distance."
         });
     }
 }
示例#4
0
        private int RawAmbientLightReading()
        {
            try
            {
                var readAmbientLightCommand = new[] { (byte)VCNL4000_Constants.VCNL4000_AMBIENTDATA.GetHashCode() };
                var ambientBuffer           = new byte[2]; //Read VCNL4000_AMBIENTDATA for two bytes long, its a 16 bit value.

                _device.WriteRead(readAmbientLightCommand, ambientBuffer);
                var rawPAmbientReading = ambientBuffer[0] << 8 | ambientBuffer[1];
                return(rawPAmbientReading);
            }
            catch (Exception ex)
            {
                SensorException?.Invoke(this, new ExceptionEventArgs
                {
                    Exception = ex,
                    Message   = "An Error Occured reading the Ambient Light Results registers."
                });
            }
            return(-1);
        }
示例#5
0
        private int RawProximityReading()
        {
            try
            {
                var readProximityCommand = new[] { (byte)VCNL4000_Constants.VCNL4000_PROXIMITYDATA_1.GetHashCode() };
                var proximityBuffer      = new byte[2]; //Read VCNL4000_PROXIMITYDATA_1 and VCNL4000_PROXIMITYDATA_2 at the same time as they are adjacent register addresses

                _device.WriteRead(readProximityCommand, proximityBuffer);
                var rawProximityReading = proximityBuffer[0] << 8 | proximityBuffer[1];
                return(rawProximityReading);
            }
            catch (Exception ex)
            {
                SensorException?.Invoke(this, new ExceptionEventArgs
                {
                    Exception = ex,
                    Message   = "An Error Occured reading the Proximity Results registers."
                });
            }
            return(-1);
        }