How can I define a button per IO number?

I have “FEZ Spider Kit”.

I kown, if I use normal method just like as follow:

In “Program.gadgeteer” put the “button” symbol to the socket “4”.

and in “Program.cs” write codes:



          void ProgramStarted()
        { 
             .....................
           .........................

        button.ButtonPressed += new GTM.GHIElectronics.Button.ButtonEventHandler(button_ButtonPressed);
          ...........................
          ...................
        }

        void button_ButtonPressed(GTM.GHIElectronics.Button sender, GTM.GHIElectronics.Button.ButtonState state)
        {

            if (j == 0)
            {
                j = 1;
                button.TurnLEDOn();


            }
            else
            {
                j = 0;
                button.TurnLEDOff();
            }
 
        }


my question is, how can I use the IO number to define the button?

I don’t want to put the “button” symbol to the socket to define the button.

I kown the button pin is “IO33” and LED is “IO37”. I want to controll the individual pin.

You can use the core Output port, in your example you could do the following.


// Get the output port for pin 37. Set the initial state to false.
Microsoft.SPOT.Hardware.OutputPort pin = new Microsoft.SPOT.Hardware.OutputPort((Microsoft.SPOT.Hardware.Cpu.Pin)37, false);

// Write the state to the pin
pin.Write(true);

Actually for a button you want to use an InputPort. If you use it on a port that supports interrupts you can use the .Interrupt event, otherwise you’ll need a thread constantly calling .Read to check the state.

1 Like

:slight_smile: InputPort, yes the other half of the question.

Use (Cpu.Pin) to convert either an int or the FEZ enumerated pin.

@ Skewworks -

thanks!!

If I define the “InutPort”, how can I define a Interrupt event for the IO37 pin?



void ProgramStarted()
        {
            ......
           ...............
         
           static InputPort  GPIO_Pin_4 = new InputPort((Cpu.Pin)GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.IO37, true, Port.ResistorMode.PullDown);

           // how can I define a interrupt event for the IO37 ???
         
}

InputPort is not an interrupt port. InterruptPort is how you define the “input” with an interrupt handler.

Yes as Brett said sorry I wasn’t more clear. InputPort if it does NOT support interrupt on the pin and constantly call .Read; InterruptPort if it DOES.

You can see what pins do and dont support interrupt on the product page for your device. Interruptible pins are marked with a *.