예제 #1
0
        // Read a byte from sensor
        private static byte ReadByte(bool sendAck)
        {
            byte resp = 0x00;

            if (_dataPort.Active)
            {
                _dataPort.Active = false;
            }

            // read bits from MSB
            for (int i = 7; i >= 0; i--)
            {
                _clkPort.Write(true);
                resp |= (_dataPort.Read()) ? (byte)(1 << i) : (byte)0;
                _clkPort.Write(false);
            }

            // set data port as output
            _dataPort.Active = true;
            // send or not ACK
            _dataPort.Write(!sendAck);
            _clkPort.Write(true);
            _clkPort.Write(false);
            _dataPort.Write(true);

            return(resp);
        }
예제 #2
0
        private int GetDistance()
        {
            // First we need to pulse the port from high to low.
            _port.Active = true;  // Put port in write mode
            _port.Write(true);    // Pulse pin
            _port.Write(false);
            _port.Active = false; // Put port in read mode;

            var lineState = false;

            // Wait for the line to go high, for start of pulse.
            while (lineState == false)
            {
                lineState = _port.Read();
            }

            var startOfPulseAt = DateTime.Now.Ticks; // Save start ticks.

            // Wait for line to go low.
            while (lineState)
            {
                lineState = _port.Read();
            }

            var endOfPulse = DateTime.Now.Ticks; // Save end ticks.

            var ticks = (int)(endOfPulse - startOfPulseAt);

            return(ticks / 580);
        }
예제 #3
0
        private bool WriteByte(byte data)
        {
            bool writtenOK = false;
            byte lastBit   = 0;          // Assume that the data pin is currently driving low
            byte nextBit;

            // This routine assumes that the clock and data are both driving low
            // Data is transmitted MSB first
            for (int bit = 0; bit < 8; bit++)
            {
                nextBit = (byte)(data & 0x80);
                if (nextBit != lastBit)    // If the state of the data line must change
                {
                    if (nextBit == 0)      // If the data pin must now drive low
                    {
                        sdaPort.Active = true;
                    }
                    else
                    {
                        sdaPort.Active = false;
                    }
                    lastBit = nextBit;
                }
                data         <<= 1;
                sclPort.Active = false;                    // Let clock line go high
                while (!sclPort.Read() && !timeout)
                {
                    ;                                      // Wait until the clock line actually goes high
                }
                sclPort.Active = true;                     // Drive the clock back low
            }

            // Check the acknowledge (9th) bit
            if (lastBit == 0)
            {
                sdaPort.Active = false;                // Change the data pin to an input (stop driving it if it was being driven)
            }
            sclPort.Active = false;                    // Let the clock line go high
            while (!sclPort.Read() && !timeout)
            {
                ;                                      // Wait for the clock line to actually go high
            }
            writtenOK      = !sdaPort.Read();          // The data line must be driven low by the device to acknowledge the data
            sclPort.Active = true;                     // Drive the clock back low

            // Leave the data pin in an expected state (low)
            sdaPort.Active = true;

            return(writtenOK && !timeout);
        }
예제 #4
0
        public long ReadRightIR()
        {
            // Turn on IR LEDs (optional)
            LEDIREmmitter.Write(true);

            // Make the I/O line connected to that sensor an output and drive it high
            RightIR.Active = true;   // Set to output pin
            RightIR.Write(true);     // Set pin to high to charge capacitor

            // Wait several microseconds to give the 1 nF capacitor node time to reach 5 V
            Thread.Sleep(5);  // Wait about 10 milliseconds to allow capacitor to charge

            // Take starting time
            StartTicks = System.DateTime.Now.Ticks;

            // Make the I/O line an input (with internal pull-up disabled)
            RightIR.Active = false;    // Set to input pin

            // Measure the time for the capacitor node to discharge by waiting for the I/O line to go low
            while (RightIR.Read() == true)
            {
            }

            //StopTicks = new tick count;
            StopTicks = System.DateTime.Now.Ticks;

            // If time is less than the threshold value, then interpret result as a detection of a white line
            TickDiff = StopTicks - StartTicks;

            // Turn off IR LEDs (optional)
            LEDIREmmitter.Write(false);

            return(TickDiff);
        }
        public static void Main()
        {
            TristatePort tristatePort = new TristatePort(Cpu.Pin.GPIO_Pin4,
                                                         false, //initial state
                                                         false, //no glitch filter
                                                         Port.ResistorMode.PullUp);

            Debug.Print("Port is inactive and acts as input port.");
            Debug.Print("Input = " + tristatePort.Read());

            tristatePort.Active = true;
            Debug.Print("Port is active and acts as output port.");
            tristatePort.Write(true);
        }
