/// <summary>
 /// Construct an I2cMux with a given muxed port
 /// </summary>
 /// <param name="UpstreamPort">The upstream port to mux</param>
 /// <param name="numPorts">The number of ports that belong to this mux</param>
 public I2cMux(I2c UpstreamPort, int numPorts)
 {
     this.UpstreamPort = UpstreamPort;
     UpstreamPort.Enabled = true;
     for (int i = 0; i < numPorts; i++)
         Ports.Add(new I2cMuxPort(this, i));
 }
        /// <summary>
        /// Construct an MPU9150 9-Dof IMU
        /// </summary>
        /// <param name="i2c">The I2c module this module is connected to.</param>
        /// <param name="addressPin">The address of the module</param>
        public Mpu6050(I2c i2c, bool addressPin = false, int ratekHz = 400)
        {
            this.dev = new SMBusDevice((byte)(addressPin ? 0x69 : 0x68), i2c, ratekHz);

            byte result = dev.ReadByteData((byte)Registers.WHO_AM_I).Result;

            if (result != 0x71)
                throw new Exception("Incorrect part number attached to bus");


            dev.WriteByteData((byte)Registers.PWR_MGMT_1, 0x00).Wait(); // wake up
            Task.Delay(100).Wait();
            dev.WriteByteData((byte)Registers.PWR_MGMT_1, 0x01).Wait(); // Auto select clock source to be PLL gyroscope reference if ready else
            Task.Delay(200).Wait();
            dev.WriteByteData((byte)Registers.CONFIG, 0x03).Wait();
            dev.WriteByteData((byte)Registers.SMPLRT_DIV, 0x04).Wait();

            AccelerometerScale = AccelScale.AFS_2G;
            GyroscopeScale = GyroScale.GFS_250DPS;

            var c = dev.ReadByteData((byte)Registers.ACCEL_CONFIG2).Result;
            c &= ~0x0f & 0xff;
            c |= 0x03;
            dev.WriteByteData((byte)Registers.ACCEL_CONFIG2, c).Wait();
        }
Example #3
0
        public void devices_list_parsed()
        {
            // Arrange
            var i2cdetectResult = "     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f\n" +
                                  "00:          -- -- -- -- -- -- -- -- -- -- -- -- --\n" +
                                  "10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\n" +
                                  "20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\n" +
                                  "30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- -- --\n" +
                                  "40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\n" +
                                  "50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\n" +
                                  "60: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --\n" +
                                  "70: -- -- -- -- -- -- -- --\n";

            var processRunner = Substitute.For <IProcessRunner>();

            processRunner.Run("i2cdetect", "-y 1").Returns(i2cdetectResult);

            // Act
            var i2c        = new I2c(processRunner, null);
            var i2cDevices = i2c.GetI2cDevices();

            i2cDevices.Wait();

            // Assert
            Assert.Collection(i2cDevices.Result, id => Assert.Equal(0x3c, id));
        }
        /// <summary>
        /// Create a ADJDS311 device
        /// </summary>
        /// <param name="I2CDevice">The I2c port to use</param>
        /// <param name="LedPin">The pin attached to the led</param>
        public Adjds311(I2c I2CDevice, DigitalOutPin LedPin) : base(0x74, I2CDevice)
        {
            _I2C = I2CDevice;
            _I2C.Enabled = true;

            led = LedPin;
            led.MakeDigitalPushPullOut();

            WriteByteData(CAP_RED, 15);
            WriteByteData(CAP_GREEN, 15);
            WriteByteData(CAP_BLUE, 15);
            WriteByteData(CAP_CLEAR, 15);

            WriteByteData(INT_RED_LO, 0x00);
            WriteByteData(INT_RED_HI, 0x4);
            WriteByteData(INT_GREEN_LO, 0x00);
            WriteByteData(INT_GREEN_HI, 0x5);
            WriteByteData(INT_BLUE_LO, 0x00);
            WriteByteData(INT_BLUE_HI, 0x9);
            WriteByteData(INT_CLEAR_LO, 0x00);
            WriteByteData(INT_CLEAR_HI, 0x2);
            
            

        }
