Ejemplo n.º 1
0
        /// <summary>Constructs a new instance.</summary>
        /// <param name="socketNumber">The socket that this module is plugged in to.</param>
        public TempHumidity(int socketNumber)
        {
            Socket socket = Socket.GetSocket(socketNumber, true, this, null);

            socket.EnsureTypeIsSupported(new char[] { 'X', 'Y' }, this);

            this.sck     = GTI.DigitalOutputFactory.Create(socket, Socket.Pin.Four, false, this);
            this.data    = GTI.DigitalIOFactory.Create(socket, Socket.Pin.Three, true, GTI.GlitchFilterMode.Off, GTI.ResistorMode.Disabled, this);
            this.running = false;
        }
Ejemplo n.º 2
0
        // Note: A constructor summary is auto-generated by the doc builder.
        /// <summary></summary>
        /// <param name="socketNumber">The socket that this module is plugged in to.</param>
        public TemperatureSensor(int socketNumber)
        {
            // This finds the Socket instance from the user-specified socket number.  
            // This will generate user-friendly error messages if the socket is invalid.
            // If there is more than one socket on this module, then instead of "null" for the last parameter, 
            // put text that identifies the socket to the user (e.g. "S" if there is a socket type S)
            Socket socket = Socket.GetSocket(socketNumber, true, this, null);

            _sck = GTI.DigitalOutputFactory.Create(socket, Socket.Pin.Four, false, this);
            _data = GTI.DigitalIOFactory.Create(socket, Socket.Pin.Three, true, GTI.GlitchFilterMode.Off, GTI.ResistorMode.Disabled, this);

            new Thread(TakeMeasurements).Start();
        }
Ejemplo n.º 3
0
        // Note: A constructor summary is auto-generated by the doc builder.
        /// <summary></summary>
        /// <remarks>This automatically checks that the socket supports Type X or Y as appropriate, and reserves the SDA and SCL pins.
        /// An exception will be thrown if there is a problem with these checks.</remarks>
        /// <param name="socket">The socket for this software I2C device interface.</param>
        /// <param name="address">The address of the I2C device.</param>
        /// <param name="clockRateKHz">The maximum clock speed supported by the I2C device.</param>
        /// <param name="sdaPin">The socket pin used for I2C data.</param>
        /// <param name="sclPin">The socket pin used for I2C clock.</param>
        /// <param name="module">The module using this I2C interface, which can be null if unspecified.</param>
        public SoftwareI2CBus(Socket socket, Socket.Pin sdaPin, Socket.Pin sclPin, ushort address, int clockRateKHz, Module module)
        {
            this.address      = (byte)address;
            this.clockRateKHz = clockRateKHz;

            // see if we've already reserved the pins and got instances of the ports, otherwise do that.
            string sdaPinString = socket.ToString() + "___" + sdaPin;

            if (!ReservedSdaPinPorts.Contains(sdaPinString))
            {
                sdaPort = DigitalIOFactory.Create(socket, sdaPin, false, GlitchFilterMode.Off, ForceManagedPullUps ? ResistorMode.PullUp : ResistorMode.Disabled, module);
                ReservedSdaPinPorts.Add(sdaPinString, sdaPort);
            }
            else
            {
                sdaPort = (DigitalIO)ReservedSdaPinPorts[sdaPinString];
            }

            string sclPinString = socket.ToString() + "___" + sclPin;

            if (!ReservedSclPinPorts.Contains(sclPinString))
            {
                sclPort = DigitalIOFactory.Create(socket, sclPin, false, GlitchFilterMode.Off, ForceManagedPullUps ? ResistorMode.PullUp : ResistorMode.Disabled, module);
                ReservedSclPinPorts.Add(sclPinString, sclPort);
            }
            else
            {
                sclPort = (DigitalIO)ReservedSclPinPorts[sclPinString];
            }

            this.socket = socket;
            this.sdaPin = sdaPin;
            this.sclPin = sclPin;

            lock (SoftwareI2CTimeoutList)
            {
                timeoutCount = -1;       // Prevent the TimeoutHandler thread from watching this port for now
                SoftwareI2CTimeoutList.Add(this);

                if (timeoutThread == null)
                {
                    threadExit    = false;
                    timeoutThread = new Thread(TimeoutHandler);
                    timeoutThread.Start();
                }
            }
        }
