Exemplo n.º 1
0
        static async Task App()
        {
            var board = await ConnectionService.Instance.GetFirstDeviceAsync();

            await board.ConnectAsync();

            adcPin      = board.Pins[10];
            adcPin.Mode = PinMode.AnalogInput;
            // connect the first shift register to our board.
            var shift1 = new Hc595(board.Spi, board.Pins[6], 8);

            // connect the second one to the first one.
            // Up to 255shift registers can be connected at once
            var shift2 = new Hc595(shift1);

            var ledController1 = new GpioLedDriver <ShiftOutPin>(shift1.Pins, false, shift1);
            var ledController2 = new GpioLedDriver <ShiftOutPin>(shift2.Pins, false, shift2);
            var ledController3 = new LedShiftRegister(shift2, board.Pwm1);

            ledController3.Brightness = 0.5;

            var ledCollection = ledController1.Leds.Concat(ledController2.Leds).ToList();

            bar1 = new BarGraph(ledCollection);
            bar2 = new BarGraph(ledController3.Leds);

            led1().Forget();
            led2().Forget();



            //board.Disconnect();
        }
 internal void StartPin(Pin pin)
 {
     if (pins.ContainsKey(pin.PinNumber))
         return;
     pins.Add(pin.PinNumber, new SoftPwmPinConfig() { Pin = pin, PulseWidthUs = 0, UsePulseWidth = true });
     UpdateConfig();
 }
        /// <summary>
        /// Construct an SPI device attached to a particular module
        /// </summary>
        /// <param name="SpiModule">The module this device is attached to</param>
        /// <param name="ChipSelect">The chip select pin used by this device</param>
        /// <param name="csMode">The ChipSelectMode to use with this device</param>
        /// <param name="SpeedMHz">The speed to operate this device at</param>
        /// <param name="mode">The SpiMode of this device</param>
        public SpiDevice(Spi SpiModule, Pin ChipSelect, ChipSelectMode csMode = ChipSelectMode.SpiActiveLow, double SpeedMHz = 1, SpiMode mode = SpiMode.Mode00)
        {
            this.ChipSelectMode = csMode;
            this.ChipSelect = ChipSelect;
            this.spi = SpiModule;
            this.Frequency = SpeedMHz;
            this.Mode = mode;

            this.ChipSelect.Mode = PinMode.PushPullOutput;

            spi.Enabled = true;
        }
        /// <summary>
        /// Construct a new instance of a Max7219 device
        /// </summary>
        /// <param name="SpiModule">A reference to the Treehopper SPI module</param>
        /// <param name="LoadPin">The pin attached tot he LOAD input</param>
        /// <param name="Address">The index of the Max7219 device attached to this bus</param>
        /// <param name="SpeedMHz">The SPI speed to use. The maximum is 10 MHz.</param>
        public Max7219(Spi SpiModule, Pin LoadPin, int Address = 0, double SpeedMHz = 1) : base(64, true, false)
        {
            if (SpeedMHz > 10)
                throw new ArgumentOutOfRangeException("SpeedMhz", "The MAX7219 supports a maximum clock rate of 10 MHz.");

            dev = new SpiDevice(SpiModule, LoadPin, SpeedMHz: SpeedMHz, csMode: ChipSelectMode.PulseHighAtEnd);

            this.address = Address;
            sendTest(false);

            ScanLimit = 7;
            sendDecodeMode(0);
            Clear().Wait();
            Shutdown = false;
            setBrightness(1);
        }
        internal void StopPin(Pin Pin)
        {
            // first check to make sure the higher PWM pins have been disabled first
            if (Pin.PinNumber == 8 & mode != PwmPinEnableMode.Pin7_Pin8)
                throw new Exception("You must disable PWM functionality on Pin 10 (PWM3) before disabling Pin 9's PWM functionality. See http://treehopper.io/pwm");
            if (Pin.PinNumber == 7 & mode != PwmPinEnableMode.Pin7)
                throw new Exception("You must disable PWM functionality on Pin 9 and 10 (PWM2 and PWM3) before disabling Pin 8's PWM functionality. See http://treehopper.io/pwm");

            switch (Pin.PinNumber)
            {
                case 7:
                    mode = PwmPinEnableMode.None;
                    break;
                case 8:
                    mode = PwmPinEnableMode.Pin7;
                    break;
                case 9:
                    mode = PwmPinEnableMode.Pin7_Pin8;
                    break;
            }

            SendConfig();
        }
        internal void StartPin(Pin Pin)
        {
            // first check to make sure the previous PWM pins have been enabled first.
            if(Pin.PinNumber == 8 & mode != PwmPinEnableMode.Pin7)
                throw new Exception("You must enable PWM functionality on Pin 8 (PWM1) before you enable PWM functionality on Pin 9 (PWM2). See http://treehopper.io/pwm");
            if (Pin.PinNumber == 9 & mode != PwmPinEnableMode.Pin7_Pin8)
                throw new Exception("You must enable PWM functionality on Pin 8 and 9 (PWM1 and PWM2) before you enable PWM functionality on Pin 10 (PWM3). See http://treehopper.io/pwm");
            
            switch(Pin.PinNumber)
            {
                case 7:
                    mode = PwmPinEnableMode.Pin7;
                    break;
                case 8:
                    mode = PwmPinEnableMode.Pin7_Pin8;
                    break;
                case 9:
                    mode = PwmPinEnableMode.Pin7_Pin8_Pin9;
                    break;
            }

            SendConfig();

        }
 internal void StopPin(Pin pin)
 {
     pins.Remove(pin.PinNumber);
     UpdateConfig();
     pin.MakeDigitalInput();
 }