Example #5
0
        /// <summary>
        /// Reads data from the specified address.
        /// </summary>
        /// <param name="address">The address.</param>
        /// <param name="count">The count. Recommended 32 as maximum.</param>
        /// <returns>The byte array that was read.</returns>
        public byte[] Read(byte address, int count)
        {
            var data = new byte[7];

            data[0] = 0x04;                  // Set Address
            data[1] = address;               // Address Literal
            data[2] = 0x02;                  // Start Condition
            data[3] = 0x06;                  // Write Command
            data[4] = Convert.ToByte(count); // data length;
            data[5] = 0x03;                  // Stop condition
            data[6] = 0x00;                  // End command;

            var output   = new byte[count];
            var outCount = BoardException.ValidateResult(
                I2c.BbI2CZip(Handle, data, Convert.ToUInt32(data.Length), output, Convert.ToUInt32(output.Length)));

            if (output.Length == outCount)
            {
                return(output);
            }

            var result = new byte[outCount];

            Buffer.BlockCopy(output, 0, result, 0, outCount);
            return(result);
        }
Example #6
0
        private bool _isDisposed; // To detect redundant calls

        /// <summary>
        /// Initializes a new instance of the <see cref="SoftI2cBus"/> class.
        /// </summary>
        /// <param name="dataPin">The data pin.</param>
        /// <param name="clockPin">The clock pin.</param>
        /// <param name="baudRate">The baud rate.</param>
        internal SoftI2cBus(GpioPin dataPin, GpioPin clockPin, int baudRate)
        {
            BoardException.ValidateResult(I2c.BbI2COpen((UserGpio)dataPin.PinNumber, (UserGpio)clockPin.PinNumber, Convert.ToUInt32(baudRate)));
            Handle   = (UserGpio)dataPin.PinNumber;
            DataPin  = dataPin;
            ClockPin = clockPin;
            BaudRate = baudRate;
        }
Example #7
0
        public double Read(Ltc2305Channels channelNumber = Ltc2305Channels.Channel0)
        {
            //  SendReceive(byte address, byte[] dataToWrite, byte numBytesToRead)
            byte[] data   = I2c.SendReceive(this.address, new byte[] { (byte)((byte)channelNumber | (byte)Ltc2305ConfigBits.UnipolarMode) }, 2);
            int    code   = data[1] | (data[0] << 8);
            double retVal = CodeToVoltage(code, 5.0, Ltc2305ConfigBits.UnipolarMode);

            return(retVal);
        }
 /// <summary>
 /// Construct a TSL2591 ambient light sensor
 /// </summary>
 /// <param name="I2cModule">The I2c module this sensor is attached to</param>
 public Tsl2591(I2c I2cModule) : base(0x29, I2cModule, 100)
 {
     var t = ReadByteData(CommandBit | (byte)Registers.DeviceId);
     t.Wait();
     byte retVal = t.Result;
     if(retVal != 0x50)
     {
         throw new Exception("TSL2591 not found on bus. Check your connections");
     }
 }
Example #9
0
        // When the display powers up, it is configured as follows:
        //
        // 1. Display clear
        // 2. Function set:
        //    DL = 1; 8-bit interface data
        //    N = 0; 1-line display
        //    F = 0; 5x8 dot character font
        // 3. Display on/off control:
        //    D = 0; Display off
        //    C = 0; Cursor off
        //    B = 0; Blinking off
        // 4. Entry mode set:
        //    I/D = 1; Increment by 1
        //    S = 0; No shift
        //
        // Note, however, that resetting the Arduino doesn't reset the LCD, so we
        // can't assume that its in that state when a sketch starts (and the
        // LiquidCrystal ructor is called).

        public I2cCharLcd(TreehopperUSB board, int lcd_Addr, int lcd_cols, int lcd_rows)
        {
            _I2C          = board.I2C;
            _Addr         = lcd_Addr;
            _cols         = lcd_cols;
            _rows         = lcd_rows;
            _backlightval = LCD_NOBACKLIGHT;

            _I2C.Start();
            _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
            begin(_cols, _rows);
        }
