Main Site Documentation

CPU.PIN type


#1

Hi guys,

how can i identify the type of cpu.pin being passed in a method?

For ex: (pseudo code)


gadget mygadget = new gadget((Cpu.Pin)FEZ_Pin.Interrupt.IO12);

//in my gadget code:
public gadget(Cpu.Pin myPin)
{
  				switch (myPin.Type)
				{
					case: Digital:
                                                //Create polling method
						break;
					case: Interrupt:
                                                //Create Interrupt handler
						break;
					default:
						break;
				}

}


#2

I don’t think you can do it that way - enums (which Cpu.Pin & FEZ_Pin are) lose their type when you cast them to something else… It’s not like a sub class/base class where you can go back and forth between the two types.

For something like this, I’d probably add another parameter to the constructor to specify the type. That or (somewhat gross) have a lookup table/switch-case that determines the type based on the pin number…


#3

No… Wait… I think I’m full of it…

This is from .NET, not .NETMF, but works:


	enum EnumA
	{
		A0,
		A1,
		A2
	}

	enum EnumB
	{
		B0,
		B1,
		B2
	}

	class Test
	{
		static void DisplayType(Enum x)
		{
			Console.WriteLine(x.GetType() + " = " + x.ToString() + ", EnumB? = " + (x.GetType() == typeof (EnumB) ? "yes" : "no"));
		}

		static void Main(string[] args)
		{
			DisplayType(EnumA.A0);
			DisplayType(EnumB.B2);
		}
	}

It says:

Test.EnumA = A0, EnumB? = no
Test.EnumB = B2, EnumB? = yes