本文整理汇总了C#中Windows.Devices.I2c.I2cDevice.Write方法的典型用法代码示例。如果您正苦于以下问题:C# I2cDevice.Write方法的具体用法?C# I2cDevice.Write怎么用?C# I2cDevice.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Windows.Devices.I2c.I2cDevice
的用法示例。
在下文中一共展示了I2cDevice.Write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Init
public async Task Init()
{
var deviceSelector = I2cDevice.GetDeviceSelector();
var controllers = await DeviceInformation.FindAllAsync(deviceSelector);
_bme280 = await I2cDevice.FromIdAsync(controllers[0].Id, new I2cConnectionSettings(ADDR) { BusSpeed = I2cBusSpeed.FastMode });
ValidateChipId();
SetTrimmingParamaters();
_bme280.Write(new byte[] { REGISTER_CTRL_HUM, CTRL_HUM_OSRS_H });
_bme280.Write(new byte[] { REGISTER_CTRL_MEAS, CTRL_MEAS_OSRS_P | CTRL_MEAS_OSRS_T | CTRL_MEAS_MODE });
_bme280.Write(new byte[] { REGISTER_CONFIG, CONFIG_TSB | CONFIG_FILTER });
}
示例2: InitI2CAccel
private async void InitI2CAccel()
{
string aqs = I2cDevice.GetDeviceSelector(); /* Get a selector string that will return all I2C controllers on the system */
var dis = await DeviceInformation.FindAllAsync(aqs); /* Find the I2C bus controller device with our selector string */
if (dis.Count == 0)
{
Text_Status.Text = "No I2C controllers were found on the system";
return;
}
var settings = new I2cConnectionSettings(ACCEL_I2C_ADDR);
settings.BusSpeed = I2cBusSpeed.FastMode;
I2CAccel = await I2cDevice.FromIdAsync(dis[0].Id, settings); /* Create an I2cDevice with our selected bus controller and I2C settings */
if (I2CAccel == null)
{
Text_Status.Text = string.Format(
"Slave address {0} on I2C Controller {1} is currently in use by " +
"another application. Please ensure that no other applications are using I2C.",
settings.SlaveAddress,
dis[0].Id);
return;
}
/*
* Initialize the accelerometer:
*
* For this device, we create 2-byte write buffers:
* The first byte is the register address we want to write to.
* The second byte is the contents that we want to write to the register.
*/
byte[] WriteBuf_DataFormat = new byte[] { ACCEL_REG_DATA_FORMAT, 0x01 }; /* 0x01 sets range to +- 4Gs */
byte[] WriteBuf_PowerControl = new byte[] { ACCEL_REG_POWER_CONTROL, 0x08 }; /* 0x08 puts the accelerometer into measurement mode */
/* Write the register settings */
try
{
I2CAccel.Write(WriteBuf_DataFormat);
I2CAccel.Write(WriteBuf_PowerControl);
}
/* If the write fails display the error and stop running */
catch (Exception ex)
{
Text_Status.Text = "Failed to communicate with device: " + ex.Message;
return;
}
/* Now that everything is initialized, create a timer so we read data every 100mS */
periodicTimer = new Timer(this.TimerCallback, null, 0, 100);
}
示例3: Execute
public override void Execute(I2cDevice i2CDevice)
{
i2CDevice.Write(GenerateClearSignalCachePackage());
int offset = 0;
while (offset < _signal.Length)
{
var buffer = _signal.Skip(offset).Take(30).ToArray();
offset += buffer.Length;
i2CDevice.Write(GenerateFillSignalCachePackage(buffer));
}
i2CDevice.Write(GenerateSendCachedSignalPackage());
}
示例4: Execute
public override void Execute(I2cDevice i2CDevice)
{
i2CDevice.Write(GenerateRegisterSensorPackage());
byte[] buffer = new byte[9];
i2CDevice.Read(buffer);
ParseResponse(buffer);
}
示例5: Write
public static bool Write(I2cDevice device, byte reg, byte command)
{
byte[] val = new byte[2];
val[0] = reg;
val[1] = command;
try {
device.Write(val);
return true;
} catch {
return false;
}
}
示例6: Write
public static void Write(I2cDevice device, byte reg, byte command, string exceptionMessage)
{
try
{
byte[] buffer = { reg, command };
device.Write(buffer);
}
catch (Exception exception)
{
throw new SensorException(exceptionMessage, exception);
}
}
示例7: Initialize
private async Task Initialize()
{
try
{
if (Interlocked.CompareExchange(ref _isInitialized, 1, 0) == 1)
{
return;
}
// Get a selector string that will return all I2C controllers on the system
string deviceSelector = I2cDevice.GetDeviceSelector();
// Find the I2C bus controller device with our selector string
var dis = await DeviceInformation.FindAllAsync(deviceSelector);
if (dis.Count == 0)
{
throw new Hdc100XInitializationException("No I2C controllers were found on the system");
}
var settings = new I2cConnectionSettings((int)_busAddress)
{
BusSpeed = I2cBusSpeed.FastMode
};
_sensorDevice = await I2cDevice.FromIdAsync(dis[0].Id, settings);
if (_sensorDevice == null)
{
throw new Hdc100XInitializationException(string.Format(
"Slave address {0} on I2C Controller {1} is currently in use by " +
"another application. Please ensure that no other applications are using I2C.",
settings.SlaveAddress,
dis[0].Id));
}
// Configure sensor:
// - measure with 14bit precision
// - measure both temperature and humidity
_sensorDevice.Write(new byte[] { 0x02, 0x10, 0x00 });
_initalizationCompletion.SetResult(true);
}
catch (Hdc100XInitializationException)
{
_initalizationCompletion.SetResult(false);
throw;
}
catch (Exception exc)
{
_initalizationCompletion.SetResult(false);
throw new Hdc100XInitializationException("Unexpected error during initialization", exc);
}
}
示例8: InitializeAsync
private async Task InitializeAsync()
{
// Initializing the ADS 1115
// Initialisation du ADS 1115
var i2CSettings = new I2cConnectionSettings(0x48)
{
BusSpeed = I2cBusSpeed.FastMode,
SharingMode = I2cSharingMode.Shared
};
var i2C1 = I2cDevice.GetDeviceSelector("I2C1");
var devices = await DeviceInformation.FindAllAsync(i2C1);
_converter = await I2cDevice.FromIdAsync(devices[0].Id, i2CSettings);
// Write in the config register. 0xc4 0Xe0 = 1100010011100000
// = listen to A0, default gain, continuous conversion, 860 SPS, assert after one conversion (= READY signal)
// see http://www.adafruit.com/datasheets/ads1115.pdf p18 for details
// Ecrit dans le registre config. 0xc4 0Xe0 = 1100010011100000
// = ecouter A0, gain par défaut, conversion continue, 860 SPS, signal READY après chaque conversion
// see http://www.adafruit.com/datasheets/ads1115.pdf p18 for details
_converter.Write(new byte[] { 0x01, 0xc4, 0xe0 });
// Configure the Lo_thresh (0x02) and Hi_Thresh (0x03) registers so the READY signal will be sent
// Configure les registres Lo_thresh (0x02) et Hi_Thresh (0x03) pour que le signal READY soit envoyé
_converter.Write(new byte[] { 0x02, 0x00, 0x00 });
_converter.Write(new byte[] { 0x03, 0xff, 0xff });
// Instanciate the READY pin and listen to change
// Instancie la broche REASY et écoute les changements
var gpio = GpioController.GetDefault();
_inGpioPin = gpio.OpenPin(READY_PIN);
_inGpioPin.ValueChanged += InGpioPinOnValueChanged;
}
示例9: PCA9685
private byte PCA9685_MODE1 = 0x00; // location for Mode1 register address
#endregion Fields
#region Constructors
public PCA9685(I2cDevice device)
{
m_device = device;
try
{
device.Write(new byte[] { PCA9685_MODE1, 0x21 });
AllOff();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
}
示例10: Measure
/// <summary>
/// Measures data from the HTU21D.
/// </summary>
/// <param name="device">The I2C device</param>
/// <param name="command">MEASURE_TEMP or MEASURE_HUMIDITY</param>
/// <param param name="gsl">The global sensor lock</param>
/// <returns>The measured value in quids FS</returns>
private async Task<int> Measure(I2cDevice device, int command, object gsl) {
byte[] data = new byte[3];
// No Hold Master mode
lock (gsl) {
device.Write(new byte[] { (byte)command });
}
await Task.Delay(30);
// Max measurement time is 50 ms for temperature and 16 ms for humidity
for (int i = 0; i < 3; i++) {
await Task.Delay(15);
try {
lock (gsl) {
device.Read(data);
}
// Data is MSB-first in the first 2 bytes
int value = data[1] & 0xFC;
return value | ((data[0] & 0xFF) << 8);
} catch (IOException) {
// Swallow the NACK and try again in 10 ms
}
}
throw new IOException("Still measuring, increase delay!");
}
示例11: Initialise
public async Task Initialise(byte HardwareDeviceAddress)
{
System.Diagnostics.Debug.WriteLine("Initalise Started");
System.Diagnostics.Debug.WriteLine("Finding Device");
I2cConnectionSettings i2cSettings = new I2cConnectionSettings(HardwareDeviceAddress);
i2cSettings.BusSpeed = I2cBusSpeed.FastMode;
string DeviceSelector = I2cDevice.GetDeviceSelector(I2C_CONTROLLER_NAME);
DeviceInformationCollection i2cDeviceControllers = await DeviceInformation.FindAllAsync(DeviceSelector);
i2cDeviceChannel = await I2cDevice.FromIdAsync(i2cDeviceControllers[0].Id, i2cSettings);
System.Diagnostics.Debug.WriteLine("Device Found");
byte[] writeBuffer;
// Shutdown Register
writeBuffer = new byte[] { 0x00, 0x01 };
i2cDeviceChannel.Write(writeBuffer);
Debug.WriteLine("Shutdown Register set");
// Pin Enable Registers
writeBuffer = new byte[] { 0x13, 0xff };
i2cDeviceChannel.Write(writeBuffer);
writeBuffer = new byte[] { 0x14, 0xff };
i2cDeviceChannel.Write(writeBuffer);
writeBuffer = new byte[] { 0x15, 0xff };
i2cDeviceChannel.Write(writeBuffer);
System.Diagnostics.Debug.WriteLine("Initalise Complete");
}
示例12: InitSPI
public async void InitSPI()
{
String aqs = I2cDevice.GetDeviceSelector("I2C1");
var deviceInfo = await DeviceInformation.FindAllAsync(aqs);
sensor = await I2cDevice.FromIdAsync(deviceInfo[0].Id, new I2cConnectionSettings(0x44));
byte[] resetCommand = { 0x30, 0xA2 };
sensor.Write(resetCommand);
TPtimer = ThreadPoolTimer.CreatePeriodicTimer(Timer_Tick, TimeSpan.FromMilliseconds(ENVTime)); // .FromMilliseconds(1000));
}
示例13: InitializeSystem
private async void InitializeSystem(byte[] pinDirection, int inputCheckInterval)
{
byte[] readBuffer;
byte bitMask0;
byte bitMask1;
// initialize I2C communications
string deviceSelector = I2cDevice.GetDeviceSelector();
var i2cDeviceControllers = await DeviceInformation.FindAllAsync(deviceSelector);
if (i2cDeviceControllers.Count == 0)
{
throw new NullReferenceException("No I2C controllers were found on this system. Maybe it isn't a Raspberry Pi?");
}
var i2cSettings = new I2cConnectionSettings(_portExpanderAddress);
i2cSettings.BusSpeed = I2cBusSpeed.FastMode;
i2cPortExpander = await I2cDevice.FromIdAsync(i2cDeviceControllers[0].Id, i2cSettings);
if (i2cPortExpander == null)
{
throw new Exception(string.Format(
"Slave address {0} is currently in use on {1}. " +
"Please ensure that no other applications are using I2C.",
i2cSettings.SlaveAddress,
i2cDeviceControllers[0].Id));
}
// initialize I2C Port Expander registers
try
{
// initialize local copies of the IODIR, GPIO, and OLAT registers
readBuffer = new byte[1];
// read in each register value on register at a time. I'm doing this one at a time
// to keep it straight in my head.
i2cPortExpander.WriteRead(new byte[] { PORT_EXPANDER_IODIR0_REGISTER_ADDRESS }, readBuffer);
iodir0Register = readBuffer[0];
i2cPortExpander.WriteRead(new byte[] { PORT_EXPANDER_IODIR1_REGISTER_ADDRESS }, readBuffer);
iodir1Register = readBuffer[0];
i2cPortExpander.WriteRead(new byte[] { PORT_EXPANDER_GPIO0_REGISTER_ADDRESS }, readBuffer);
gpio0Register = readBuffer[0];
i2cPortExpander.WriteRead(new byte[] { PORT_EXPANDER_GPIO1_REGISTER_ADDRESS }, readBuffer);
gpio1Register = readBuffer[0];
i2cPortExpander.WriteRead(new byte[] { PORT_EXPANDER_OLAT0_REGISTER_ADDRESS }, readBuffer);
olat0Register = readBuffer[0];
i2cPortExpander.WriteRead(new byte[] { PORT_EXPANDER_OLAT1_REGISTER_ADDRESS }, readBuffer);
olat1Register = readBuffer[0];
// configure the output pins to be logic high, leave the other pins as they are.
olat0Register |= pinDirection[0];
_i2CWriteBuffer = new byte[] { PORT_EXPANDER_OLAT0_REGISTER_ADDRESS, olat0Register };
i2cPortExpander.Write(_i2CWriteBuffer);
olat1Register |= pinDirection[1];
_i2CWriteBuffer = new byte[] { PORT_EXPANDER_OLAT1_REGISTER_ADDRESS, olat1Register };
i2cPortExpander.Write(_i2CWriteBuffer);
// configure the specified pins to be an output and leave the other pins as they are.
// input is logic low, output is logic high
bitMask0 = (byte)(0xFF ^ pinDirection[0]); // set the GPIO pin mask bit to '0' for bits that are set, all other bits to '1'
iodir0Register &= bitMask0;
_i2CWriteBuffer = new byte[] { PORT_EXPANDER_IODIR0_REGISTER_ADDRESS, iodir0Register };
i2cPortExpander.Write(_i2CWriteBuffer);
bitMask1 = (byte)(0xFF ^ pinDirection[1]); // set the GPIO pin mask bit to '0' for bits that are set, all other bits to '1'
iodir1Register &= bitMask1;
_i2CWriteBuffer = new byte[] { PORT_EXPANDER_IODIR1_REGISTER_ADDRESS, iodir1Register };
i2cPortExpander.Write(_i2CWriteBuffer);
}
catch (Exception e)
{
throw new Exception("Failed to initialize I2C port expander: " + e.Message);
}
// setup our input checking timer
inputStatusCheckTimer = new DispatcherTimer();
inputStatusCheckTimer.Interval = TimeSpan.FromMilliseconds(inputCheckInterval);
inputStatusCheckTimer.Tick += InputStatusCheckTimer_Tick;
inputStatusCheckTimer.Start();
}
示例14: BeginAsync
/// <summary>
/// Initialize the Sparkfun Weather Shield
/// </summary>
/// <remarks>
/// Setup and instantiate the I2C device objects for the HTDU21D and the MPL3115A2
/// and initialize the blue and green status LEDs.
/// </remarks>
internal async Task BeginAsync()
{
/*
* Acquire the GPIO controller
* MSDN GPIO Reference: https://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.gpio.aspx
*
* Get the default GpioController
*/
GpioController gpio = GpioController.GetDefault();
/*
* Test to see if the GPIO controller is available.
*
* If the GPIO controller is not available, this is
* a good indicator the app has been deployed to a
* computing environment that is not capable of
* controlling the weather shield. Therefore we
* will disable the weather shield functionality to
* handle the failure case gracefully. This allows
* the invoking application to remain deployable
* across the Universal Windows Platform.
*/
if ( null == gpio )
{
available = false;
enable = false;
return;
}
/*
* Initialize the blue LED and set to "off"
*
* Instantiate the blue LED pin object
* Write the GPIO pin value of low on the pin
* Set the GPIO pin drive mode to output
*/
BlueLEDPin = gpio.OpenPin(STATUS_LED_BLUE_PIN, GpioSharingMode.Exclusive);
BlueLEDPin.Write(GpioPinValue.Low);
BlueLEDPin.SetDriveMode(GpioPinDriveMode.Output);
/*
* Initialize the green LED and set to "off"
*
* Instantiate the green LED pin object
* Write the GPIO pin value of low on the pin
* Set the GPIO pin drive mode to output
*/
GreenLEDPin = gpio.OpenPin(STATUS_LED_GREEN_PIN, GpioSharingMode.Exclusive);
GreenLEDPin.Write(GpioPinValue.Low);
GreenLEDPin.SetDriveMode(GpioPinDriveMode.Output);
/*
* Acquire the I2C device
* MSDN I2C Reference: https://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.i2c.aspx
*
* Use the I2cDevice device selector to create an advanced query syntax string
* Use the Windows.Devices.Enumeration.DeviceInformation class to create a collection using the advanced query syntax string
* Take the device id of the first device in the collection
*/
string advanced_query_syntax = I2cDevice.GetDeviceSelector("I2C1");
DeviceInformationCollection device_information_collection = await DeviceInformation.FindAllAsync(advanced_query_syntax);
string deviceId = device_information_collection[0].Id;
/*
* Establish an I2C connection to the HTDU21D
*
* Instantiate the I2cConnectionSettings using the device address of the HTDU21D
* - Set the I2C bus speed of connection to fast mode
* - Set the I2C sharing mode of the connection to shared
*
* Instantiate the the HTDU21D I2C device using the device id and the I2cConnectionSettings
*/
I2cConnectionSettings htdu21d_connection = new I2cConnectionSettings(HTDU21D_I2C_ADDRESS);
htdu21d_connection.BusSpeed = I2cBusSpeed.FastMode;
htdu21d_connection.SharingMode = I2cSharingMode.Shared;
htdu21d = await I2cDevice.FromIdAsync(deviceId, htdu21d_connection);
/*
* Establish an I2C connection to the MPL3115A2
*
* Instantiate the I2cConnectionSettings using the device address of the MPL3115A2
* - Set the I2C bus speed of connection to fast mode
* - Set the I2C sharing mode of the connection to shared
*
* Instantiate the the MPL3115A2 I2C device using the device id and the I2cConnectionSettings
*/
I2cConnectionSettings mpl3115a2_connection = new I2cConnectionSettings(MPL3115A2_I2C_ADDRESS);
mpl3115a2_connection.BusSpeed = I2cBusSpeed.FastMode;
mpl3115a2_connection.SharingMode = I2cSharingMode.Shared;
mpl3115a2 = await I2cDevice.FromIdAsync(deviceId, mpl3115a2_connection);
//.........这里部分代码省略.........
示例15: Init
public override string Init(I2cDevice device) {
// Trigger a soft reset which will wipe the registers to defaults
// This means that resolution is 12-bit RH/14-bit Temperature
device.Write(new byte[] { 0xFE });
return "TEMP_C,REL_HUMID";
}