Ejemplo n.º 4
0
        // Note: A constructor summary is auto-generated by the doc builder.
        /// <summary></summary>
        /// <remarks>This automatically checks that the socket supports Type X or Y as appropriate, and reserves the SDA and SCL pins.
        /// An exception will be thrown if there is a problem with these checks.</remarks>
        /// <param name="socket">The socket for this software I2C device interface.</param>
        /// <param name="address">The address of the I2C device.</param>
        /// <param name="clockRateKHz">The maximum clock speed supported by the I2C device.</param>
        /// <param name="sdaPin">The socket pin used for I2C data.</param>
        /// <param name="sclPin">The socket pin used for I2C clock.</param>
        /// <param name="module">The module using this I2C interface, which can be null if unspecified.</param>
        public SoftwareI2CBus(Socket socket, Socket.Pin sdaPin, Socket.Pin sclPin, ushort address, int clockRateKHz, Module module)
        {
            this.address = (byte)address;
            this.clockRateKHz = clockRateKHz;

            // see if we've already reserved the pins and got instances of the ports, otherwise do that.
            string sdaPinString = socket.ToString() + "___" + sdaPin;
            if (!ReservedSdaPinPorts.Contains(sdaPinString))
            {
                sdaPort = DigitalIOFactory.Create(socket, sdaPin, false, GlitchFilterMode.Off, ForceManagedPullUps ? ResistorMode.PullUp : ResistorMode.Disabled, module);
                ReservedSdaPinPorts.Add(sdaPinString, sdaPort);
            }
            else
            {
                sdaPort = (DigitalIO)ReservedSdaPinPorts[sdaPinString];
            }

            string sclPinString = socket.ToString() + "___" + sclPin;
            if (!ReservedSclPinPorts.Contains(sclPinString))
            {
                sclPort = DigitalIOFactory.Create(socket, sclPin, false, GlitchFilterMode.Off, ForceManagedPullUps ? ResistorMode.PullUp : ResistorMode.Disabled, module);
                ReservedSclPinPorts.Add(sclPinString, sclPort);
            }
            else
            {
                sclPort = (DigitalIO)ReservedSclPinPorts[sclPinString];
            }

            this.socket = socket;
            this.sdaPin = sdaPin;
            this.sclPin = sclPin;

            lock (SoftwareI2CTimeoutList)
            {
                timeoutCount = -1;       // Prevent the TimeoutHandler thread from watching this port for now
                SoftwareI2CTimeoutList.Add(this);

                if (timeoutThread == null)
                {
                    threadExit = false;
                    timeoutThread = new Thread(TimeoutHandler);
                    timeoutThread.Start();
                }
            }
        }
Ejemplo n.º 5
0
        public SoftwareI2CBus(Socket socket, Socket.Pin sdaPin, Socket.Pin sclPin, ushort address, int clockRateKHz, Module module)
        {
            this.address      = (byte)address;
            this.clockRateKHz = clockRateKHz;
            string key = socket.ToString() + "___" + sdaPin;

            if (!ReservedSdaPinPorts.Contains(key))
            {
                this.sdaPort = DigitalIOFactory.Create(socket, sdaPin, false, GlitchFilterMode.Off, ForceManagedPullUps ? ResistorMode.PullUp : ResistorMode.Disabled, module);
                ReservedSdaPinPorts.Add(key, this.sdaPort);
            }
            else
            {
                this.sdaPort = (DigitalIO)ReservedSdaPinPorts[key];
            }
            string str2 = socket.ToString() + "___" + sclPin;

            if (!ReservedSclPinPorts.Contains(str2))
            {
                this.sclPort = DigitalIOFactory.Create(socket, sclPin, false, GlitchFilterMode.Off, ForceManagedPullUps ? ResistorMode.PullUp : ResistorMode.Disabled, module);
                ReservedSclPinPorts.Add(str2, this.sclPort);
            }
            else
            {
                this.sclPort = (DigitalIO)ReservedSclPinPorts[str2];
            }
            this.socket = socket;
            this.sdaPin = sdaPin;
            this.sclPin = sclPin;
            ArrayList list = SoftwareI2CTimeoutList;

            lock (list)
            {
                this.timeoutCount = -1;
                SoftwareI2CTimeoutList.Add(this);
                if (timeoutThread == null)
                {
                    threadExit    = false;
                    timeoutThread = new Thread(new ThreadStart(SoftwareI2CBus.TimeoutHandler));
                    timeoutThread.Start();
                }
            }
        }
