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

Delay interruption handling


Best Answer JerseyTechGuy, 05 April 2013 - 07:32 PM

I've always felt it was a good practice to remove event handlers when not needed or simply just to flush out long lived event handlers.  The nice thing about CW2s approach is these are GPIO port specific.  If your port is called myPort then you are calling myPort.EnableInterrupt() or myPort.DisableInterrupt().  I am using this for the menu system on Pandora's Box controlled by a 5 way Tactile switch.  I have interrupts for any button press (Up/down/left/right/enter) and during certain menu operations they may need to be enabled/disabled however when the box goes into scan mode (logging mode) I remove the handler all together instead of just disabling it as it could be running several hours or days in logging mode.

 

I believe when you explicitly remove the handler (-=) GC automatically happens so there are no remnants left behind.

Go to the full post


  • Please log in to reply
8 replies to this topic

#1 PShchegolevatykh

PShchegolevatykh

    Member

  • Members
  • PipPip
  • 12 posts

Posted 05 April 2013 - 02:37 PM

Hi everyone. I have Netudino Plus with PIR motion sensor. Sensor sends interruptions to micro-controller so I have sensor.OnInterrupt += MyInterruptionHandler. The problem is that PIR sends them too fast and too many. I need to delay this somehow. For example I want to receive them  every five or ten minutes. How can I do that in .NET MF?



#2 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 05 April 2013 - 06:10 PM

If you are using a NativeEventHandler for the interrupt handler one of the parameters should be the time the event was triggered.  Since you have the time you should easily be able to store it in a variable and check how much time has elapsed since the last trigger.  So in essence you cannot tell the event handler to only fire every 5 or 10 minutes but you can tell the handler to run / not run code unless it matches your time criteria.



#3 Paul Newton

Paul Newton

    Advanced Member

  • Members
  • PipPipPip
  • 724 posts
  • LocationBerkshire, UK

Posted 05 April 2013 - 06:17 PM

Hi,

 

(I like the way the new forum tells you when someone else posts a reply while you are typing - Hi Dave!)

 

I don't know if this is suitable for your project:

 

How about having a five or ten minute periodic timer whose event handler enables the GPIO interrupt.

When the GPIO interrupt event handler goes off, it immediately disables further interrupts - this way .Net is not bombarded by lots of interrupts. Having done that, it can signal the rest of your code that a pulse has been seen.

 

This scheme would only respond to the first interrupt in every five or ten minute window of time.

 

Paul



#4 PShchegolevatykh

PShchegolevatykh

    Member

  • Members
  • PipPip
  • 12 posts

Posted 05 April 2013 - 06:24 PM

Thanks Dave. That should work.

Paul, I think your solution is better but could you please show me some code that disables GPIO interrupt? I don't know how to do that. :(



#5 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 05 April 2013 - 06:37 PM

Thanks Dave. That should work.

Paul, I think your solution is better but could you please show me some code that disables GPIO interrupt? I don't know how to do that. :(

 

The disable is much like the enable whereas you use sensor.OnInterrupt += MyInterruptionHandler to enable it...

 

You would use sensor.OnInterrupt -= MyInterruptionHandler to disable it. Notice the (-=) instead of (+=).  This removes the handler.

 

If this is not what Paul was thinking, please correct me.



#6 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 05 April 2013 - 06:45 PM

In addition to what Dave suggested, there are also EnableInterrupt() and DisableInterrupt() methods...

#7 Paul Newton

Paul Newton

    Advanced Member

  • Members
  • PipPipPip
  • 724 posts
  • LocationBerkshire, UK

Posted 05 April 2013 - 07:11 PM

Thanks Dave & CW2 - I can't pull c# out of the air like that! Both suggestions will cut the processor load. My knowledge of the framework is not good enough here, can the events be set up independantly of the interrupt being enabled? If they can, then I think leaving the event connected and turning the interrupt on/off is best. Best because it will stop the activity at the earliest point. One thing I would be cautious of is making calls that may use up memory. I think the event += and -= may create lots of objects that then need to be garbage collected. Not sure about that.... Paul

#8 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 05 April 2013 - 07:32 PM   Best Answer

I've always felt it was a good practice to remove event handlers when not needed or simply just to flush out long lived event handlers.  The nice thing about CW2s approach is these are GPIO port specific.  If your port is called myPort then you are calling myPort.EnableInterrupt() or myPort.DisableInterrupt().  I am using this for the menu system on Pandora's Box controlled by a 5 way Tactile switch.  I have interrupts for any button press (Up/down/left/right/enter) and during certain menu operations they may need to be enabled/disabled however when the box goes into scan mode (logging mode) I remove the handler all together instead of just disabling it as it could be running several hours or days in logging mode.

 

I believe when you explicitly remove the handler (-=) GC automatically happens so there are no remnants left behind.



#9 zee

zee

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts

Posted 30 April 2013 - 05:21 AM

Hi,

 

I have 2 questions regarding about PIR Sensor. I am using Netduino Plus 2.

 

1) Is it a need to have a resistor on it? Because, if i set it to Disabled, I need to wave a lot of times over the sensor for it to detect and after detecting it, it will keep on going print out the output even when i stop waving. If i set it to PullUp with 1K resistor without wave over it, it will automatically print the output.

 

2) I have insert the interrupt handling together with it, and the outcome seems to be ongoing even if I have stop waving over it. I want it to print out the output once upon detecting the motion, and stop, and print once again upon the next waving.

 

 

Here are my codes. I got it from this forum.

using System;using System.Net;using System.Net.Sockets;using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.Netduino;using System.Collections;namespace Motion_Sensor{    public class Program    {        static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);        static InterruptPort PIR =             new InterruptPort(Pins.GPIO_PIN_A0, false, ResistorModes.PullUp, InterruptModes.InterruptEdgeLow);public static void Main()        {            // write your code here            Debug.Print("Wave!");            Thread.Sleep(1000);            PIR.OnInterrupt                 += new NativeEventHandler(motion_OnInterrupt);              Thread.Sleep(-1);        }        static void motion_OnInterrupt(uint data1, uint data2, DateTime time)        {            PIR.DisableInterrupt();            Debug.Print("Found movement ");            led.Write(true);                        Thread.Sleep(3000);            led.Write(false);            Thread.Sleep(3000);            PIR.EnableInterrupt();        }}}





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.