/// <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); } else { Debug.Print("NO: " + adr); } } catch (Exception) { continue; } finally { //otestovat yda se dela pokazde device.Dispose(); device = null; } } Debug.Print("Scanning finished."); }
/// <summary> /// Reads the raw counts from the ADC /// </summary> /// <param name="number">Number of bytes to read (four if 18-bit conversion else three) </param> /// <returns>Result of 12, 14, 16 or 18-bit conversion</returns> double dataReadRaw(byte number) { Int32 data = 0; byte[] inbuffer = new byte[number]; I2CDevice.I2CReadTransaction readTransaction = I2CDevice.CreateReadTransaction(inbuffer); I2CDevice.I2CTransaction[] xAction = new I2CDevice.I2CTransaction[] { readTransaction }; _i2cBus = new I2CDevice(_config); int transferred = _i2cBus.Execute(xAction, _transactionTimeOut); _i2cBus.Dispose(); if (transferred < inbuffer.Length) { throw new System.IO.IOException("Error i2cBus " + _sla); } else if ((inbuffer[number - 1] & 0x80) == 0) // End of conversion { if (_resolution == SampleRate.EighteenBits) { data = (((Int32)inbuffer[0]) << 16) + (((Int32)inbuffer[1]) << 8) + (Int32)inbuffer[2]; } else { data = (((Int32)inbuffer[0]) << 8) + (Int32)inbuffer[1]; } _endOfConversion = true; return(data &= _dataMask); } else { _endOfConversion = false; return(0); // return something } }
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); }
private I2CDevice.I2CReadTransaction CreateInternalAddressReadTransaction(byte[] buffer, uint internalAddress, byte internalAddressSize) { I2CDevice.I2CReadTransaction readTransaction = I2CDevice.CreateReadTransaction(buffer); ModifyToRepeatedStartTransaction(internalAddress, internalAddressSize, readTransaction, typeof(I2CDevice.I2CReadTransaction)); return(readTransaction); }
public byte CurrentByteRead() { byte[] readBuffer = { 0x0 }; I2CDevice.I2CReadTransaction read = I2CDevice.CreateReadTransaction(readBuffer); I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { read }; m_transfer_count = I2C_device.Execute(transactions, m_timeout); m_transfer_type = transfer_type.read; return(readBuffer[0]); }
private static byte[] ReadFromCompass() { byte[] inBuffer = new byte[6]; I2CDevice.I2CReadTransaction readTransaction = I2CDevice.CreateReadTransaction(inBuffer); I2CDevice.I2CTransaction[] transaction = new I2CDevice.I2CTransaction[] { readTransaction }; compass.Execute(transaction, 1000); return(inBuffer); }
protected byte Read(byte address, byte[] buffer) { I2CDevice.I2CWriteTransaction write = I2CDevice.CreateWriteTransaction(new Byte[] { address }); I2CDevice.I2CReadTransaction read = I2CDevice.CreateReadTransaction(buffer); I2CDevice.I2CTransaction[] readTransaction = new I2CDevice.I2CTransaction[] { write, read }; var result = I2CBus.Execute(Config, readTransaction, 1000); return(buffer[0]); }
public void InitializeI2C() { myI2CDeviceConfiguration = new I2CDevice.Configuration(0x0, 0x0); myI2CDevice = new I2CDevice(myI2CDeviceConfiguration); #if false myI2CDeviceRead = new I2CDevice.I2CReadTransaction( ); myI2CDeviceTrans = new I2CDevice.I2CTransaction(); myI2CDeviceWrite = new I2CDevice.I2CWriteTransaction(); #endif }
protected byte[] Read(int length) { var buffer = new byte[length]; var transactions = new I2CDevice.I2CReadTransaction[] { I2CDevice.CreateReadTransaction(buffer) }; var resultLength = Device.Execute(transactions, Timeout); if (resultLength != length) { throw new Exception("Could not read from device."); } return(buffer); }
public byte RandomByteRead(ushort address) { byte[] dummyAddress = { HighAddress(address), LowAddress(address) }; byte[] readBuffer = { 0x0 }; // // non-data write relocates internal EEPROM counter for random read // I2CDevice.I2CReadTransaction read = I2CDevice.CreateReadTransaction(readBuffer); I2CDevice.I2CWriteTransaction writeCounter = I2CDevice.CreateWriteTransaction(dummyAddress); I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { writeCounter, read }; m_transfer_count = I2C_device.Execute(transactions, m_timeout) - 2; m_transfer_type = transfer_type.read; return(readBuffer[0]); }
public void ReadSequential(ushort Address, uint Length, ref byte[] ReadBuffer) { if ( ((Address + Length) > EEPROM_Size) || (ReadBuffer.Length < Length)) { return; } byte[] dummyAddress = { HighAddress(Address), LowAddress(Address) }; I2CDevice.I2CReadTransaction read = I2CDevice.CreateReadTransaction(ReadBuffer); I2CDevice.I2CWriteTransaction writeCounter = I2CDevice.CreateWriteTransaction(dummyAddress); I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { writeCounter, read }; m_transfer_count = I2C_device.Execute(transactions, m_timeout) - 2; m_transfer_type = transfer_type.read; }
private static byte[] gatherData() { byte[] inBuffer = new byte[2]; I2CDevice.I2CReadTransaction readTransaction = I2CDevice.CreateReadTransaction(inBuffer); I2CDevice.I2CTransaction[] transaction = new I2CDevice.I2CTransaction[1]; transaction[0] = readTransaction; int transferred = device.Execute(transaction, 100); /*Debug.Print("Transferred " + transferred.ToString() + " bytes!"); * foreach (byte b in inBuffer) { * * Debug.Print("" + b); * }*/ return(inBuffer); }
public void read(Object state) { monitor = new object(); byte[] buffer = new byte[bufferSize]; I2CDevice.I2CWriteTransaction transactionWrite = I2CDevice.CreateWriteTransaction(new byte[] { 0x01 }); I2CDevice.I2CReadTransaction transactionRead = I2CDevice.CreateReadTransaction(buffer); int i = magnetometer.Execute(new I2CDevice.I2CTransaction[] { transactionWrite, transactionRead }, 1000); if (i == 8 && buffer[6] == 0xC4) { //X magX = ToShortC2(new byte[] { buffer[1], buffer[0] }); //Y magY = ToShortC2(new byte[] { buffer[3], buffer[2] }); //Z magZ = ToShortC2(new byte[] { buffer[5], buffer[4] }); MAGHeadingRad = exMath.Atan2(-1 * magY, magX); /* * if ((magX == 0) && (magY < 0)) * MAGHeadingRad = exMath.PI / 2.0; * if ((magX == 0) && (magY > 0)) * MAGHeadingRad = 3.0 * exMath.PI / 2.0; * if (magX < 0) * MAGHeadingRad = exMath.PI - exMath.Atan2(magX, magY); * if ((magX > 0) && (magY < 0)) * MAGHeadingRad = -exMath.Atan2(magX, magY); * if ((magX > 0) && (magY > 0)) * MAGHeadingRad = 2.0 * exMath.PI - exMath.Atan2(magX, magY); */ MAGHeadingDeg = (float)exMath.ToDeg((float)MAGHeadingRad); Debug.Print("X: " + magX.ToString() + " Y: " + magY.ToString() + " Z: " + magZ.ToString() + " " + MAGHeadingDeg.ToString()); //Debug.Print(buffer[0] + " " + buffer[1] + " " + buffer[2] + " " + buffer[3]); //Debug.Print("Y: " + magY.ToString()); //Engine.getInstance().Debug("X: " + magX.ToString() + " Y: " + magY.ToString() + " " + MAGHeadingDeg.ToString()); } }
public static void Main() { I2CDevice.Configuration config = new I2CDevice.Configuration(58, //address 100 //clockrate in KHz ); I2CDevice device = new I2CDevice(config); //prepare buffer to write byte AA byte[] outBuffer = new byte[] { 0xAA }; I2CDevice.I2CWriteTransaction writeTransaction = device.CreateWriteTransaction(outBuffer); //prepare buffer to read four bytes byte[] inBuffer = new byte[4]; I2CDevice.I2CReadTransaction readTransaction = device.CreateReadTransaction(inBuffer); //execute both transactions I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { writeTransaction, readTransaction }; int transferred = device.Execute(transactions, 100 //timeout in ms ); //transferred bytes should be 1 + 4 = 5 }
void t_Tick(Object state) { lock (monitor) { byte[] buffer = new byte[bufferSize]; I2CDevice.I2CReadTransaction transactionRead = I2CDevice.CreateReadTransaction(buffer); int i = nimu.Execute(new I2CDevice.I2CTransaction[] { transactionRead }, 1000); if (i == bufferSize) { if (buffer[0] == (byte)255 && buffer[1] == (byte)255 && buffer[2] == (byte)255 && buffer[3] == (byte)255) { double tempTime = digitalSensitivityTime * ToUShortC2(new byte[] { buffer[8], buffer[7] }); if (tempTime < lastTime) { counter++; } lastValueTimer = tempTime; lastTime = tempTime + (rangeTimer * counter); //X lastAcc[0] = GlobalVal.gravity * digitalSensitivtyAccel * ToShortC2(new byte[] { buffer[20], buffer[19] }); //Y lastAcc[1] = GlobalVal.gravity * digitalSensitivtyAccel * ToShortC2(new byte[] { buffer[22], buffer[21] }); //Z lastAcc[2] = GlobalVal.gravity * digitalSensitivtyAccel * ToShortC2(new byte[] { buffer[24], buffer[23] }); //lastAcc = accel_filter(lastAcc[0], lastAcc[1], lastAcc[2]); //Debug.Print(lastAcc[0].ToString() + " - " + lastAcc[1].ToString() + " - " + lastAcc[2].ToString()); //X lastGyr[0] = (double)exMath.ToRad((float)(digitalSensitivityGyro * ToShortC2(new byte[] { buffer[14], buffer[13] }))); //Y lastGyr[1] = (double)exMath.ToRad((float)(digitalSensitivityGyro * ToShortC2(new byte[] { buffer[16], buffer[15] }))); //Z lastGyr[2] = (double)exMath.ToRad((float)(digitalSensitivityGyro * ToShortC2(new byte[] { buffer[18], buffer[17] }))); //X lastMag[0] = digitalSensitivityMag * ToShortC2(new byte[] { buffer[26], buffer[25] }); //Y lastMag[1] = digitalSensitivityMag * ToShortC2(new byte[] { buffer[28], buffer[27] }); //Z lastMag[2] = digitalSensitivityMag * ToShortC2(new byte[] { buffer[30], buffer[29] }); //lastMag = IIR3AxisFilter(lastMag[0], lastMag[1], lastMag[2]); //X lastTemp[0] = digitalSensitivityTemp * ToShortC2(new byte[] { buffer[32], buffer[31] }) + 25; //Y lastTemp[1] = digitalSensitivityTemp * ToShortC2(new byte[] { buffer[34], buffer[33] }) + 25; //Z lastTemp[2] = digitalSensitivityTemp * ToShortC2(new byte[] { buffer[36], buffer[35] }) + 25; } else { } } } }