Exemplo n.º 8
0
 internal Pwm(Pin pin)
 {
     Pin = pin;
 }
 internal double GetDutyCycle(Pin pin)
 {
     return pins[pin.PinNumber].DutyCycle;
 }
 internal double GetPulseWidth(Pin pin)
 {
     return pins[pin.PinNumber].PulseWidth;
 }
 internal void SetDutyCycle(Pin pin, double dutyCycle)
 {
     pins[pin.PinNumber].DutyCycle = dutyCycle;
     pins[pin.PinNumber].UsePulseWidth = false;
     UpdateDutyCycles();
 }
 internal void SetPulseWidth(Pin pin, double pulseWidth)
 {
     pins[pin.PinNumber].PulseWidth = pulseWidth;
     pins[pin.PinNumber].UsePulseWidth = true;
     UpdateDutyCycles();
 }
Exemplo n.º 13
0
 internal HardwarePwm(Pin pin)
 {
     _pin   = pin;
     _board = pin.Board;
 }
 internal async void SetDutyCycle(Pin pin, double dutyCycle)
 {
     using (await mutex.LockAsync().ConfigureAwait(false))
     {
         pins[pin.PinNumber].DutyCycle = dutyCycle;
         pins[pin.PinNumber].UsePulseWidth = false;
         UpdateConfig();
     }
         
 }
 /// <summary>
 /// Construct a new 74HC595-type shift register that is directly connected to a Treehopper's SPI port
 /// </summary>
 /// <remarks>
 /// This class supports all 74HC595 shift registers. The name of the class comes from the widely-available TI part.
 /// </remarks>
 public Sn74hc595(Spi spiModule, Pin latchPin, double speedMhz = 1) : base(spiModule, latchPin, 8, SpiMode.Mode00, ChipSelectMode.PulseHighAtEnd, speedMhz)
 {
 }
 /// <summary>
 /// Construct an STP16CPC26 attached directly to a board SPI module
 /// </summary>
 /// <param name="SpiModule">The board's SPI module</param>
 /// <param name="LatchPin">The pin to use for latches</param>
 /// <param name="OutputEnablePin">The output enable pin, if any, to use.</param>
 public Stp16cpc26(Spi SpiModule, Pin LatchPin, DigitalOutPin OutputEnablePin = null) : base(SpiModule, LatchPin, 2)
 {
     oe = OutputEnablePin;
     Start();
 }
 internal void StopPin(Pin pin)
 {
     pins.Remove(pin.PinNumber);
     UpdateConfig();
 }
 internal AnalogIn(Pin Pin)
 {
     pin = Pin;
 }
 internal async void SetPulseWidth(Pin pin, double pulseWidth)
 {
     using (await mutex.LockAsync().ConfigureAwait(false))
     {
         pins[pin.PinNumber].PulseWidthUs = pulseWidth;
         pins[pin.PinNumber].UsePulseWidth = true;
         UpdateConfig();
     }
 }
