Someone has to have some ideas. Please. I tried the analog ping and they work as expected. As I mentioned before it is looking like the board has come up with the other devices enabled instead of as a "blank slate". As I mentioned this code works fine on the netduino plus.
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
namespace DummyLoadPortSensor
{
public class SwitchMonitor
{
private Object switchStateLock;
private SwitchStateEnum currentSwitchState = SwitchStateEnum.SwitchIsUnknown;
private SwitchStateEnum previousSwitchState = SwitchStateEnum.SwitchIsUnknown;
private ushort state;
// private SecretLabs.NETMF.Hardware.AnalogInput analogInput;
private Microsoft.SPOT.Hardware.InputPort input;
private Microsoft.SPOT.Hardware.OutputPort led;
private int pollTime;
private TCPWriterInputQueue tcpWriterInputQueue;
#region Constructor
/// <summary>
/// This is the constructor for a switch monitor.
/// It takes 2 arguments, both ints. The first one
/// is the general purpose IO number (0-13) and the
/// second one is the polling interval in ms. For
/// debounce purposes it takes 12 polling cycles
/// with a non changed value before a change from a
/// prevoius state is registered. i.e. a polling
/// interval of 100(ms) would register a change if
/// the switch were in the new position for about
/// 1.2 seconds.
/// </summary>
/// <param name="gpioLineNumber">The input pin to monitor 0-13</param>
/// <param name="pollInterval">The input pin to monitor 0-13</param>
public SwitchMonitor(int gpioLineNumber, int pollInterval, TCPWriterInputQueue tcpWriterInputQueue)
{
this.tcpWriterInputQueue = tcpWriterInputQueue;
pollTime = pollInterval;
switchStateLock = new Object();
led = new Microsoft.SPOT.Hardware.OutputPort(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.ONBOARD_LED, false);
state = new ushort();
switch(gpioLineNumber)
{
case 0:
// analogInput = new SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A0);
input = new Microsoft.SPOT.Hardware.InputPort(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_D0, true, Microsoft.SPOT.Hardware.Port.ResistorMode.PullUp);
break;
case 1:
// analogInput = new SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A1);
input = new Microsoft.SPOT.Hardware.InputPort(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_D1, true, Microsoft.SPOT.Hardware.Port.ResistorMode.PullUp);
break;
case 2:
input = new Microsoft.SPOT.Hardware.InputPort(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_D2, true, Microsoft.SPOT.Hardware.Port.ResistorMode.PullUp);
break;
case 3:
input = new Microsoft.SPOT.Hardware.InputPort(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_D3, true, Microsoft.SPOT.Hardware.Port.ResistorMode.PullUp);
break;
case 4:
input = new Microsoft.SPOT.Hardware.InputPort(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_D4, true, Microsoft.SPOT.Hardware.Port.ResistorMode.PullUp);
break;
case 5:
input = new Microsoft.SPOT.Hardware.InputPort(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_D5, true, Microsoft.SPOT.Hardware.Port.ResistorMode.PullUp);
break;
case 6:
input = new Microsoft.SPOT.Hardware.InputPort(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_D6, true, Microsoft.SPOT.Hardware.Port.ResistorMode.PullUp);
break;
case 7:
input = new Microsoft.SPOT.Hardware.InputPort(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_D7, true, Microsoft.SPOT.Hardware.Port.ResistorMode.PullUp);
break;
case 8:
input = new Microsoft.SPOT.Hardware.InputPort(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_D8, true, Microsoft.SPOT.Hardware.Port.ResistorMode.PullUp);
break;
case 9:
input = new Microsoft.SPOT.Hardware.InputPort(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_D9, true, Microsoft.SPOT.Hardware.Port.ResistorMode.PullUp);
break;
case 10:
input = new Microsoft.SPOT.Hardware.InputPort(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_D10, true, Microsoft.SPOT.Hardware.Port.ResistorMode.PullUp);
break;
case 11:
input = new Microsoft.SPOT.Hardware.InputPort(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_D11, true, Microsoft.SPOT.Hardware.Port.ResistorMode.PullUp);
break;
case 12:
input = new Microsoft.SPOT.Hardware.InputPort(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_D12, true, Microsoft.SPOT.Hardware.Port.ResistorMode.PullUp);
break;
case 13:
input = new Microsoft.SPOT.Hardware.InputPort(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_D13, true, Microsoft.SPOT.Hardware.Port.ResistorMode.PullUp);
break;
default:
break;
}
}
#endregion
#region PrivateParts
/// <summary>
/// Read the current state of the hardware pin.
/// Returns 1 if the switch is closed and 0
/// if otherwise.
/// </summary>
private ushort RawSwitchIsClosed()
{
// Debug.Print("Analog " + analogInput.Read());
if (input.Read() == true)
{
Debug.Print("Raw Closed");
return 1;
}
else
{
Debug.Print("Raw Open");
return 0;
}
}
/// <summary>
/// Read the debounced state of the switch.
/// The switch has to be stable in its current
/// state for 12 polling cycles in order to be
/// reported as open or closed. Otherwise
/// unknown is returned.
/// </summary>
private SwitchStateEnum DebouncedSwitch()
{
state = (ushort)(((state << 1) | RawSwitchIsClosed() | 0xF000) & 0x0000FFFF);
if (state == 0xFFFF)
{
return SwitchStateEnum.SwitchIsClosed;
}
else if (state == 0xF000)
{
return SwitchStateEnum.SwitchIsOpen;
}
else
{
return SwitchStateEnum.SwitchIsUnknown;
}
}
#endregion
#region WorkerThread
/// <summary>
/// The main thread that monitors the changes
/// in the switch state and send notifications
/// of the changes to the TCP writer thread.
/// </summary>
public void monitorSwitch()
{
while(true)
{
SwitchStateEnum switchState = DebouncedSwitch();
switch(switchState)
{
case SwitchStateEnum.SwitchIsClosed:
lock (switchStateLock)
{
currentSwitchState = SwitchStateEnum.SwitchIsClosed;
if (previousSwitchState == SwitchStateEnum.SwitchIsUnknown)
{
currentSwitchState = SwitchStateEnum.SwitchIsClosed;
previousSwitchState = currentSwitchState;
Debug.Print("Initial state Closed");
/*
* Not notifying a listener because there should
* not be one. There should not be a condition
* where there is already a listener while this
* thread is starting up.
*/
}
else if (currentSwitchState != previousSwitchState)
{
Debug.Print("Transition to Closed");
tcpWriterInputQueue.notifyWriterOfOutput("SwitchClosedEvent");
previousSwitchState = currentSwitchState;
}
led.Write(true); // Turn on LED
}
break;
case SwitchStateEnum.SwitchIsOpen:
lock (switchStateLock)
{
currentSwitchState = SwitchStateEnum.SwitchIsOpen;
if (previousSwitchState == SwitchStateEnum.SwitchIsUnknown)
{
currentSwitchState = SwitchStateEnum.SwitchIsOpen;
previousSwitchState = currentSwitchState;
Debug.Print("Initial state Open");
/*
* Not notifying a listener because there should
* not be one. There should not be a condition
* where there is already a listener while this
* thread is starting up.
*/
}
else if (currentSwitchState != previousSwitchState)
{
Debug.Print("Transition to Open");
tcpWriterInputQueue.notifyWriterOfOutput("SwitchOpenEvent");
previousSwitchState = currentSwitchState;
}
led.Write(false); // Turn off LED
}
break;
default:
// This handles the unknown case.
break;
}
Thread.Sleep(pollTime);
}
}
/// <summary>
/// Returns the current state of the switch (opened or closed)
/// </summary>
public SwitchStateEnum getSwitchState()
{
SwitchStateEnum result;
lock (switchStateLock)
{
result = currentSwitchState;
}
return result;
}
#endregion
}
}