Ok,
After a weekend away and a busy week at work, I have returned to this with "proper" diodes and finally managed to get the project to work. I see the same as Paul in that the values returned seem to be reversed from his theory. Below is a work-in-progress driver class for the setup. It needs a lot of work, especially in improving area of key-debounce. I am seeing far too often double key presses. I would like to improve the oversized switch statement as well.
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
#if Netduino
using SecretLabs.NETMF.Hardware.Netduino;
#else
using SecretLabs.NETMF.Hardware.NetduinoPlus;
#endif
namespace KeypadTest
{
public class KeypadDriver
{
public delegate void KeyPressHandler(object sender, char keyPressed);
public KeypadDriver(int debounce=250)
{
this.debounce = debounce;
keyReader = new Thread(new ThreadStart(ReadKeyPad));
keyReader.Start();
}
public event KeyPressHandler OnKeyPressed;
private void ReadKeyPad()
{
while (true)
{
readbuffer[0] = 0x00;
writebuffer[0] = 0x01;
spi.WriteRead(writebuffer, readbuffer);
if (readbuffer[0] != 0xFF)
{
char result = '+'; // need to define something ...
switch (readbuffer[0])
{
case 0x88:
result = '1';
break;
case 0x84:
result = '2';
break;
case 0x82:
result = '3';
break;
case 0x81:
result = 'A';
break;
case 0x48:
result = '4';
break;
case 0x44:
result = '5';
break;
case 0x42:
result = '6';
break;
case 0x41:
result = 'B';
break;
case 0x28:
result = '7';
break;
case 0x24:
result = '8';
break;
case 0x22:
result = '9';
break;
case 0x21:
result = 'C';
break;
case 0x18:
result = '*';
break;
case 0x14:
result = '0';
break;
case 0x12:
result = '#';
break;
case 0x11:
result = 'D';
break;
}
if (OnKeyPressed != null)
{
OnKeyPressed(this, result);
}
}
Thread.Sleep(debounce);
}
}
private Thread keyReader;
private int debounce;
private byte[] readbuffer = new byte[1];
private byte[] writebuffer = new byte[1];
private SPI spi = new SPI(new SPI.Configuration(Pins.GPIO_NONE, false, 0, 0, false, true, 32, SPI.SPI_module.SPI1));
}
}
An example use:-
public class Program
{
public static void Main()
{
// write your code here
KeypadDriver driver = new KeypadDriver();
driver.OnKeyPressed += new KeypadDriver.KeyPressHandler(driver_OnKeyPressed);
while (true)
{
Thread.Sleep(250);
}
}
static void driver_OnKeyPressed(object sender, char keyPressed)
{
Debug.Print("Key Pressed = " + keyPressed.ToString());
}
}
Once again thanks to Mario and Paul for the efforts they both put in answering my original query. Paul, please feel free to add this code to the wiki article, I'm afraid I had a look but could not work out the process.
Glen