How to convert a Socket.Pin to a Cpu.Pin?

I’m trying to use a Gadgeteer with some code that was written for traditional NETMF. I need to pass a Cpu.Pin value to the class. Is there a programmatic way to get a Socket.Pin from an Extender module and cast it as a Cpu.Pin? Of course, I can get the value from the schematics but that’s not a very “rapid” method.

Maybe have a look here : GHI Electronics – Where Hardware Meets Software
near the bottom of the page. There’s a method to have the pin number.

Yea, I’m familiar with that method. It requires getting the numbers off the schematics. There should be a Gadgeteer function that returns the pin # given a socket pin #.

Would something like this work:


using Microsoft.SPOT.Hardware;
using GT = Gadgeteer;


                var mySocket = Socket.GetSocket(5,true,null,null);
                mySocket.CpuPins[1] = Cpu.Pin.GPIO_Pin15;
                for (var i = 0; i < 10; i++) //since the socket have 10 pins... loop through them all.
                {                    
                    Debug.Print("CPU PIN= " + mySocket.CpuPins[i].ToString());                 
                }



//output: notice that i changed pin 1 to cpu.pin 15

..without  mySocket.CpuPins[1] = Cpu.Pin.GPIO_Pin15;
CPU PIN= -1
[b][italic]CPU PIN= -1[/italic][/b]
CPU PIN= -1
CPU PIN= 23
CPU PIN= 43
CPU PIN= 41
CPU PIN= 44
CPU PIN= 40
CPU PIN= 39
CPU PIN= 42

//with  mySocket.CpuPins[1] = Cpu.Pin.GPIO_Pin15;

CPU PIN= -1
[b][italic]CPU PIN= 15[/italic][/b]
CPU PIN= -1
CPU PIN= 23
CPU PIN= 43
CPU PIN= 41
CPU PIN= 44
CPU PIN= 40
CPU PIN= 39
CPU PIN= 42


may not be the complete code you need but it’s a start…

check out the Gadgeteer.Socket.cs source file.
Jay.

Schematic?

I think you’re missing the point. Since Gadgeteer already knows this info, I should be able to determine which Cpu.Pin # I need at runtime through something like this:

GT.Socket.GetSocket(7,true,null,null).GetPinId(<some Socket.Pin value>)

and from the Module base class there should be something like…

extender.GetPinId(<some Socket.Pin value>)

this way we can stick with the spirit of Gadgeteer and be able to write code that is as flexible as Gadgeteer itself is.

I will dig into the Gadgeteer source and see if there’s some non-obvious way to do this but it seems that these functions may be private or protected currently.

updated the code to return useful info:
this is using Gadgeteer… Socket is the same as Gadgeteer.Socket…
if you want those at the Module level you can always extend it to include the needed function.


            var mySocket = Socket.GetSocket(6, true, null, null);            
            for (var i = 1; i < 10; i++)
            {
                Debug.Print("Socket Number= " + mySocket.Name + " has Socket Pin=" + i + " with assigned CPU PIN=" + mySocket.CpuPins[i].ToString());                
            }

OutPut:


//note: -1 means that no CPU PIN is assigned.
Socket Number= 6 has it's Pin=1 with an assigned CPU PIN=-1
Socket Number= 6 has it's Pin=2 with an assigned CPU PIN=-1
Socket Number= 6 has it's Pin=3 with an assigned CPU PIN=18
Socket Number= 6 has it's Pin=4 with an assigned CPU PIN=20
Socket Number= 6 has it's Pin=5 with an assigned CPU PIN=22
Socket Number= 6 has it's Pin=6 with an assigned CPU PIN=10
Socket Number= 6 has it's Pin=7 with an assigned CPU PIN=36
Socket Number= 6 has it's Pin=8 with an assigned CPU PIN=38
Socket Number= 6 has it's Pin=9 with an assigned CPU PIN=35

BTW this array has the CPU pins assigned to the Socket Pins:


GT.Socket.GetSocket(7,true,null,null).CpuPins //which you can loop to get the Socket pin.. they start at index of 0 pin 1.

unless I’m missing something…

Jay.

Let me better describe what I want to be able to do. I want to be able to create a class such as this…


        public class Piezo
        {
            public Piezo(Cpu.Pin pin)
            {
                ....
            }
            .....
        }

that can be used from both Gadgeteer and from a Panda-II (traditional NETMF). However, since the idea of individual pins should be more or less transparent to the user on Gadgeteer I should be able to move my module from one socket to another w/o the user having to figure out what the Cpu.Pin # is for Socket.Pin #7. So, something like the below should “just work” if I move my module (in the designer and physically) from say socket 7 to socket 4 on a Hydra. I realize this is a terrible example since there is only one PWM socket on the Hydra but lets pretend we’re talking about an OutputPort.


var piz = new Piezo(extender.GetPinId((Socket.Pin)7)

Hi,
now I’m confused even more. :frowning:
can you please provide how you do it now, using schematics…so at least we can understand what exactly you are doing and how you want to do it differently/better…

an example using real info would help replicate what you are doing here so we can all see it and find a way to do it your way…

I’m interested in this because i myself building an application where i should be able to add new modules at run-time and be able to use those modules without recompiling the application.

thanks.

Well, we don’t really have to use schematics anymore now that the Hydra SDK is out. The technique you show above of iterating through the pins would be the ideal way to get that data now. However, you still end up with a CPU level list of pins and what I’m proposing is that the Module “know” what pins it uses. That way the developer only needs to know that they are using Socket.Pin #7 and there be a way to cast this to a Cpu.Pin. Currently, to use code that is expecting a Cpu.Pin feels very Frankenstein.

BTW, hot swapping modules is not a supported activity. Gus discussed this in a previous thread.

EDIT: Jay Jay, I appologize. I just took a fresh look at your first post and I think it does exactly what I’m looking for! Since Cpu.Pins is an array, I can pass it the socket pin # I need and get back the Cpu pin #. I think I’ll get caught up on some rest this weekend… :frowning:

Exactly what i was thinking you were after…

BTW i’m not looking to Hot swap… what I’m doing is building an application that can accept a number of predetermined Sensors and Modules… which can be added at later time by the end user if you will; and be able to configure those Modules and Sensors without having to recompile the app…but through a interface.

Cheers,
Jay.