Ejemplo n.º 1
0
        public GpioOutputPin(IGpioConnectionDriver driver, ProcessorPin pin)
        {
            this.driver = driver;
            this.pin = pin;

            driver.Allocate(pin, PinDirection.Output);
        }
        /// <summary>
        /// Allocates the specified pin.
        /// </summary>
        /// <param name="pin">The pin.</param>
        /// <param name="direction">The direction.</param>
        public void Allocate(ProcessorPin pin, PinDirection direction)
        {
            var gpioId = string.Format("gpio{0}", (int)pin);
            if (Directory.Exists(Path.Combine(gpioPath, gpioId)))
            {
                // Reinitialize pin virtual file
                using (var streamWriter = new StreamWriter(Path.Combine(gpioPath, "unexport"), false))
                    streamWriter.Write((int) pin);
            }

            // Export pin for file mode
            using (var streamWriter = new StreamWriter(Path.Combine(gpioPath, "export"), false))
                streamWriter.Write((int)pin);

            // Set the direction on the pin and update the exported list
            SetPinMode(pin, direction == PinDirection.Input ? Interop.BCM2835_GPIO_FSEL_INPT : Interop.BCM2835_GPIO_FSEL_OUTP);

            // Set direction in pin virtual file
            var filePath = Path.Combine(gpioId, "direction");
            using (var streamWriter = new StreamWriter(Path.Combine(gpioPath, filePath), false))
                streamWriter.Write(direction == PinDirection.Input ? "in" : "out");

            if (direction == PinDirection.Input)
            {
                PinResistor pinResistor;
                if (!pinResistors.TryGetValue(pin, out pinResistor) || pinResistor != PinResistor.None)
                    SetPinResistor(pin, PinResistor.None);

                SetPinDetectedEdges(pin, PinDetectedEdges.Both);
                InitializePoll(pin);
            }
        }
