Toolbox.NETMF.Hardware.MatrixKeyPad


Toolbox.NETMF.Hardware.MatrixKeyPad(RowPins, ColPins)
  • RowPins The pins referring to the keypad's rows
  • ColPins The pins referring to the keypad's columns

Inner working of a matrix keypad

For some applications a simple keypad can be very useful. There are a lot of them on the market, most work a bit the same. I wrote a small driver for keypads that work as a matrix keypad.
The working of the matrix keypad is explained in this image:
288612

Every row and column has a pin. They could be numbered like this:
Pin 1: Column 1
Pin 2: Column 2
Pin 3: Column 3
Pin 4: Row 1
Pin 5: Row 2
Pin 6: Row 3
Pin 7: Row 4
But keep in mind, this can be different on your keypad. Always check the datasheet or when no datasheet is available open it and follow the lines.

If I press button 8, it’s row 3 and column 2. At that moment, pin 6 and 2 are connected to each other. So I can scan if key 8 is pressed by putting a signal on pin 6 and read it on pin 2. If the signal exists, button 8 is pressed.

Methods

  • Read() Returns the pressed button or -1 when no button is pressed

Events

  • OnKeyDown(KeyCode, Unused, Time) When a key is pressed, this event is triggered. See KeyCode for the pressed key.
  • OnKeyUp(KeyCode, Unused, Time) When a key is released, this event is triggered. See KeyCode for the pressed key.

Sample code in C#

public class Program
{
    public static void Main()
    {
        // Row pins. The keypad exists out of 4 rows.
        Cpu.Pin[] RowPins = { Pins.GPIO_PIN_D4, Pins.GPIO_PIN_D5, Pins.GPIO_PIN_D6, Pins.GPIO_PIN_D7 };
        // Col pins. The keypad exists out of 3 columns.
        Cpu.Pin[] ColPins = { Pins.GPIO_PIN_D1, Pins.GPIO_PIN_D2, Pins.GPIO_PIN_D3 };
        // Initializes the new keypad
        MatrixKeyPad kb = new MatrixKeyPad(RowPins, ColPins);

        // Bind both events
        kb.OnKeyDown += new NativeEventHandler(kb_OnKeyDown);
        kb.OnKeyUp += new NativeEventHandler(kb_OnKeyUp);

        // Lets wait forever for events to occure
        Thread.Sleep(Timeout.Infinite);
    }

    /// <summary>
    /// Triggered when a key is released
    /// </summary>
    /// <param name="KeyCode">The key code</param>
    /// <param name="Unused">Not used</param>
    /// <param name="time">Date and time of the event</param>
    static void kb_OnKeyUp(uint KeyCode, uint Unused, DateTime time)
    {
        Debug.Print("Key released: " + KeyCode.ToString());
    }

    /// <summary>
    /// Triggered when a key is pressed
    /// </summary>
    /// <param name="KeyCode">The key code</param>
    /// <param name="Unused">Not used</param>
    /// <param name="time">Date and time of the event</param>
    static void kb_OnKeyDown(uint KeyCode, uint Unused, DateTime time)
    {
        Debug.Print("Key pressed: " + KeyCode.ToString());
    }
}

Sample code in VB

Module MatrixKeypad_Sample

    ' Defines the keypad with events
    Dim WithEvents kb As MatrixKeyPad

    Sub Main()
        ' Row pins. The keypad exists out of 4 rows.
        Dim RowPins(0 To 3) As Cpu.Pin
        RowPins(0) = Pins.GPIO_PIN_D4
        RowPins(1) = Pins.GPIO_PIN_D5
        RowPins(2) = Pins.GPIO_PIN_D6
        RowPins(3) = Pins.GPIO_PIN_D7
        ' Col pins. The keypad exists out of 3 columns.
        Dim ColPins(0 To 2) As Cpu.Pin
        ColPins(0) = Pins.GPIO_PIN_D1
        ColPins(1) = Pins.GPIO_PIN_D2
        ColPins(2) = Pins.GPIO_PIN_D3
        ' Initializes the new keypad
        kb = New MatrixKeyPad(RowPins, ColPins)

        ' Lets wait forever for events to occure
        Thread.Sleep(Timeout.Infinite)
    End Sub

    ''' <summary>
    ''' Triggered when a key is pressed
    ''' </summary>
    ''' <param name="KeyCode">The key code</param>
    ''' <param name="Unused">Not used</param>
    ''' <param name="time">Date and time of the event</param>
    Private Sub kb_OnKeyDown(ByVal KeyCode As UInteger, ByVal Unused As UInteger, ByVal time As Date) Handles kb.OnKeyDown
        Debug.Print("Key pressed: " + KeyCode.ToString())
    End Sub

    ''' <summary>
    ''' Triggered when a key is released
    ''' </summary>
    ''' <param name="KeyCode">The key code</param>
    ''' <param name="Unused">Not used</param>
    ''' <param name="time">Date and time of the event</param>
    Private Sub kb_OnKeyUp(ByVal KeyCode As UInteger, ByVal Unused As UInteger, ByVal time As Date) Handles kb.OnKeyUp
        Debug.Print("Key released: " + KeyCode.ToString())
    End Sub
End Module

Related documents

Last edited Jan 1, 2013 at 9:13 PM by Garrcomm, version 4