本文整理汇总了C#中Microsoft.SPOT.Hardware.I2CDevice类的典型用法代码示例。如果您正苦于以下问题:C# I2CDevice类的具体用法?C# I2CDevice怎么用?C# I2CDevice使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
I2CDevice类属于Microsoft.SPOT.Hardware命名空间,在下文中一共展示了I2CDevice类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Accelerometer
public Accelerometer(I2CDevice bus, ushort deviceAddress = 0x53, int clockRate = 400)
{
this.bus = bus;
configuration = new I2CDevice.Configuration(deviceAddress, clockRate);
ConfigureDevice();
}
示例2: ShieldStudioView
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);
}
示例3: Sketch01
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());
}
}
示例4: SiliconLabsSI7005
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");
}
}
}
示例5: TSL2561
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 !!!
}
示例6: Main
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);
}
}
示例7: Init
/// <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;
}
}
示例8: Read
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);
}
示例9: ScanAddresses
/// <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.");
}
示例10: Execute
public int Execute(I2CDevice.I2CTransaction[] xActions, int timeout)
{
if (this.isDisposed)
throw new ObjectDisposedException();
singletonDevice.Config = this.Config;
return singletonDevice.Execute(xActions, timeout);
}
示例11: Main
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);
}
示例12: NativeI2CBus
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);
}
示例13: Sketch
// 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);
}
}
示例14: run
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];
}
}
示例15: I2CPlug
/// <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);
}