private int GetFifoCount(I2CDevice device) { if (!device.Write(new[] { FIFO_COUNTH })) { return(0); } byte[] buffer; var r = device.Read(2, out buffer); if (r) { return((buffer[0] << 8) | buffer[1]); //Get byte count } return(0); }
internal bool WriteBit(I2CDevice device, byte regAddr, byte bitNum, byte data) { byte[] b; device.Write(new[] { regAddr }); device.Read(1, out b); if (data != 0) { b[0] = (byte)(1 << bitNum); } else { b[0] = (byte)(b[0] & (byte)(~(1 << bitNum))); } return(device.Write(new[] { regAddr, b[0] })); }
internal async Task InitHardware() { try { _ioController = GpioController.GetDefault(); _interruptPin = _ioController.OpenPin(17); _interruptPin.Write(GpioPinValue.Low); _interruptPin.SetDriveMode(GpioPinDriveMode.Input); _interruptPin.ValueChanged += _interruptPin_ValueChanged; _mpu9150 = new I2CDevice((byte)Mpu9150Setup.Address, I2cBusSpeed.FastMode); await _mpu9150.Open(); await Task.Delay(100); // power up _mpu9150.Write((byte)Mpu9150Setup.PowerManagement1, 0x80); // reset the device await Task.Delay(100); _mpu9150.Write((byte)Mpu9150Setup.PowerManagement1, 0x2); _mpu9150.Write((byte)Mpu9150Setup.UserCtrl, 0x04); //reset fifo _mpu9150.Write((byte)Mpu9150Setup.PowerManagement1, 1); // clock source = gyro x _mpu9150.Write((byte)Mpu9150Setup.GyroConfig, 0); // +/- 250 degrees sec, max sensitivity _mpu9150.Write((byte)Mpu9150Setup.AccelConfig, 0); // +/- 2g, max sensitivity _mpu9150.Write((byte)Mpu9150Setup.Config, 1); // 184 Hz, 2ms delay _mpu9150.Write((byte)Mpu9150Setup.SampleRateDiv, 19); // set rate 50Hz _mpu9150.Write((byte)Mpu9150Setup.FifoEnable, 0x78); // enable accel and gyro to read into fifo _mpu9150.Write((byte)Mpu9150Setup.UserCtrl, 0x40); // reset and enable fifo _mpu9150.Write((byte)Mpu9150Setup.InterruptEnable, 0x1); } catch (Exception ex) { Debug.WriteLine(ex); } }
internal async Task InitializeHardware() { try { _ioController = GpioController.GetDefault(); _interruptPin = _ioController.OpenPin(17); _interruptPin.Write(GpioPinValue.Low); _interruptPin.SetDriveMode(GpioPinDriveMode.Input); _interruptPin.ValueChanged += Interrupt; _mpu9150 = new I2CDevice((byte)Mpu9150Setup.Address, I2cBusSpeed.FastMode); await _mpu9150.Open(); await Task.Delay(5); // power up _mpu9150.Write((byte)Mpu9150Setup.PowerManagement1, 0x80);// reset the device await Task.Delay(100); _mpu9150.Write((byte)Mpu9150Setup.PowerManagement1, 0x2 ); _mpu9150.Write((byte)Mpu9150Setup.UserCtrl, 0x04);//reset fifo _mpu9150.Write((byte)Mpu9150Setup.PowerManagement1, 1 ); // clock source = gyro x _mpu9150.Write((byte)Mpu9150Setup.GyroConfig, 0); // +/- 250 degrees sec, max sensitivity _mpu9150.Write((byte)Mpu9150Setup.AccelConfig, 0); // +/- 2g, max sensitivity _mpu9150.Write((byte)Mpu9150Setup.Config, 1);// 184 Hz, 2ms delay _mpu9150.Write((byte)Mpu9150Setup.SampleRateDiv, 19); // set rate 50Hz _mpu9150.Write((byte)Mpu9150Setup.FifoEnable, 0x78); // enable accel and gyro to read into fifo _mpu9150.Write((byte)Mpu9150Setup.UserCtrl, 0x40); // reset and enable fifo _mpu9150.Write((byte)Mpu9150Setup.InterruptEnable, 0x1); } catch (Exception ex) { Debug.WriteLine(ex); } }
internal bool WriteBits(I2CDevice device, byte regAddr, byte bitStart, byte length, byte data) { // 010 value to write // 76543210 bit numbers // xxx args: bitStart=4, length=3 // 00011100 mask byte // 10101111 original value (sample) // 10100011 original & ~mask // 10101011 masked | value byte[] b; device.Write(new[] { regAddr }); if (device.Read(regAddr, out b)) { var mask = (byte)(((1 << length) - 1) << (bitStart - length + 1)); data <<= (bitStart - length + 1); // shift data into correct position data &= mask; // zero all non-important bits in data b[0] &= (byte)(~(mask)); // zero all important bits in existing byte b[0] |= data; // combine data with existing byte return(device.Write(new[] { regAddr, b[0] })); } return(false); }
/// <summary> /// Adafruit 12bit, 16 channel, I2C PWM controller. /// Adapted from the Adafruit library for the Arduino /// </summary> internal Pca9685() { _pca9685 = new I2CDevice(0x40, I2cBusSpeed.FastMode); }
internal Ads1115() { _ads1115 = new I2CDevice(DefaultAddress, I2cBusSpeed.StandardMode); }
private int GetFifoCount(I2CDevice device) { if (!device.Write(new[] {FIFO_COUNTH})) return 0; byte[] buffer; var r = device.Read(2, out buffer); if (r) return (buffer[0] << 8) | buffer[1]; //Get byte count return 0; }
internal bool WriteBits(I2CDevice device, byte regAddr, byte bitStart, byte length, byte data) { // 010 value to write // 76543210 bit numbers // xxx args: bitStart=4, length=3 // 00011100 mask byte // 10101111 original value (sample) // 10100011 original & ~mask // 10101011 masked | value byte[] b; device.Write(new[] { regAddr }); if (device.Read(regAddr, out b)) { var mask = (byte)(((1 << length) - 1) << (bitStart - length + 1)); data <<= (bitStart - length + 1); // shift data into correct position data &= mask; // zero all non-important bits in data b[0] &= (byte)(~(mask)); // zero all important bits in existing byte b[0] |= data; // combine data with existing byte return device.Write(new[] { regAddr, b[0] }); } return false; }
internal bool WriteBit(I2CDevice device, byte regAddr, byte bitNum, byte data) { byte[] b; device.Write(new[] { regAddr }); device.Read(1, out b); if (data != 0) { b[0] = (byte)(1 << bitNum); } else { b[0] = (byte)(b[0] & (byte)(~(1 << bitNum))); } return device.Write(new[] { regAddr, b[0] }); }
internal Mpr121() { _i2CDevice = new I2CDevice(0x5A, I2cBusSpeed.StandardMode); Task.Delay(50).Wait(); }