private async Task Update() { var m1speed = (byte)Math.Round(Math.Abs(m1_speed) * 255); var m2speed = (byte)Math.Round(Math.Abs(m2_speed) * 255); await dev.WriteBufferDataAsync((byte)Registers.MotorSpeedSet, new[] { m1speed, m2speed }).ConfigureAwait(false); await Task.Delay(10).ConfigureAwait(false); if (m1_speed >= 0 && m2_speed >= 0) { await dev.WriteBufferDataAsync((byte)Registers.DirectionSet, new byte[] { (byte)MotorSetDirection.BothClockwise, 0x01 }) .ConfigureAwait(false); } else if (m1_speed >= 0 && m2_speed < 0) { await dev.WriteBufferDataAsync((byte)Registers.DirectionSet, new byte[] { (byte)MotorSetDirection.Motor1ClockwiseMotor2CounterClockwise, 0x01 }) .ConfigureAwait(false); } else if (m1_speed < 0 && m2_speed < 0) { await dev.WriteBufferDataAsync((byte)Registers.DirectionSet, new byte[] { (byte)MotorSetDirection.BothCounterClockwise, 0x01 }) .ConfigureAwait(false); } else if (m1_speed < 0 && m2_speed >= 0) { await dev.WriteBufferDataAsync((byte)Registers.DirectionSet, new byte[] { (byte)MotorSetDirection.Motor1CounterClockwiseMotor2Clockwise, 0x01 }) .ConfigureAwait(false); } }
/// <summary> /// Flush the data to the PCA9685 /// </summary> /// <param name="force">Whether to force the update</param> /// <returns>An awaitable task</returns> public async Task FlushAsync(bool force = false) { foreach (var pin in Pins) { setPinValue(pin); } // send all the registers dev.WriteBufferDataAsync((byte)Registers.LedOnLowBase, pinRegisters).Wait(); }
private Task SetPosition(short position) { var data = new byte[4]; data[0] = 0xff; data[1] = 0xff; data[2] = (byte)(position >> 8); data[3] = (byte)(position & 0xff); return(dev.WriteBufferDataAsync((byte)Command.SetPosition, data)); }
public Task WriteCommandAsync(uint[] command) { var bytes = new byte[command.Length]; for (var i = 0; i < command.Length; i++) { bytes[i] = (byte)command[i]; } return(dev.WriteBufferDataAsync(0x80, bytes)); }
/// <summary> /// Construct a new ADS1115 /// </summary> /// <param name="i2c">The i2C port this ADC is attached to</param> /// <param name="channelMode">Whether this ADC is single-ended or differential</param> /// <param name="addr">The address pin of this module</param> /// <param name="refVoltage">The supply (reference) voltage of this module</param> /// <param name="speed">The speed to use when communicating with this module, in kHz</param> public Ads1115(I2C i2c, ChannelMode channelMode = ChannelMode.SingleEnded, bool addr = false, double refVoltage = 3.3, int speed = 400) { dev = new SMBusDevice((byte)(0x48 | (addr ? 1 : 0)), i2c, speed); Mode = channelMode; dev.WriteBufferDataAsync(0x01, new byte[] { 0x01, 0x83 }).Wait(); for (var i = 0; i < (channelMode == ChannelMode.SingleEnded ? 4 : 2); i++) { Pins.Add(new AdsPin(this)); } }
/// <summary> /// Flush the data out to the LED driver /// </summary> /// <param name="force">Whether the data should be sent even if the data does not appear to have changed</param> /// <returns>An awaitable task</returns> public override Task FlushAsync(bool force = false) { var controls = new byte[36]; for (var i = 0; i < states.Length; i++) { controls[i] = (byte)((((int)Currents[i]) << 1) | (states[i] ? 1 : 0)); } // send the 36 PWM registers, followed by the PWM update register (0x25), followed by the 36 LED control registers, followed by the global control register var dataToWrite = values.Concat(new byte[] { 0x00 }).Concat(controls).Concat(new byte[1] { 0x00 }).ToArray(); return(dev.WriteBufferDataAsync((byte)Registers.PwmBase, dataToWrite)); }
public Pca9632(I2C i2c) { dev = new SMBusDevice(0x62, i2c); mode1.Ai = RegisterAutoIncrement.AutoIncrementAllRegisters; mode1.Sleep = false; dev.WriteByteDataAsync((byte)Registers.Mode1, mode1.ToByte()).Wait(); // clear PWM values dev.WriteBufferDataAsync((byte)Registers.Pwm0 | 0x80, new byte[4] { 0x00, 0x00, 0x00, 0x00 }).Wait(); ledOut.Ldr0 = LedOutputState.LedPwm; ledOut.Ldr1 = LedOutputState.LedPwm; ledOut.Ldr2 = LedOutputState.LedPwm; ledOut.Ldr3 = LedOutputState.LedPwm; dev.WriteByteDataAsync((byte)Registers.LedOut, ledOut.ToByte()).Wait(); Pins.Add(new Pin(this, 0)); Pins.Add(new Pin(this, 1)); Pins.Add(new Pin(this, 2)); Pins.Add(new Pin(this, 3)); }
/// <summary> /// Flush the data out to the LED driver /// </summary> /// <param name="force">Whether the data should be sent even if the data does not appear to have changed</param> /// <returns>An awaitable task</returns> public override Task FlushAsync(bool force = false) { var states = new byte[3]; for (var i = 0; i < currentStates.Length; i++) { var bit = i % 6; var theByte = i / 6; if (currentStates[i]) { states[theByte] |= (byte)(1 << bit); } else { states[theByte] &= (byte)~(1 << bit); } } var dataToWrite = currentValues.Concat(states).Concat(new byte[1] { 0x00 }).ToArray(); return(dev.WriteBufferDataAsync((byte)Registers.PwmBase, dataToWrite)); }
/// <summary> /// Flush the LED states to the HT16K33 driver /// </summary> /// <param name="force">Whether to force an update, even if no data appears to have changed</param> /// <returns></returns> public override Task FlushAsync(bool force = false) { return(dev.WriteBufferDataAsync(0x00, data.GetBytes())); }