Пример #1
0
        public void tb(PigpiodIf pigpiodIf, CancellationToken ct)
        {
            int    h, e, b;
            string exp;
            string buf;

            byte[] bytes = new byte[128];

            Console.WriteLine("\r\nSMBus / I2C tests.");

            /* this test requires an ADXL345 on I2C bus 1 addr 0x53 */

            h = pigpiodIf.i2c_open(1, 0x53, 0);
            CHECK(11, 1, h, 0, 0, "i2c open", ct);

            e = pigpiodIf.i2c_write_device((UInt32)h, new byte[] { 0x00 });             /* move to known register */
            CHECK(11, 2, e, 0, 0, "i2c write device", ct);

            bytes = new byte[1];
            b     = pigpiodIf.i2c_read_device((UInt32)h, bytes);
            CHECK(11, 3, b, 1, 0, "i2c read device", ct);
            CHECK(11, 4, bytes[0], 0xE5, 0, "i2c read device", ct);

            b = pigpiodIf.i2c_read_byte((UInt32)h);
            CHECK(11, 5, b, 0xE5, 0, "i2c read byte", ct);

            b = pigpiodIf.i2c_read_byte_data((UInt32)h, 0);
            CHECK(11, 6, b, 0xE5, 0, "i2c read byte data", ct);

            b = pigpiodIf.i2c_read_byte_data((UInt32)h, 48);
            CHECK(11, 7, b, 2, 0, "i2c read byte data", ct);

            exp   = "\x1D[aBcDeFgHjKM]";
            bytes = System.Text.Encoding.UTF8.GetBytes(exp);

            e = pigpiodIf.i2c_write_device((UInt32)h, bytes);
            CHECK(11, 8, e, 0, 0, "i2c write device", ct);

            e     = pigpiodIf.i2c_write_device((UInt32)h, new byte[] { 0x1D });
            bytes = new byte[bytes.Length - 1];
            b     = pigpiodIf.i2c_read_device((UInt32)h, bytes);
            buf   = System.Text.Encoding.UTF8.GetString(bytes);
            CHECK(11, 9, b, buf.Length, 0, "i2c read device", ct);
            CHECK(11, 10, string.Compare(buf, 0, exp, 1, buf.Length - 1), 0, 0, "i2c read device", ct);

            if (string.Compare(buf, 0, exp, 1, buf.Length - 1) != 0)
            {
                Console.WriteLine("got [{0}] expected [{1}]", buf, exp.Substring(1));
            }

            e = pigpiodIf.i2c_write_byte_data((UInt32)h, 0x1d, 0xAA);
            CHECK(11, 11, e, 0, 0, "i2c write byte data", ct);

            b = pigpiodIf.i2c_read_byte_data((UInt32)h, 0x1d);
            CHECK(11, 12, b, 0xAA, 0, "i2c read byte data", ct);

            e = pigpiodIf.i2c_write_byte_data((UInt32)h, 0x1d, 0x55);
            CHECK(11, 13, e, 0, 0, "i2c write byte data", ct);

            b = pigpiodIf.i2c_read_byte_data((UInt32)h, 0x1d);
            CHECK(11, 14, b, 0x55, 0, "i2c read byte data", ct);

            exp   = "[1234567890#]";
            bytes = System.Text.Encoding.UTF8.GetBytes(exp);

            e = pigpiodIf.i2c_write_block_data((UInt32)h, 0x1C, bytes);
            CHECK(11, 15, e, 0, 0, "i2c write block data", ct);

            e   = pigpiodIf.i2c_write_device((UInt32)h, new byte[] { 0x1D });
            b   = pigpiodIf.i2c_read_device((UInt32)h, bytes);
            buf = System.Text.Encoding.UTF8.GetString(bytes);
            CHECK(11, 16, b, buf.Length, 0, "i2c read device", ct);
            CHECK(11, 17, string.Compare(buf, 0, exp, 0, buf.Length), 0, 0, "i2c read device", ct);

            if (string.Compare(buf, 0, exp, 0, buf.Length) != 0)
            {
                Console.WriteLine("got [{0}] expected [{1}]", buf, exp);
            }

            b   = pigpiodIf.i2c_read_i2c_block_data((UInt32)h, 0x1D, bytes);
            buf = System.Text.Encoding.UTF8.GetString(bytes);
            CHECK(11, 18, b, buf.Length, 0, "i2c read i2c block data", ct);
            CHECK(11, 19, string.Compare(buf, 0, exp, 0, buf.Length), 0, 0, "i2c read i2c block data", ct);

            if (string.Compare(buf, 0, exp, 0, buf.Length) != 0)
            {
                Console.WriteLine("got [{0}] expected [{1}]", buf, exp);
            }

            exp   = "(-+=;:,<>!%)";
            bytes = System.Text.Encoding.UTF8.GetBytes(exp);

            e = pigpiodIf.i2c_write_i2c_block_data((UInt32)h, 0x1D, bytes);
            CHECK(11, 20, e, 0, 0, "i2c write i2c block data", ct);

            b   = pigpiodIf.i2c_read_i2c_block_data((UInt32)h, 0x1D, bytes);
            buf = System.Text.Encoding.UTF8.GetString(bytes);
            CHECK(11, 21, b, buf.Length, 0, "i2c read i2c block data", ct);
            CHECK(11, 22, string.Compare(buf, 0, exp, 0, buf.Length), 0, 0, "i2c read i2c block data", ct);

            if (string.Compare(buf, 0, exp, 0, buf.Length) != 0)
            {
                Console.WriteLine("got [{0}] expected [{1}]", buf, exp);
            }

            e = pigpiodIf.i2c_close((UInt32)h);
            CHECK(11, 23, e, 0, 0, "i2c close", ct);
        }