public DevicesController(IConfiguration configuration)
        {
            this._configuration = configuration;

            // This condition is used to check if a relay is used with the Raspberry Pi.
            // A relay behaves differently from a regular circuit in that relay is
            // triggered on when the GPIO output is LOW and vice versa.
            // The relay is enabled by default, if no command line parameter is given.
            var disableRelay = this._configuration["disable-relay"];

            if (!string.IsNullOrEmpty(disableRelay) && disableRelay == "true")
            {
                high = GpioPinValue.High;
                low  = GpioPinValue.Low;
            }
            else
            {
                high = GpioPinValue.Low;
                low  = GpioPinValue.High;
            }

            this.GpioController = Bifrost.Devices.Gpio.GpioController.Instance;
            this.GpioPin        = this.GpioController.OpenPin(this.pinId);
            this.GpioPin.SetDriveMode(GpioPinDriveMode.Output);
            this.GpioPin.Write(high);
        }
Ejemplo n.º 2
0
 public Transmitter433(IGpioPin pin)
 {
     _pin = pin;
     pin.InputPullMode = GpioPinResistorPullMode.PullUp;
     pin.PinMode       = GpioPinDriveMode.Output;
     pin.Value         = false;
 }
Ejemplo n.º 3
0
        public IGpioPin CreatePin(BcmPin id, GpioPinDriveMode mode = GpioPinDriveMode.Output)
        {
            IGpioPin pin = Pi.Gpio[id];

            pin.PinMode = mode;
            return(pin);
        }
Ejemplo n.º 4
0
        public void Start(IGpioPin pin)
        {
            _pin = pin ?? throw new Exception("Change reader needs to be supplied a GPIO pin");

            Watch(_valueChangedFileSystemWatcher, _pin.GetValuePath());
            Watch(_directionChangedFileSystemWatcher, _pin.GetDirectionPath());
        }
Ejemplo n.º 5
0
 public Servo(int pin)
 {
     mServoPin         = Pi.Gpio[pin];
     mServoPin.PinMode = GpioPinDriveMode.Output;
     WiringPi.SoftPwmCreate(mServoPin.BcmPinNumber, 0, maxServoPwmVal);
     WiringPi.SoftPwmWrite(mServoPin.BcmPinNumber, 16);
 }
Ejemplo n.º 6
0
        public Display()
        {
            cs         = Pi.Gpio[P1.Pin24];         // GPIO8 = Pin 24
            cs.PinMode = GpioPinDriveMode.Output;   // use pin as ouptut pin
            cs.Write(false);

            res         = Pi.Gpio[P1.Pin35];        // GPI19 = Pin 35
            res.PinMode = GpioPinDriveMode.Output;  // use pin as ouptut pin
            res.Write(true);
            Thread.Sleep(10);
            res.Write(false);
            Thread.Sleep(10);
            res.Write(true);

            dnc         = Pi.Gpio[P1.Pin36];        // GPI16 = Pin 36
            dnc.PinMode = GpioPinDriveMode.Output;  // use pin as ouptut pin
            dnc.Write(false);

            I2CtoSPI spiDevice = new I2CtoSPI(0, 0);

            disp = new SSD1306(spiDevice,
                               SSD1306.DisplayModel.Display128X64, SSD1306.VccSourceMode.Switching);

            bitmap   = new Bitmap(128, 64);
            Graphics = Graphics.FromImage(bitmap);
            Clear();
        }
Ejemplo n.º 7
0
        public bool TryUnexport(IGpioPin pin)
        {
            using (var export = File.OpenWrite("/sys/class/gpio/unexport"))
                export.WriteByte((byte)pin.Number);

            throw new System.NotImplementedException();
        }
