Main Site Documentation

Get Active Socket


#1

I’m currently using the Hydra and trying to initialize a GPIO pin within a socket. However whenever I initialize a new socket control is always passed to it on the hydra board, so I cannot toggle a previously initialized socket.

Here’s an example:

  1. I initialize socket 1, pin 2 on the Hydra board
  2. I am successfully able to toggle it to high and low
  3. I initialize socket 3, pin 4
  4. I am successfully able to toggle it high and low
  5. I now want to toggle socket 1, pin 2 again, but when I set it to high or low, it is in fact socket 3, pin 4 that is toggling.
  6. I initialize socket 5, pin 6
  7. I want to toggle socket 1, pin 2 again but now socket 5, pin 6 is toggling

Is there a method that allows me to set the active socket to avoid this problem?


#2

Please show your code.


#3

Here is how I initialize a new pin on a socket:


    public class ToggleSocketPin : GTM.Module, ISetupPinConfig
    {
        /// <summary>
        /// Global socket variables
        /// </summary>
        GT.Socket socket;
        private static GTI.DigitalOutput[] Enable = new GTI.DigitalOutput[11];
        private bool[] _state = new bool[11];
        private int _pin;
        private string[] PinName = new string[11];

        public ToggleSocketPin(int socketNumber)
        {
            socket = GT.Socket.GetSocket(socketNumber, true, this, null);

            try
            {
                socket.EnsureTypeIsSupported('F', this);
            }
            catch (Gadgeteer.Socket.InvalidSocketException)
            {
                Debug.Print("Unable to find correct socket type");
            }
        }

        /// <summary>
        /// Initialize one of the pins that exist on the socket
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pinNumber"></param>
        /// <param name="state"></param>
        public void ConfigPin(string name, int pinNumber, bool state)
        {
            _pin = pinNumber;
            _state[pinNumber] = state;
            PinName[pinNumber] = name;

            Enable[pinNumber] = new GTI.DigitalOutput(socket, (GT.Socket.Pin)pinNumber, state, this);
        }

        /// <summary>
        /// Retrieve the pin number from a given name
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public int GetPinNumber(string name)
        {
            for (int i = 0; i < PinName.Length; i++)
            {
                if (PinName[i] == name)
                    return i;
            }
            return 0;
        }

        /// <summary>
        /// Find the state of the pin
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool ObtainState(string name)
        {
            int number = GetPinNumber(name);
            if (number == 0)
                throw new Exception();

             _state[number] = Enable[number].Read();

            return _state[number];
        }

        /// <summary>
        /// Set the boolean value of the pin
        /// </summary>
        /// <param name="name"></param>
        /// <param name="state"></param>
        public void SetPin(string name, bool state)
        {
            int number = GetPinNumber(name);
            if (number == 0)
                throw new Exception();

            _state[number] = state;
            Enable[number].Write(state);
        }
    }

I can initialize a socket like this:



            Socket8 = new ToggleSocketPin(8);
            Socket8.ConfigPin("Trigger", 4, true);

            Socket13 = new ToggleSocketPin(13);
            Socket13.ConfigPin("Select", 6, true);

However when I want to toggle ‘Trigger’, Select will flip


            Socket8.SetPin("Trigger", false);
            Socket8.SetPin("Trigger", true);
 

#4

First problem is that your Enable is static (shared between all instances of the ToggleSocketPin). So the last Digital output for the same pin number will replace the previous one. This would explain the behavior you are experiencing with same pin numbers on different sockets.

Making it non static would simplify things a lot. Using strings as selectors is overkill in my opinion as well.

There are probably more issue but start with this one first.


#5

Actually it appears as if the issue was because I declared the Enable as static. That was a good catch - thanks for the quick response.

I’m using strings as a selector for my own purposes - I know it’s overkill but it simplifies things for others down the road.