public Accelerometer(I2CDevice bus, ushort deviceAddress = 0x53, int clockRate = 400) { this.bus = bus; configuration = new I2CDevice.Configuration(deviceAddress, clockRate); ConfigureDevice(); }
public ShieldStudioView() { _device = new I2CDevice(new I2CDevice.Configuration(0x50, 400)); // select configuration register // MAX6953 Table 6 // disable shutdown _device.Execute(new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { 0x04, 0x01 }) }, TIMEOUT); // MAX9653 Table 23 // set Intensity for Digit 0 and 2 // all segments 10 ma _device.Execute(new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { 0x01, 0x33 }) }, TIMEOUT); // MAX9653 Table 24 // set Intensity for Digit 1 and 3 // all segments 10 ma _device.Execute(new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { 0x02, 0x33 }) }, TIMEOUT); // turn on all LEDs in test mode. // MAX6953 Table 22 _device.Execute(new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { 0x07, 0x01 }) }, TIMEOUT); // disable test mode. // MAX6953 Table 22 _device.Execute(new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { 0x07, 0x00 }) }, TIMEOUT); }
public static void Sketch01() { //create I2C object //note that the netmf i2cdevice configuration requires a 7-bit address! It set the 8th R/W bit automatically. I2CDevice.Configuration con = new I2CDevice.Configuration(0x68, 100); I2CDevice MyI2C = new I2CDevice(con); // Create transactions // We need 2 in this example, we are reading from the device // First transaction is writing the "read command" // Second transaction is reading the data I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[2]; // create write buffer (we need one byte) byte[] RegisterNum = new byte[1] { 0x14 }; xActions[0] = I2CDevice.CreateWriteTransaction(RegisterNum); // create read buffer to read the register byte[] RegisterValue = new byte[1]; xActions[1] = I2CDevice.CreateReadTransaction(RegisterValue); // Now we access the I2C bus using a timeout of one second // if the execute command returns zero, the transaction failed (this // is a good check to make sure that you are communicating with the device correctly // and don’t have a wiring issue or other problem with the I2C device) if (MyI2C.Execute(xActions, 1000) == 0) { Debug.Print("Failed to perform I2C transaction"); } else { Debug.Print("Register value: " + RegisterValue[0].ToString()); } }
public static void Main() { //our 10 bit address const ushort address10Bit = 0x1001; //binary 1000000001 = 129 const byte addressMask = 0x78; //reserved address mask 011110XX for 10 bit addressing //first MSB part of address ushort address1 = addressMask | (address10Bit >> 8); //is 7A and contains the two MSB of the 10 bit address I2CDevice.Configuration config = new I2CDevice.Configuration(address1, 100); I2CDevice device = new I2CDevice(config); //second LSB part of address byte address2 = (byte)(address10Bit & 0xFF); //the other 8 bits (LSB) byte[] address2OutBuffer = new byte[] { address2 }; I2CDevice.I2CWriteTransaction addressWriteTransaction = device.CreateWriteTransaction(address2OutBuffer); //prepare buffer to write data byte[] outBuffer = new byte[] { 0xAA }; I2CDevice.I2CWriteTransaction writeTransaction = device.CreateWriteTransaction(outBuffer); //prepare buffer to read data byte[] inBuffer = new byte[4]; I2CDevice.I2CReadTransaction readTransaction = device.CreateReadTransaction(inBuffer); //execute transactions I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { addressWriteTransaction, writeTransaction, readTransaction }; device.Execute(transactions, 100); }
public TSL2561(byte I2CAddress, int ClockinKHz) { I2CConfig = new I2CDevice.Configuration(I2CAddress, ClockinKHz); I2C = new I2CDevice(I2CConfig); // read the ID register. var Actions = new I2CDevice.I2CTransaction[2]; byte[] rx = new byte[1]; Actions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x0a }); Actions[1] = I2CDevice.CreateReadTransaction(rx); if (I2C.Execute(Actions, 1000) == 0) { Debug.Print("Read ID Register failed"); // exit or something } else { Debug.Print("ID value: " + rx[0].ToString()); } // 4 msb must be 0001 for a TSL2561 if ((rx[0] & 0xf0) != 0xa0) { // exit or something } setGain(0x10); Thread.Sleep(5); // Mandatory after each Write transaction !!! }
public static void Main() { var i2c = new I2CDevice(new I2CDevice.Configuration(0x42, 400)); var sensor = new MjTextileSensor.MjTextileSensor(i2c); byte majorVersion; byte minorVersion; sensor.GetVersion(out majorVersion, out minorVersion); Debug.Print("Firmware version is " + majorVersion + "." + minorVersion + "."); var values = new byte[10]; for (; ; ) { int valuesCount = sensor.GetSensorValues(values); if (valuesCount != 10) { Thread.Sleep(1000); continue; } var message = ""; for (int i = 0; i < valuesCount; i++) { message += values[i].ToString() + " "; } Debug.Print(message); Thread.Sleep(1000); } }
/// <summary> /// Turns on the Oscillator. Turns on the Display. Turns off Blinking. Sets Brightness to full. /// </summary> public void Init() { Config = new I2CDevice.Configuration(HT16K33_ADRESS, HT16K33_CLKRATE); Matrix = new I2CDevice(Config); byte[] write = new byte[1]; write[0] = HT16K33_OSC_ON; // IC Oscillator ON byte[] write2 = new byte[1]; write2[0] = HT16K33_DISPLAY_ON; // Display ON I2CDevice.I2CTransaction[] i2cTx = new I2CDevice.I2CTransaction[1]; i2cTx[0] = I2CDevice.CreateWriteTransaction(write); I2CDevice.I2CTransaction[] i2cTx2 = new I2CDevice.I2CTransaction[1]; i2cTx2[0] = I2CDevice.CreateWriteTransaction(write2); Matrix.Execute(i2cTx, Timeout); Matrix.Execute(i2cTx2, Timeout); // initialize DisplayBuffer for (int i = 0; i < 8; i++) { DisplayBuffer[i] = 0x00; } }
void run() { values[0] = values[1] = values[2] = -1; oldValues[0] = oldValues[1] = oldValues[2] = -1; I2CDevice.Configuration configuration = new I2CDevice.Configuration(deviceAddress, 400); I2CDevice adxl345 = new I2CDevice(configuration); writeCommand(adxl345, 0x2d, 0); writeCommand(adxl345, 0x2d, 16); writeCommand(adxl345, 0x2d, 8); // Set the g range to 2g - typical output: 17, -32, 257 (scale = 3.9 => 66 mg, -124 mg, 1002 mg) writeCommand(adxl345, 0x31, 0); // Set the g range to 16g - typical output: 2, -4, 31 (scale = 31.2 => 62.4 mg, -124.8 mg, 967 mg) // writeCommand(adxl345, 0x31, 3); while (true) { readValues(adxl345, values); if ((changed(values[0], oldValues[0])) || (changed(values[1], oldValues[1])) || (changed(values[2], oldValues[2]))) { //Debug.Print("Values: " + values[0] + ", " + values[1] + ", " + values[2]+"\n"); reporter.report(values[0] + "," + values[1] + "," + values[2]+"\n\r"); } oldValues[0] = values[0]; oldValues[1] = values[1]; oldValues[2] = values[2]; } }
public SiliconLabsSI7005(byte deviceId = DeviceIdDefault, int clockRateKHz = ClockRateKHzDefault, int transactionTimeoutmSec = TransactionTimeoutmSecDefault) { this.deviceId = deviceId; this.clockRateKHz = clockRateKHz; this.transactionTimeoutmSec = transactionTimeoutmSec; using (OutputPort i2cPort = new OutputPort(Pins.GPIO_PIN_SDA, true)) { i2cPort.Write(false); Thread.Sleep(250); } using (I2CDevice device = new I2CDevice(new I2CDevice.Configuration(deviceId, clockRateKHz))) { byte[] writeBuffer = { RegisterIdDeviceId }; byte[] readBuffer = new byte[1]; // The first request always fails I2CDevice.I2CTransaction[] action = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(writeBuffer), I2CDevice.CreateReadTransaction(readBuffer) }; if( device.Execute(action, transactionTimeoutmSec) == 0 ) { // throw new ApplicationException("Unable to send get device id command"); } } }
/// <summary> /// Create a new abstract I2C device. /// </summary> /// <param name="Address">I2C address.</param> /// <param name="ClockRateKhz">I2C clockrate.</param> public I2CPlug(Byte Address, UInt32 ClockRateKhz = DefaultClockRate) { this._Address = Address; this.I2C_Config = new I2CDevice.Configuration(this.Address, (Int32) ClockRateKhz); this.I2C_Device = new I2CDevice(this.I2C_Config); }
public void Read(I2CDevice.Configuration config, byte[] buffer, int transactionTimeout) { _slaveDevice.Config = config; I2CDevice.I2CTransaction[] i2CTransactions = new I2CDevice.I2CTransaction[] { I2CDevice.CreateReadTransaction(buffer) }; lock (_slaveDevice) _slaveDevice.Execute(i2CTransactions, transactionTimeout); }
public int Execute(I2CDevice.I2CTransaction[] xActions, int timeout) { if (this.isDisposed) throw new ObjectDisposedException(); singletonDevice.Config = this.Config; return singletonDevice.Execute(xActions, timeout); }
/// <summary> /// Scan range of addresses and print devices to debug output. /// </summary> /// <param name="startAddress">Start of scanning (included)</param> /// <param name="endAddress">End of scanning (included)</param> /// <param name="clockRateKhz">frequency in Khz</param> public static void ScanAddresses(ushort startAddress, ushort endAddress, ushort clockRateKhz = 100) { Debug.Print("Scanning..."); for (ushort adr = startAddress; adr <= endAddress; adr++) { I2CDevice device = new I2CDevice(new I2CDevice.Configuration(adr, clockRateKhz)); byte[] buff = new byte[1]; try { I2CDevice.I2CReadTransaction read = I2CDevice.CreateReadTransaction(buff); var ret = device.Execute(new I2CDevice.I2CTransaction[] { read }, 1000); if(ret > 0) Debug.Print("Device on address: "+adr+ " (0x"+adr.ToString("X")+")"); } catch (Exception){ continue; } finally { //otestovat yda se dela pokazde device.Dispose(); device = null; } } Debug.Print("Scanning finished."); }
public NativeI2CBus(Socket socket, ushort address, int clockRateKhz, Module module) { if (_device == null) _device = new Hardware.I2CDevice(new Hardware.I2CDevice.Configuration(0, 50)); _configuration = new Hardware.I2CDevice.Configuration(address, clockRateKhz); }
private void Dispose(bool fDisposing) { if (!this.isDisposed) { try { if (instanceCount > 0) { instanceCount--; if (instanceCount == 0) { if (singletonDevice != null) { singletonDevice.Dispose(); singletonDevice = null; } } } } finally { this.isDisposed = true; } } }
// Explorations into I2C usage public static void Sketch() { // Create the config object with device address and clock speed I2CDevice.Configuration c = new I2CDevice.Configuration(0x68, 100); I2CDevice d = new I2CDevice(c); byte[] read_byte = new byte[1]; byte test_value = 0x00; while (test_value < 0xff) { // Read the register I2CDevice.I2CWriteTransaction w = I2CDevice.CreateWriteTransaction(new byte[] { 0x14 }); I2CDevice.I2CReadTransaction r = I2CDevice.CreateReadTransaction(read_byte); int bytes_exchanged = d.Execute(new I2CDevice.I2CTransaction[] { w, r }, 100); // Write to the register w = I2CDevice.CreateWriteTransaction(new byte[] { 0x14, test_value }); bytes_exchanged = d.Execute(new I2CDevice.I2CTransaction[] { w }, 100); foreach (byte b in read_byte) { Debug.Print(b.ToString()); } test_value++; Thread.Sleep(3000); } }
public int Read(XI2CDevice.Configuration configuration, byte[] data) { _device.Config = configuration; XI2CDevice.I2CReadTransaction read = XI2CDevice.CreateReadTransaction(data); return(_device.Execute(new XI2CDevice.I2CTransaction[] { read }, _readTimeout)); }
public I2CBus(int readTimeout, int writeTimeout) { _readTimeout = readTimeout; _writeTimeout = writeTimeout; _defaultConfiguration = new XI2CDevice.Configuration(0, 0); _device = new XI2CDevice(_defaultConfiguration); }
public override int Execute(I2CDevice.I2CTransaction[] transactions, int millisecondsTimeout) { lock (_device) { _device.Config = _configuration; return _device.Execute(transactions, millisecondsTimeout); } }
private void InitBlinkM(I2CDevice i2cDevice, I2CDevice.Configuration blinkM) { BlinkMCommand.StopScript.Execute(i2cDevice, blinkM); // BlinkMCommand.GetAddress.Execute(i2cDevice, blinkM); // BlinkMCommand.GetVersion.Execute(i2cDevice, blinkM); BlinkMCommand.SetFadeSpeed.Execute(i2cDevice, blinkM, 255); BlinkMCommand.FadeToHSB.Execute(i2cDevice, blinkM, 0, 0, 0); }
public static byte GetRegister(this I2CDevice device, I2CDevice.Configuration config, int timeout, byte register) { byte result; if (TryGetRegister(device, config, timeout, register, out result)) return result; else throw new Exception(); }
public static byte GetBits(this I2CDevice device, I2CDevice.Configuration config, int timeout, byte register, byte bitStart, byte length) { byte result; if (device.TryGetBits(config, timeout, register, bitStart, length, out result)) return result; else throw new Exception(); }
public static bool GetBit(this I2CDevice device, I2CDevice.Configuration config, int timeout, byte register, byte bitNum) { bool result; if (device.TryGetBit(config, timeout, register, bitNum, out result)) return result; else throw new Exception(); }
public static int Execute(this I2CDevice device, I2CDevice.Configuration config, I2CDevice.I2CTransaction[] transactions, int timeout) { var ConfigSav = device.Config; device.Config = config; var RetVal = device.Execute(transactions, timeout); device.Config = ConfigSav; return RetVal; }
// Create new instance using a pre-created I2CDevice public TWIDisplay(I2CDevice display) { m_data = new byte[4]; m_dots = 0; m_display = display; setRotateMode(); }
public SSD1306(I2CDevice device,int width = 128,int height = 64) { this._device = device; this._width = width; this._height = height; this._pages = _height / 8; buffer = new byte[width * _pages]; }
void Connect () { if (i2cDevice != null) return; this.i2cConfig = new I2CDevice.Configuration (this.Address, clockRateKhz); this.i2cDevice = new I2CDevice (this.i2cConfig); }
public int Read(XI2CDevice.Configuration configuration, byte[] data) { _device.Config = configuration; XI2CDevice.I2CReadTransaction read = XI2CDevice.CreateReadTransaction(data); return _device.Execute(new XI2CDevice.I2CTransaction[] { read }, _readTimeout); }
private CapacitiveTouchController(Cpu.Pin portId) { transactions = new I2CDevice.I2CTransaction[2]; resultBuffer = new byte[1]; addressBuffer = new byte[1]; i2cBus = new I2CDevice(new I2CDevice.Configuration(0x38, 400)); touchInterrupt = new InterruptPort(portId, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth); touchInterrupt.OnInterrupt += (a, b, c) => this.OnTouchEvent(); }
public NativeI2CBus(Socket socket, ushort address, int clockRateKhz, Module module) { if (_device == null) { _device = new Hardware.I2CDevice(new Hardware.I2CDevice.Configuration(0, 50)); } _configuration = new Hardware.I2CDevice.Configuration(address, clockRateKhz); }
public bool Write(XI2CDevice.Configuration configuration, byte[] data) { _device.Config = configuration; XI2CDevice.I2CWriteTransaction transaction = XI2CDevice.CreateWriteTransaction(data); int transferred = _device.Execute(new XI2CDevice.I2CTransaction[] { transaction }, _writeTimeout); return (transferred == data.Length); }
public bool Write(XI2CDevice.Configuration configuration, byte[] data) { _device.Config = configuration; XI2CDevice.I2CWriteTransaction transaction = XI2CDevice.CreateWriteTransaction(data); int transferred = _device.Execute(new XI2CDevice.I2CTransaction[] { transaction }, _writeTimeout); return(transferred == data.Length); }
public I2CDevice2(I2CDevice.Configuration config) { // In the constructor a if (config == null) throw new ArgumentNullException(); this.Config = config; if (singletonDevice == null) singletonDevice = new I2CDevice(config); instanceCount++; }
public I2CDevice2(I2CDevice.Configuration config) { // In the constructor a if (config == null) { throw new ArgumentNullException(); } this.Config = config; if (singletonDevice == null) { singletonDevice = new I2CDevice(config); } instanceCount++; }
public int ReadRegister(XI2CDevice.Configuration configuration, byte register, byte[] data, int length) { _device.Config = configuration; byte[] tmp = new byte[length]; XI2CDevice.I2CWriteTransaction write = XI2CDevice.CreateWriteTransaction(new byte[] { register }); XI2CDevice.I2CReadTransaction read = XI2CDevice.CreateReadTransaction(tmp); int transferred = _device.Execute(new XI2CDevice.I2CTransaction[] { write, read }, _readTimeout); if (transferred == (length + 1)) { Array.Copy(tmp, data, transferred - 1); return(transferred - 1); } return(-1); }