/// <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 bool ReadDigital(DigitalPin pin)
        {
            if (!Enum.IsDefined(typeof(DigitalPin), pin))
            {
                throw new ArgumentException(nameof(pin));
            }

            return(this.gpio.Read((int)pin));
        }
        /// <summary>
        /// Sets the drive mode of the given pin.
        /// </summary>
        /// <param name="pin">The pin to set.</param>
        /// <param name="driveMode">The new drive mode of the pin.</param>
        public void SetDigitalDriveMode(DigitalPin pin, GpioPinDriveMode driveMode)
        {
            if (!Enum.IsDefined(typeof(DigitalPin), pin))
            {
                throw new ArgumentException(nameof(pin));
            }

            this.gpio.SetDriveMode((int)pin, driveMode);
        }
        /// <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 void WriteDigital(DigitalPin pin, bool state)
        {
            if (!Enum.IsDefined(typeof(DigitalPin), pin))
            {
                throw new ArgumentException(nameof(pin));
            }

            this.gpio.Write((int)pin, state);
        }
        public void Action(ActionBase baseAction, CancellationToken cancelToken, dynamic config)
        {
            LedBuzzerSimpleAction action = (LedBuzzerSimpleAction)baseAction;

            // note that config is dynamic so we cast the pin values to integer
            using (DigitalPin pinLed = GpioHeader.Instance.CreatePin((int)config.pinBuzzer, DigitalPinDirection.Output))
                using (DigitalPin pinBuzzer = GpioHeader.Instance.CreatePin((int)config.pinLed, DigitalPinDirection.Output))
                {
                    if (cancelToken.IsCancellationRequested)
                    {
                        return;
                    }

                    if (action.PreDelayMs > 0)
                    {
                        Thread.Sleep(action.PreDelayMs);
                    }

                    if (cancelToken.IsCancellationRequested)
                    {
                        return;
                    }

                    for (int loopCounter = 0; loopCounter < action.LoopCount; ++loopCounter)
                    {
                        pinLed.Output((PaulTechGuy.RPi.GpioLib.PinValue)action.StartValue);
                        pinBuzzer.Output((PaulTechGuy.RPi.GpioLib.PinValue)action.StartValue);

                        if (action.StartDurationMs > 0)
                        {
                            Thread.Sleep(action.StartDurationMs);
                        }

                        // only output if it changes
                        if (action.EndValue != action.StartValue)
                        {
                            pinLed.Output((PaulTechGuy.RPi.GpioLib.PinValue)action.EndValue);
                            pinBuzzer.Output((PaulTechGuy.RPi.GpioLib.PinValue)action.EndValue);
                        }

                        if (action.EndDurationMs > 0)
                        {
                            Thread.Sleep(action.EndDurationMs);
                        }

                        if (cancelToken.IsCancellationRequested)
                        {
                            return;
                        }
                    }

                    if (action.PostDelayMs > 0)
                    {
                        Thread.Sleep(action.PostDelayMs);
                    }
                }
        }
        static void Main()
        {
            var _config = new configs.config(configs.Configuration.GetConfiguration());

            Console.WriteLine("Starting... RaspiFanController StartingSettings is...");
            Console.WriteLine($"PwmPin :{_config.PWMPin} GpioLogicPin: {_config.LogicPin}");

            Console.WriteLine("\n**FanSpeedSettings");
            Console.WriteLine($"*Frequency: {_config.PwmFrequency}Hz	FanMaxSpeed: {_config.MaxSpeed}%  FanMinSpeed: {_config.MinSpeed}%");
            Console.WriteLine($"*FanMaxTemp:{_config.MaxSpeedTemp}C	FanMinTemp: {_config.MinSpeedTemp}C");

            using (var logicPin = new DigitalPin(_config.LogicPin, GpioPinDirection.Out))
                using (var pwmPin = new PwmPin(_config.PWMPin, _config.PwmFrequency, 1))
                {
                    Console.CancelKeyPress += (s, e) =>
                    {
                        Console.WriteLine("終了中...");
                        logicPin.Dispose();
                        pwmPin.Dispose();
                    };
                    logicPin.State = true;
                    Thread.Sleep(3000);

                    while (true)
                    {
                        float perSpeed = 1;

                        if (!float.TryParse(File.ReadAllText("/sys/class/thermal/thermal_zone0/temp"), out float temp))
                        {
                            return;
                        }
                        temp /= 1000;

                        if (_config.MaxSpeedTemp != _config.MinSpeedTemp)
                        {
                            float perTemp = (temp - _config.MinSpeedTemp) / (_config.MaxSpeedTemp - _config.MinSpeedTemp);
                            perSpeed = (_config.MinSpeed + ((_config.MaxSpeed - _config.MinSpeed) * perTemp)) / 100;

                            if (perTemp >= 1)
                            {
                                perSpeed = _config.MaxSpeed;
                            }
                            if (perTemp <= 0)
                            {
                                perSpeed = _config.MaxSpeed > _config.MinSpeed ? _config.MaxSpeed : _config.MinSpeed;
                            }
                        }

                        perSpeed = Math.Min(Math.Max(perSpeed, 0), 1);

                        Console.WriteLine($"[{DateTime.Now:yyyy/mm/dd HH:MM:ss}] Temp: {temp}C, Spd: {perSpeed * 100}%");
                        pwmPin.Duty = perSpeed;
                        Thread.Sleep(_config.GetStatePeriod * 1000);
                    }
                }
        }
        /// <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 void WriteDigital(DigitalPin pin, bool state)
        {
            IGpio gpioPin = pin == DigitalPin.DIO16 ? this.dio16 : this.dio26;

            //if (gpioPin. != GpioPinDriveMode.Output)
            //gpioPin.SetDriveMode(GpioPinDriveMode.Output);
            // Initialize the pin as a high output
            gpioPin.SetDirection(Gpio.DirectionOutInitiallyHigh);
            // Low voltage is considered active
            gpioPin.SetActiveType(Gpio.ActiveHigh);

            gpioPin.Value = (state ? true : false);
        }
