public Pca9685Device(int busNumber, byte chipNumber, int?clockSpeed, I2cBusSpeed speed = I2cBusSpeed.FastMode, I2cSharingMode sharingMode = I2cSharingMode.Exclusive) { // Validate if (clockSpeed.HasValue && (clockSpeed == 0 || clockSpeed.Value > ClockSpeedMaximum)) { throw new ArgumentOutOfRangeException(nameof(clockSpeed)); } // Get address var address = GetI2cAddress(chipNumber); // Connect to hardware _hardware = I2cExtensions.Connect(busNumber, address, speed, sharingMode); // Initialize configuration ClockIsExternal = clockSpeed.HasValue; ClockSpeed = clockSpeed ?? InternalClockSpeed; FrequencyDefault = CalculateFrequency(PrescaleDefault, ClockSpeed); FrequencyMinimum = CalculateFrequency(PrescaleMaximum, ClockSpeed); // Inverse relationship (max = min) FrequencyMaximum = CalculateFrequency(PrescaleMinimum, ClockSpeed); // Inverse relationship (min = max) // Build channels _channels = new Collection <Pca9685ChannelValue>(); Channels = new ReadOnlyCollection <Pca9685ChannelValue>(_channels); for (var index = 0; index < ChannelCount; index++) { _channels.Add(new Pca9685ChannelValue(index)); } // Set "all call" address I2cExtensions.WriteJoinByte(_hardware, (byte)Pca9685Register.AllCall, I2cAllCallAddress); // Enable auto-increment and "all call" I2cExtensions.WriteReadWriteBit(_hardware, (byte)Pca9685Register.Mode1, (byte)(Pca9685Mode1Bits.AutoIncrement | Pca9685Mode1Bits.AllCall), true); // Read current values and update properties ReadAll(); }
/// <summary> /// Leaves sleep mode. /// </summary> /// <remarks> /// Clears the <see cref="Pca9685Register.Mode1"/> register <see cref="Pca9685Mode1Bits.Sleep"/> bit /// then waits for <see cref="ModeSwitchDelay"/> to allow the oscillator to start. /// </remarks> /// <returns> /// True when mode was changed, false when not sleeping. /// </returns> public bool Wake() { // Read sleep bit (do nothing when already sleeping) var sleeping = I2cExtensions.WriteReadBit(_hardware, (byte)Pca9685Register.Mode1, (byte)Pca9685Mode1Bits.Sleep); if (!sleeping) { return(false); } // Clear sleep bit I2cExtensions.WriteReadWriteBit(_hardware, (byte)Pca9685Register.Mode1, (byte)Pca9685Mode1Bits.Sleep, false); // Wait for completion Task.Delay(ModeSwitchDelay).Wait(); // Update related properties ReadMode1(); // Return changed return(true); }