Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Input as event


Best Answer brianmakabro, 16 April 2015 - 07:25 PM

How about something like:

 

public class Program
{
    public enum Modes
    {
         ModeUnknown,
         Mode1,
         Mode2,
         Mode3
    }
 
 
       static OutputPort interuptled = new OutputPort(Pins.ONBOARD_LED, false);
 
       static Modes currentMode = Modes.ModeUnknown;
 
       public static void Main()
       {
 
            InterruptPort button = new InterruptPort(Pins.ONBOARD_SW1, false, 
                                     Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh);
 
            button.OnInterrupt += new NativeEventHandler(button_OnInterrupt);
 
            currentMode = Modes.Mode2;
 
            Thread.Sleep(Timeout.Infinite);  
        }
 
 
 
        static void button_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            switch (currentMode)
            {
                case Modes.Mode1:
                    interuptled.Write(!interuptled.Read());
                    break;
 
                case Modes.Mode2:
                    // DO MODE 2 WORK...
                    break;
                
                case Modes.Mode3:
                    // DO MODE 3 WORK...
                    break;
                
                default:
                    // UNKNOWN MODE?  RAISE ERROR?
                    break;
            }
        }
    }
Go to the full post


  • Please log in to reply
5 replies to this topic

#1 kalio20

kalio20

    Member

  • Members
  • PipPip
  • 22 posts

Posted 16 April 2015 - 11:23 AM

Hi,

 

is it possible to wait for a signal for example on Pin 9 for a high or low signal and start an interreupt or event on this signal?

 

Greets

 

Zoro



#2 KiwiDev

KiwiDev

    Advanced Member

  • Members
  • PipPipPip
  • 100 posts
  • LocationNew Zealand

Posted 16 April 2015 - 11:49 AM

Hi Kalio,

 

An InterruptPort can be configured to call an eventhandler in response to an EdgeLow, EdgeHigh, EdgeBoth, EdgeLevelHigh, or EdgeLevelLow transition(s) on a pin.

 

You may need to add "debounce" logic to your code.

 

@KiwiBryn

blog.devmobile.co.nz

public class Program
{
   static OutputPort interuptled = new OutputPort(Pins.ONBOARD_LED, false);

   public static void Main()
   {
      InterruptPort button = new InterruptPort(Pins.ONBOARD_SW1, false, 
                                 Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh);

         button.OnInterrupt += new NativeEventHandler(button_OnInterrupt);

         Thread.Sleep(Timeout.Infinite);  
      }



      static void button_OnInterrupt(uint data1, uint data2, DateTime time)
      {
         interuptled.Write(!interuptled.Read());
      }
   }
}



#3 kalio20

kalio20

    Member

  • Members
  • PipPip
  • 22 posts

Posted 16 April 2015 - 12:20 PM

Ok, thanks that helps me alot, but is there a way to activate the interrupt or similar just when i need it? For example if i got 3 Modes, and i am in Mode 2, and it start execute when i got my interrupt on Pin.Any..., but if i am in Mode 3, then the interrupt should be blocked and only available for Mode 2?

 

Thanks in advance

 

 

kalio!



#4 KiwiDev

KiwiDev

    Advanced Member

  • Members
  • PipPipPip
  • 100 posts
  • LocationNew Zealand

Posted 16 April 2015 - 07:16 PM

Hi Zoro,

 

If you just want to activate/deactivate the interrupt in specific state(s) then have a look at 

 

         button.DisableInterrupt();
 
 
         button.EnableInterrupt();
 
 
You could call these from your state machine logic
 

@KiwiBryn

blog.devmobile.co.nz

 


#5 brianmakabro

brianmakabro

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationLouisville, KY

Posted 16 April 2015 - 07:25 PM   Best Answer

How about something like:

 

public class Program
{
    public enum Modes
    {
         ModeUnknown,
         Mode1,
         Mode2,
         Mode3
    }
 
 
       static OutputPort interuptled = new OutputPort(Pins.ONBOARD_LED, false);
 
       static Modes currentMode = Modes.ModeUnknown;
 
       public static void Main()
       {
 
            InterruptPort button = new InterruptPort(Pins.ONBOARD_SW1, false, 
                                     Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh);
 
            button.OnInterrupt += new NativeEventHandler(button_OnInterrupt);
 
            currentMode = Modes.Mode2;
 
            Thread.Sleep(Timeout.Infinite);  
        }
 
 
 
        static void button_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            switch (currentMode)
            {
                case Modes.Mode1:
                    interuptled.Write(!interuptled.Read());
                    break;
 
                case Modes.Mode2:
                    // DO MODE 2 WORK...
                    break;
                
                case Modes.Mode3:
                    // DO MODE 3 WORK...
                    break;
                
                default:
                    // UNKNOWN MODE?  RAISE ERROR?
                    break;
            }
        }
    }


#6 kalio20

kalio20

    Member

  • Members
  • PipPip
  • 22 posts

Posted 17 April 2015 - 08:40 AM

Thank u very much for ur answers! 

They really help me alot, and is easy to understand!

Very thank you!

 

Greets 

 

kalio






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.