Exemple #7
0
        public Switch(DigitalPin pin, PullUpDownMode pud)
        {
            Pin = pin;

            Pin.PullUpDown(pud);
            Pin.SetIOMode(PinMode.Input);
            Pin.SetupInterrupt(InterruptMode.Both);
            Pin.AddInterruptCallback(InterruptCB);

            Value = pin.DigitalRead();

            Timer.Start();
        }
Exemple #8
0
        public static DigitalWire NewDigitalWire(string name, DigitalPin start, DigitalPin end, DigitalSignal signal)
        {
            start.Signal = signal;
            end.Signal   = signal;

            return(new DigitalWire()
            {
                Id = Guid.NewGuid(),
                Name = name,
                Signal = signal,
                StartPin = start,
                EndPin = end
            });
        }
Exemple #9
0
 bool AreDigitalPinsIdentical(DigitalPin pin1, DigitalPin pin2)
 {
     if (
         pin1.PinID == pin2.PinID &&
         pin1.ParentDevice == pin2.ParentDevice
         )
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
        /// <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 bool ReadDigital(DigitalPin pin)
        {
            //if (!Enum.IsDefined(typeof(DigitalPin), pin)) throw new ArgumentException(nameof(pin));

            IGpio gpioPin = pin == DigitalPin.DIO16 ? this.dio16 : this.dio26;

            //if (gpioPin.GetDriveMode() != GpioPinDriveMode.Input)
            //  gpioPin.SetDriveMode(GpioPinDriveMode.Input);

            // Initialize the pin as an input
            gpioPin.SetDirection(Gpio.DirectionIn);
            // High voltage is considered active
            gpioPin.SetActiveType(Gpio.ActiveHigh);

            return(gpioPin.Value == true);
        }
Exemple #11
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 bool ReadDigital(DigitalPin pin)
        {
            if (!Enum.IsDefined(typeof(DigitalPin), pin))
            {
                throw new ArgumentException(nameof(pin));
            }

            var gpioPin = pin == DigitalPin.DIO16 ? this.dio16 : this.dio26;

            if (gpioPin.GetDriveMode() != GpioPinDriveMode.Input)
            {
                gpioPin.SetDriveMode(GpioPinDriveMode.Input);
            }

            return(gpioPin.Read() == GpioPinValue.High);
        }
Exemple #12
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 bool ReadDigital(DigitalPin pin)
        {
            if (!Enum.IsDefined(typeof(DigitalPin), pin))
            {
                throw new ArgumentException(nameof(pin));
            }

            var gpioPin = pin == DigitalPin.DIO16 ? this.dio16 : this.dio26;

            if (gpioPin.PinMode != GpioPinDriveMode.Input)
            {
                gpioPin.PinMode = GpioPinDriveMode.Input;
            }

            return(gpioPin.Read() == true);
        }
Exemple #13
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 void WriteDigital(DigitalPin pin, bool state)
        {
            if (!Enum.IsDefined(typeof(DigitalPin), pin))
            {
                throw new ArgumentException(nameof(pin));
            }

            var gpioPin = pin == DigitalPin.DIO16 ? this.dio16 : this.dio26;

            if (gpioPin.GetDriveMode() != GpioPinDriveMode.Output)
            {
                gpioPin.SetDriveMode(GpioPinDriveMode.Output);
            }

            gpioPin.Write(state ? GpioPinValue.High : GpioPinValue.Low);
        }
Exemple #14
0
        public RotaryEncoder(DigitalPin p1, DigitalPin p2, PullUpDownMode pud)
        {
            Pin1 = p1;
            Pin2 = p2;

            Pin1.SetIOMode(PinMode.Input);
            Pin1.PullUpDown(pud);
            Pin1.SetupInterrupt(InterruptMode.Both);
            Pin1.AddInterruptCallback(UpdateSequence);

            Pin2.SetIOMode(PinMode.Input);
            Pin2.PullUpDown(pud);
            Pin2.SetupInterrupt(InterruptMode.Both);
            Pin2.AddInterruptCallback(UpdateSequence);

            RotationSequence = GetRotationSequence();
        }
Exemple #15
0
        public override void Awake()
        {
            base.Awake();

            _digitalPin = Owner.GetComponent <DigitalPin>();
            if (_digitalPin == null)
            {
                Debug.LogWarning("There exist no DigitalPin!");
            }
            else
            {
                _digitalPin.OnStarted             += OnStarted;
                _digitalPin.OnStopped             += OnStopped;
                _digitalPin.OnUpdated             += OnUpdated;
                _digitalPin.OnChangedMode         += OnChangedMode;
                _digitalPin.OnChangedDigitalInput += OnChangedDigitalInput;
            }
        }
Exemple #16
0
    public override void OnInspectorGUI()
    {
        this.serializedObject.Update();
        DigitalPin digitalPin = (DigitalPin)target;

        EditorGUILayout.PropertyField(id, new GUIContent("ID"));
        digitalPin.mode = (DigitalPin.Mode)EditorGUILayout.EnumPopup("Mode", digitalPin.mode);
        if (digitalPin.mode == DigitalPin.Mode.OUTPUT)
        {
            int index = 0;
            if (digitalPin.digitalValue == true)
            {
                index = 1;
            }
            int newIndex = GUILayout.SelectionGrid(index, new string[] { "FALSE", "TRUE" }, 2);
            if (index != newIndex)
            {
                if (newIndex == 0)
                {
                    digitalPin.digitalValue = false;
                }
                else
                {
                    digitalPin.digitalValue = true;
                }
            }
        }
        else if (digitalPin.mode == DigitalPin.Mode.INPUT || digitalPin.mode == DigitalPin.Mode.INPUT_PULLUP)
        {
            int index = 0;
            if (digitalPin.digitalValue == true)
            {
                index = 1;
            }
            GUILayout.SelectionGrid(index, new string[] { "FALSE", "TRUE" }, 2);
        }
        else if (digitalPin.mode == DigitalPin.Mode.PWM)
        {
            digitalPin.analogValue = EditorGUILayout.Slider("Ratio", digitalPin.analogValue, 0f, 1f);
        }

        EditorUtility.SetDirty(target);
        this.serializedObject.ApplyModifiedProperties();
    }
Exemple #17
0
        /// <summary>
        ///     Write to a digital pin that has been toggled to output mode with pinMode() method.
        /// </summary>
        /// <param name="pin">The digital pin to write to.</param>
        /// <param name="value">Value either Arduino.LOW or Arduino.HIGH.</param>
        public void DigitalWrite(int pin, DigitalPin value)
        {
            var intValue   = (int)value;
            int portNumber = (pin >> 3) & 0x0F;

            Log("[digital] - Writing value {0} on pin {1} (port number {2})".FormatWith(value, pin, portNumber));
            var message = new byte[3];

            if (intValue == 0)
            {
                _digitalOutputData[portNumber] &= ~(1 << (pin & 0x07));
            }
            else
            {
                _digitalOutputData[portNumber] |= (1 << (pin & 0x07));
            }

            message[0] = (byte)(DigitalMessage | portNumber);
            message[1] = (byte)(_digitalOutputData[portNumber] & 0x7F);
            message[2] = (byte)(_digitalOutputData[portNumber] >> 7);
            _serialPort.Write(message, 0, 3);
        }
Exemple #18
0
        public LCD12864(DigitalPin en, DigitalPin rs, DigitalPin rw, DigitalPin rst, DigitalPin[] d)
        {
            EN = en;
            RS = rs;
            RW = rw;
            RST = rst;
            DB = d;

            EN.SetIOMode(PinMode.Output);
            EN.DigitalWrite(0);
            RS.SetIOMode(PinMode.Output);
            RW.SetIOMode(PinMode.Output);

            for (int i = 0; i < 8; i++)
            {
                DB[i].SetIOMode(PinMode.Output);
            }

            RST.SetIOMode(PinMode.Output);
            RST.DigitalWrite(0);
            Wrapper.delayMicroseconds(10);
            RST.DigitalWrite(1);
            Wrapper.delay(100);

            WriteInstruction(Convert.ToInt32("00110000", 2)); // Function set
            WriteInstruction(Convert.ToInt32("00110000", 2)); // Function set
            WriteInstruction(Convert.ToInt32("00001100", 2)); // Display on/off
            WriteInstruction(Convert.ToInt32("00000001", 2)); // Display clear
            WriteInstruction(Convert.ToInt32("00000110", 2)); // Entry mode set
            WriteInstruction(Convert.ToInt32("00110100", 2)); // Extended Mode
            WriteInstruction(Convert.ToInt32("00110110", 2)); // Graphics mode

            // Screen clear
            Buffer = new LCDBitmap(128, 64, 1);
            Draw(new LCDBitmap(128, 64, 0));
        }
Exemple #19
0
 /// <summary>
 /// Opens a specific pin.
 /// </summary>
 /// <param name="pin">The pin to open.</param>
 /// <param name="pinMode">The desired pin mode.</param>
 public void OpenPin(DigitalPin pin, Domain.PinController.PinMode pinMode)
 {
     this._controller.OpenPin(pin.Number, this.AdaptPinMode(pinMode));
 }
Exemple #20
0
 /// <summary>
 /// Closes the pin.
 /// </summary>
 /// <param name="pin">The pin to close.</param>
 public void ClosePin(DigitalPin pin)
 {
     this._controller.ClosePin(pin.Number);
 }
Exemple #21
0
 /// <summary>
 /// Sets the given Pin HIGH.
 /// </summary>
 /// <param name="pin">The Pin to set HIGH</param>
 public void SetHigh(DigitalPin pin)
 {
     this._controller.Write(pin.Number, PinValue.High);
 }
Exemple #22
0
 /// <summary>
 /// Sets the given Pin LOW.
 /// </summary>
 /// <param name="pin">The Pin to set LOW</param>
 public void SetLow(DigitalPin pin)
 {
     this._controller.Write(pin.Number, PinValue.Low);
 }
		/// <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 bool ReadDigital(DigitalPin pin) {
			if (!Enum.IsDefined(typeof(DigitalPin), pin)) throw new ArgumentException(nameof(pin));

			return this.gpio.Read((int)pin);
		}
		/// <summary>
		/// Sets the drive mode of the given pin.
		/// </summary>
		/// <param name="pin">The pin to set.</param>
		/// <param name="driveMode">The new drive mode of the pin.</param>
		public void SetDigitalDriveMode(DigitalPin pin, GpioPinDriveMode driveMode) {
			if (!Enum.IsDefined(typeof(DigitalPin), pin)) throw new ArgumentException(nameof(pin));

			this.gpio.SetDriveMode((int)pin, driveMode);
		}
        internal async Task <bool> ParseMessageAsync(BlynkConnection blynkConnection)
        {
            bool result = true;

            try {
                BlynkLogManager.LogMethodBegin(nameof(ParseMessageAsync));
                BlynkLogManager.LogInformation(string.Format("Message Received command type : {0}", this.BlynkCommandType));

                switch (this.BlynkCommandType)
                {
                case BlynkCommandType.BLYNK_CMD_RESPONSE:
                    blynkConnection.ResponseReceivedNotification?.Invoke(this.ResponseCode);
                    return(result);

                case BlynkCommandType.BLYNK_CMD_PING:
                    return(await blynkConnection.SendResponseAsync(this.MessageId));

                case BlynkCommandType.BLYNK_CMD_BRIDGE:
                    return(await blynkConnection.SendResponseAsync(this.MessageId));

                case BlynkCommandType.BLYNK_CMD_HARDWARE: {
                    var hardwareCommandType = this.GetHardwareCommandType();
                    BlynkLogManager.LogInformation(string.Format("Hardware command type : {0}", hardwareCommandType));

                    switch (hardwareCommandType)
                    {
                    case HardwareCommandType.VirtualRead: {
                        string pinString;

                        this.messageBuffer.Extract(out pinString);

                        var pinNumber = int.Parse(pinString);

                        var pin = blynkConnection.VirtualPinNotification.PinReadRequest?.Invoke(pinNumber);                                          // blynkConnection.ReadVirtualPinRequest?.Invoke( pinNumber );

                        if (pin == null)
                        {
                            return(await blynkConnection.SendResponseAsync(this.MessageId, BlynkResponse.NO_DATA));
                        }
                        else
                        {
                            return(await pin.SendVirtualPinWriteAsync(blynkConnection, this.MessageId, blynkConnection.CancellationToken));
                        }
                    }

                    case HardwareCommandType.VirtualWrite: {
                        string pinNumberAsString;

                        this.messageBuffer.Extract(out pinNumberAsString);

                        var pin = new VirtualPin()
                        {
                            PinNumber = int.Parse(pinNumberAsString)
                        };

                        this.messageBuffer.Extract(pin.Values);

                        blynkConnection.VirtualPinNotification.PinWriteNotification?.Invoke(pin);

                        return(await blynkConnection.SendResponseAsync(this.MessageId));
                    }

                    case HardwareCommandType.DigitalRead: {
                        string pinString;

                        this.messageBuffer.Extract(out pinString);

                        var pinNumber = int.Parse(pinString);

                        var pin = blynkConnection.DigitalPinNotification.PinReadRequest?.Invoke(pinNumber);                                          // blynkConnection.ReadDigitalPinRequest?.Invoke( pinNumber );

                        if (pin == null)
                        {
                            return(await blynkConnection.SendResponseAsync(this.MessageId, BlynkResponse.NO_DATA));
                        }
                        else
                        {
                            return(await pin.SendDigitalPinWriteAsync(blynkConnection, this.MessageId, blynkConnection.CancellationToken));
                        }
                    }

                    case HardwareCommandType.DigitalWrite: {
                        string pinNumberAsString;
                        string valueAsString;

                        this.messageBuffer.Extract(out pinNumberAsString)
                        .Extract(out valueAsString);

                        var pin = new DigitalPin()
                        {
                            PinNumber = int.Parse(pinNumberAsString),
                            Value     = int.Parse(valueAsString) == 1
                        };

                        //blynkConnection.WriteDigitalPinNotification?.Invoke( pin );
                        blynkConnection.DigitalPinNotification.PinWriteNotification?.Invoke(pin);

                        return(await blynkConnection.SendResponseAsync(this.MessageId));
                    }

                    case HardwareCommandType.AnalogRead: {
                        string pinString;

                        this.messageBuffer.Extract(out pinString);

                        var pinNumber = int.Parse(pinString);

                        var pin = blynkConnection.AnalogPinNotification.PinReadRequest?.Invoke(pinNumber);                                          // blynkConnection.ReadAnalogPinRequest( pinNumber );

                        if (pin == null)
                        {
                            return(await blynkConnection.SendResponseAsync(this.MessageId, BlynkResponse.NO_DATA));
                        }
                        else
                        {
                            return(await pin.SendAnalogPinWriteAsync(blynkConnection, this.MessageId, blynkConnection.CancellationToken));
                        }
                    }

                    case HardwareCommandType.AnalogWrite: {
                        string pinNumberAsString;
                        string valueAsString;

                        this.messageBuffer.Extract(out pinNumberAsString)
                        .Extract(out valueAsString);

                        var pin = new AnalogPin()
                        {
                            PinNumber = int.Parse(pinNumberAsString),
                            Value     = short.Parse(valueAsString)
                        };

                        //blynkConnection.WriteAnalogPinNotification?.Invoke( pin );
                        blynkConnection.AnalogPinNotification.PinWriteNotification?.Invoke(pin);

                        return(await blynkConnection.SendResponseAsync(this.MessageId));
                    }

                    case HardwareCommandType.PinMode: {
                        string pin;
                        string mode;
                        while (this.messageBuffer.Position < this.MessageLength)
                        {
                            this.messageBuffer.Extract(out pin)
                            .Extract(out mode);

                            PinMode pinMode = PinMode.Invalid;

                            switch (mode)
                            {
                            case "in":
                                pinMode = PinMode.Input;
                                break;

                            case "out":
                                pinMode = PinMode.Output;
                                break;

                            case "pu":
                                pinMode = PinMode.PullUp;
                                break;

                            case "pd":
                                pinMode = PinMode.PullDown;
                                break;

                            case "pwm":
                                pinMode = PinMode.Pwm;
                                break;
                            }

                            if (pinMode != PinMode.Invalid)
                            {
                                blynkConnection.PinModeNotification?.Invoke(pin, pinMode);
                            }
                        }
                        return(await blynkConnection.SendResponseAsync(this.MessageId));
                    }
                    }
                    break;
                }
                }
            }
            catch (Exception ex) {
                BlynkLogManager.LogException("Error parsing message", ex);
            }
            finally {
                BlynkLogManager.LogMethodEnd(nameof(ParseMessageAsync));
            }
            return(result);
        }
        public void Action(ActionBase baseAction, CancellationToken cancelToken, dynamic config)
        {
            RgbSimpleAction action = (RgbSimpleAction)baseAction;

            // note that config is dynamic so we cast the pin values to integer
            using (DigitalPin pin1 = GpioHeader.Instance.CreatePin((int)config.pins[0], DigitalPinDirection.Output))
                using (DigitalPin pin2 = GpioHeader.Instance.CreatePin((int)config.pins[1], DigitalPinDirection.Output))
                    using (DigitalPin pin3 = GpioHeader.Instance.CreatePin((int)config.pins[2], DigitalPinDirection.Output))
                    {
                        if (cancelToken.IsCancellationRequested)
                        {
                            return;
                        }

                        if (action.PreDelayMs > 0)
                        {
                            Thread.Sleep(action.PreDelayMs);
                        }

                        if (cancelToken.IsCancellationRequested)
                        {
                            return;
                        }

                        DigitalPin[] pins = new DigitalPin[] { pin1, pin2, pin3 };
                        for (int loopCounter = 0; loopCounter < action.LoopCount; ++loopCounter)
                        {
                            for (int i = 0; i < pins.Length; ++i)
                            {
                                pins[i].Output((PaulTechGuy.RPi.GpioLib.PinValue)action.RgbStartValues[i]);
                            }

                            if (action.StartDurationMs > 0)
                            {
                                Thread.Sleep(action.StartDurationMs);
                            }

                            for (int i = 0; i < pins.Length; ++i)
                            {
                                // only output if it changes
                                if (action.RgbEndValues[i] != action.RgbStartValues[i])
                                {
                                    pins[i].Output((PaulTechGuy.RPi.GpioLib.PinValue)action.RgbEndValues[i]);
                                }
                            }

                            if (action.EndDurationMs > 0)
                            {
                                Thread.Sleep(action.EndDurationMs);
                            }

                            if (cancelToken.IsCancellationRequested)
                            {
                                return;
                            }
                        }

                        if (action.PostDelayMs > 0)
                        {
                            Thread.Sleep(action.PostDelayMs);
                        }
                    }
        }