Exemplo n.º 20
0
        /// <summary>
        /// Starts the Treehopper's SPI interface.
        /// </summary>
        /// <param name="Mode">Configures clock polarity and phase</param>
        /// <param name="RateMHz"></param>
        /// <param name="ChipSelect"></param>
        /// <param name="ChipSelectPolarity"></param>
        /// <param name="ChipSelectDelayMilliseconds"></param>
        /// <param name="InputMode">Chooses whether incoming data is sampled in the middle of the valid data period, or at the end. Most modern sensors produce valid output data in the middle of the waveform period.</param>
        public void Start(SPIMode Mode, double RateMHz, Pin ChipSelect = null, PinPolarity ChipSelectPolarity = PinPolarity.ActiveLow, int ChipSelectDelayMilliseconds = 0, SPISampleMode InputMode = SPISampleMode.Middle)
        {
            this.ChipSelect = ChipSelect;
            this.ChipSelectPolarity = ChipSelectPolarity;
            if(this.ChipSelect != null)
            {
                this.ChipSelect.MakeDigitalOutput();
                if (ChipSelectPolarity == PinPolarity.ActiveLow)
                    this.ChipSelect.DigitalValue = true;
                else
                    this.ChipSelect.DigitalValue = false;
            }

            double SSPADD = (120.0 / RateMHz - 1);
            if (SSPADD > 255)
            {
                throw new Exception("SPI Rate out of limits. Valid rate is 46.875 kHz - 12 MHz");
            }
            byte[] dataToSend = new byte[4];
            dataToSend[0] = (byte)DeviceCommands.SPIConfig;
            dataToSend[1] = (byte)Mode;
            dataToSend[2] = (byte)InputMode;
            dataToSend[3] = (byte)SSPADD;
            device.sendCommsConfigPacket(dataToSend);
        }
 /// <summary>
 /// Construct an STP16CPC26 attached directly to a board SPI module
 /// </summary>
 /// <param name="SpiModule">The board's SPI module</param>
 /// <param name="LatchPin">The pin to use for latches</param>
 /// <param name="OutputEnablePin">The PWM pin to use, allowing controllable global brightness.</param>
 public Stp16cpc26(Spi SpiModule, Pin LatchPin, Pwm OutputEnablePin) : base(SpiModule, LatchPin, 2)
 {
     this.oePwm = OutputEnablePin;
     Start();
 }
 internal void SetDutyCycle(Pin Pin, double value)
 {
     UInt16 PwmRegisterValue = (UInt16)Math.Round((value) * UInt16.MaxValue);
     byte[] newValue = BitConverter.GetBytes(PwmRegisterValue);
     switch(Pin.PinNumber)
     {
         case 7:
             DutyCyclePin7 = newValue;
             break;
         case 8:
             DutyCyclePin8 = newValue;
             break;
         case 9:
             DutyCyclePin9 = newValue;
             break;
     }
     SendConfig();
 }
Exemplo n.º 23
0
 internal AnalogIn(Pin Pin)
 {
     pin = Pin;
 }
 void Pin2_ValueChanged(Pin sender, bool value)
 {
     Outputs["Pin2"].Value = value ? 1.0 : 0.0;
 }
Exemplo n.º 25
0
 internal Task StopPinAsync(Pin pin)
 {
     _pins.Remove(pin.PinNumber);
     return(UpdateConfigAsync());
 }