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

Event Handler limited to 45 times


  • Please log in to reply
7 replies to this topic

#1 ptownsin

ptownsin

    New Member

  • Members
  • Pip
  • 4 posts

Posted 16 September 2010 - 07:51 AM

Hello all, This is my first post here, but can I say what a great resource this site is, and how impressed I am with the ease of using Netduino. In no time at all I've prototyped an LCD display, analog in, RTC and an event handler for a 2 bit gray rotary encoder. What I observe consistently is that after approx 45 events from the rotary encoder, they no longer get triggered. I have a clock on the LCD so I can see that the processor isn't locked up, that keeps ticking, but the digital encoder events stop. It's always after the same number of events too. Is there some kind of tidy-up I've missed out? Thanks Paul.

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 16 September 2010 - 07:54 AM

Hi Paul, Welcome to the community! Hmm, really odd... We use event handlers thousands of times in code and haven't had any issues...but let's see if we can help track down the issue for you here. Can you make a small sample app (20 lines or less) that demonstrates the issue? If not, anything that you can share here will help... Thanks for the post, Paul. Chris

#3 ptownsin

ptownsin

    New Member

  • Members
  • Pip
  • 4 posts

Posted 16 September 2010 - 08:47 AM

Chris,
These are the snippets for the encoder driver and how I've used it.

      public class RotaryEncoder
    {

        public delegate void RotaryEncoderEventHandler();

        public event NativeEventHandler RotaryEncoderInterrupt;

        private readonly Cpu.Pin _port1;
        private readonly Cpu.Pin _port2;
        private readonly uint _encoderid;
        InputPort _ipPort2;

        public RotaryEncoder(Cpu.Pin port1, Cpu.Pin port2)
            : this(0, port1, port2)
        { }

        public RotaryEncoder(uint encoderId, Cpu.Pin port1, Cpu.Pin port2)
        {
            _encoderid = encoderId;
            _port1 = port1;
            _port2 = port2;

            InterruptPort ipPort1 = new InterruptPort(
                            _port1, 
                            false, 
                            Port.ResistorMode.PullUp, 
                            Port.InterruptMode.InterruptEdgeHigh);
            _ipPort2 = new InputPort(_port2, false, Port.ResistorMode.PullUp);
            ipPort1.OnInterrupt += new NativeEventHandler(ipPort1_OnInterrupt);

        }

        void ipPort1_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            if (_ipPort2.Read())
                RotaryEncoderInterrupt(_encoderid, 0, time);
            else
                RotaryEncoderInterrupt(_encoderid, 1, time);  
        }

    }


        public static void Main()
        {
            RotaryEncoder rEnc = new RotaryEncoder(Pins.GPIO_PIN_D13, Pins.GPIO_PIN_D12);
            rEnc.RotaryEncoderInterrupt += new NativeEventHandler(rEnc_RotaryEncoderInterrupt);
            Thread.Sleep(Timeout.Infinite);
        }

        static void rEnc_RotaryEncoderInterrupt(uint data1, uint data2, DateTime time)
        {
            if (data2==0)
            {
                rotateCW();
            }
            else
            {
                rotateCCW();
            }  
        }

The rotateCW and rotateCCW functions then go off and write to the LCD

Thanks for your prompt reply.

Paul.

#4 Flores

Flores

    Advanced Member

  • Members
  • PipPipPip
  • 64 posts

Posted 16 September 2010 - 08:52 AM

I guess you are re-attaching the eventhandler on each event.. this will cause stackoverflows

Edit: judging the code this is not true

#5 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 16 September 2010 - 09:18 AM

Please try storing ipPort1 in a class member variable (like _ipPort2) - in all likelihood, it gets garbage collected.

#6 ptownsin

ptownsin

    New Member

  • Members
  • Pip
  • 4 posts

Posted 16 September 2010 - 09:33 AM

Please try storing ipPort1 in a class member variable (like _ipPort2) - in all likelihood, it gets garbage collected.


Ah yes, that would make sense. I'll give it a try this evening and give feedback...
Cheers

#7 ptownsin

ptownsin

    New Member

  • Members
  • Pip
  • 4 posts

Posted 16 September 2010 - 05:45 PM

Yup that fixed it. Thanks for the tips guys, onward and upwards now :-) Cheers Paul.

#8 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 16 September 2010 - 05:46 PM

Thanks everyone for pitching in and helping figure this out.




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.