Exemple #1
0
        private ushort[] runCommand(SGPCommand cmd, ushort[] parameters = null)
        {
            bus.SetDevice(this.address);

            var bytesToWrite = new byte[cmd.OpCode.Length + (cmd.ParameterLength * (WORD_LENGTH + CRC_LENGTH))];

            if (cmd.ParameterLength > 0 && parameters?.Length != cmd.ParameterLength)
            {
                throw new ArgumentException(nameof(parameters));
            }

            Array.Copy(cmd.OpCode, 0, bytesToWrite, 0, cmd.OpCode.Length);
            if (parameters != null)
            {
                foreach (var i in Enumerable.Range(0, parameters.Length))
                {
                    Span <byte> span = bytesToWrite.AsSpan().Slice(i * (WORD_LENGTH + CRC_LENGTH) + cmd.OpCode.Length, (WORD_LENGTH + CRC_LENGTH));
                    span[0] = (byte)(parameters[i] >> 8);
                    span[1] = (byte)(parameters[i] & 0xFF);
                    span[2] = CRC(span.Slice(0, WORD_LENGTH));
                }
            }
            var argBytes = bytesToWrite.AsSpan(1);

            bus.WriteI2cBlockData(bytesToWrite[0], argBytes.ToArray());
            Thread.Sleep(cmd.ResponseTimeMs);
            if (cmd.ResponseLength > 0)
            {
                var data = new byte[cmd.ResponseLength * (WORD_LENGTH + CRC_LENGTH)];
                bus.ReadI2cBlockData(0, data);
                ushort[] result = ReadCheckSummedWords(data);
                return(result);
            }
            return(Array.Empty <ushort>());
        }