C# GPIO programming - Sample code for GPIO InterruptPort programming


1. Download and open C# project file
  1. Download source code.

    InterruptPort.Sample1.zip



2. C# source code for InterruptPort.Sample1
  1. Program.cs

      
    
    using System;
    
    using Microsoft.SPOT;
    using Microsoft.SPOT.Input;
    using Microsoft.SPOT.Presentation;
    using Microsoft.SPOT.Presentation.Controls;
    using Microsoft.SPOT.Hardware;
    
    using SPL.Script.Engine;
    
    namespace SPLMFConsoleWindow1
    {
        public class Program : Microsoft.SPOT.Application
        {
            public static void Main()
            {
                Program myApplication = new Program();
    
                Window mainWindow = myApplication.CreateWindow();
    
                GPIOButtonInputProvider inputProvider = new GPIOButtonInputProvider(null);
    
                myApplication.Run(mainWindow);
            }
    
            private Window mainWindow;
    
            //##########################  Sample Code  #################################
            private ConsoleWindow _consoleWindow;
    
            private InterruptPort _port = null;
            //##########################################################################
    
            public Window CreateWindow()
            {
                mainWindow = new Window();
                mainWindow.Height = SystemMetrics.ScreenHeight;
                mainWindow.Width = SystemMetrics.ScreenWidth;
    
                //##########################  Sample Code  #################################
                Font font = Resources.GetFont(Resources.FontResources.small);
                _consoleWindow = new ConsoleWindow(font);
    
                mainWindow.Child = _consoleWindow;
                //##########################################################################
    
                mainWindow.AddHandler(Buttons.ButtonUpEvent, new ButtonEventHandler(OnButtonUp), false);
    
                mainWindow.Visibility = Visibility.Visible;
    
                Buttons.Focus(mainWindow);
    
                //##########################  Sample Code  #################################
                _consoleWindow.WriteLine("Please push any button!.");
                _consoleWindow.WriteLine("Select button will clear screen.");
                //##########################################################################
    
    
                //##########################  Sample Code  #################################
                _port = new InterruptPort(Cpu.Pin.GPIO_Pin6, false, Port.ResistorMode.PullDown, Port.InterruptMode.InterruptEdgeBoth);
                _port.OnInterrupt += new NativeEventHandler(_port_OnInterrupt);
                //##########################################################################
    
                return mainWindow;
            }
    
            //##########################  Sample Code  #################################
            void _port_OnInterrupt(uint data1, uint data2, DateTime time)
            {
                bool state = _port.Read();
    
                this.Dispatcher.BeginInvoke(
                    new DispatcherOperationCallback(
                        delegate(object report)
                        {
                            _consoleWindow.WriteLine("[Interrupt] Curruent state is " + state.ToString());
                            return null;
                        }
                        ), null
                    );
            }
    
            //##########################################################################
    
            private void OnButtonUp(object sender, ButtonEventArgs e)
            {
                Debug.Print(e.Button.ToString());
    
                //##########################  Sample Code  #################################
                if (e.Button == Microsoft.SPOT.Hardware.Button.VK_SELECT)
                {
                    _consoleWindow.ClearConsole();
                }
                else if (e.Button == Microsoft.SPOT.Hardware.Button.VK_UP)
                {
                    bool state = _port.Read();
                    _consoleWindow.WriteLine("Curruent state is " + state.ToString());
                }
                //##########################################################################
            }
        }
    }
    
    
      



3. Execute sample code
  1. Open "Properties" page of C# project.

  2. Choose ".NET Micro Framework" tab.

  3. Select "SPLMFConsoleEmulator" from the emulator list.



  4. Press "F5" key to run sample code.

  5. Click Up-Button, then you can see the current state of port is printed.




4. Change the state of port through emulator
  1. Choose "Emulator" tab.

  2. Add below script on the editor.

      
    port6.Write(true)
    
      




  3. Press "F5" key or click "Execute Script on the Emulator".

  4. When the state of port is changed, "_port_OnInterrupt" procedure is called automatically.