Ejemplo n.º 3
0
        public SpiConnection(ProcessorPin clock, ProcessorPin ss, ProcessorPin? miso, ProcessorPin? mosi, Endianness endianness)
        {
            this.clock = clock;
            this.ss = ss;
            this.miso = miso;
            this.mosi = mosi;
            this.endianness = endianness;

            driver = GpioConnectionSettings.DefaultDriver;

            driver.Allocate(clock, PinDirection.Output);
            driver.Write(clock, false);

            driver.Allocate(ss, PinDirection.Output);
            driver.Write(ss, true);

            if (mosi.HasValue)
            {
                driver.Allocate(mosi.Value, PinDirection.Output);
                driver.Write(mosi.Value, false);
            }

            if (miso.HasValue)
                driver.Allocate(miso.Value, PinDirection.Input);
        }
        /// <summary>
        /// Allocates the specified pin.
        /// </summary>
        /// <param name="pin">The pin.</param>
        /// <param name="direction">The direction.</param>
        public void Allocate(ProcessorPin pin, PinDirection direction)
        {
            Release(pin);

            using (var streamWriter = new StreamWriter(Path.Combine(gpioPath, "export"), false))
                streamWriter.Write((int)pin);

            if (!gpioPathList.ContainsKey(pin))
            {
                var gpio = new FileGpioHandle { GpioPath = GuessGpioPath(pin) };
                gpioPathList.Add(pin, gpio);
            }

            var filePath = Path.Combine(gpioPathList[pin].GpioPath, "direction");
            try {
                SetPinDirection(filePath, direction);
            }
            catch (UnauthorizedAccessException) {
                // program hasn't been started as root, give it a second to correct file permissions
                Thread.Sleep(TimeSpan.FromSeconds(1));
                SetPinDirection(filePath, direction);
            }

            gpioPathList[pin].GpioStream = new FileStream(Path.Combine(GuessGpioPath(pin), "value"), FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="I2cDriver"/> class.
        /// </summary>
        /// <param name="sdaPin">The SDA pin.</param>
        /// <param name="sclPin">The SCL pin.</param>
        public I2cDriver(ProcessorPin sdaPin, ProcessorPin sclPin)
        {
            this.sdaPin = sdaPin;
            this.sclPin = sclPin;

            var bscBase = GetBscBase(sdaPin, sclPin);

            var memoryFile = Interop.open("/dev/mem", Interop.O_RDWR + Interop.O_SYNC);
            try
            {
                gpioAddress = Interop.mmap(IntPtr.Zero, Interop.BCM2835_BLOCK_SIZE, Interop.PROT_READ | Interop.PROT_WRITE, Interop.MAP_SHARED, memoryFile, Interop.BCM2835_GPIO_BASE);
                bscAddress = Interop.mmap(IntPtr.Zero, Interop.BCM2835_BLOCK_SIZE, Interop.PROT_READ | Interop.PROT_WRITE, Interop.MAP_SHARED, memoryFile, bscBase);
            }
            finally
            {
                Interop.close(memoryFile);
            }

            if (bscAddress == (IntPtr) Interop.MAP_FAILED)
                throw new InvalidOperationException("Unable to access device memory");

            // Set the I2C pins to the Alt 0 function to enable I2C access on them
            SetPinMode((uint) (int) sdaPin, Interop.BCM2835_GPIO_FSEL_ALT0); // SDA
            SetPinMode((uint) (int) sclPin, Interop.BCM2835_GPIO_FSEL_ALT0); // SCL

            // Read the clock divider register
            var dividerAddress = bscAddress + (int) Interop.BCM2835_BSC_DIV;
            var divider = (ushort) SafeReadUInt32(dividerAddress);
            waitInterval = GetWaitInterval(divider);

            var addressAddress = bscAddress + (int) Interop.BCM2835_BSC_A;
            SafeWriteUInt32(addressAddress, (uint) currentDeviceAddress);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="GpioOutputBinaryPin"/> class.
        /// </summary>
        /// <param name="driver">The driver.</param>
        /// <param name="pin">The pin.</param>
        /// <param name="resistor">The resistor.</param>
        public GpioOutputBinaryPin(IGpioConnectionDriver driver, ProcessorPin pin, PinResistor resistor = PinResistor.None)
        {
            this.driver = driver;
            this.pin = pin;

            driver.Allocate(pin, PinDirection.Output);
            driver.SetPinResistor(pin, resistor);
        }
        /// <summary>
        /// Allocates the specified pin.
        /// </summary>
        /// <param name="pin">The pin.</param>
        /// <param name="direction">The direction.</param>
        public void Allocate(ProcessorPin pin, PinDirection direction)
        {
            // Set the direction on the pin and update the exported list
            SetPinMode(pin, direction == PinDirection.Input ? Interop.BCM2835_GPIO_FSEL_INPT : Interop.BCM2835_GPIO_FSEL_OUTP);

            if (direction == PinDirection.Input)
                SetPinResistor(pin, PinResistor.None);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="Hd44780LcdConnection"/> class.
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <param name="registerSelectPin">The register select pin.</param>
        /// <param name="clockPin">The clock pin.</param>
        /// <param name="dataPins">The data pins.</param>
        public Hd44780LcdConnection(Hd44780LcdConnectionSettings settings, ProcessorPin registerSelectPin, ProcessorPin clockPin, IEnumerable<ProcessorPin> dataPins)
        {
            settings = settings ?? new Hd44780LcdConnectionSettings();

            this.registerSelectPin = registerSelectPin;
            this.clockPin = clockPin;
            this.dataPins = dataPins.ToArray();

            if (this.dataPins.Length != 4 && this.dataPins.Length != 8)
                throw new ArgumentOutOfRangeException("dataPins", this.dataPins.Length, "There must be either 4 or 8 data pins");

            width = settings.ScreenWidth;
            height = settings.ScreenHeight;
            if (height < 1 || height > 2)
                throw new ArgumentOutOfRangeException("ScreenHeight", height, "Screen must have either 1 or 2 rows");
            if (width * height > 80)
                throw new ArgumentException("At most 80 characters are allowed");

            if (settings.PatternWidth != 5)
                throw new ArgumentOutOfRangeException("PatternWidth", settings.PatternWidth, "Pattern must be 5 pixels width");
            if (settings.PatternHeight != 8 && settings.PatternHeight != 10)
                throw new ArgumentOutOfRangeException("PatternHeight", settings.PatternWidth, "Pattern must be either 7 or 10 pixels height");
            if (settings.PatternHeight == 10 && height == 2)
                throw new ArgumentException("10 pixels height pattern cannot be used with 2 rows");

            functions = (settings.PatternHeight == 8 ? Functions.Matrix5x8 : Functions.Matrix5x10)
                | (height == 1 ? Functions.OneLine : Functions.TwoLines)
                | (this.dataPins.Length == 4 ? Functions.Data4bits : Functions.Data8bits);

            entryModeFlags = /*settings.RightToLeft
                ? EntryModeFlags.EntryRight | EntryModeFlags.EntryShiftDecrement
                :*/ EntryModeFlags.EntryLeft | EntryModeFlags.EntryShiftDecrement;

            encoding = settings.Encoding;

            connectionDriver = new MemoryGpioConnectionDriver();

            connectionDriver.Allocate(registerSelectPin, PinDirection.Output);
            connectionDriver.Write(registerSelectPin, false);

            connectionDriver.Allocate(clockPin, PinDirection.Output);
            connectionDriver.Write(clockPin, false);

            foreach (var dataPin in this.dataPins)
            {
                connectionDriver.Allocate(dataPin, PinDirection.Output);
                connectionDriver.Write(dataPin, false);
            }

            WriteByte(0x33, false); // Initialize
            WriteByte(0x32, false);

            WriteCommand(Command.SetFunctions, (int) functions);
            WriteCommand(Command.SetDisplayFlags, (int) displayFlags);
            WriteCommand(Command.SetEntryModeFlags, (int) entryModeFlags);

            Clear();
        }
        /// <summary>
        /// Waits for a pin to reach the specified state, then measures the time it remains in this state.
        /// </summary>
        /// <param name="driver">The driver.</param>
        /// <param name="pin">The measure pin.</param>
        /// <param name="waitForUp">if set to <c>true</c>, wait for the pin to be up.</param>
        /// <param name="phase1Timeout">The first phase timeout.</param>
        /// <param name="phase2Timeout">The second phase timeout.</param>
        /// <returns>
        /// The time the pin remains up, in milliseconds.
        /// </returns>
        public static decimal Time(this IGpioConnectionDriver driver, ProcessorPin pin, bool waitForUp = true, decimal phase1Timeout = 0, decimal phase2Timeout = 0)
        {
            driver.Wait(pin, waitForUp, phase1Timeout);

            var waitDown = DateTime.Now.Ticks;
            driver.Wait(pin, !waitForUp, phase2Timeout);

            return (DateTime.Now.Ticks - waitDown)/10000m;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="GpioOutputBinaryPin"/> class.
        /// </summary>
        /// <param name="driver">The driver.</param>
        /// <param name="pin">The pin.</param>
        /// <param name="resistor">The resistor.</param>
        public GpioOutputBinaryPin(IGpioConnectionDriver driver, ProcessorPin pin, PinResistor resistor = PinResistor.None)
        {
            this.driver = driver;
            this.pin = pin;

            driver.Allocate(pin, PinDirection.Output);
            if ((driver.GetCapabilities() & GpioConnectionDriverCapabilities.CanSetPinResistor) > 0)
                driver.SetPinResistor(pin, resistor);
        }
        /// <summary>
        /// Reads the status of the specified pin.
        /// </summary>
        /// <param name="pin">The pin.</param>
        /// <returns>
        /// The pin status.
        /// </returns>
        public bool Read(ProcessorPin pin)
        {
            int shift;
            var offset = Math.DivRem((int) pin, 32, out shift);

            var pinGroupAddress = gpioAddress + (int) (Interop.BCM2835_GPLEV0 + offset);
            var value = SafeReadUInt32(pinGroupAddress);

            return (value & (1 << shift)) != 0;
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HcSr04Connection"/> class.
        /// </summary>
        /// <param name="triggerPin">The trigger pin.</param>
        /// <param name="echoPin">The echo pin.</param>
        public HcSr04Connection(ProcessorPin triggerPin, ProcessorPin echoPin)
        {
            this.triggerPin = triggerPin;
            this.echoPin = echoPin;

            driver = new MemoryGpioConnectionDriver();

            driver.Allocate(triggerPin, PinDirection.Output);
            driver.Allocate(echoPin, PinDirection.Input);
        }
        /// <summary>
        /// Reads the status of the specified pin.
        /// </summary>
        /// <param name="pin">The pin.</param>
        /// <returns>
        /// The pin status.
        /// </returns>
        public bool Read(ProcessorPin pin)
        {
            var gpioId = string.Format("gpio{0}", (int) pin);
            var filePath = Path.Combine(gpioId, "value");

            using (var streamReader = new StreamReader(new FileStream(Path.Combine(gpioPath, filePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
            {
                var rawValue = streamReader.ReadToEnd();
                return !string.IsNullOrEmpty(rawValue) && rawValue[0] == '1';
            }
        }
        /// <summary>
        /// Reads the status of the specified pin.
        /// </summary>
        /// <param name="pin">The pin.</param>
        /// <returns>
        /// The pin status.
        /// </returns>
        public bool Read(ProcessorPin pin)
        {
            var shift = (byte)((int)pin % 32);
            var value = Interop.bcm2835_gpioperi_read((uint)pin / 32);

            return (value & (1 << shift)) != 0;

            /*
            var value = Interop.bcm2835_gpio_lev((uint)pin);
            return value != 0;
            */
        }
        /// <summary>
        /// Waits for the specified pin to reach a given state.
        /// </summary>
        /// <param name="driver">The driver.</param>
        /// <param name="pin">The pin.</param>
        /// <param name="waitForUp">if set to <c>true</c>, waits for the pin to become up; otherwise, waits for the pin to become down.</param>
        /// <param name="timeout">The timeout.</param>
        public static void Wait(this IGpioConnectionDriver driver, ProcessorPin pin, bool waitForUp = true, int timeout = 0)
        {
            if (timeout == 0)
                timeout = 5000;

            var startWait = DateTime.Now;
            while (driver.Read(pin) != waitForUp)
            {
                if (DateTime.Now.Ticks - startWait.Ticks >= 10000*timeout)
                    throw new TimeoutException();
            }
        }
        /// <summary>
        /// Allocates the specified pin.
        /// </summary>
        /// <param name="pin">The pin.</param>
        /// <param name="direction">The direction.</param>
        public void Allocate(ProcessorPin pin, PinDirection direction)
        {
            var gpioId = string.Format("gpio{0}", (int)pin);
            if (Directory.Exists(Path.Combine(gpioPath, gpioId)))
                Release(pin);

            using (var streamWriter = new StreamWriter(Path.Combine(gpioPath, "export"), false))
                streamWriter.Write((int)pin);

            var filePath = Path.Combine(gpioId, "direction");
            using (var streamWriter = new StreamWriter(Path.Combine(gpioPath, filePath), false))
                streamWriter.Write(direction == PinDirection.Input ? "in" : "out");
        }
        /// <summary>
        /// Measures the time the specified pin remains up.
        /// </summary>
        /// <param name="driver">The driver.</param>
        /// <param name="pin">The measure pin.</param>
        /// <param name="waitForDown">if set to <c>true</c>, waits for the pin to become down; otherwise, waits for the pin to become up.</param>
        /// <param name="timeout">The timeout.</param>
        /// <returns>
        /// The time the pin remains up, in milliseconds.
        /// </returns>
        public static decimal Time(this IGpioConnectionDriver driver, ProcessorPin pin, bool waitForDown = true, int timeout = 0)
        {
            if (timeout == 0)
                timeout = 5000;

            var waitDown = DateTime.Now;
            while (driver.Read(pin) == waitForDown)
            {
                if (DateTime.Now.Ticks - waitDown.Ticks >= 10000*timeout)
                    throw new TimeoutException();
            }

            return (DateTime.Now.Ticks - waitDown.Ticks)/10000m;
        }
        /// <summary>
        /// Allocates the specified pin.
        /// </summary>
        /// <param name="pin">The pin.</param>
        /// <param name="direction">The direction.</param>
        public void Allocate(ProcessorPin pin, PinDirection direction)
        {
            // Set the direction on the pin and update the exported list
            // BCM2835_GPIO_FSEL_INPT = 0
            // BCM2835_GPIO_FSEL_OUTP = 1
            Interop.bcm2835_gpio_fsel((uint)pin, (uint)(direction == PinDirection.Input ? 0 : 1));

            if (direction == PinDirection.Input)
            {
                // BCM2835_GPIO_PUD_OFF = 0b00 = 0
                // BCM2835_GPIO_PUD_DOWN = 0b01 = 1
                // BCM2835_GPIO_PUD_UP = 0b10 = 2
                Interop.bcm2835_gpio_set_pud((uint)pin, 0);
            }
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HcSr04Connection"/> class.
        /// </summary>
        /// <param name="triggerPin">The trigger pin.</param>
        /// <param name="echoPin">The echo pin.</param>
        public HcSr04Connection(ProcessorPin triggerPin, ProcessorPin echoPin)
        {
            this.triggerPin = triggerPin;
            this.echoPin = echoPin;

            Timeout = DefaultTimeout;

            driver = GpioConnectionSettings.DefaultDriver;

            driver.Allocate(echoPin, PinDirection.Input);
            driver.Allocate(triggerPin, PinDirection.Output);

            try
            {
                GetDistance();
            } catch {}
        }
Ejemplo n.º 20
0
Archivo: PIR.cs Proyecto: kjwitt/AVA
        public PIR()
        {
            ConnectorPin PIRPin = ConnectorPin.P1Pin07;

            procPin = PIRPin.ToProcessor();

            //driver = GpioConnectionSettings.DefaultDriver;

            driver.Allocate(procPin, PinDirection.Input);

            Debug.DebugStatement("Sensor Setup Completed",ConsoleColor.White);

            Thread PIRThread = new Thread(SetStatus);
            PIRThread.Start();

            Debug.DebugStatement("Thread Started",ConsoleColor.White);
        }
        /// <summary>
        /// Allocates the specified pin.
        /// </summary>
        /// <param name="pin">The pin.</param>
        /// <param name="direction">The direction.</param>
        public void Allocate(ProcessorPin pin, PinDirection direction)
        {
            var gpioId = string.Format("gpio{0}", (int)pin);
            if (Directory.Exists(Path.Combine(gpioPath, gpioId)))
                Release(pin);

            using (var streamWriter = new StreamWriter(Path.Combine(gpioPath, "export"), false))
                streamWriter.Write((int)pin);

            var filePath = Path.Combine(gpioPath, gpioId, "direction");
            try {
                SetPinDirection(filePath, direction);
            } catch (UnauthorizedAccessException) {
                // program hasn't been started as root, give it a second to correct file permissions
                Thread.Sleep(TimeSpan.FromSeconds(1));
                SetPinDirection(filePath, direction);
            }
        }
        /// <summary>
        /// Sets the pin resistor.
        /// </summary>
        /// <param name="pin">The pin.</param>
        /// <param name="resistor">The resistor.</param>
        public void SetPinResistor(ProcessorPin pin, PinResistor resistor)
        {
            // Set the pullup/down resistor for a pin
            //
            // The GPIO Pull-up/down Clock Registers control the actuation of internal pull-downs on
            // the respective GPIO pins. These registers must be used in conjunction with the GPPUD
            // register to effect GPIO Pull-up/down changes. The following sequence of events is
            // required:
            // 1. Write to GPPUD to set the required control signal (i.e. Pull-up or Pull-Down or neither
            // to remove the current Pull-up/down)
            // 2. Wait 150 cycles ? this provides the required set-up time for the control signal
            // 3. Write to GPPUDCLK0/1 to clock the control signal into the GPIO pads you wish to
            // modify ? NOTE only the pads which receive a clock will be modified, all others will
            // retain their previous state.
            // 4. Wait 150 cycles ? this provides the required hold time for the control signal
            // 5. Write to GPPUD to remove the control signal
            // 6. Write to GPPUDCLK0/1 to remove the clock
            //
            // RPi has P1-03 and P1-05 with 1k8 pullup resistor

            uint pud;
            switch(resistor)
            {
                case PinResistor.None:
                    pud = Interop.BCM2835_GPIO_PUD_OFF;
                    break;
                case PinResistor.PullDown:
                    pud = Interop.BCM2835_GPIO_PUD_DOWN;
                    break;
                case PinResistor.PullUp:
                    pud = Interop.BCM2835_GPIO_PUD_UP;
                    break;

                default:
                    throw new ArgumentOutOfRangeException("resistor", resistor, string.Format(CultureInfo.InvariantCulture, "{0} is not a valid value for pin resistor", resistor));
            }

            WriteResistor(pud);
            HighResolutionTimer.Sleep(0.005m);
            SetPinResistorClock(pin, true);
            HighResolutionTimer.Sleep(0.005m);
            WriteResistor(Interop.BCM2835_GPIO_PUD_OFF);
            SetPinResistorClock(pin, false);
        }
        public Sda5708Connection(ProcessorPin load, ProcessorPin data, ProcessorPin sdclk, ProcessorPin reset)
        {
            this._load = load;
            this._data = data;
            this._sdclk = sdclk;
            this._reset = reset;

            this._baseConnection = new GpioConnection (
                load.Output (),
                data.Output (),
                sdclk.Output (),
                reset.Output ());

            this._baseConnection [reset] = false;
            this._baseConnection [reset] = false;
            Thread.Sleep (50);
            this._baseConnection [reset] = true;

            this.Clear();
        }
		public GroveRgbConnection(ConnectorPin dataPin, ConnectorPin clockPin, int ledCount)
		{
			ledColors = new List<RgbColor>();
			for(int i = 0; i < ledCount; i++)
			{
				// Initialize all leds with white color
				ledColors.Add(new RgbColor());
			}
			this.dataPin = dataPin.ToProcessor();
			this.clockPin = clockPin.ToProcessor();
			if (Raspberry.Board.Current.IsRaspberryPi) 
			{
				driver = new GpioConnectionDriver();
			}
			else 
			{
				driver = new FileGpioConnectionDriver();
			}
			driver.Allocate(this.dataPin, PinDirection.Output);
			driver.Allocate(this.clockPin, PinDirection.Output);
		}
Ejemplo n.º 25
0
 /// <summary>
 /// Blinks the specified pin.
 /// </summary>
 /// <param name="pin">The pin.</param>
 /// <param name="duration">The duration, in millisecond.</param>
 public void Blink(ProcessorPin pin, decimal duration = -1)
 {
     Toggle(pin);
     Sleep(duration);
     Toggle(pin);
 }
Ejemplo n.º 26
0
        private static void Main(string[] args)
        {
            Console.CursorVisible = false;


            var driver = GpioConnectionSettings.DefaultDriver;

            led1      = ConnectorPin.P1Pin11.Output();
            led2      = ConnectorPin.P1Pin13.Output();
            buttonPin = ProcessorPin.Pin18;
            var filename = @"/usr/share/scratch/Media/Sounds/Electronic/ComputerBeeps1.wav";
            var stream   = new MemoryStream();

            using (var file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                file.CopyTo(stream);
            }
            stream.Position = 0;
            //player = new SoundPlayer (filename);
            var isPlaying = false;

            driver.Allocate(buttonPin, PinDirection.Input);
            driver.Allocate(led1.Pin, PinDirection.Output);
            driver.Allocate(led2.Pin, PinDirection.Output);



            const ConnectorPin triggerPin = ConnectorPin.P1Pin10;
            const ConnectorPin echoPin    = ConnectorPin.P1Pin08;

            Console.WriteLine("HC-SR04 Sample: measure distance");
            Console.WriteLine();
            Console.WriteLine("\tTrigger: {0}", triggerPin);
            Console.WriteLine("\tEcho: {0}", echoPin);
            Console.WriteLine();

            var interval = GetInterval(args);
            //var driver = GpioConnectionSettings.DefaultDriver;
            bool toggle = true;

            using (var connection = new HcSr04Connection(
                       driver.Out(triggerPin.ToProcessor()),
                       driver.In(echoPin.ToProcessor()))) {
                while (true)
                {
                    try {
                        var distance = connection.GetDistance().Centimeters;
                        if (distance < 100)
                        {
                            if (!isPlaying)
                            {
                                isPlaying = true;
                                Task.Run(() => {
                                    stream.Position = 0;

                                    //player = new SoundPlayer(
                                    lock (stream) {
                                        using (player = new SoundPlayer(stream)) {
                                            try {
                                                player.PlaySync();
                                            } catch (Exception ex) {
                                                Console.WriteLine(ex.ToString());
                                            }
                                            Timer.Sleep(TimeSpan.FromSeconds(3));
                                        }
                                    }
                                    isPlaying = false;
                                });
                            }
                            driver.Write(led1.Pin, toggle);
                            toggle = !toggle;
                            driver.Write(led2.Pin, toggle);
                        }
                        else
                        {
                            if (isPlaying)
                            {
                                //lock (player) {
                                //	player.Stop ();
                                //}
                                //player.Dispose();
                                //	isPlaying = false;
                            }
                            driver.Write(led1.Pin, false);
                            driver.Write(led2.Pin, false);
                        }
                        //Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0:0.0}cm", distance).PadRight(16));
                        //Console.CursorTop--;
                    } catch (TimeoutException e) {
                        Console.WriteLine("(Timeout): " + e.Message);
                    }

                    Timer.Sleep(interval);
                }
            }

            Console.CursorVisible = true;
        }
Ejemplo n.º 27
0
 /// <summary>
 /// Removes the specified pin.
 /// </summary>
 /// <param name="pin">The pin.</param>
 public void Remove(ProcessorPin pin)
 {
     Remove(pinConfigurations[pin]);
 }
Ejemplo n.º 28
0
 internal PinConfiguration GetConfiguration(ProcessorPin pin)
 {
     return(pinConfigurations[pin]);
 }
Ejemplo n.º 29
0
 /// <summary>
 /// Sets the detected edges on an input pin.
 /// </summary>
 /// <param name="pin">The pin.</param>
 /// <param name="edges">The edges.</param>
 /// <remarks>
 /// By default, both edges may be detected on input pins.
 /// </remarks>
 public void SetPinDetectedEdges(ProcessorPin pin, PinDetectedEdges edges)
 {
     throw new NotSupportedException("Edge detection is not supported by memory GPIO connection driver");
 }
Ejemplo n.º 30
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GpioInputOutputBinaryPin"/> class.
 /// </summary>
 /// <param name="driver">The driver.</param>
 /// <param name="pin">The pin.</param>
 /// <param name="resistor">The resistor.</param>
 public GpioInputOutputBinaryPin(IGpioConnectionDriver driver, ProcessorPin pin, PinResistor resistor = PinResistor.None)
 {
     this.driver   = driver;
     this.pin      = pin;
     this.resistor = resistor;
 }
Ejemplo n.º 31
0
 /// <summary>
 /// Toggles the specified pin.
 /// </summary>
 /// <param name="pin">The pin.</param>
 public void Toggle(ProcessorPin pin)
 {
     this[pin] = !this[pin];
 }
Ejemplo n.º 32
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PinConfiguration"/> class.
 /// </summary>
 /// <param name="pin">The pin.</param>
 protected PinConfiguration(ProcessorPin pin)
 {
     this.Pin = pin;
 }
Ejemplo n.º 33
0
 /// <summary>
 /// Sets the pin resistor.
 /// </summary>
 /// <param name="pin">The pin.</param>
 /// <param name="resistor">The resistor.</param>
 public void SetPinResistor(ProcessorPin pin, PinResistor resistor)
 {
     throw new NotSupportedException("Resistor are not supported by file GPIO connection driver");
 }
Ejemplo n.º 34
0
 /// <summary>
 /// </summary>
 public void Release(ProcessorPin pin)
 {
 }
Ejemplo n.º 35
0
 /// <summary>
 /// Modified the status of a pin.
 /// </summary>
 /// <param name="pin">The pin.</param>
 /// <param name="value">The pin status.</param>
 public void Write(ProcessorPin pin, bool value)
 {
     gpioPathList[pin].GpioStream.Seek(0, SeekOrigin.Begin);
     gpioPathList[pin].GpioStream.WriteByte(value ? (byte)'1' : (byte)'0');
     gpioPathList[pin].GpioStream.Flush();
 }
Ejemplo n.º 36
0
 public void Release(ProcessorPin pin)
 {
     // TODO: Release pin ?
 }
Ejemplo n.º 37
0
 /// <summary>
 /// Modified the status of a pin.
 /// </summary>
 /// <param name="pin">The pin.</param>
 /// <param name="value">The pin status.</param>
 public void Write(ProcessorPin pin, bool value)
 {
     Interop.bcm2835_gpio_write((uint)pin, (uint)(value ? 1 : 0));
 }
Ejemplo n.º 38
0
 public Led(ProcessorPin pin, IGpioConnectionDriver driver = null) : this(new OutputPinConfiguration(pin), driver)
 {
 }
Ejemplo n.º 39
0
 public static void Remove(ProcessorPin processorPin)
 {
     //if (CommonHelper.IsBoard)
     _gpioConnectionGlobalPin.Remove(processorPin);
 }
        /// <summary>
        /// Sets the detected edges on an input pin.
        /// </summary>
        /// <param name="pin">The pin.</param>
        /// <param name="edges">The edges.</param>
        /// <remarks>By default, both edges may be detected on input pins.</remarks>
        public void SetPinDetectedEdges(ProcessorPin pin, PinDetectedEdges edges)
        {
            var edgePath = Path.Combine(gpioPath, string.Format("gpio{0}/edge", (int)pin));

            HelperFileStream.SaveToFile(edgePath, ToString(edges));
        }
Ejemplo n.º 41
0
        private static uint GetBscBase(ProcessorPin sdaPin, ProcessorPin sclPin)
        {
            switch (GpioConnectionSettings.BoardConnectorRevision)
            {
                case 1:
                    if (sdaPin == ProcessorPin.Pin0 && sclPin == ProcessorPin.Pin1)
                        return Interop.BCM2835_BSC0_BASE;
                    throw new InvalidOperationException("No I2C device exist on the specified pins");

                case 2:
                    if (sdaPin == ProcessorPin.Pin28 && sclPin == ProcessorPin.Pin29)
                        return Interop.BCM2835_BSC0_BASE;
                    if (sdaPin == ProcessorPin.Pin2 && sclPin == ProcessorPin.Pin3)
                        return Interop.BCM2835_BSC1_BASE;
                    throw new InvalidOperationException("No I2C device exist on the specified pins");

                case 3:
                    if (sdaPin == ProcessorPin.Pin2 && sclPin == ProcessorPin.Pin3)
                        return Interop.BCM2835_BSC1_BASE;
                    throw new InvalidOperationException("No I2C device exist on the specified pins");

                default:
                    throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "Board revision {0} is not supported", GpioConnectionSettings.BoardConnectorRevision));
            }
        }
Ejemplo n.º 42
0
        public Sda5708Connection(ProcessorPin load, ProcessorPin data, ProcessorPin sdclk, ProcessorPin reset)
        {
            this._load  = load;
            this._data  = data;
            this._sdclk = sdclk;
            this._reset = reset;

            this._baseConnection = new GpioConnection(
                load.Output(),
                data.Output(),
                sdclk.Output(),
                reset.Output());

            this._baseConnection [reset] = false;
            this._baseConnection [reset] = false;
            Thread.Sleep(50);
            this._baseConnection [reset] = true;

            this.Clear();
        }
Ejemplo n.º 43
0
 /// <summary>
 /// Removes the specified pin.
 /// </summary>
 /// <param name="pin">The pin.</param>
 public void Remove(ProcessorPin pin)
 {
     Remove(pinConfigurations[pin]);
 }
Ejemplo n.º 44
0
 /// <summary>
 /// Blinks the specified pin.
 /// </summary>
 /// <param name="pin">The pin.</param>
 /// <param name="duration">The duration.</param>
 public void Blink(ProcessorPin pin, TimeSpan duration = new TimeSpan())
 {
     Toggle(pin);
     Sleep(duration);
     Toggle(pin);
 }
Ejemplo n.º 45
0
 /// <summary>
 /// Blinks the specified pin.
 /// </summary>
 /// <param name="pin">The pin.</param>
 /// <param name="duration">The duration, in millisecond.</param>
 public void Blink(ProcessorPin pin, decimal duration = -1)
 {
     Toggle(pin);
     Sleep(duration);
     Toggle(pin);
 }
Ejemplo n.º 46
0
 /// <summary>
 /// Gets a bidirectional pin on the current driver.
 /// </summary>
 /// <param name="driver">The driver.</param>
 /// <param name="pin">The pin.</param>
 /// <param name="resistor">The resistor.</param>
 /// <returns>
 /// The GPIO input binary pin.
 /// </returns>
 public static GpioInputOutputBinaryPin InOut(this IGpioConnectionDriver driver, ProcessorPin pin, PinResistor resistor = PinResistor.None)
 {
     return(new GpioInputOutputBinaryPin(driver, pin, resistor));
 }
Ejemplo n.º 47
0
 /// <summary>
 /// Initializes a new instance of the <see cref="OutputPinConfiguration"/> class.
 /// </summary>
 /// <param name="pin">The pin.</param>
 public OutputPinConfiguration(ProcessorPin pin) : base(pin)
 {
 }
Ejemplo n.º 48
0
 /// <summary>
 /// Releases the specified pin.
 /// </summary>
 /// <param name="pin">The pin.</param>
 public void Release(ProcessorPin pin)
 {
     using (var streamWriter = new StreamWriter(Path.Combine(gpioPath, "unexport"), false))
         streamWriter.Write((int)pin);
 }
Ejemplo n.º 49
0
 /// <summary>
 /// Releases the specified pin.
 /// </summary>
 /// <param name="pin">The pin.</param>
 public void Release(ProcessorPin pin)
 {
     SetPinMode(pin, Interop.BCM2835_GPIO_FSEL_INPT);
 }
Ejemplo n.º 50
0
 /// <summary>
 /// Sets the pin resistor.
 /// </summary>
 /// <param name="pin">The pin.</param>
 /// <param name="resistor">The resistor.</param>
 public void SetPinResistor(ProcessorPin pin, PinResistor resistor)
 {
     throw new NotSupportedException();
 }
Ejemplo n.º 51
0
 /// <summary>
 /// Determines whether the connection contains the specified pin.
 /// </summary>
 /// <param name="pin">The pin.</param>
 /// <returns>
 ///   <c>true</c> if the connection contains the specified pin; otherwise, <c>false</c>.
 /// </returns>
 public bool Contains(ProcessorPin pin)
 {
     return pinConfigurations.ContainsKey(pin);
 }
Ejemplo n.º 52
0
 /// <summary>
 /// Sets the detected edges on an input pin.
 /// </summary>
 /// <param name="pin">The pin.</param>
 /// <param name="edges">The edges.</param>
 /// <exception cref="System.NotImplementedException"></exception>
 /// <remarks>
 /// By default, both edges may be detected on input pins.
 /// </remarks>
 public void SetPinDetectedEdges(ProcessorPin pin, PinDetectedEdges edges)
 {
     throw new NotSupportedException();
 }
Ejemplo n.º 53
0
 /// <summary>
 /// Toggles the specified pin.
 /// </summary>
 /// <param name="pin">The pin.</param>
 public void Toggle(ProcessorPin pin)
 {
     this[pin] = !this[pin];
 }
Ejemplo n.º 54
0
 /// <summary>
 /// Gets an output pin on the current driver.
 /// </summary>
 /// <param name="driver">The driver.</param>
 /// <param name="pin">The pin.</param>
 /// <returns>The GPIO output binary pin.</returns>
 public static GpioOutputBinaryPin Out(this IGpioConnectionDriver driver, ProcessorPin pin)
 {
     return(new GpioOutputBinaryPin(driver, pin));
 }
Ejemplo n.º 55
0
 internal PinConfiguration GetConfiguration(ProcessorPin pin)
 {
     return pinConfigurations[pin];
 }
Ejemplo n.º 56
0
 /// <summary>
 /// Gets or sets the status of the specified pin.
 /// </summary>
 public bool this[ProcessorPin pin]
 {
     get { return(this[pinConfigurations[pin]]); }
     set { this[pinConfigurations[pin]] = value; }
 }
Ejemplo n.º 57
0
 /// <summary>
 /// Toogle a pin switch from On to off or Off to on. One time
 /// </summary>
 /// <param name="processorPin"></param>
 public static void Toogle(ProcessorPin processorPin)
 {
     GlobalConnectionPin.Toggle(processorPin);
 }
Ejemplo n.º 58
0
 /// <summary>
 /// Determines whether the connection contains the specified pin.
 /// </summary>
 /// <param name="pin">The pin.</param>
 /// <returns>
 ///   <c>true</c> if the connection contains the specified pin; otherwise, <c>false</c>.
 /// </returns>
 public bool Contains(ProcessorPin pin)
 {
     return(pinConfigurations.ContainsKey(pin));
 }
Ejemplo n.º 59
0
 /// <summary>
 /// </summary>
 public void SetPinResistor(ProcessorPin pin, PinResistor resistor)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SwitchInputPinConfiguration"/> class.
 /// </summary>
 /// <param name="pin">The pin.</param>
 public SwitchInputPinConfiguration(ProcessorPin pin) : base(pin)
 {
 }