Esempio n. 1
0
        private bool[] readTemperatureIntoBoolArray() {

            bool[] response = new bool[8];

            while (_data.Read()) ;

            clock(true);

            bool ack = false; //_data.Read();

            clock(false);

            if (ack) ErrorPrint("Communication failure when trying to read temperature.");

            // Read first byte in
            for (short i = 0; i < 8; i++)
            {
                response[i] = _data.Read();
                clock(true);

                clock(false);
            }

            return response;
            
            
        }
Esempio n. 2
0
        // this relies on argument checking already being done in WriteRead
        private void DoManagedWriteRead(byte address, byte[] writeBuffer, int writeOffset, int writeLength, byte[] readBuffer, int readOffset, int readLength, out int numWritten, out int numRead)
        {
            lock (sdaPort)
            {
                lock (SoftwareI2CTimeoutList)
                {
                    timeout      = false;
                    timeoutCount = 0;        // Enable timeout checking for this port
                }

                numWritten = 0;
                numRead    = 0;

                if (writeLength != 0)
                {
                    // The clock pin should be pulled high - if not, there is a short on the bus
                    // or a nonresponsive device, etc.
                    if (!sclPort.Read())
                    {
                        throw new ApplicationException("Software I2C: clock signal on socket " + socket + " is being held low.");
                    }

                    // Generate the start pulse
                    sdaPort.Mode = IOMode.Output;
                    sclPort.Mode = IOMode.Output;

                    // Write the address and data bytes to the device (low order address bit is 0 for write)
                    if (WriteByte((byte)(address << 1)))
                    {
                        for (int index = writeOffset; index < writeOffset + writeLength; index++)
                        {
                            if (!WriteByte(writeBuffer[index]))
                            {
                                break;
                            }
                            numWritten++;
                        }
                    }

                    if (readLength == 0 || numWritten != writeLength)
                    {
                        // send stop pulse if not reading, or if write failed
                        sclPort.Mode = IOMode.Input;                    // Allow clock pin to float high
                        while (!sclPort.Read() && !timeout)
                        {
                            ;                                           // Wait for the clock pin to go high
                        }
                        sdaPort.Mode = IOMode.Input;                    // Allow data pin to float high
                    }
                    else
                    {
                        // set up for repeated start condition;
                        sdaPort.Mode = IOMode.Input;
                        while (!sdaPort.Read() && !timeout)
                        {
                            ;
                        }
                        sclPort.Mode = IOMode.Input;
                    }
                }

                if (timeout)
                {
                    throw new ApplicationException("Software I2C: clock signal on socket " + socket + " is being held low.");
                }

                if (numWritten == writeLength && readLength != 0)
                {
                    int limit = (readOffset + readLength) - 1;

                    // The clock pin should be pulled high
                    // If it is not, the bus is shorted or a device is nonresponsive
                    if (!sclPort.Read())
                    {
                        throw new ApplicationException("Software I2C: clock signal on socket " + socket + " is being held low.");
                    }

                    // Generate the start pulse
                    sdaPort.Mode = IOMode.Output;
                    sclPort.Mode = IOMode.Output;

                    // Write the address and then read the data bytes from the device (low order address bit is 1 for read)
                    if (WriteByte((byte)((address << 1) | 1)))
                    {
                        int lastIndex = readOffset + readLength - 1;
                        for (int index = readOffset; index < readOffset + readLength; index++)
                        {
                            if (!ReadByte(index == lastIndex, out readBuffer[index]))
                            {
                                break;
                            }
                            numRead++;
                        }
                    }

                    // Generate the stop pulse
                    sclPort.Mode = IOMode.Input;               // Release the clock line
                    while (!sclPort.Read() & !timeout)
                    {
                        ;                                      // Wait for the clock line to go high
                    }
                    sdaPort.Mode = IOMode.Input;               // Release the data line
                }

                if (timeout)
                {
                    throw new ApplicationException("Software I2C: clock signal on socket " + socket + " is being held low.");
                }

                lock (SoftwareI2CTimeoutList)
                {
                    timeoutCount = -1;       // Disable timeout checking for this port
                }
            }
        }