Raspberry.IO.InterIntegratedCircuit.I2cDriver.I2cDriver C# (CSharp) Method

I2cDriver() public method

Initializes a new instance of the I2cDriver class.
public I2cDriver ( ProcessorPin sdaPin, ProcessorPin sclPin ) : System
sdaPin ProcessorPin The SDA pin.
sclPin ProcessorPin The SCL pin.
return System
        public I2cDriver(ProcessorPin sdaPin, ProcessorPin sclPin)
        {
            this.sdaPin = sdaPin;
            this.sclPin = sclPin;

            var bscBase = GetBscBase(sdaPin, sclPin);

            var memoryFile = Interop.open("/dev/mem", Interop.O_RDWR + Interop.O_SYNC);
            try
            {
                gpioAddress = Interop.mmap(
                    IntPtr.Zero,
                    Interop.BCM2835_BLOCK_SIZE,
                    Interop.PROT_READ | Interop.PROT_WRITE,
                    Interop.MAP_SHARED,
                    memoryFile,
                    GetProcessorGpioAddress(Board.Current.Processor));

                bscAddress = Interop.mmap(
                    IntPtr.Zero,
                    Interop.BCM2835_BLOCK_SIZE,
                    Interop.PROT_READ | Interop.PROT_WRITE,
                    Interop.MAP_SHARED,
                    memoryFile,
                    bscBase);
            }
            finally
            {
                Interop.close(memoryFile);
            }

            if (bscAddress == (IntPtr) Interop.MAP_FAILED)
                throw new InvalidOperationException("Unable to access device memory");

            // Set the I2C pins to the Alt 0 function to enable I2C access on them
            // remembers if the values were actually changed to clear them or not upon dispose
            wasSdaPinSet = SetPinMode((uint)(int)sdaPin, Interop.BCM2835_GPIO_FSEL_ALT0); // SDA
            wasSclPinSet = SetPinMode((uint) (int) sclPin, Interop.BCM2835_GPIO_FSEL_ALT0); // SCL

            // Read the clock divider register
            var dividerAddress = bscAddress + (int) Interop.BCM2835_BSC_DIV;
            var divider = (ushort) SafeReadUInt32(dividerAddress);
            waitInterval = GetWaitInterval(divider);

            var addressAddress = bscAddress + (int) Interop.BCM2835_BSC_A;
            SafeWriteUInt32(addressAddress, (uint) currentDeviceAddress);
        }