Beispiel #1
0
 /// <summary>
 /// Retrieves the spi bus. If the bus channel is not registered it sets it up automatically.
 /// If it had been previously registered, then the bus is simply returned.
 /// </summary>
 /// <param name="channel">The channel.</param>
 /// <param name="frequency">The frequency.</param>
 /// <returns></returns>
 internal static SpiChannel Retrieve(SpiChannelNumber channel, int frequency)
 {
     lock (SyncRoot)
     {
         if (Buses.ContainsKey(channel))
         {
             return(Buses[channel]);
         }
         var newBus = new SpiChannel(channel, frequency);
         Buses[channel] = newBus;
         return(newBus);
     }
 }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SpiChannel"/> class.
        /// </summary>
        /// <param name="channel">The channel.</param>
        /// <param name="frequency">The frequency.</param>
        private SpiChannel(SpiChannelNumber channel, int frequency)
        {
            lock (SyncRoot)
            {
                Frequency      = frequency.Clamp(MinFrequency, MaxFrequency);
                Channel        = (int)channel;
                FileDescriptor = WiringPi.WiringPiSPISetup((int)channel, Frequency);

                if (FileDescriptor < 0)
                {
                    HardwareException.Throw(nameof(SpiChannel), channel.ToString());
                }
            }
        }