Beispiel #1
0
        public async Task <double> GetDistanceInCmAsync(int timeoutInMilliseconds)
        {
            double distance = MAX_DISTANCE_cm;

            _ticks = 0L;
            long ticktimeout = timeoutInMilliseconds * _ticks_per_second / 1000;
            bool find_edge   = Edge.leading;

            try
            {
                _stopwatch.Reset();
                await SendPulse(PULSE_WIDTH_ms);

                _stopwatch.Start();
                while (_ticks < ticktimeout)
                {
                    if (find_edge == _hat.ReadDigital(FEZHAT.DigitalPin.DIO16))
                    {
                        if (find_edge)
                        {
                            find_edge = Edge.trailing;
                        }
                        else
                        {
                            break;
                        }
                    }
                    await Task.Delay(TimeSpan.FromTicks(_tick_resolution));

                    _ticks = _stopwatch.ElapsedTicks;
                }
            }
            finally
            {
                _stopwatch.Stop();
                distance = _ticks * SPEED_OF_SOUND_cmps / _ticks_per_second / 2.0;
            }
            return(Math.Round(distance * 10.0) / 10.0); // round to 1 decimal place
        }