Today Jan Kučera asked question in Micro Framework Discussion group, about joining pins together to output byte values. Writing such a implementation of OutputPort could not be difficult, so I give it a try. In 30 minutes I have the class for .NET Micro Framework done and tested.

MultiOutputPort class

Class is called MultiOutputClass and takes two arguments. First argument is array of Cpu.Pin that represents bits of output value. First member of the array is the least significant bit of the value to output. Second parameter is initial value for MultiOutputPort.

/// <summary>
/// Join multiple pins to write numbers
/// </summary>
public class MultiOutputPort : IDisposable
{   
    /// <summary>
    /// Creates new MultiOutputPort object
    /// </summary>
    /// <param name="portList">Array of Cpu.Pin port. Least signigicant bit is first</param>
    /// <param name="initialValue">Initial value for MultiOutputPort</param>
    public MultiOutputPort(Cpu.Pin[] portList, int initialValue)
    {
        // Argument validation
        if (portList == null || portList.Length == 0)
                 throw new ArgumentOutOfRangeException();

        // Saving performance. Do not repeat query for array length
        int portListLength = portList.Length;
        // Create array of OutputPorts 
        this._portList = new OutputPort[portListLength];
        for (int i = 0; i < portListLength; i++)
        {
             this._portList[i] = new OutputPort(portList[i], ((initialValue & 1) == 1));
             initialValue >>= 1;
        }
    }
    
    /// <summary>
    /// Writes a value to the port output
    /// </summary>
    /// <param name="outputValue">Number to write out</param>
    public void Write(int outputValue)
    {
        int portListLength = this._portList.Length;
        for (int i = 0; i < portListLength; i++)     
        {
            this._portList[i].Write(((outputValue & 1) == 1));
            outputValue >>= 1;
        }
    }

    #region IDisposable Members

    public void Dispose()    
    {        
        // Dispose array members        
        int portListLength = this._portList.Length;
            
        for (int i = 0; i < portListLength; i++)        
        {            
            this._portList[i].Dispose();                        
        }                  
    }    
    
    #endregion    
    
    /// <summary>    
    /// OutputPort array    
    /// Least Significant bit is fisrt    
    /// </summary>    
    private OutputPort[] _portList;
}

Not limited to bytes

Since the output pins are set as an array, the output value could not be necessary byte. It can be nibble (4 bits), short or even int. Example of using this class follows.

MultiOutputPort port = new MultiOutputPort(new Cpu.Pin[8] {
                                            Cpu.Pin.GPIO_Pin0, 
                                            Cpu.Pin.GPIO_Pin1,
                                            Cpu.Pin.GPIO_Pin2,
                                            Cpu.Pin.GPIO_Pin3,
                                            Cpu.Pin.GPIO_Pin4,
                                            Cpu.Pin.GPIO_Pin5,
                                            Cpu.Pin.GPIO_Pin6,
                                            Cpu.Pin.GPIO_Pin7 } , 0x00);

port.Write(0x01);
port.Write(0x0F);
port.Write(0xF0);
port.Write(0xFF);

Update

In my next blog post I will show implementation of MultiInputPort to read byte values from joined pins.