// 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 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 int Read(XI2CDevice.Configuration configuration, byte[] data) { _device.Config = configuration; XI2CDevice.I2CReadTransaction read = XI2CDevice.CreateReadTransaction(data); return(_device.Execute(new XI2CDevice.I2CTransaction[] { read }, _readTimeout)); }
/// <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 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() { //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 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"); } } }
public override int Execute(params Hardware.I2CDevice.I2CTransaction[] transactions) { int transacted; lock (_device) { _device.Config = _configuration; transacted = _device.Execute(transactions, this.timeout); } if (this.LengthErrorBehavior == ErrorBehavior.ThrowException) { int count = 0; for (int i = 0; i < transactions.Length; i++) { count += transactions[i].Buffer.Length; } if (count != transacted) { throw NewLengthErrorException(); } } return(transacted); }
public static void Main() { OutputPort Led = new OutputPort(Pins.ONBOARD_LED, false); byte[] Addr=new byte[2]; Addr[0]=0x00; Addr[1]=0x01; byte[] TxBuff = new byte[4]; TxBuff[0] = (byte)'1'; TxBuff[1] = (byte)'2'; TxBuff[2] = (byte)'3'; TxBuff[3] = (byte)'4'; byte[] RxBuff= new byte[4]; I2CDevice.Configuration I2C_Configuration = new I2CDevice.Configuration(0x50, 400); I2CDevice I2C1 = new I2CDevice(I2C_Configuration); I2CDevice.I2CTransaction[] WriteTran = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(Addr), I2CDevice.CreateWriteTransaction(TxBuff) }; I2CDevice.I2CTransaction[] ReadTran = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(Addr), I2CDevice.CreateReadTransaction(RxBuff) }; while(true) { Led.Write(true); I2C1.Execute(WriteTran, 1000); Debug.Print("Write Success!"); Thread.Sleep(200); I2C1.Execute(ReadTran, 1000); Debug.Print("Read Success!"); string ReadOut = new string(System.Text.Encoding.UTF8.GetChars(RxBuff)); Debug.Print("EEPROM CONTENT:"+ReadOut); Led.Write(false); Thread.Sleep(200); } }
public int Execute(I2CDevice.I2CTransaction[] xActions, int timeout) { if (this.isDisposed) { throw new ObjectDisposedException(); } singletonDevice.Config = this.Config; return(singletonDevice.Execute(xActions, timeout)); }
public static byte GetData(I2CDevice FPGA, byte Port) { int I2CTimeout = 1000; byte[] buffer = new byte[1]; var transaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] {Port}), I2CDevice.CreateReadTransaction(buffer) }; FPGA.Execute(transaction, I2CTimeout); return buffer[0]; }
public static void SetRedLED(I2CDevice FPGA, bool state) { int I2CTimeout = 1000; byte[] buffer = new byte[2]; buffer[0] = 0x10; buffer[1] = state ? (byte)0x01 : (byte)0x00; var transaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(buffer) }; FPGA.Execute(transaction, I2CTimeout); }
void readValues(I2CDevice adxl345, short[] result) { byte[] values = new byte[6]; I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[2]; byte[] RegisterNum = new byte[1] { valuesRegister }; xActions[0] = adxl345.CreateWriteTransaction(RegisterNum); xActions[1] = adxl345.CreateReadTransaction(values); if (adxl345.Execute(xActions, 1000) == 0) throw new Exception("Unable to read accelerometer"); result[0] = (short)(values[0] + (values[1] << 8)); result[1] = (short)(values[2] + (values[3] << 8)); result[2] = (short)(values[4] + (values[5] << 8)); }
public Magnetometer() { monitor = new object(); magnetometer = new I2CDevice(new I2CDevice.Configuration(address, freq)); //I2CDevice.I2CWriteTransaction activeAutoReset_RAWMode = I2CDevice.CreateWriteTransaction(new byte[] { 0x12, 0xC0 }); I2CDevice.I2CWriteTransaction activeMode = I2CDevice.CreateWriteTransaction(new byte[] { 0x10, 0x01 }); I2CDevice.I2CWriteTransaction correct = I2CDevice.CreateWriteTransaction(new byte[] { 0x11, 0x00 }); //I2CDevice.I2CWriteTransaction activeMode_FastMode = I2CDevice.CreateWriteTransaction(new byte[] { 0x10, 0x05 }); int i = magnetometer.Execute(new I2CDevice.I2CTransaction[] { activeMode, correct }, 1000); //2285 //-3062 byte[] XOFF = FromShortC2(2630); I2CDevice.I2CWriteTransaction XOFFM = I2CDevice.CreateWriteTransaction(new byte[] { 0x09, XOFF[1] }); I2CDevice.I2CWriteTransaction XOFFL = I2CDevice.CreateWriteTransaction(new byte[] { 0x0A, XOFF[0] }); byte[] YOFF = FromShortC2(-3192); I2CDevice.I2CWriteTransaction YOFFM = I2CDevice.CreateWriteTransaction(new byte[] { 0x0B, YOFF[1] }); I2CDevice.I2CWriteTransaction YOFFL = I2CDevice.CreateWriteTransaction(new byte[] { 0x0C, YOFF[0] }); int i2 = magnetometer.Execute(new I2CDevice.I2CTransaction[] { XOFFM, XOFFL, YOFFM, YOFFL }, 1000); update = new Timer(new TimerCallback(read), new object(), 1000, 500); }
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 } } }
public static void Test_Nunchuck() { // http://www.robotshop.com/media/files/PDF/inex-zx-nunchuck-datasheet.pdf // // To communicate with the Nunchuk, we must send a handshake signal. If you are // using a black Wii Nunchuk, send 2 bytes 0xF0, 0x55 to initialize the first register // and 0xFB, 0x00 to initialize the second register of the Nunchuk. On a white Wii // Nunchuk, send 0x40, 0x00 followed by 0x00. The I2C address of both Wii Nunchuks // is 0x52. The frequency used to communicate with the Wii Nunchuk is 100KHz. I2CDevice.Configuration con = new I2CDevice.Configuration((0xA4 >> 1), 100); I2CDevice MyI2C = new I2CDevice(con); I2CDevice.I2CTransaction[] xRestartActions = new I2CDevice.I2CTransaction[1]; I2CDevice.I2CTransaction[] xConversionActions = new I2CDevice.I2CTransaction[1]; I2CDevice.I2CTransaction[] xDataReadActions = new I2CDevice.I2CTransaction[1]; // black send 2 bytes 0xF0, 0x55 and 0xFB, 0x00 // white send 0x40, 0x00 //byte[] RestartNunchuck = new byte[4] { 0xF0, 0x55, 0xFB, 0x00 }; // Not tested byte[] RestartNunchuck = new byte[2] { 0x40, 0x00 }; xRestartActions[0] = I2CDevice.CreateWriteTransaction(RestartNunchuck); //byte[] ConversionCommand = new byte[2] { 0xFB, 0x00 }; // maybe? byte[] ConversionCommand = new byte[1] { 0x00 }; xConversionActions[0] = I2CDevice.CreateWriteTransaction(ConversionCommand); byte[] DataStream = new byte[6]; xDataReadActions[0] = I2CDevice.CreateReadTransaction(DataStream); MyI2C.Execute(xRestartActions, 1000); Thread.Sleep(100); int deadzoneX = 8; int deadzoneY = 8; while (true) { Debug.Print("<3 MC"); MyI2C.Execute(xConversionActions, 1000); Thread.Sleep(100); MyI2C.Execute(xDataReadActions, 1000); DataStream[0] = nunchuk_decode_byte(DataStream[0]); DataStream[1] = nunchuk_decode_byte(DataStream[1]); DataStream[2] = nunchuk_decode_byte(DataStream[2]); DataStream[3] = nunchuk_decode_byte(DataStream[3]); DataStream[4] = nunchuk_decode_byte(DataStream[4]); DataStream[5] = nunchuk_decode_byte(DataStream[5]); // Taken from http://home.kendra.com/mauser/Joystick.html // // 1. Get X and Y from the Joystick, do whatever scaling and calibrating you need to do based on your hardware. // 2. Invert X // 3. Calculate R+L (Call it V): V =(100-ABS(X)) * (Y/100) + Y // 4. Calculate R-L (Call it W): W= (100-ABS(Y)) * (X/100) + X // 5. Calculate R: R = (V+W) /2 // 6. Calculate L: L= (V-W)/2 // 7. Do any scaling on R and L your hardware may require. // 8. Send those values to your Robot. // 9. Go back to 1. // int joystickX = (DataStream[0] > 127 - (deadzoneX / 2) && DataStream[0] < 127 + (deadzoneX / 2)) ? 0 : DataStream[0] - 127; int joystickY = (DataStream[1] > 127 - (deadzoneY / 2) && DataStream[1] < 127 + (deadzoneY / 2)) ? 0 : DataStream[1] - 127; float X = (float)joystickX; float Y = (float)joystickY; X *= -1; float V = (100.0f - (float)System.Math.Abs((int)X)) * (Y / 100.0f) + Y; float W = (100.0f - (float)System.Math.Abs((int)Y)) * (X / 100.0f) + X; float L = (V - W) / 2.0f; float R = (V + W) / 2.0f; if (L >= 0) MC_WriteCommand('1', 'f', (short)(10.0f * L)); else MC_WriteCommand('1', 'r', (short)(10.0f * -L)); if (R >= 0) MC_WriteCommand('2', 'f', (short)(10.0f * R)); else MC_WriteCommand('2', 'r', (short)(10.0f * -R)); if ((DataStream[5] & 0x01) == 0) { int accelX = (DataStream[2] << 2) | ((DataStream[5] >> 2) & 0x03); int accelY = (DataStream[3] << 2) | ((DataStream[5] >> 4) & 0x03); int accelZ = (DataStream[4] << 2) | ((DataStream[5] >> 6) & 0x03); Debug.Print( "A " + accelX.ToString() + "," + accelY.ToString() + "," + accelZ.ToString()); //Debug.Print("Z button"); } if ((DataStream[5] & 0x02) == 0) { //Debug.Print( // "J " + joystickX.ToString() + "," + joystickY.ToString() + // " -> " + // "M " + L.ToString() + "," + R.ToString()); if (mCMUCam3Controller_COM1.BytesToRead > 0) { byte[] buffer = new byte[2]; mCMUCam3Controller_COM1.Read(buffer, 0, 1); string buf = System.Text.Encoding.UTF8.GetChars(buffer).ToString(); Debug.Print(buf); } } //Thread.Sleep(150); } }
/// <summary> /// Returns the value contained in a register /// et + /// </summary> /// <param name="RegisterNumber">The register number</param> /// <returns></returns> private byte GetRegister(Registers RegisterNumber) { // Buffer d'écriture byte[] outBuffer = new byte[] { (byte)RegisterNumber }; I2CDevice.I2CWriteTransaction writeTransaction = I2CDevice.CreateWriteTransaction(outBuffer); // Buffer de lecture byte[] inBuffer = new byte[1]; I2CDevice.I2CReadTransaction readTransaction = I2CDevice.CreateReadTransaction(inBuffer); // Tableau des transactions I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { writeTransaction, readTransaction }; // Exécution des transactions busI2C = new I2CDevice(ConfigSRF08); // Connexion virtuelle du SRF08 au bus I2C if (busI2C.Execute(transactions, TRANSACTIONEXECUTETIMEOUT) != 0) { // Success //Debug.Print("Received the first data from at device " + busI2C.Config.Address + ": " + ((int)inBuffer[0]).ToString()); } else { // Failed //Debug.Print("Failed to execute transaction at device: " + busI2C.Config.Address + "."); } busI2C.Dispose(); // Déconnexion virtuelle de l'objet Lcd du bus I2C return inBuffer[0]; }
/// <summary> /// Only triggers a shot ulrasons /// </summary> /// <param name="units">unit of measure expected</param> public void TrigShotUS(MeasuringUnits units) { this.unit = units; // Calcul du mot de commande à partir de l'unité de mesure byte commandByte = (byte)(80 + (byte)units); // Création d'un buffer et d'une transaction pour l'accès au module en écriture byte[] outbuffer = new byte[] { (byte)Registers.Command, commandByte }; I2CDevice.I2CTransaction WriteUnit = I2CDevice.CreateWriteTransaction(outbuffer); // Tableaux des transactions I2CDevice.I2CTransaction[] T_WriteUnit = new I2CDevice.I2CTransaction[] { WriteUnit }; // Exécution de la transactions busI2C = new I2CDevice(ConfigSRF08); // Connexion virtuelle de l'objet SRF08 au bus I2C busI2C.Execute(T_WriteUnit, TRANSACTIONEXECUTETIMEOUT); // Transaction : Activation US busI2C.Dispose(); // Déconnexion virtuelle de l'objet SRF08 du bus I2C }
/// <summary> /// Triggers a shot ulrasons, wait for 75ms and return result in the unit of measure /// </summary> /// <param name="units">unit of measure expected</param> /// <returns>range in cm or inches or millisec</returns> public UInt16 ReadRange(MeasuringUnits units) { this.unit = units; // Calcul du mot de commande à partir de l'unité de mesure byte command = (byte)(80 + (byte)units); // Création d'un buffer et d'une transaction pour l'accès au module en écriture byte[] outbuffer = new byte[] { (byte)Registers.Command, command }; I2CDevice.I2CTransaction WriteUnit = I2CDevice.CreateWriteTransaction(outbuffer); // Création d'un buffer et d'une transaction pour l'accès au module en lecture byte[] inbuffer = new byte[4]; I2CDevice.I2CTransaction ReadDist = I2CDevice.CreateReadTransaction(inbuffer); // Tableaux des transactions I2CDevice.I2CTransaction[] T_WriteUnit = new I2CDevice.I2CTransaction[] { WriteUnit }; I2CDevice.I2CTransaction[] T_ReadDist = new I2CDevice.I2CTransaction[] { ReadDist }; // Exécution des transactions busI2C = new I2CDevice(ConfigSRF08); // Connexion virtuelle de l'objet SRF08 au bus I2C busI2C.Execute(T_WriteUnit, TRANSACTIONEXECUTETIMEOUT); // Transaction : Activation US Thread.Sleep(75); // attente echo US busI2C.Execute(T_ReadDist, TRANSACTIONEXECUTETIMEOUT); // Transaction : Lecture distance UInt16 range = (UInt16)((UInt16)(inbuffer[3] << 8) + inbuffer[2]); // Calcul de la distance busI2C.Dispose(); // Déconnexion virtuelle de l'objet SRF08 du bus I2C return range; }
/// <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; } }
public double Humidity() { using (I2CDevice device = new I2CDevice(new I2CDevice.Configuration(deviceId, clockRateKHz))) { //Debug.Print("Humidity Measurement start"); byte[] CmdBuffer = { RegisterIdConfiguration, CommandMeasureHumidity }; I2CDevice.I2CTransaction[] CmdAction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(CmdBuffer), }; if (device.Execute(CmdAction, transactionTimeoutmSec) == 0) { throw new ApplicationException("Unable to send measure humidity command"); } //Debug.Print("Measurement wait"); bool humidityConversionInProgress = true; // Wait for measurement do { byte[] WaitWriteBuffer = { RegisterIdStatus }; byte[] ValueReadBuffer = new byte[1]; I2CDevice.I2CTransaction[] waitAction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(WaitWriteBuffer), I2CDevice.CreateReadTransaction(ValueReadBuffer) }; if (device.Execute(waitAction, transactionTimeoutmSec) == 0) { throw new ApplicationException("Unable to read status register"); } if ((ValueReadBuffer[RegisterIdStatus] & StatusReadyMask) != StatusReadyMask) { humidityConversionInProgress = false; } } while (humidityConversionInProgress); //Debug.Print("Measurement read"); byte[] valueWriteBuffer = { REG_DATA_H }; byte[] valueRreadBuffer = new byte[2]; I2CDevice.I2CTransaction[] valueAction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(valueWriteBuffer), I2CDevice.CreateReadTransaction(valueRreadBuffer) }; if (device.Execute(valueAction, transactionTimeoutmSec) == 0) { throw new ApplicationException("Unable to read data register"); } int hum = valueRreadBuffer[0]; hum = hum << 8; hum = hum + valueRreadBuffer[1]; hum = hum >> 4; /* Formula: Humidity(%) = (Value/16) - 24 */ double humidity = (hum / 16.0) - 24.0; //Debug.Print(" Humidity " + humidity.ToString("F1")); return humidity; } }
public void SetValue(byte newValue) { I2CDevice MyI2C = new I2CDevice(i2con); //create transactions (we need 2 in this example) I2CDevice.I2CTransaction[] setActions = new I2CDevice.I2CTransaction[1]; // create write buffer (we need one byte) byte[] RegisterNum = new byte[2] { 2, newValue }; setActions[0] = I2CDevice.CreateWriteTransaction(RegisterNum); MyI2C.Execute(setActions, 1000); MyI2C.Dispose(); }
/* There is no generic public Write EEPROM command. * Specific commands are available for setting EEPROM values * where appropriate to the device. */ void WriteEeprom(EEPROMAddress addr, byte data) { myI2Command[0] = I2CDevice.CreateWriteTransaction(new byte[] { (byte)Command.WriteEEPROM, (byte)addr, data }); // Exécution de la transaction BusI2C = new I2CDevice(ConfigHM6352); // Connexion virtuelle de l'objet HMC6352 au bus I2C myBytesTransmitted = BusI2C.Execute(myI2Command, 100); BusI2C.Dispose(); // Déconnexion virtuelle de l'objet HMC6352 du bus I2C }
public byte[] Read(I2CDevice i2cDevice, byte address, int length) { byte[] data = new byte[length]; i2cDevice.Config = _config; I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { 1 }), I2CDevice.CreateReadTransaction(data) }; i2cDevice.Execute(transactions, 1000); return data; }
public void Write(I2CDevice i2cDevice, byte address, byte[] data) { i2cDevice.Config = _config; I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(data), }; i2cDevice.Execute(transactions, 1000); }
private void i2c_write(byte[] toWrite, byte address, I2CDevice startAddress) { I2CDevice.I2CTransaction[] reading = new I2CDevice.I2CTransaction[] { CreateWriteTransaction(toWrite, address, 1) }; int bytesRead = startAddress.Execute(reading, 100); }
private void i2c_readAck(byte[] toRead, byte address, I2CDevice startAddress) { I2CDevice.I2CTransaction[] reading = new I2CDevice.I2CTransaction[] { CreateReadTransaction(toRead, address, 1) }; int bytesRead = startAddress.Execute(reading, 100); }
public void Init() { I2CDevice MyI2C = new I2CDevice(i2con); //create transactions (we need 2 in this example) I2CDevice.I2CTransaction[] initActions = new I2CDevice.I2CTransaction[1]; // create write buffer (we need one byte) //initActions[0] = MyI2C.CreateWriteTransaction(new byte[] { 0, (byte)(DialMode.AUT | DialMode.CYC | DialMode.OneFree) }); I2CDevice.CreateWriteTransaction(new byte[] { 0, (byte)(DialMode.AUT | DialMode.CYC | DialMode.OneFree) }); int result = MyI2C.Execute(initActions, 1000); //Debug.Print("Device Init Result " + result); MyI2C.Dispose(); }
public double Temperature() { using (I2CDevice device = new I2CDevice(new I2CDevice.Configuration(deviceId, clockRateKHz))) { //Debug.Print("Temperature Measurement start"); byte[] CmdBuffer = { RegisterIdConfiguration, CommandMeasureTemperature }; I2CDevice.I2CTransaction[] CmdAction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(CmdBuffer), }; if (device.Execute(CmdAction, transactionTimeoutmSec) == 0) { throw new ApplicationException("Unable to send measure temperature command"); } //Debug.Print("Measurement wait"); bool conversionInProgress = true; // Wait for measurement do { byte[] WaitWriteBuffer = { RegisterIdStatus }; byte[] WaitReadBuffer = new byte[1]; I2CDevice.I2CTransaction[] waitAction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(WaitWriteBuffer), I2CDevice.CreateReadTransaction(WaitReadBuffer) }; if (device.Execute(waitAction, transactionTimeoutmSec) == 0) { throw new ApplicationException("Unable to read status register"); } if ((WaitReadBuffer[RegisterIdStatus] & StatusReadyMask) != StatusReadyMask) { conversionInProgress = false; } } while (conversionInProgress); //Debug.Print("Measurement read"); // Read temperature value byte[] valueWriteBuffer = { REG_DATA_H }; byte[] valueReadBuffer = new byte[2]; I2CDevice.I2CTransaction[] valueAction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(valueWriteBuffer), I2CDevice.CreateReadTransaction(valueReadBuffer) }; if (device.Execute(valueAction, transactionTimeoutmSec) == 0) { throw new ApplicationException("Unable to read data register"); } // Convert bye to centigrade int temp = valueReadBuffer[0]; temp = temp << 8; temp = temp + valueReadBuffer[1]; temp = temp >> 2; /* Formula: Temperature(C) = (Value/32) - 50 */ double temperature = (temp / 32.0) - 50.0; //Debug.Print(" Temp " + temperature.ToString("F1")); return temperature; } }
public void Read() { I2CDevice MyI2C = new I2CDevice(i2con); //create transactions (we need 2 in this example) I2CDevice.I2CTransaction[] readActions = new I2CDevice.I2CTransaction[2]; // create write buffer (we need one byte) byte[] RegisterNum = new byte[1] { 2 }; readActions[0] = I2CDevice.CreateWriteTransaction(RegisterNum); // create read buffer to read the register byte[] RegisterValue = new byte[1]; readActions[1] = I2CDevice.CreateReadTransaction(RegisterValue); // Now we access the I2C bus and timeout in one second if no responce MyI2C.Execute(readActions, 1000); if (currentValue != RegisterValue[0]) { currentValue = RegisterValue[0]; if (OnChange != null) { OnChange(dialId, currentValue); } } MyI2C.Dispose(); //Debug.Print("Register value: " + RegisterValue[0].ToString()); }
void writeCommand(I2CDevice adxl345, byte register, byte value) { byte[] values = new byte[2]; values[0] = register; values[1] = value; I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[1]; xActions[0] = adxl345.CreateWriteTransaction(values); if (adxl345.Execute(xActions, 1000) == 0) throw new Exception("Unable to initialize accelerometer"); }
public void WakeUp() { myI2Command[0] = I2CDevice.CreateWriteTransaction(new byte[] { (byte)Command.WakeUp }); // Exécution de la transaction BusI2C = new I2CDevice(ConfigHM6352); // Connexion virtuelle de l'objet HMC6352 au bus I2C myBytesTransmitted = BusI2C.Execute(myI2Command, 100); BusI2C.Dispose(); // Déconnexion virtuelle de l'objet HMC6352 du bus I2C }
// Set the operational mode. // Mode = Standby, Query, Continuous // Frequency = 1, 45, 10, 20 Hz // Periodic reset = true/false public void SetOperationalMode(OperationalMode mode, Frequency freq, Boolean periodicReset) { byte r = periodicReset ? (byte)(0x01 << 3) : (byte) 0; byte f = (byte)((byte)freq << 5); byte op = (byte)((byte)mode | r | f); myI2Command[0] = I2CDevice.CreateWriteTransaction(new byte[] { (byte)Command.WriteEEPROM, (byte)EEPROMAddress.OperationalModeByte, op }); // Exécution de la transaction BusI2C = new I2CDevice(ConfigHM6352); // Connexion virtuelle de l'objet HMC6352 au bus I2C myBytesTransmitted = BusI2C.Execute(myI2Command, 100); BusI2C.Dispose(); // Déconnexion virtuelle de l'objet HMC6352 du bus I2C }