Skip to content

FEZ Panda II with Matrix Keypad

After a few days of attempting to get this to work I thought I would save some time for anyone else attempting this particular activity.

I am using the JM09K keypad that I bought quite a while ago.

I am also using the FEZ Panda II

Where to start?

I was looking at a lot of different projects and it seemed that some people were using external pull-up resistors. But I was sure I wouldn’t need any of those! I saw that Arduino and Raspberry Pi projects had just stuck a header on the keypad and stuck it right in to the dev board. Turns out that it works a treat!

So I looked at the Arduino Keypad library and found that you needed to change the column pins from output to input. At first I thought that I would not be able to do that, but it turns out that the Port class implements IDisposable! So we can just wrap the Port declarations inside a using statement which will dispose of the pin reservation after we have finished using it.

The Keypad class

Influenced by the Arduino Keypad libraries use of a Keymap I decided that I would try and mimic some of its behavior.

using Microsoft.SPOT.Hardware;

namespace Keypad_Test
{
    class Keypad
    {
        public char[][] Keymap
        {
            get;
            private set;
        }

        public Cpu.Pin[] RowPins
        {
            get;
            private set;
        }

        public Cpu.Pin[] ColumnPins
        {
            get;
            private set;
        }

        public int HoldTime
        {
            get;
            set;
        }

        public Keypad(char[][] userKeymap, Cpu.Pin[] rows, Cpu.Pin[] cols)
        {
            Keymap = userKeymap;
            RowPins = rows;
            ColumnPins = cols;

            HoldTime = 500;
        }

        // Look below for the code that goes here!
        //public char[] GetKeys() { }
    }
}


We use the HoldTime to allow for a delay between key scans.

Now lets have a look at the method we use for getting the keys (I am sure I could refactor it to look a bit nicer though!).

public char[] GetKeys()
{
    var s = "";

    for (var i = 0; i < ColumnPins.Length; i++)
    {
        using (var output = new OutputPort(ColumnPins[i], false))
        {
            for (var j = 0; j < RowPins.Length; j++)
            {
                using (var row = new InputPort(RowPins[j], false, Port.ResistorMode.PullUp))
                {
                    if (!row.Read())
                        s += Keymap[j][i];
                }
            }
            output.Write(true);
        }
        using (var input = new InputPort(ColumnPins[i], false, Port.ResistorMode.Disabled))
        {
        }
    }

    char[] result = new char[s.Length];
    for (var i = 0; i < s.Length; i++)
        result[i] = s[i];

    if (result.Length > 0)
        System.Threading.Thread.Sleep(HoldTime);
    return result;
}


Simple enough, right!

Now lets use it in our main program!

using GHIElectronics.NETMF.FEZ;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System;

namespace Keypad_Test
{
    public class Program
    {
        static Cpu.Pin[] ColumnPins = new[]
            {
                (Cpu.Pin)FEZ_Pin.Digital.Di22,
                (Cpu.Pin)FEZ_Pin.Digital.Di20,
                (Cpu.Pin)FEZ_Pin.Digital.Di24
            };

        static Cpu.Pin[] RowPins = new[]
            {
                (Cpu.Pin)FEZ_Pin.Digital.Di21,
                (Cpu.Pin)FEZ_Pin.Digital.Di26,
                (Cpu.Pin)FEZ_Pin.Digital.Di25,
                (Cpu.Pin)FEZ_Pin.Digital.Di23
            };

        static char[][] Keypad = new char[][]
            {
                new char[] { '1', '2', '3' },
                new char[] { '4', '5', '6' },
                new char[] { '7', '8', '9' },
                new char[] { '*', '0', '#' }
            };
        
        public static void Main()
        {
            try
            {
                var keypad = new Keypad(Keypad, RowPins, ColumnPins);
                keypad.HoldTime = 200;

                while (true)
                {
                    var keys = keypad.GetKeys();
                    if(keys.Length > 0)
                        Debug.Print(new string(keys));
                }
            }
            catch (Exception ex)
            {
                Debug.Print("Error: " + ex.Message);
            }
        }
    }
}

Published inC#NET Micro Framework

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *