Main Site Documentation

Button event not firing!


#1

Hi Guys!

Can some one help me with the following test application. The LED are flashing on the button, the Debug button is ON, I just do not get the event. I have tried Port 4 & 8 along with both buttons that came with my Spider.

Any help, much appreciated!!!

Regards

MarkB

Version
VS2012 Enterprise
Spider Firmware 4.2.10.1
Assembly: mscorlib (4.2.0.0)
Assembly: Microsoft.SPOT.Native (4.2.0.0)
Assembly: Microsoft.SPOT.Security.PKCS11 (4.2.0.0)
Assembly: System.Security (4.2.0.0) Loading Deployment Assemblies.
Attaching deployed file.
Assembly: Microsoft.SPOT.Hardware.PWM (4.2.0.1) Attaching deployed file.
Assembly: Gadgeteer (2.42.0.0) Attaching deployed file.
Assembly: Microsoft.SPOT.Graphics (4.2.0.0) Attaching deployed file.
Assembly: Microsoft.SPOT.Hardware (4.2.0.0) Attaching deployed file.
Assembly: Microsoft.SPOT.IO (4.2.0.0) Attaching deployed file.
Assembly: GTM.GHIElectronics.Button (1.1.2.0) Attaching deployed file.
Assembly: Microsoft.SPOT.TinyCore (4.2.0.0) Attaching deployed file.
Assembly: System.IO (4.2.0.0) Attaching deployed file.
Assembly: GHI.Premium.System (4.2.10.0) Attaching deployed file.
Assembly: Test (1.0.0.0) Attaching deployed file.
Assembly: GHI.Premium.IO (4.2.10.0) Attaching deployed file.
Assembly: Microsoft.SPOT.Net (4.2.0.0) Attaching deployed file.
Assembly: GHI.Premium.Hardware (4.2.10.0) Attaching deployed file.
Assembly: GHIElectronics.Gadgeteer.FEZSpider (1.1.2.0) Resolving.

namespace Test {
    using Gadgeteer;
    using GTM = Gadgeteer.Modules;
    
    
    public partial class Program : Gadgeteer.Program {
        
        private Gadgeteer.Modules.GHIElectronics.Button button;
        
        public static void Main() {
            // Important to initialize the Mainboard first
            Program.Mainboard = new GHIElectronics.Gadgeteer.FEZSpider();
            Program p = new Program();
            p.InitializeModules();
            p.ProgramStarted();
            // Starts Dispatcher
            p.Run();
        }
        
        private void InitializeModules() {
            this.button = new GTM.GHIElectronics.Button(4);
        }
    }
}
using System.Threading;
using Microsoft.SPOT;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;

namespace Test
{
    public partial class Program
    {
        void ProgramStarted()
        {
            button.ButtonPressed += button_ButtonPressed;

            var startUpTimer = new GT.Timer(200, GT.Timer.BehaviorType.RunOnce);
            startUpTimer.Tick += OnStartUpTimer;
            startUpTimer.Start();
        }

        private void OnStartUpTimer(GT.Timer timer)
        {
            Mainboard.SetDebugLED(true);
            while (true)
            {
                button.LEDMode = button.LEDMode == Gadgeteer.Modules.GHIElectronics.Button.LEDModes.On
                                     ? Gadgeteer.Modules.GHIElectronics.Button.LEDModes.Off
                                     : Gadgeteer.Modules.GHIElectronics.Button.LEDModes.On;
                Thread.Sleep(1000);
            }
        }

        private static void button_ButtonPressed(GTM.GHIElectronics.Button sender, GTM.GHIElectronics.Button.ButtonState state)
        {
            Debug.Print("ButtonPressed  State: " + state);

            Mainboard.SetDebugLED(false);
            Thread.Sleep(500);
            Mainboard.SetDebugLED(true);
        }
    }
}

#2

Using code tags will make your post more readable. This can be done in two ways:[ol]
Click the “101010” icon and paste your code between the

 tags or...
Select the code within your post and click the "101010" icon.[/ol]
(Generated by QuickReply)

#3

@ Architect - Thanks!! Did not know that - sorry


#4

Interesting I see nothing wrong with the code. Try different Gadgeteer cable.


#5

@ MarkB - There is only one system thread for handling event in Gadgeteer. When your timer fires, you are going into a loop in the OnStartUpTimer() event handler. You must exit the event handler for other events to fire.


#6

Thanks Mike, I’m sure my main application is releasing the main thread. I’ll check that first thing in the morning (it’s early morning here in Oz).

Thanks


#7

@ Mike - Good catch. Missed that while(true)


#8

That’s right. I have tried the following code which works:


void ProgramStarted()
{     
    Mainboard.SetDebugLED(true);     
   
    button.ButtonPressed += button_ButtonPressed;
           
    var blinkTimer = new GT.Timer(1000);
    blinkTimer.Tick += buttonLEDblink;
    blinkTimer.Start();      
 }

 private void buttonLEDblink(GT.Timer timer)
 {
     button.LEDMode = button.LEDMode ==   
     Gadgeteer.Modules.GHIElectronics.Button.LEDModes.On
               ? Gadgeteer.Modules.GHIElectronics.Button.LEDModes.Off
              : Gadgeteer.Modules.GHIElectronics.Button.LEDModes.On;
 }
                   
private static void button_ButtonPressed(GTM.GHIElectronics.Button sender,  GTM.GHIElectronics.Button.ButtonState state)
{
    Debug.Print("ButtonPressed  State: " + state);

    Mainboard.SetDebugLED(false);
    Thread.Sleep(500);
    Mainboard.SetDebugLED(true);
}