예제 #6
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.Active = true;
                    sclPort.Active = true;

                    // 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.Active = false;                    // Allow clock pin to float high
                        while (!sclPort.Read() && !timeout)
                        {
                            ;                                      // Wait for the clock pin to go high
                        }
                        sdaPort.Active = false;                    // Allow data pin to float high
                    }
                    else
                    {
                        // set up for repeated start condition;
                        sdaPort.Active = false;
                        while (!sdaPort.Read() && !timeout)
                        {
                            ;
                        }
                        sclPort.Active = false;
                    }
                }

                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.Active = true;
                    sclPort.Active = true;

                    // 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.Active = false;               // Release the clock line
                    while (!sclPort.Read() & !timeout)
                    {
                        ;                                 // Wait for the clock line to go high
                    }
                    sdaPort.Active = false;               // 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
                }
            }
        }
예제 #7
0
        public int read()
        {
            const int buffersize = 5;

            int[] buffer = new int[buffersize];
            int   cnt    = 7;
            int   idx    = 0;

            //clear buffer
            for (int i = 0; i < buffersize; i++)
            {
                buffer[i] = 0;
            }



            //Write frame delimiter to sensor
            Triport.Active = true;
            Triport.Write(false);
            Thread.Sleep(18); //sleep 18 miliseconds
            Triport.Write(true);

            //Now switch port to input mode and wait for signal
            long time = Microseconds();

            //(40) ~= 61 microseconds
            //DelayMicroseconds(1);
            long delta = Microseconds() - time;

            delta = Microseconds() - time;

            Debug.Print((delta / 10).ToString());


            Triport.Active = false; //set tristate to disabled

            //InputPort Inport = new InputPort(IOPin,true,

            // Countdown values are pulled from arduiono code, may require tuning
            int loopcount = 10000;

            while (Triport.Read() == false)
            {
                if (loopcount-- == 0)
                {
                    return(-2);
                }
            }

            loopcount = 10000;
            while (Triport.Read() == true)
            {
                if (loopcount-- == 0)
                {
                    return(-3);
                }
            }


            //read 40 bits or timeout
            for (int i = 0; i < 40; i++)
            {
                loopcount = 10000;
                while (Triport.Read() == false)
                {
                    if (loopcount-- == 0)
                    {
                        return(-4);
                    }
                }

                long t = Microseconds();

                loopcount = 10000;
                while (Triport.Read() == true)
                {
                    if (loopcount-- == 0)
                    {
                        return(-5);
                    }
                }


                if ((Microseconds() - t) > 40)
                {
                    buffer[idx] |= (1 << cnt);
                }
                if (cnt == 0) //next byte?
                {
                    cnt = 7;
                    idx++;
                }
                else
                {
                    cnt--;
                }
            }

            //write to vars
            humidity    = buffer[0];
            temperature = buffer[2];
            int sum = buffer[0] + buffer[2];

            if (buffer[4] != sum)
            {
                return(-1);
            }
            return(0);
        }
예제 #8
0
 bool ReadSCL()
 {
     MakePinInput(scl);
     return(scl.Read());
 }
        public override bool Read()
        {
            Mode = Socket.SocketInterfaces.IOMode.Input;

            return(_port.Read());
        }