Ejemplo n.º 1
0
        /// <summary>
        /// Write a GrovePi command
        /// </summary>
        /// <param name="command">The GrovePi command</param>
        /// <param name="pin">The pin to write the command</param>
        /// <param name="param1">First parameter</param>
        /// <param name="param2">Second parameter</param>
        public void WriteCommand(GrovePiCommand command, GrovePort pin, byte param1, byte param2)
        {
            Span <byte> outArray = stackalloc byte[4]
            {
                (byte)command, (byte)(pin), param1, param2
            };
            byte        tries   = 0;
            IOException innerEx = null;

            // When writing/reading to the I2C port, GrovePi doesn't respond on time in some cases
            // So we wait a little bit before retrying
            // In most cases, the I2C read/write can go thru without waiting
            while (tries < MaxRetries)
            {
                try
                {
                    _i2cDevice.Write(outArray);
                    return;
                }
                catch (IOException ex)
                {
                    // Give it another try
                    innerEx = ex;
                    tries++;
                    Thread.Sleep(10);
                }
            }

            throw new IOException($"{nameof(WriteCommand)}: Failed to write command {command}", innerEx);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Read data from GrovePi
        /// </summary>
        /// <param name="command">The GrovePi command</param>
        /// <param name="pin">The pin to read</param>
        /// <returns></returns>
        public byte[] ReadCommand(GrovePiCommand command, GrovePort pin)
        {
            int numberBytesToRead = 0;

            switch (command)
            {
            case GrovePiCommand.DigitalRead:
                numberBytesToRead = 1;
                break;

            case GrovePiCommand.AnalogRead:
            case GrovePiCommand.UltrasonicRead:
            case GrovePiCommand.LetBarGet:
                numberBytesToRead = 3;
                break;

            case GrovePiCommand.Version:
                numberBytesToRead = 4;
                break;

            case GrovePiCommand.DhtTemp:
                numberBytesToRead = 9;
                break;

            // No other commands are for read
            default:
                return(null);
            }

            byte[]      outArray = new byte[numberBytesToRead];
            byte        tries    = 0;
            IOException innerEx  = null;

            // When writing/reading the I2C port, GrovePi doesn't respond on time in some cases
            // So we wait a little bit before retrying
            // In most cases, the I2C read/write can go thru without waiting
            while (tries < MaxRetries)
            {
                try
                {
                    _i2cDevice.Read(outArray);
                    return(outArray);
                }
                catch (IOException ex)
                {
                    // Give it another try
                    innerEx = ex;
                    tries++;
                    Thread.Sleep(10);
                }
            }

            throw new IOException($"{nameof(ReadCommand)}: Failed to write command {command}", innerEx);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Read data from GrovePi
        /// </summary>
        /// <param name="command">The GrovePi command</param>
        /// <param name="pin">The pin to read</param>
        /// <returns></returns>
        public byte[]? ReadCommand(GrovePiCommand command, GrovePort pin)
        {
            int numberBytesToRead = command switch
            {
                GrovePiCommand.DigitalRead => 1,
                GrovePiCommand.AnalogRead or GrovePiCommand.UltrasonicRead or GrovePiCommand.LetBarGet => 3,
                GrovePiCommand.Version => 4,
                GrovePiCommand.DhtTemp => 9,
                _ => 0,
            };

            if (numberBytesToRead == 0)
            {
                return(null);
            }

            byte[]      outArray = new byte[numberBytesToRead];
            byte        tries    = 0;
            IOException?innerEx  = null;

            // When writing/reading the I2C port, GrovePi doesn't respond on time in some cases
            // So we wait a little bit before retrying
            // In most cases, the I2C read/write can go thru without waiting
            while (tries < MaxRetries)
            {
                try
                {
                    _i2cDevice.Read(outArray);
                    return(outArray);
                }
                catch (IOException ex)
                {
                    // Give it another try
                    innerEx = ex;
                    tries++;
                    Thread.Sleep(10);
                }
            }

            throw new IOException($"{nameof(ReadCommand)}: Failed to write command {command}", innerEx);
        }