Example #10
0
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="alsoManaged"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        private void Dispose(bool alsoManaged)
        {
            if (_isDisposed)
            {
                return;
            }
            _isDisposed = true;

            if (alsoManaged)
            {
                I2c.BbI2CClose(Handle);
            }
        }
        /// <summary>
        /// Construct a PCF-series I/O expander
        /// </summary>
        /// <param name="i2c">the I2c module to use</param>
        /// <param name="numPins">The number of pins of this expander</param>
        /// <param name="Address0">The state of the Address0 pin</param>
        /// <param name="Address1">The state of the Address1 pin</param>
        /// <param name="Address2">The state of the Address2 pin</param>
        /// <param name="baseAddress">The base address of the chip</param>
        public PcfInterface(I2c i2c, int numPins, bool Address0, bool Address1, bool Address2, byte baseAddress) : base(numPins)
        {
            byte address = (byte)(baseAddress | (Address0 ? 1 : 0) | (Address1 ? 1 : 0) << 1 | (Address2 ? 1 : 0) << 2);
            dev = new SMBusDevice(address, i2c);

            numBytes = numPins / 8;

            oldValues = new byte[numBytes];
            newValues = new byte[numBytes];
            
            // make all pins inputs by default
            AutoFlush = false;
            foreach (var pin in Pins)
                pin.Mode = InputOutputPinMode.DigitalInput;
            AutoFlush = true;
            Flush(true).Wait();

        }
Example #12
0
        /// <summary>
        /// Writes data to the specified address.
        /// </summary>
        /// <param name="address">The address.</param>
        /// <param name="buffer">The buffer. Recommended 32 bytes max.</param>
        public void Write(byte address, byte[] buffer)
        {
            var data = new byte[7 + buffer.Length];

            data[0] = 0x04;                        // Set Address
            data[1] = address;                     // Address Literal
            data[2] = 0x02;                        // Start Condition
            data[3] = 0x07;                        // Write Command
            data[4] = Convert.ToByte(data.Length); // data length;
            data[data.Length - 2] = 0x03;          // Stop condition
            data[data.Length - 1] = 0x00;          // End command;

            // copy buffer data to command
            Buffer.BlockCopy(buffer, 0, data, 5, data.Length);

            var output = new byte[32];

            BoardException.ValidateResult(
                I2c.BbI2CZip(Handle, data, Convert.ToUInt32(data.Length), output, Convert.ToUInt32(output.Length)));
        }
        public ColorSensor_ADJDS311(I2c I2CDevice, Pin LedPin) : base(0x74, I2CDevice)
        {
            _I2C = I2CDevice;
            _I2C.Start();

            led = LedPin;
            led.MakeDigitalOutput();

            WriteByteData(CAP_RED, 15);
            WriteByteData(CAP_GREEN, 15);
            WriteByteData(CAP_BLUE, 15);
            WriteByteData(CAP_CLEAR, 15);

            WriteByteData(INT_RED_LO, 0x00);
            WriteByteData(INT_RED_HI, 0x4);
            WriteByteData(INT_GREEN_LO, 0x00);
            WriteByteData(INT_GREEN_HI, 0x5);
            WriteByteData(INT_BLUE_LO, 0x00);
            WriteByteData(INT_BLUE_HI, 0x9);
            WriteByteData(INT_CLEAR_LO, 0x00);
            WriteByteData(INT_CLEAR_HI, 0x2);
        }
        public ColorSensor_ADJDS311(I2c I2CDevice, Pin LedPin)
            : base(0x74, I2CDevice)
        {
            _I2C = I2CDevice;
            _I2C.Start();

            led = LedPin;
            led.MakeDigitalOutput();

            WriteByteData(CAP_RED, 15);
            WriteByteData(CAP_GREEN, 15);
            WriteByteData(CAP_BLUE, 15);
            WriteByteData(CAP_CLEAR, 15);

            WriteByteData(INT_RED_LO, 0x00);
            WriteByteData(INT_RED_HI, 0x4);
            WriteByteData(INT_GREEN_LO, 0x00);
            WriteByteData(INT_GREEN_HI, 0x5);
            WriteByteData(INT_BLUE_LO, 0x00);
            WriteByteData(INT_BLUE_HI, 0x9);
            WriteByteData(INT_CLEAR_LO, 0x00);
            WriteByteData(INT_CLEAR_HI, 0x2);
        }