Exemple #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 void WriteDigital(DigitalPin pin, bool state) {
            if (!Enum.IsDefined(typeof(DigitalPin), pin)) throw new ArgumentException(nameof(pin));

            var gpioPin = pin == DigitalPin.DIO16 ? this.dio16 : this.dio26;

            if (gpioPin.GetDriveMode() != GpioPinDriveMode.Output)
                gpioPin.SetDriveMode(GpioPinDriveMode.Output);

            gpioPin.Write(state ? GpioPinValue.High : GpioPinValue.Low);
        }
        /// <summary>
        ///     Write to a digital pin that has been toggled to output mode with pinMode() method.
        /// </summary>
        /// <param name="pin">The digital pin to write to.</param>
        /// <param name="value">Value either Arduino.LOW or Arduino.HIGH.</param>
        public void DigitalWrite(int pin, DigitalPin value)
        {
            var intValue = (int) value;
            int portNumber = (pin >> 3) & 0x0F;
            Log("[digital] - Writing value {0} on pin {1} (port number {2})".FormatWith(value, pin, portNumber));
            var message = new byte[3];

            if (intValue == 0)
                _digitalOutputData[portNumber] &= ~(1 << (pin & 0x07));
            else
                _digitalOutputData[portNumber] |= (1 << (pin & 0x07));

            message[0] = (byte) (DigitalMessage | portNumber);
            message[1] = (byte) (_digitalOutputData[portNumber] & 0x7F);
            message[2] = (byte) (_digitalOutputData[portNumber] >> 7);
            _serialPort.Write(message, 0, 3);
        }
