Example #1
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();
                }
            }
        }
Example #2
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();
                }
            }
        }