Example #15
0
        // When the display powers up, it is configured as follows:
        //
        // 1. Display clear
        // 2. Function set:
        //    DL = 1; 8-bit interface data
        //    N = 0; 1-line display
        //    F = 0; 5x8 dot character font
        // 3. Display on/off control:
        //    D = 0; Display off
        //    C = 0; Cursor off
        //    B = 0; Blinking off
        // 4. Entry mode set:
        //    I/D = 1; Increment by 1
        //    S = 0; No shift
        //
        // Note, however, that resetting the Arduino doesn't reset the LCD, so we
        // can't assume that its in that state when a sketch starts (and the
        // LiquidCrystal ructor is called).
        public I2cCharLcd(TreehopperUSB board, int lcd_Addr,int lcd_cols,int lcd_rows)
        {
            _I2C = board.I2C;
              _Addr = lcd_Addr;
              _cols = lcd_cols;
              _rows = lcd_rows;
              _backlightval = LCD_NOBACKLIGHT;

              _I2C.Start();
              _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
              begin(_cols, _rows);
        }
        /// <summary>
        /// Construct a new LTC2305
        /// </summary>
        /// <param name="address">The address to use</param>
        /// <param name="I2cModule">The I2c module this ADC is attached to</param>
        public Ltc2305(byte address, I2c I2cModule) : base(address, I2cModule, 100)
        {

        }
 public Mpu9150(I2c i2c, bool addressPin = false, int ratekHz = 400) : base(i2c, addressPin, ratekHz)
 {
     dev.WriteByteData((byte)Registers.INT_PIN_CFG, 0x22).Wait();
     mag = new SMBusDevice(0x0C, i2c, ratekHz);
 }
Example #18
0
 public SMBusDevice(byte address, I2c I2CModule, int rateKHz = 100)
 {
     this.address = address;
     I2c          = I2CModule;
     I2c.Start(rateKHz);
 }
 /// <summary>
 /// Construct a new TM1650 with a given I2c interface
 /// </summary>
 /// <param name="i2c">The I2c interface to use</param>
 public Tm1650(I2c i2c) : base(32, false, false)
 {
     this.i2c = i2c;
     i2c.Enabled = true;
     Enable = true;
 }
Example #20
0
 public Ltc2305(byte address, I2c I2cModule) : base(address, I2cModule, 100)
 {
 }
Example #21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="I2cDevice" /> class.
 /// </summary>
 /// <param name="busId">The bus.</param>
 /// <param name="address">The address.</param>
 internal I2cDevice(I2cBusId busId, byte address)
 {
     Handle  = I2c.I2cOpen(Convert.ToUInt32(busId), address);
     Address = address;
     BusId   = busId;
 }
        /// <summary>
        /// Construct a PCF8574
        /// </summary>
        /// <param name="I2c">The I2c interface to use</param>
        /// <param name="Address0">The state of the Address0 pin</param>
        /// <param name="Address1">The state of the Address1 pin</param>
        /// <param name="Address2">The state of the Address2 pin</param>
        public Pcf8574(I2c I2c, bool Address0 = false, bool Address1 = false, bool Address2 = false) : base(I2c, 8, Address0, Address1, Address2, 0x20)
        {

        }
 public Mlx90615(I2c module)
 {
     this.dev = new SMBusDevice(0x5B, module, 30);
     Object =  new TempRegister(dev, 0x27);
     Ambient = new TempRegister(dev, 0x26);
 }
 /// <summary>
 /// Construct a PCF8575
 /// </summary>
 /// <param name="i2c">The I2c interface to use</param>
 /// <param name="Address0">The state of the Address0 pin</param>
 /// <param name="Address1">The state of the Address1 pin</param>
 /// <param name="Address2">The state of the Address2 pin</param>
 public Pcf8575(I2c i2c, bool Address0 = false, bool Address1 = false, bool Address2 = false) : base(i2c, 16, Address0, Address1, Address2, 0x20)
 {
 }
 /// <summary>
 /// Construct an <see cref="I2cMux"/> using a standard 4052-style two-bit 4:1 mux. 
 /// </summary>
 /// <param name="MuxedPort">The upstream port to mux</param>
 /// <param name="a">The A (S0) input of the 4052</param>
 /// <param name="b">The B (S1) input of the 4052</param>
 public I2cMux4052(I2c MuxedPort, DigitalOutPin a, DigitalOutPin b) : base(MuxedPort, 4)
 {
     this.a = a;
     this.b = b;
 }