Exemple #29
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 bool ReadDigital(DigitalPin pin) {
            if (!Enum.IsDefined(typeof(DigitalPin), pin)) throw new ArgumentException(nameof(pin));

            var gpioPin = pin == DigitalPin.DIO16 ? this.dio16 : this.dio26;

            if (gpioPin.GetDriveMode() != GpioPinDriveMode.Input)
                gpioPin.SetDriveMode(GpioPinDriveMode.Input);

            return gpioPin.Read() == GpioPinValue.High;
        }
		/// <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 void WriteDigital(DigitalPin pin, bool state) {
			if (!Enum.IsDefined(typeof(DigitalPin), pin)) throw new ArgumentException(nameof(pin));

			this.gpio.Write((int)pin, state);
		}
        public void Action(ActionBase baseAction, CancellationToken cancelToken, dynamic config)
        {
            // this is no different than a standard LED, but we'll have code
            // here just in case differences come up

            BuzzerSimpleAction action = (BuzzerSimpleAction)baseAction;

            // note that config is dynamic so we cast the pin values to integer
            using (DigitalPin pin = GpioHeader.Instance.CreatePin((int)config.pin, DigitalPinDirection.Output))
            {
                if (cancelToken.IsCancellationRequested)
                {
                    return;
                }

                if (action.PreDelayMs > 0)
                {
                    Thread.Sleep(action.PreDelayMs);
                }

                if (cancelToken.IsCancellationRequested)
                {
                    return;
                }

                for (int loopCounter = 0; loopCounter < action.LoopCount; ++loopCounter)
                {
                    pin.Output((PaulTechGuy.RPi.GpioLib.PinValue)action.LedStartValue);

                    if (action.StartDurationMs > 0)
                    {
                        Thread.Sleep(action.StartDurationMs);
                    }

                    // only output if it changes
                    if (action.LedEndValue != action.LedStartValue)
                    {
                        pin.Output((PaulTechGuy.RPi.GpioLib.PinValue)action.LedEndValue);
                    }

                    if (action.EndDurationMs > 0)
                    {
                        Thread.Sleep(action.EndDurationMs);
                    }

                    if (cancelToken.IsCancellationRequested)
                    {
                        return;
                    }
                }

                if (cancelToken.IsCancellationRequested)
                {
                    return;
                }

                if (action.PostDelayMs > 0)
                {
                    Thread.Sleep(action.PostDelayMs);
                }
            }
        }