public SensorData GetData() { lock (bus) { if (rawData == null) { rawData = new Byte[6]; getDataWriteTransaction = I2CDevice.CreateWriteTransaction(new[] { (Byte)RegisterMap.DataX0 }); getDataReadTransaction = I2CDevice.CreateReadTransaction(rawData); getDataTransaction = new I2CDevice.I2CTransaction[] { getDataWriteTransaction, getDataReadTransaction }; } bus.Config = configuration; bus.Execute(getDataTransaction, 50); } // Convert the raw byte data into the raw acceleration data for each axis and convert it into Gs return new SensorData { X = resolutionMultiplier * (short)(rawData[1] << 8 | rawData[0]), Y = resolutionMultiplier * (short)(rawData[3] << 8 | rawData[2]), Z = resolutionMultiplier * (short)(rawData[5] << 8 | rawData[4]) }; }
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 }
/// <summary> /// Sets the time on the clock using the datetime object. Milliseconds are not used. /// </summary> /// <param name="dt">A DateTime object used to set the clock</param> public void Set(DateTime dt) { var transaction = new I2CDevice.I2CWriteTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { DS1307_RTC_START_ADDRESS, DecToBcd(dt.Second), DecToBcd(dt.Minute), DecToBcd(dt.Hour), DecToBcd((int)dt.DayOfWeek), DecToBcd(dt.Day), DecToBcd(dt.Month), DecToBcd(dt.Year - 2000)} ) }; if (Clock.Execute(transaction, TimeOutMs) == 0) { throw new Exception("I2C write transaction failed"); } }
/// <summary> /// Writes an arbitrary RTC or RAM register /// </summary> /// <param name="address">Register address between 0x00 and 0x3f</param> /// <param name="val">The value of the byte to write at that address</param> public void WriteRegister(byte address, byte val) { if (address > DS1307_RAM_END_ADDRESS) { throw new ArgumentOutOfRangeException("Invalid register address"); } var transaction = new I2CDevice.I2CWriteTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] {address, (byte) val}) }; if (Clock.Execute(transaction, TimeOutMs) == 0) { throw new Exception("I2C write transaction failed"); } }
/// <summary> /// Writes to the clock's user RAM registers as a block /// </summary> /// <param name="buffer">A byte buffer of size DS1307_RAM_SIZE</param> public void SetRAM(byte[] buffer) { if (buffer.Length != DS1307_RAM_SIZE) { throw new ArgumentOutOfRangeException("Invalid buffer length"); } // Allocate a new buffer large enough to include the RAM start address byte and the payload var TrxBuffer = new byte[sizeof(byte) /*Address byte*/ + DS1307_RAM_SIZE]; // Set the RAM start address TrxBuffer[0] = DS1307_RAM_START_ADDRESS; // Copy the user buffer after the address buffer.CopyTo(TrxBuffer, 1); // Write to the clock's RAM var transaction = new I2CDevice.I2CWriteTransaction[] {I2CDevice.CreateWriteTransaction(TrxBuffer)}; if (Clock.Execute(transaction, TimeOutMs) == 0) { throw new Exception("I2C write transaction failed"); } }
/// <summary> /// Writes an array of bytes to the I2C device. /// </summary> /// <param name="writeBuffer">The array of bytes that will be sent to the I2C device.</param> /// <param name="timeout">The amount of time, in milliseconds, that the system will wait before resuming execution of the transaction.</param> /// <returns>The number of bytes of data transferred in the transaction.</returns> public int Write(byte[] writeBuffer, int timeout) { var transactions = new I2CDevice.I2CWriteTransaction[] { I2CDevice.CreateWriteTransaction(writeBuffer) }; return Interface.Execute(transactions, timeout); }
public void Write(byte[] frame) { st = I2CDevice.CreateWriteTransaction(frame); matrix8x8.Execute(new I2CDevice.I2CTransaction[] { st }, timeOut); }
public void WriteRegister(byte address, byte[] data) { if (address > DS1307_RAM_END_ADDRESS) throw new ArgumentOutOfRangeException("Invalid register address"); if (address + data.Length > DS1307_RAM_END_ADDRESS) throw new ArgumentException("Buffer overrun"); byte[] txData = new byte[data.Length + 1]; txData[0] = address; data.CopyTo(txData, 1); lock (clock) { clock.Config = config; var transaction = new I2CDevice.I2CWriteTransaction[] { I2CDevice.CreateWriteTransaction(txData) }; if (clock.Execute(transaction, DS1307_I2C_TRANSACTION_TIMEOUT_MS) == 0) { throw new Exception("I2C write transaction failed"); } } }