Ejemplo n.º 6
0
        private void DoManagedWriteRead(byte address, byte[] writeBuffer, int writeOffset, int writeLength, byte[] readBuffer, int readOffset, int readLength, out int numWritten, out int numRead)
        {
            DigitalIO sdaPort = this.sdaPort;

            lock (sdaPort)
            {
                ArrayList list = SoftwareI2CTimeoutList;
                lock (list)
                {
                    this.timeout      = false;
                    this.timeoutCount = 0;
                }
                numWritten = 0;
                numRead    = 0;
                if (writeLength != 0)
                {
                    if (!this.sclPort.Read())
                    {
                        throw new ApplicationException("Software I2C: clock signal on socket " + this.socket + " is being held low.");
                    }
                    this.sdaPort.Mode = IOMode.Output;
                    this.sclPort.Mode = IOMode.Output;
                    if (this.WriteByte((byte)(address << 1)))
                    {
                        for (int i = writeOffset; i < (writeOffset + writeLength); i++)
                        {
                            if (!this.WriteByte(writeBuffer[i]))
                            {
                                break;
                            }
                            numWritten++;
                        }
                    }
                    if ((readLength == 0) || (numWritten != writeLength))
                    {
                        this.sclPort.Mode = IOMode.Input;
                        while (!this.sclPort.Read() && !this.timeout)
                        {
                        }
                        this.sdaPort.Mode = IOMode.Input;
                    }
                    else
                    {
                        this.sdaPort.Mode = IOMode.Input;
                        while (!this.sdaPort.Read() && !this.timeout)
                        {
                        }
                        this.sclPort.Mode = IOMode.Input;
                    }
                }
                if (this.timeout)
                {
                    throw new ApplicationException("Software I2C: clock signal on socket " + this.socket + " is being held low.");
                }
                if ((numWritten == writeLength) && (readLength != 0))
                {
                    if (!this.sclPort.Read())
                    {
                        throw new ApplicationException("Software I2C: clock signal on socket " + this.socket + " is being held low.");
                    }
                    this.sdaPort.Mode = IOMode.Output;
                    this.sclPort.Mode = IOMode.Output;
                    if (this.WriteByte((byte)((address << 1) | 1)))
                    {
                        int num2 = (readOffset + readLength) - 1;
                        for (int j = readOffset; j < (readOffset + readLength); j++)
                        {
                            if (!this.ReadByte(j == num2, out readBuffer[j]))
                            {
                                break;
                            }
                            numRead++;
                        }
                    }
                    this.sclPort.Mode = IOMode.Input;
                    while (!this.sclPort.Read() & !this.timeout)
                    {
                    }
                    this.sdaPort.Mode = IOMode.Input;
                }
                if (this.timeout)
                {
                    throw new ApplicationException("Software I2C: clock signal on socket " + this.socket + " is being held low.");
                }
                list = SoftwareI2CTimeoutList;
                lock (list)
                {
                    this.timeoutCount = -1;
                }
            }
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Sends a reset pulse on the daisylink chain.  This resets all DaisyLink nodes to INIT state, that is, waiting for a DaisyLink message.
 /// </summary>
 /// <remarks>
 /// It is recommended to reboot the mainboard after calling this method because communication to the DaisyLink nodes will fail.
 /// </remarks>
 internal void SendResetPulse()
 {
     lock (portLock)
     {
         if (daisyLinkInterruptPort != null)
         {
             daisyLinkInterruptPort.Interrupt -= daisyLinkInterruptPort_OnInterrupt;
             daisyLinkInterruptPort.Dispose();       // Ask hardware drivers to unreserve this pin
             daisyLinkInterruptPort = null;
         }
         if (daisyLinkResetPort == null)
         {
             daisyLinkResetPort = DigitalIOFactory.Create(Socket, daisyLinkPin, false, GlitchFilterMode.Off, ResistorMode.PullUp, null);
         }
         daisyLinkResetPort.Mode = IOMode.Output;       // Should drive the neighbor bus high
         Thread.Sleep(2);                        // 2 milliseconds is definitely more than 1 ms
         daisyLinkResetPort.Mode = IOMode.Input;      // Pull-downs should take the neighbor bus back low
         daisyLinkResetPort.Dispose();           // Remove this pin from the hardware's reserved pin list
         daisyLinkResetPort = null;
     }
 }
Ejemplo n.º 8
0
            private DaisyLink(Socket socket, DaisyLinkModule module)
            {
                Ready = false;
                this.Socket = socket;

                this.ReservedCount = 0;
                this.NodeCount = 0;

                daisyLinkBus = I2CBusFactory.Create(socket, defaultI2cAddress, 10, i2cDataPin, i2cClockPin, module);

                // The link pin (port) is initialized as an input.  It is only driven during initialization of the daisylinked modules.
                daisyLinkResetPort = DigitalIOFactory.Create(socket, daisyLinkPin, false, GlitchFilterMode.Off, ResistorMode.PullUp, module);
                daisyLinkInterruptPort = null;
            }