davebriggs
Posts: 3
Joined: Thu Mar 23, 2017 5:12 pm

Windows 10 IOT Multi Tasking

Thu Mar 23, 2017 5:52 pm

I am trying to get a simple multi tasking timer session going on which I can build additional timers (probably 3 or 4) for different I/O tasks. Can someone tell me what I have done wrong in the code below? The button works and the I/O works (reads input, drives an LED on the output) but the timer does not run.Thanks

Code: Select all

using System;
using Windows.Devices.Gpio;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Threading.Tasks;


namespace App1
{
  
    public sealed partial class MainPage : Page
    {
        private const int LED_PIN = 5;
        private const int INP_PIN = 6;
        private GpioPin pin;
        private GpioPin ipPin;
        private GpioPinValue pinValue;
        private GpioPinValue ipPinValue;
        private int ledToggle1;
        private int ledToggle2;
        private int ledToggle3;
        private int countMon;
        private DispatcherTimer timer1;
        private DispatcherTimer timer2;
        private SolidColorBrush redBrush = new SolidColorBrush(Windows.UI.Colors.Red);
        private SolidColorBrush greenBrush = new SolidColorBrush(Windows.UI.Colors.Green);
        private SolidColorBrush blueBrush = new SolidColorBrush(Windows.UI.Colors.Blue);
        private SolidColorBrush blackBrush = new SolidColorBrush(Windows.UI.Colors.Black);

        public MainPage()
        {
            this.InitializeComponent();
            this.InitGPIO();
        }

        private async Task setupTimer()
        {
            this.timer1 = new DispatcherTimer();
            this.timer1.Interval = TimeSpan.FromMilliseconds(500);
            this.timer1.Tick += this.Timer_Tick1;
            this.timer1.Start();
        }

        private void Timer_Tick1(object sender, object e)
        {
            if (ledToggle2 == 0)
            {
                LED2.Fill = greenBrush;
                LED3.Fill = blueBrush;
                ledToggle2 = 1;
            }
            else
            {
                LED2.Fill = redBrush;
                LED3.Fill = blackBrush;
                ledToggle2 = 0;
            }
        }

        private async void timer1_loaded(object sender, PointerRoutedEventArgs e)
        {
            await setupTimer();
        }

        private void InitGPIO()
        {
            var gpio = GpioController.GetDefault();

            countMon = 1;

            // Show an error if there is no GPIO controller
            if (gpio == null)
            {
                pin = null;
                //                GpioStatus.Text = "There is no GPIO controller on this device.";
                return;
            }

            pin = gpio.OpenPin(LED_PIN);
            pinValue = GpioPinValue.High;
            pin.Write(pinValue);
            pin.SetDriveMode(GpioPinDriveMode.Output);

            ipPin = gpio.OpenPin(INP_PIN);
            ipPin.SetDriveMode(GpioPinDriveMode.Input);
            ipPinValue = ipPin.Read();

            ledToggle1 = 0;
            ledToggle2 = 0;
            ledToggle3 = 0;
            GpioStatus.Text = "GPIO pin initialized correctly.";
           

        }

        private async void Button1_Click(object sender, RoutedEventArgs e)
        {
            if(ledToggle1 == 0)
            {
                LED1.Fill = redBrush;
                ledToggle1 = 1;
                pinValue = GpioPinValue.High;
                pin.Write(pinValue);
            }
            else
            {
                LED1.Fill = greenBrush;
                ledToggle1 = 0;
                pinValue = GpioPinValue.Low;
                pin.Write(pinValue);
            }
            
        }

        private async void Button2_Click(object sender, RoutedEventArgs e)
        {
            if (ledToggle1 == 0)
            {
                LED1.Fill = blueBrush;
                ledToggle1 = 1;
                pinValue = GpioPinValue.High;
                pin.Write(pinValue);
            }
            else
            {
                LED1.Fill = blackBrush;
                ledToggle1 = 0;
                pinValue = GpioPinValue.Low;
                pin.Write(pinValue);
            }
            inputMon();
        }

        //       private async void inputMon(object sender, RoutedEventArgs e)
        private void inputMon()
        {
            countMon = 1;
           
                if (ipPin.Read() == GpioPinValue.High)
                {
                    pinValue = GpioPinValue.High;
                    pin.Write(pinValue);
                }
                else
                {
                    pinValue = GpioPinValue.Low;
                    pin.Write(pinValue);
                }            
        }   
    }
}

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12021
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Windows 10 IOT Multi Tasking

Thu Mar 23, 2017 11:57 pm

added code tags + moved to WIN10IOT.

haroldpulcher
Posts: 58
Joined: Tue Jun 09, 2015 8:08 pm
Contact: Website

Re: Windows 10 IOT Multi Tasking

Fri Mar 24, 2017 2:53 pm

I don't see where you are calling setupTimer to start the timer going. the 'timer1_loaded' calls it, but I can't tell if that is ever being called.

beta-tester
Posts: 1236
Joined: Fri Jan 04, 2013 1:57 pm
Location: de_DE

Re: Windows 10 IOT Multi Tasking

Sun Mar 26, 2017 5:50 am

... and why you mark setupTimer() as async - there is nothing you are waiting for with await?
so timer1_loaded(..) does not need an async as well.
wondering, why you didn't get a compiler error/warning for that...
or you don't, because you never called that method - as "haroldpulcher" told.
{ I only give negative feedback }
RPi Model B (rev1, 256MB) & B (rev2, 512MB) & B+, RPi2B, RPi3B, RPi3B+, RPiZeroW, ...

Return to “Windows 10 for IoT”