Ejemplo n.º 8
0
        public void Initialize()
        {
            if (IsInitialized)
            {
                throw new InvalidOperationException($"{nameof(WaterSystem)} is already initialized");
            }
            Logger.LogTrace("Initializing..");
            var waterControllerConfigs = new[]
            {
                new { Id = "B01", PinId = BcmPin.Gpio26, LitersPerSecond = 0.01 },
                new { Id = "B02", PinId = BcmPin.Gpio20, LitersPerSecond = 0.01 },
                new { Id = "B03", PinId = BcmPin.Gpio21, LitersPerSecond = 0.01 },
            };

            foreach (var config in waterControllerConfigs)
            {
                IGpioPin pin = GpioPinFactory.CreatePin(config.PinId);
                pin.Write(WaterController.PIN_OFF);
                IWaterController waterController = WaterControllerFactory.CreateWaterController(config.Id, pin, config.LitersPerSecond);

                IdToWaterController.Add(config.Id, waterController);
            }

            IsInitialized = true;
            Logger.LogInformation("Application initialized");
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RFIDControllerMfrc522" /> class.
 /// </summary>
 /// <param name="spiPort">The spi port.</param>
 /// <param name="spiFrequency">The spi frequency.</param>
 /// <param name="outputPort">The output port.</param>
 public RFIDControllerMfrc522(ISpiChannel spiPort, int spiFrequency, IGpioPin outputPort)
 {
     Pi.Spi.Channel0Frequency = spiFrequency;
     _spiPort    = spiPort;
     _outputPort = outputPort;
     InitializeComponent();
 }
        public bool TryOpenPin(int pinNumber, GpioSharingMode sharingMode, out IGpioPin pin, out GpioOpenStatus openStatus)
        {
            pin = null;

            if ((pinNumber < 0) || (pinNumber > PinCount - 1))
            {
                openStatus = GpioOpenStatus.UnknownError;
                return(false);
            }

            if ((_gpioPin[pinNumber] == null) || (this._gpioPin[pinNumber].IsDisposed))
            {
                this._gpioPin[pinNumber] = new MCP23017GpioPin(this, pinNumber, sharingMode);
            }
            else if ((sharingMode == GpioSharingMode.Exclusive) || (this._gpioPin[pinNumber].SharingMode == GpioSharingMode.Exclusive))
            {
                openStatus = GpioOpenStatus.SharingViolation;
                return(false);
            }

            pin        = this._gpioPin[pinNumber];
            openStatus = GpioOpenStatus.PinOpened;

            return(true);
        }
Ejemplo n.º 11
0
 public Wheel(IVehicleCommunication comWithWheel, IGpioPin powerPin)
 {
     vehicleCommunication = comWithWheel;
     _powerPin            = powerPin;
     Error = new Error();
     Power = true;
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Button"/> class.
 /// </summary>
 /// <param name="gpioPin">The gpio pin.</param>
 /// <param name="pullMode">The input pull mode.</param>
 public Button(IGpioPin gpioPin, GpioPinResistorPullMode pullMode = GpioPinResistorPullMode.PullDown)
 {
     _gpioPin = gpioPin;
     _gpioPin.InputPullMode = pullMode;
     _gpioPin.PinMode       = GpioPinDriveMode.Input;
     _gpioPin.RegisterInterruptCallback(EdgeDetection.FallingAndRisingEdge, HandleInterrupt);
 }
Ejemplo n.º 13
0
        public void InitGpio()
        {
            try
            {
                gpioController = GpioController.Instance;

                //Set Direction of pins (Input or Output)

                pin17 = gpioController.OpenPin(17);
                pin17.SetDriveMode(GpioPinDriveMode.Input);

                pin22 = gpioController.OpenPin(22);
                pin22.SetDriveMode(GpioPinDriveMode.Input);

                //Init value
                dt1            = DateTime.Now;
                stateChange    = 0;
                oldStatusPin17 = "";

                DeviceReady = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Init Gpio Error : " + ex.Message);
                DeviceReady = false;
            }
        }
Ejemplo n.º 14
0
        public DHT(IGpioPin datatPin, DHTSensorTypes sensor)
        {
            _logger.Information("Sensor setup...");

            if (datatPin != null)
            {
                try
                {
                    _dataPin      = datatPin;
                    _firstReading = true;
                    _prevReading  = DateTime.MinValue;
                    _data         = new UInt32[6];
                    _sensorType   = sensor;
                    //Init the data pin
                    _dataPin.PinMode = GpioPinDriveMode.Output;
                    _dataPin.Write(GpioPinValue.High);
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, "Error in DHT setup");
                }
            }
            else
            {
                throw new ArgumentException("Parameter cannot be null.", "dataPin");
            }
        }
 protected virtual void OnValueChanged(IGpioPin sender, ValueChangedEventArgs args)
 {
     if(this.ValueChanged != null)
     {
         this.ValueChanged(sender, args);
     }
 }
Ejemplo n.º 16
0
        public tm1637()
        {
            //Pi.Init<BootstrapWiringPi>();

            /* clkPin = gpio.OpenPin(pinClock);
             * clkPin.SetDriveMode(GpioPinDriveMode.Output);
             *
             * dataPin = gpio.OpenPin(pinData);
             * dataPin.SetDriveMode(GpioPinDriveMode.Output);*/

            Pi.Init <BootstrapWiringPi>();
            clkPin  = Pi.Gpio[BcmPin.Gpio23];
            dataPin = Pi.Gpio[BcmPin.Gpio24];
            // Configure the pin as an output
            clkPin.PinMode  = GpioPinDriveMode.Output;
            dataPin.PinMode = GpioPinDriveMode.Output;

            clkPin.Write(GpioPinValue.Low);
            dataPin.Write(GpioPinValue.Low);
            //Init Display
            for (int i = 0; i < 4; i++)
            {
                this.digits[i] = 0x00;
            }
            //Set Brightness
            setBrightness(brightness);
            //Display blinks during Startup
            startupShow();
        }
Ejemplo n.º 17
0
        public StepperMotor(int nbrSteps, IGpioPin pin1, IGpioPin pin2, IGpioPin pin3, IGpioPin pin4, IGpioPin pin5)
        {
            if (nbrSteps <= 0)
            {
                throw new Exception(StepNbrError);
            }
            _nbrSteps = nbrSteps;

            _pins      = new[] { pin1, pin2, pin3, pin4, pin5 };
            _positions = new[]
            {
                new[] { false, true, true, false, true },
                new[] { false, true, false, false, true },
                new[] { false, true, false, true, true },
                new[] { false, true, false, true, false },
                new[] { true, true, false, true, false },
                new[] { true, false, false, true, false },
                new[] { true, false, true, true, false },
                new[] { true, false, true, false, false },
                new[] { true, false, true, false, true },
                new[] { false, false, true, false, true }
            };

            SetSpeed(DefaultRevsPerMinute);
        }
Ejemplo n.º 18
0
 public LEDOUT(int pin, int pwm)
 {
     Pi.Init <BootstrapWiringPi>();
     ledPin         = (GpioPin)Pi.Gpio[pin];
     ledPin.PinMode = GpioPinDriveMode.Output;
     WiringPi.SoftPwmCreate(pin, pwm, 10);
 }
Ejemplo n.º 19
0
 public LEDOUT(int pin)
 {
     Pi.Init <BootstrapWiringPi>();
     ledPin         = Pi.Gpio[pin];
     ledPin.PinMode = GpioPinDriveMode.Output;
     ledPin.Write(false);
 }
Ejemplo n.º 20
0
 public RelayDriver(IGpioPin pin, string assetId, Action <string, bool?> onStatusChanged)
 {
     _onStatusChanged = onStatusChanged;
     _pin             = pin;
     _pin.PinMode     = GpioPinDriveMode.Output;
     AssetId          = assetId;
     TurnRelayOff();
 }
        public LidarDistance(ILidarPacketReceiver packetReceiver, IGpioPin powerPin, params VerticalAngle[] verticalAngles)
        {
            _packetReceiver = packetReceiver;
            _powerPin       = powerPin;
            Config          = new LidarDistanceConfiguration(verticalAngles);

            Error = new Error();
        }
Ejemplo n.º 22
0
        public Button(IGpioPin gpioPin)
        {
            _stopwatch = new System.Diagnostics.Stopwatch();
            _stopwatch.Start();

            _button           = new Unosquare.RaspberryIO.Peripherals.Button(gpioPin, GpioPinResistorPullMode.PullUp);
            _button.Pressed  += button_Pressed;
            _button.Released += button_Released;
        }
Ejemplo n.º 23
0
 public IRIN(int pin)
 {
     Pi.Init <BootstrapWiringPi>();
     mPin         = pin;
     gpio         = Pi.Gpio[mPin];
     gpio.PinMode = GpioPinDriveMode.Input;
     mSignalVal   = gpio.Read();
     IRSensor     = new InfraredSensor(gpio, true);
 }
Ejemplo n.º 24
0
        private void UpdateLineStatus(IGpioPin line, bool newStatus)
        {
            var oldStatus = line.Read(); // on when false , off when true

            if (oldStatus == newStatus)
            {
                line.Write(!newStatus);
            }
        }
Ejemplo n.º 25
0
        /// <summary>
        /// Reads the current state of the given pin.
        /// </summary>
        /// <param name="pin">The pin to read.</param>
        /// <returns>True if high, false is low.</returns>
        public static bool ReadDigital(IGpioPin gpioPin)
        {
            if (gpioPin.PinMode != GpioPinDriveMode.Input)
            {
                gpioPin.PinMode = GpioPinDriveMode.Input;
            }

            return(gpioPin.Read() == true);
        }
Ejemplo n.º 26
0
        public MotorInformation(Axis axis, IGpioPin motorStepPin)
        {
            SteppingPin = motorStepPin;
            SteppingPin.ValueChanged += pinValueChanged;
            Axis      = axis;
            Direction = MoveDirection.Stopped;

            stepTimer = new Timer(timerTickCallback, null, Timeout.Infinite, Timeout.Infinite);
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Write the given value to the given pin.
        /// </summary>
        /// <param name="pin">The pin to set.</param>
        /// <param name="state">The new state of the pin.</param>
        public static void WriteDigital(IGpioPin gpioPin, bool state)
        {
            if (gpioPin.PinMode != GpioPinDriveMode.Output)
            {
                gpioPin.PinMode = GpioPinDriveMode.Output;
            }

            gpioPin.Write(state ? GpioPinValue.High : GpioPinValue.Low);
        }
Ejemplo n.º 28
0
 public Stepper(ArmConfig config, int i) : base(config, i)
 {
     GlobalRev        = config.SERVO_SPECS[i, 3] == 1;
     this.pin         = Pi.Gpio[config.SERVO_SPECS[i, 1]];
     this.pin.PinMode = GpioPinDriveMode.Output;
     this.pin.Write(false);
     this.dir         = Pi.Gpio[config.SERVO_SPECS[i, 2]];
     this.dir.PinMode = GpioPinDriveMode.Output;
     this.dir.Write(GlobalRev);
 }
Ejemplo n.º 29
0
        public Ultrasonic(IVehicleCommunication comWithUltrasonic, IGpioPin powerPin, IGpioPin ultrasoundInterruptPin)
        {
            _vehicleCommunication = comWithUltrasonic;
            _power    = powerPin;
            TimeStamp = DateTime.Now;
            Error     = new Error();

            _newDataAvailablePin = ultrasoundInterruptPin;
            Power = true;
        }
Ejemplo n.º 30
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DhtSensor" /> class.
        /// </summary>
        /// <param name="dataPin">The data pin. Must be a GPIO-only pin on the P1 Header of the Pi.</param>
        /// <exception cref="ArgumentException">dataPin When it is invalid.</exception>
        protected DhtSensor(IGpioPin dataPin)
        {
            if (!AllowedPins.Contains(dataPin))
            {
                throw new ArgumentException($"{nameof(dataPin)}, {dataPin} is not available to service this driver.");
            }

            _dataPin   = dataPin;
            _readTimer = new Timer(PerformContinuousReads, null, Timeout.Infinite, Timeout.Infinite);
        }
Ejemplo n.º 31
0
        public MotorLocator(IGpioPin clearCounter, IMotorInformation motorInformation)
        {
            stepCounter       = motorInformation.SteppingPin;
            clearCounterPin   = clearCounter;
            motorInfo         = motorInformation;
            position          = 0;
            lastMoveDirection = MoveDirection.Stopped;

            stepCounter.ValueChanged += pinValueChanged;
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="pin"></param>
        /// <param name="mode">The drive mode of the pin. When inputPullUp is used, the pressed state will be true when a falling edge is detected.</param>
        public Button(IGpioPin pin, GpioPinDriveMode mode)
        {
            if (pin == null) throw new ArgumentNullException("pin");
            if (mode != GpioPinDriveMode.Input && mode != GpioPinDriveMode.InputPullDown && mode != GpioPinDriveMode.InputPullUp) throw new ArgumentOutOfRangeException("mode", "Drive mode should be an input drive type.");

            _pin = pin;
            _pressedEdge = mode == GpioPinDriveMode.InputPullUp ? GpioPinEdge.FallingEdge : GpioPinEdge.RisingEdge;
            _pin.SetDriveMode(mode);
            _pin.ValueChanged += _pin_ValueChanged;
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="enablePin">Enablepin, a soft pwm driver will be connected to the pin.</param>
        /// <param name="in1Pin">In1 pin.</param>
        /// <param name="in2Pin">In2 pin.</param>
        public L293d(int enablePin, int in1Pin, int in2Pin)
        {
            _enablePin = new SoftPwmDriver(enablePin);
            _in1Pin = GenericGpioController.GetDefault().OpenPin(in1Pin);
            _in1Pin.SetDriveMode(GpioPinDriveMode.Output);
            _in2Pin = GenericGpioController.GetDefault().OpenPin(in2Pin);
            _in2Pin.SetDriveMode(GpioPinDriveMode.Output);

            _in1Pin.Write(GpioPinValue.Low);
            _in2Pin.Write(GpioPinValue.Low);
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="trigPin">pin number of the trig of the sensor.</param>
        /// <param name="echoPin">pint number of the echo of the sensor.</param>
        public UltraSonicSensor(int trigPin, int echoPin)
        {
            _trigPin = GenericGpioController.GetDefault().OpenPin(trigPin);
            _trigPin.SetDriveMode(GpioPinDriveMode.Output);

            _echoPin = GenericGpioController.GetDefault().OpenPin(echoPin);
            _echoPin.SetDriveMode(GpioPinDriveMode.Input);

            _trigPin.Write(GpioPinValue.Low);

            _echoPin.ValueChanged += _echoPin_ValueChanged;
            _timer = new Stopwatch();
        }
        /// <summary>
        /// Handles the pin's value changed event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void _echoPin_ValueChanged(IGpioPin sender, ValueChangedEventArgs args)
        {
            if (args.Edge == GpioPinEdge.RisingEdge)
            {
                _timer.Reset();
                _timer.Start();
            }

            if (args.Edge == GpioPinEdge.FallingEdge)
            {
                _timer.Stop();
                this.OnDistanceSensed(this, new DistanceSensedEventArgs(_timer.Elapsed));
            }
        }
        private async void PortExpanderTest()
        {
            var controller = GenericGpioController.GetDefault();
            await controller.RegisterMcp23017(64, 0x20, 4); //add 16 additional ports from 64 to 79
            
            var pins = new IGpioPin[8];
            var btns = new Button[8];
            GpioPinValue value = GpioPinValue.Low;
            int ledToSkip = -1;

            for (var i = 0; i < btns.Length; i++)
            {
                var btnPin = controller.OpenPin(64 + i);
                var btn = new Button(btnPin, GpioPinDriveMode.InputPullUp);
                btn.PressedChanged += delegate(Button b, EventArgs e) { value = btn.IsPressed ? GpioPinValue.High : GpioPinValue.Low; ledToSkip = Array.IndexOf(btns, b); };
                btns[i] = btn;
            }
            
            for (var i = 0; i < pins.Length; i++)
            {
                var p = controller.OpenPin(72 + i);
                p.SetDriveMode(GpioPinDriveMode.Output);
                p.Write(GpioPinValue.Low);

                pins[i] = p;
            }

            while (true)
            {
                for (var i = 0; i < 8; i++)
                {
                    if (i != ledToSkip)
                    {
                        var p = pins[i];
                        p.Write(value);
                    }
                }

                await Task.Delay(TimeSpan.FromMilliseconds(50));
            }
        }
        /// <summary>
        /// Handles the valuechanged event of the interrupt pin.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void InterruptPin_ValueChanged(IGpioPin sender, ValueChangedEventArgs args)
        {
            if (this.GPINTEN.Read() == 0x00) return;            //not listening to interrupts

            var activeValue = this.InterruptActiveValue;
            var edge = activeValue.Edge();

            if (args.Edge == edge)
            {
                var interruptFlags = this.INTF.Read();
                if (interruptFlags == 0x00) return;

                var interruptValues = this.INTCAP.Read();
                var currentValues = this.GPIO.Read();

                //todo raise event that can be captured by registered pins.
                this.OnInterrupt(this, new InterruptEventArgs(interruptFlags, interruptValues, currentValues));
            }
        }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="pin">Pin the led is connected on.</param>
 public Led(int pin)
 {
     _pin = GenericGpioController.GetDefault().OpenPin(pin);
     _pin.SetDriveMode(GpioPinDriveMode.Output);
 }
 /// <summary>
 /// Captures the pin's value changed event.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="args"></param>
 private void _pin_ValueChanged(IGpioPin sender, ValueChangedEventArgs args)
 {
     this.IsPressed = args.Edge == _pressedEdge;
     this.OnPressedChanged(this, EventArgs.Empty);
 }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="device">Reference to the i2c device.</param>
        /// <param name="baseRegisterAddress">Address of the first (IODIR) register.</param>
        /// <param name="registerIncrement">Increment to get the next register.</param>
        private Mcp230xx(I2cDevice device, byte baseRegisterAddress, byte registerIncrement, IGpioPin interruptPin)
        {
            this.Device = device;
            this.IODIR = new I2cDeviceRegister(device, baseRegisterAddress);
            baseRegisterAddress += registerIncrement;

            this.IPOL = new I2cDeviceRegister(device, baseRegisterAddress);
            baseRegisterAddress += registerIncrement;

            this.GPINTEN= new I2cDeviceRegister(device, baseRegisterAddress);
            baseRegisterAddress += registerIncrement;

            this.DEFVAL = new I2cDeviceRegister(device, baseRegisterAddress);
            baseRegisterAddress += registerIncrement;

            this.INTCON = new I2cDeviceRegister(device, baseRegisterAddress);
            baseRegisterAddress += registerIncrement;

            this.IOCON = new I2cDeviceRegister(device, baseRegisterAddress);
            baseRegisterAddress += registerIncrement;

            this.GPPU = new I2cDeviceRegister(device, baseRegisterAddress);
            baseRegisterAddress += registerIncrement;

            this.INTF = new I2cDeviceRegister(device, baseRegisterAddress);
            baseRegisterAddress += registerIncrement;

            this.INTCAP = new I2cDeviceRegister(device, baseRegisterAddress);
            baseRegisterAddress += registerIncrement;

            this.GPIO = new I2cDeviceRegister(device, baseRegisterAddress);
            baseRegisterAddress += registerIncrement;

            this.OLAT = new I2cDeviceRegister(device, baseRegisterAddress);
           
            this.IODIR.Write(0x00);     //all pins output
            this.GPIO.Write(0x00);      //all pins low
            this.OLAT.Write(0x00);      //all latches low
            this.INTCON.Write(0x00);    //compare interrupt with previous value
            this.GPINTEN.Write(0x00);   //no interrupts registered

            if(interruptPin != null)
            {
                this.InterruptPin = interruptPin;
                var driveMode = this.InterruptActiveValue == GpioPinValue.Low ? GpioPinDriveMode.InputPullUp : GpioPinDriveMode.InputPullDown;
                this.InterruptPin.SetDriveMode(driveMode);
                this.InterruptPin.ValueChanged += InterruptPin_ValueChanged;

            }
        }