Ejemplo n.º 1
0
        private void ADS1115ReadValuesNative(byte busId, byte i2cAddress, byte[] configDataChunk, byte[] convertDataChunk)
        {
            System.Device.I2c.I2cConnectionSettings address = new System.Device.I2c.I2cConnectionSettings(busId, i2cAddress);
            System.Device.I2c.Drivers.UnixI2cDevice ads1115 = new System.Device.I2c.Drivers.UnixI2cDevice(address);
            var buffer = new byte[2];

            // Write config register to the ADC
            ads1115.Write(configDataChunk);

            ads1115.WriteByte(ADS1115.ADS1115_REG_POINTER_CONFIG);
            ads1115.Read(buffer);
            //var currentConfig = ToWord(buffer);
            //Console.WriteLine($"New configuration: {currentConfig.ToString("X")}");

            DateTime firstDataRead = DateTime.UtcNow;

            while (true)
            {
                // Read the conversion results
                ads1115.Write(convertDataChunk);
                ads1115.Read(buffer);
                ushort result  = ToWord(buffer);
                float  voltage = (float)(result < 32768 ? result : -(65536 - result)) * 2.048f / 65536.0f;

                j++;
                if (j % 1000 == 0)
                {
                    Console.WriteLine(String.Format("| ADC value: {0,5} V | sample rate: {1,7} SPS |", voltage.ToString("N", CultureInfo.InvariantCulture), ((double)j / (DateTime.UtcNow - firstDataRead).TotalSeconds).ToString("N")));
                }
            }
        }
Ejemplo n.º 2
0
        private void ReadDeviceRegisters()
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f");
            stringBuilder.Append(Environment.NewLine);

            var connectionSettings = new I2cConnectionSettings(BusId, DeviceAddress);
            using (var i2cDevice = new System.Device.I2c.Drivers.UnixI2cDevice(connectionSettings))
            {
                for (int startingRowAddress = 0; startingRowAddress < 255; startingRowAddress += 16)
                {
                    stringBuilder.Append($"{startingRowAddress:x2}: ");  // Beginning of row.

                    for (int rowAddress = 0; rowAddress < 16; rowAddress++)
                    {
                        int registerAddress = startingRowAddress + rowAddress;

                        // Skip the unwanted addresses.
                        if (registerAddress < FirstRegister || registerAddress > LastRegister)
                        {
                            stringBuilder.Append("   ");
                            continue;
                        }

                        i2cDevice.WriteByte((byte)registerAddress);
                        byte data = i2cDevice.ReadByte();
                        stringBuilder.Append($"{data:x2} ");
                    }

                    stringBuilder.Append(Environment.NewLine);
                }
            }

            Console.WriteLine(stringBuilder.ToString());
        }