Пример #1
0
        private async void InitGPIO()
        {
            if (LightningProvider.IsLightningEnabled)
            {
                LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();

                /*
                 * var pwmControllers = await PwmController.GetControllersAsync(LightningPwmProvider.GetPwmProvider());
                 * var pwmController = pwmControllers[1]; // the on-device controller
                 * pwmController.SetDesiredFrequency(40); // try to match 50Hz
                 *
                 * pwmLED = pwmController.OpenPin(IR_LED_PIN);
                 * pwmLED.SetActiveDutyCyclePercentage(0);
                 * pwmLED.Start();*/
            }

            var gpio = GpioController.GetDefault();

            if (gpio == null)
            {
                irLED           = null;
                debugString     = "There is no GPIO controller on this device.\n";
                GpioStatus.Text = debugString;
                return;
            }

            irReceiver = gpio.OpenPin(RCV_PIN, GpioSharingMode.Exclusive);
            irReceiver.SetDriveMode(GpioPinDriveMode.InputPullUp);
            irReceiverReader = new GpioChangeReader(irReceiver);
            irReceiverReader.Start();
            irLED = gpio.OpenPin(IR_LED_PIN);

            irLED.Write(GpioPinValue.Low);
            irLED.SetDriveMode(GpioPinDriveMode.Output);

            //irReceiver.ValueChanged += OnChange;

            debugString     = "GPIO pin initialized correctly.\n";
            GpioStatus.Text = debugString;

            flipsAmount = 0;
        }
Пример #2
0
    private void InitGpio()
    {
        gpioController = GpioController.GetDefault();
        if (gpioController == null)
        {
            return;
        }
        driverPin = gpioController.OpenPin(PIN_DRIVER);
        echoPin   = gpioController.OpenPin(PIN_ECHO);
        driverPin.Write(GpioPinValue.Low);
        driverPin.SetDriveMode(GpioPinDriveMode.Output);
        echoPin.SetDriveMode(GpioPinDriveMode.InputPullDown);

        changeReader          = new GpioChangeReader(echoPin);
        changeReader.Polarity = GpioChangePolarity.Both; // one measurement results in one rising and one falling edge
        changeReader.Start();

        // we use the stopwatch to time the trigger pulse
        stopwatch = Stopwatch.StartNew();
        stopwatch.Start();
    }
        public async void getGPIODistance1()
        {
            if (_echoBackReader != null)
            {
                ManualResetEvent mre = new ManualResetEvent(false);

                CancellationTokenSource source = new CancellationTokenSource();
                source.CancelAfter(TimeSpan.FromMilliseconds(250));

                _echoBackReader.Clear();
                _echoBackReader.Start();

                //Send pulse
                _triggerPin.Write(GpioPinValue.High);
                mre.WaitOne(TimeSpan.FromMilliseconds(0.01));
                _triggerPin.Write(GpioPinValue.Low);

                try
                {
                    await _echoBackReader.WaitForItemsAsync(2).AsTask(source.Token);

                    IList <GpioChangeRecord> changeRecords = this._echoBackReader.GetAllItems();

                    //foreach (var i in changeRecords)
                    //{
                    //    Debug.WriteLine(i.RelativeTime.ToString() + "  " +  (i.Edge == GpioPinEdge.FallingEdge ? "Falling" : "Rising"));
                    //}

                    TimeSpan d = changeRecords[1].RelativeTime.Subtract(changeRecords[0].RelativeTime);
                    GPIOTime     = d.TotalSeconds;
                    GPIODistance = ROUNDTRIPSPEEDOFSOUND * GPIOTime;
                }
                catch (OperationCanceledException)
                {
                    GPIOTime     = -1;
                    GPIODistance = -1;  //no measurement
                }
                _echoBackReader.Stop();
            }
        }
Пример #4
0
 public static void StartRecording()
 {
     statusLED.Write(GpioPinValue.High);
     irReceiverReader.Start();
 }
Пример #5
0
        private async Task ReadValuesFromDhtAsync()
        {
            try
            {
                var reading = new DhtSensorReading();

                CancellationTokenSource cancellationSource = new CancellationTokenSource();
                cancellationSource.CancelAfter(TIMEOUT_DATA_READING);

                _changeReader.Clear();
                _changeReader.Start();

                await SendInitialPulseAsync();

                // Wait for 43 falling edges to show up
                await _changeReader.WaitForItemsAsync(43).AsTask(cancellationSource.Token);

                // discard first two falling edges
                _changeReader.GetNextItem();
                _changeReader.GetNextItem();

                // pulse widths greater than 110us are considered 1's
                TimeSpan oneThreshold = new TimeSpan(110 * 10000000 / 1000000);

                TimeSpan current = _changeReader.GetNextItem().RelativeTime;

                for (int i = 0; i < 40; i++)
                {
                    TimeSpan next       = _changeReader.GetNextItem().RelativeTime;
                    TimeSpan pulseWidth = next.Duration() - current.Duration();

                    if (pulseWidth.Duration() > oneThreshold.Duration())
                    {
                        reading.Bits[40 - i - 1] = true;
                    }

                    current = next;
                }

                if (reading.IsValid())
                {
                    UpdateValuesOnUI(reading.Temperature(), reading.Humidity());
                    UpdateStatus("OK");
                }
                else
                {
                    throw new Exception("Invalid sensor data received!");
                }
            }
            catch (Exception ex)
            {
                UpdateValuesOnUI(double.NaN, double.NaN);

                if (ex is TaskCanceledException)
                {
                    UpdateStatus("Timeout while reading sensor data!");
                }
                else
                {
                    UpdateStatus(ex.Message);
                }
            }
            finally
            {
                _changeReader.Stop();
            }
        }