Main Site Documentation

KP16 keypad driver


#1

I picked up one of these keypads for fun:
https://www.ghielectronics.com/catalog/product/472

I noticed it has docs/sample to explain usage here:
https://www.ghielectronics.com/docs/203/keypad-kp16

That’s all well and good, but um, where is the module driver itself?


#2

in next week’s SDK, I suspect…


#3

You got it


#4

Until the SDK comes out, you can use the following.

using System;
using Microsoft.SPOT;

using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using GTI = Gadgeteer.Interfaces;

namespace Gadgeteer.Modules.GHIElectronics
{
	/// <summary>
	/// A Keypad KP16 module for Microsoft .NET Gadgeteer
	/// </summary>
	public class Keypad_KP16 : GTM.Module
	{
		private GTI.DigitalOutput out1;
		private GTI.DigitalOutput out2;
		private GTI.DigitalInput in1;
		private GTI.DigitalInput in2;
		private GTI.DigitalInput in3;
		private GTI.DigitalInput in4;

		/// <summary>Constructs a new instance.</summary>
		/// <param name="socketNumber">The socket that this module is plugged in to.</param>
		public Keypad_KP16(int socketNumber)
		{
			Socket socket = Socket.GetSocket(socketNumber, true, this, null);

			socket.EnsureTypeIsSupported('Y', this);

			this.out1 = new GT.Interfaces.DigitalOutput(socket, GT.Socket.Pin.Three, true, null);
			this.out2 = new GT.Interfaces.DigitalOutput(socket, GT.Socket.Pin.Four, true, null);
			this.in1 = new GT.Interfaces.DigitalInput(socket, GT.Socket.Pin.Five, GT.Interfaces.GlitchFilterMode.Off, GT.Interfaces.ResistorMode.PullUp, null);
			this.in2 = new GT.Interfaces.DigitalInput(socket, GT.Socket.Pin.Six, GT.Interfaces.GlitchFilterMode.Off, GT.Interfaces.ResistorMode.PullUp, null);
			this.in3 = new GT.Interfaces.DigitalInput(socket, GT.Socket.Pin.Seven, GT.Interfaces.GlitchFilterMode.Off, GT.Interfaces.ResistorMode.PullUp, null);
			this.in4 = new GT.Interfaces.DigitalInput(socket, GT.Socket.Pin.Eight, GT.Interfaces.GlitchFilterMode.Off, GT.Interfaces.ResistorMode.PullUp, null);
		}

		/// <summary>
		/// The possible keys that can be pressed.
		/// </summary>
		public enum Key
		{
			/// <summary>
			/// The A key.
			/// </summary>
			A,
			/// <summary>
			/// The B key.
			/// </summary>
			B,
			/// <summary>
			/// The C key.
			/// </summary>
			C,
			/// <summary>
			/// The D key.
			/// </summary>
			D,
			/// <summary>
			/// The # key.
			/// </summary>
			Pound,
			/// <summary>
			/// The * key.
			/// </summary>
			Star,
			/// <summary>
			/// The 0 key.
			/// </summary>
			Zero,
			/// <summary>
			/// The 1 key.
			/// </summary>
			One,
			/// <summary>
			/// The 2 key.
			/// </summary>
			Two,
			/// <summary>
			/// The 3 key.
			/// </summary>
			Three,
			/// <summary>
			/// The 4 key.
			/// </summary>
			Four,
			/// <summary>
			/// The 5 key.
			/// </summary>
			Five,
			/// <summary>
			/// The 6 key.
			/// </summary>
			Six,
			/// <summary>
			/// The 7 key.
			/// </summary>
			Seven,
			/// <summary>
			/// The 8 key.
			/// </summary>
			Eight,
			/// <summary>
			/// The 9 key.
			/// </summary>
			Nine,
		}

		/// <summary>
		/// Determines whether or not a given key is pressed.
		/// </summary>
		/// <param name="key">The key whose state we want to check.</param>
		/// <returns>Whether or not the key is pressed.</returns>
		public bool IsKeyPressed(Key key)
		{
			bool out1 = false;
			bool out2 = false;

			if (key == Key.One || key == Key.Four || key == Key.Seven || key == Key.Star) { out1 = false; out2 = false; }
			else if (key == Key.Two || key == Key.Five || key == Key.Eight || key == Key.Zero) { out1 = true; out2 = false; }
			else if (key == Key.Three || key == Key.Six || key == Key.Nine || key == Key.Pound) { out1 = false; out2 = true; }
			else if (key == Key.A || key == Key.B || key == Key.C || key == Key.D) { out1 = true; out2 = true; }

			this.out1.Write(out1);
			this.out2.Write(out2);
	
			if (key == Key.One || key == Key.Two || key == Key.Three || key == Key.A) return !this.in1.Read();
			else if (key == Key.Four || key == Key.Five || key == Key.Six || key == Key.B) return !this.in2.Read();
			else if (key == Key.Seven || key == Key.Eight || key == Key.Nine || key == Key.C) return !this.in3.Read();
			else if (key == Key.Star || key == Key.Zero || key == Key.Pound || key == Key.D) return !this.in4.Read();

			return false;
		}
	}
}

#5

Jeff, this was most excellent of you, thank you! Thank you as well Gus, I look forward to it.


#6

btw, what sockets is the module targeting?


#7
socket.EnsureTypeIsSupported('Y', this);

#8

thats what I was looking for, glossed right over it. thx!


#9

This driver provides core functionality. No debounce, hold, or keyup/down events. This is not necessarily a bad thing depending on what you need to actually use it for. Shall we (community) make a keypad handler class for the extra smarts? Or is GHI doing that?