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 handlers sample


  • Please log in to reply
5 replies to this topic

#1 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 09 June 2011 - 04:49 AM

Second sample, a bit more complex, using events:

Imports Microsoft.SPOT
Imports Microsoft.SPOT.Hardware
Imports SecretLabs.NETMF.Hardware
Imports SecretLabs.NETMF.Hardware.Netduino

Module Module1

    Public led As OutputPort = New OutputPort(Pins.ONBOARD_LED, False)

    Sub Main()
        Dim button As InterruptPort = New InterruptPort(Pins.ONBOARD_SW1, False, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth)
        AddHandler button.OnInterrupt, AddressOf ButtonChange
        button.EnableInterrupt()

        Thread.Sleep(Timeout.Infinite)

    End Sub

    Sub ButtonChange(ByVal data1 As UInteger, ByVal data2 As UInteger, ByVal time As Date)
        If data2 = 0 Then led.Write(False) Else led.Write(True)
    End Sub

End Module

When pushing the onboard button, the onboard led is emitting.

Attached Files


"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#2 Joe Griffith

Joe Griffith

    Advanced Member

  • Members
  • PipPipPip
  • 32 posts

Posted 27 June 2011 - 03:54 PM

For what it is worth I tried declaring the button WithEvents and putting a Handles clause at the end of the ButtonChange method. This would be my prefered syntax and it is accepted by the IDE but, alas, it doesn't work.

#3 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 27 June 2011 - 04:25 PM

True, as far as I know that's still in progress. I've reported it as a bug which was confirmed.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#4 rhamer

rhamer

    Member

  • Members
  • PipPip
  • 29 posts
  • LocationMelbourne Australia

Posted 08 June 2012 - 06:30 AM

Has anybody come accross a problem where the events stop firing? I have an application that has an event tied to an input and after a while it stops firing. The way the event handler is written is exactly as shown in the example above, and it works properly for the first 10 or so pin level changes, then suddenly just stops firing. I have tried the sample program above, and the problem does not happen, so it is difficult for me to identify exactly what is the problem. Stripping down the application is not an easy task as it is quite a big complex application, and the latest thing added was the use of the events to monitor one of the digital inputs, which is actually driven by the interrupt pin of a MCP23017 port expander. When this problem occurs, the rest of the application keeps running fine, the event handler just never gets called. If I run a poll timer in the background that manually calles the same event handler after a few minutes, then all the code inside it runs correctly. I have also used a logic analyser to monitor the state of the pin, and it is actually being pulled low by the MCP23017 as it should. Regards Rohan

#5 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 08 June 2012 - 07:31 AM

Hi Rohan, Is it possible that something is going out of scope and getting garbage collected? Also, are you calling the event from a background thread by any chance (which could itself have crashed)? I haven't seen event handlers just stop working in the background, so I'm really interested in finding out what's going on. Chris

#6 rhamer

rhamer

    Member

  • Members
  • PipPip
  • 29 posts
  • LocationMelbourne Australia

Posted 12 June 2012 - 12:17 AM

Yes you were spot on, the event handler definition was in a thread that was going out of scope. It seems to be working ok now. Thanks for your help. Regards Rohan




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.