Пример #1
0
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            // Get the deferral instance
            deferral = taskInstance.GetDeferral();

            // Instantiate the sensors and actuators
            buzzer  = DeviceFactory.Build.Buzzer(Pin.DigitalPin2);
            button  = DeviceFactory.Build.ButtonSensor(Pin.DigitalPin4);
            blueLed = DeviceFactory.Build.Led(Pin.DigitalPin5);
            redLed  = DeviceFactory.Build.Led(Pin.DigitalPin6);

            soundSensor = DeviceFactory.Build.SoundSensor(Pin.AnalogPin0);
            lightSensor = DeviceFactory.Build.LightSensor(Pin.AnalogPin2);

            display = DeviceFactory.Build.RgbLcdDisplay();

            // The IO to the GrovePi sensors and actuators can generate a lot
            // of exceptions - wrap all GrovePi API calls in try/cath statements.
            try {
                // Set the RGB backlight to red and display a message
                display.SetBacklightRgb(255, 0, 0);
                display.SetText("The Thingy is getting started");
            }
            catch (Exception ex)
            {
                // On Error, Resume Next :)
            }

            // Start a timer to check the sensors and activate the actuators five times per second
            timer = ThreadPoolTimer.CreatePeriodicTimer(Timer_Tick, TimeSpan.FromMilliseconds(200));
        }
Пример #2
0
 public BuzzerService(
     ILogger <BuzzerService> logger,
     IBuzzer buzzer)
 {
     _logger = logger ?? throw new ArgumentNullException(nameof(logger));
     _buzzer = buzzer ?? throw new ArgumentNullException(nameof(buzzer));
 }
        private void InitGrovePi()
        {
            System.Diagnostics.Debug.WriteLine(DeviceFactory.Build.GrovePi().GetFirmwareVersion());

            GroveRotary = DeviceFactory.Build.RotaryAngleSensor(Pin.AnalogPin0);
            GroveSound  = DeviceFactory.Build.SoundSensor(Pin.AnalogPin1);
            GroveLight  = DeviceFactory.Build.LightSensor(Pin.AnalogPin2);

            GroveRelay    = DeviceFactory.Build.Relay(Pin.DigitalPin2);
            GroveTempHumi = DeviceFactory.Build.DHTTemperatureAndHumiditySensor(Pin.DigitalPin3, DHTModel.Dht11);
            GroveRanger   = DeviceFactory.Build.UltraSonicSensor(Pin.DigitalPin4);
            GroveLedBar   = DeviceFactory.Build.BuildLedBar(Pin.DigitalPin5);
            GroveBuzzer   = DeviceFactory.Build.Buzzer(Pin.DigitalPin6);
            GroveButton   = DeviceFactory.Build.ButtonSensor(Pin.DigitalPin7);
            GroveLCD      = DeviceFactory.Build.RgbLcdDisplay();

            GroveLedBar.Initialize(GrovePi.Sensors.Orientation.GreenToRed);
            GroveLCD.SetBacklightRgb(255, 50, 255);

            DeviceFactory.Build.GrovePi().PinMode(Pin.DigitalPin2, PinMode.Output);
            Delay.Milliseconds(10);
            DeviceFactory.Build.GrovePi().Flush();

            DeviceFactory.Build.GrovePi().PinMode(Pin.DigitalPin6, PinMode.Output);
            Delay.Milliseconds(10);
            DeviceFactory.Build.GrovePi().Flush();
        }
Пример #4
0
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <filterpriority>2</filterpriority>
        /// <remarks>Call <see cref="Dispose"/> when you are finished using the
        /// <see cref="CyrusBuilt.MonoPi.Devices.PiBrella.PiBrellaBase"/>. The <see cref="Dispose"/> method leaves the
        /// <see cref="CyrusBuilt.MonoPi.Devices.PiBrella.PiBrellaBase"/> in an unusable state. After calling
        /// <see cref="Dispose"/>, you must release all references to the
        /// <see cref="CyrusBuilt.MonoPi.Devices.PiBrella.PiBrellaBase"/> so the garbage collector can reclaim the memory that
        /// the <see cref="CyrusBuilt.MonoPi.Devices.PiBrella.PiBrellaBase"/> was occupying.</remarks>
        public override void Dispose()
        {
            if (base.IsDisposed)
            {
                return;
            }

            this._button = null;
            if (this._buzzer != null)
            {
                this._buzzer.Stop();
                this._buzzer = null;
            }

            if (this._leds != null)
            {
                this._leds.Clear();
                this._leds = null;
            }

            if (this._inputs != null)
            {
                this._leds.Clear();
                this._leds = null;
            }

            if (this._outputs != null)
            {
                this._outputs.Clear();
                this._outputs = null;
            }
            base.Dispose();
        }
Пример #5
0
        public void Initialize()
        {
            _logger = A.Fake <ILogger <BuzzerService> >();
            _buzzer = A.Fake <IBuzzer>();
            _sut    = new BuzzerService(_logger, _buzzer);

            _frequency = 15;
            _defaultCancellationToken = CancellationToken.None;
        }
Пример #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CyrusBuilt.MonoPi.Devices.PiBrella.PiBrellaBase"/>
        /// class. This is the default constructor.
        /// </summary>
        protected PiBrellaBase()
            : base()
        {
            this._inputs = new List <IRaspiGpio>();
            this._inputs.Add(PiBrellaInput.A);
            this._inputs.Add(PiBrellaInput.B);
            this._inputs.Add(PiBrellaInput.C);
            this._inputs.Add(PiBrellaInput.D);
            this._inputs.Add(PiBrellaInput.BUTTON);

            this._inputs[0].Name = "INPUT A";
            this._inputs[1].Name = "INPUT B";
            this._inputs[2].Name = "INPUT C";
            this._inputs[3].Name = "INPUT D";
            this._inputs[4].Name = "BUTTON";
            foreach (IRaspiGpio input in this._inputs)
            {
                input.Provision();
            }

            this._outputs = new List <IRaspiGpio>();
            this._outputs.Add(PiBrellaOutput.E);
            this._outputs.Add(PiBrellaOutput.F);
            this._outputs.Add(PiBrellaOutput.G);
            this._outputs.Add(PiBrellaOutput.H);
            this._outputs.Add(PiBrellaOutput.LED_RED);
            this._outputs.Add(PiBrellaOutput.LED_YELLOW);
            this._outputs.Add(PiBrellaOutput.LED_GREEN);

            this._outputs[0].Name = "OUTPUT E";
            this._outputs[1].Name = "OUTPUT F";
            this._outputs[2].Name = "OUTPUT G";
            this._outputs[3].Name = "OUTPUT H";
            this._outputs[4].Name = "RED LED";
            this._outputs[5].Name = "YELLOW LED";
            this._outputs[6].Name = "GREEN LED";
            foreach (IRaspiGpio output in this._outputs)
            {
                output.Provision();
            }

            this._leds = new List <ILED>();
            this._leds.Add(new LEDComponent(this._outputs[4]));
            this._leds.Add(new LEDComponent(this._outputs[5]));
            this._leds.Add(new LEDComponent(this._outputs[6]));

            this._button = new ButtonComponent(this._inputs[4]);

            this._buzzer      = new BuzzerComponent(PiBrellaOutput.BUZZER);
            this._buzzer.Name = "PIBRELLA BUZZER";
            this._buzzer.Stop();
        }
Пример #7
0
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            // Connect the Button to digital port 2
            IButtonSensor button = DeviceFactory.Build.ButtonSensor(Pin.DigitalPin2);

            // Connect the Buzzer to digital port 5
            IBuzzer buzzer = DeviceFactory.Build.Buzzer(Pin.DigitalPin5);

            // Loop endlessly
            while (true)
            {
                try
                {
                    // Check the value of the button.

                    string buttonon   = button.CurrentState.ToString();
                    bool   buttonison = buttonon.Equals("On", StringComparison.OrdinalIgnoreCase);

                    // Check the state of the buzzer.  This is just to output to debug!
                    SensorStatus status   = buzzer.CurrentState;
                    bool         buzzeron = status.ToString().Equals("On", StringComparison.OrdinalIgnoreCase);

                    // Print out Diagnostics.
                    System.Diagnostics.Debug.WriteLine("Button is " + buttonon);
                    System.Diagnostics.Debug.WriteLine("Buzzer is " + status.ToString());

                    // If the Button is on . . . .
                    if (buttonison)
                    {
                        buzzer.ChangeState(GrovePi.Sensors.SensorStatus.On);
                    }
                    else
                    {
                        buzzer.ChangeState(GrovePi.Sensors.SensorStatus.Off);
                    }
                }
                catch (Exception ex)
                {
                    // NOTE: There are frequent exceptions of the following:
                    // WinRT information: Unexpected number of bytes was transferred. Expected: '. Actual: '.
                    // This appears to be caused by the rapid frequency of writes to the GPIO
                    // These are being swallowed here/

                    // If you want to see the exceptions uncomment the following:
                    // System.Diagnostics.Debug.WriteLine(ex.ToString());
                }
            }
        }
Пример #8
0
        /// <summary>
        /// Play the song
        /// </summary>
        /// <param name="speaker">Speaker to use</param>
        internal void Play(IBuzzer speaker)
        {
            // Interpret and play the song
            for (int i = 0; i < Melody.Length; i += 2)
            {
                // Extract each note and its length in beats
                var note      = Melody.Substring(i, 1);
                var beatCount = int.Parse(Melody.Substring(i + 1, 1));

                // Look up the note duration
                var noteDuration = (uint)scale[note];   // in ms

                // Play the note
                speaker.PlayNote(noteDuration * 2, noteDuration, beatCount);
            }
        }
 public void Run(IBackgroundTaskInstance taskInstance)
 {
     deferral    = taskInstance.GetDeferral();
     buzzer      = DeviceFactory.Build.Buzzer(Pin.DigitalPin2);
     button      = DeviceFactory.Build.ButtonSensor(Pin.DigitalPin4);
     blueLed     = DeviceFactory.Build.Led(Pin.DigitalPin5);
     redLed      = DeviceFactory.Build.Led(Pin.DigitalPin6);
     lightSensor = DeviceFactory.Build.LightSensor(Pin.AnalogPin2);
     display     = DeviceFactory.Build.RgbLcdDisplay();
     buttonState = SensorStatus.Off;
     try
     {
         display.SetBacklightRgb(255, 0, 0);
         display.SetText("Hey Web Summit");
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.Write("Something happened: " + ex.ToString());
         throw;
     }
     timer = ThreadPoolTimer.CreatePeriodicTimer(Timer_Tick, TimeSpan.FromMilliseconds(200));
 }
Пример #10
0
		/// <summary>
		/// Initializes a new instance of the <see cref="CyrusBuilt.MonoPi.Devices.PiBrella.PiBrellaBase"/>
		/// class. This is the default constructor.
		/// </summary>
		protected PiBrellaBase()
			: base() {
			this._inputs = new List<IRaspiGpio>();
			this._inputs.Add(PiBrellaInput.A);
			this._inputs.Add(PiBrellaInput.B);
			this._inputs.Add(PiBrellaInput.C);
			this._inputs.Add(PiBrellaInput.D);
			this._inputs.Add(PiBrellaInput.BUTTON);

			this._inputs[0].Name = "INPUT A";
			this._inputs[1].Name = "INPUT B";
			this._inputs[2].Name = "INPUT C";
			this._inputs[3].Name = "INPUT D";
			this._inputs[4].Name = "BUTTON";
			foreach (IRaspiGpio input in this._inputs) {
				input.Provision();
			}

			this._outputs = new List<IRaspiGpio>();
			this._outputs.Add(PiBrellaOutput.E);
			this._outputs.Add(PiBrellaOutput.F);
			this._outputs.Add(PiBrellaOutput.G);
			this._outputs.Add(PiBrellaOutput.H);
			this._outputs.Add(PiBrellaOutput.LED_RED);
			this._outputs.Add(PiBrellaOutput.LED_YELLOW);
			this._outputs.Add(PiBrellaOutput.LED_GREEN);

			this._outputs[0].Name = "OUTPUT E";
			this._outputs[1].Name = "OUTPUT F";
			this._outputs[2].Name = "OUTPUT G";
			this._outputs[3].Name = "OUTPUT H";
			this._outputs[4].Name = "RED LED";
			this._outputs[5].Name = "YELLOW LED";
			this._outputs[6].Name = "GREEN LED";
			foreach (IRaspiGpio output in this._outputs) {
				output.Provision();
			}

			this._leds = new List<ILED>();
			this._leds.Add(new LEDComponent(this._outputs[4]));
			this._leds.Add(new LEDComponent(this._outputs[5]));
			this._leds.Add(new LEDComponent(this._outputs[6]));

			this._button = new ButtonComponent(this._inputs[4]);

			this._buzzer = new BuzzerComponent(PiBrellaOutput.BUZZER);
			this._buzzer.Name = "PIBRELLA BUZZER";
			this._buzzer.Stop();
		}
Пример #11
0
		/// <summary>
		/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
		/// </summary>
		/// <filterpriority>2</filterpriority>
		/// <remarks>Call <see cref="Dispose"/> when you are finished using the
		/// <see cref="CyrusBuilt.MonoPi.Devices.PiBrella.PiBrellaBase"/>. The <see cref="Dispose"/> method leaves the
		/// <see cref="CyrusBuilt.MonoPi.Devices.PiBrella.PiBrellaBase"/> in an unusable state. After calling
		/// <see cref="Dispose"/>, you must release all references to the
		/// <see cref="CyrusBuilt.MonoPi.Devices.PiBrella.PiBrellaBase"/> so the garbage collector can reclaim the memory that
		/// the <see cref="CyrusBuilt.MonoPi.Devices.PiBrella.PiBrellaBase"/> was occupying.</remarks>
		public override void Dispose() {
			if (base.IsDisposed) {
				return;
			}

			this._button = null;
			if (this._buzzer != null) {
				this._buzzer.Stop();
				this._buzzer = null;
			}

			if (this._leds != null) {
				this._leds.Clear();
				this._leds = null;
			}

			if (this._inputs != null) {
				this._leds.Clear();
				this._leds = null;
			}

			if (this._outputs != null) {
				this._outputs.Clear();
				this._outputs = null;
			}
			base.Dispose();
		}
        private void InitGrovePi()
        {
            System.Diagnostics.Debug.WriteLine(DeviceFactory.Build.GrovePi().GetFirmwareVersion());

            GroveRotary = DeviceFactory.Build.RotaryAngleSensor(Pin.AnalogPin0);
            GroveSound = DeviceFactory.Build.SoundSensor(Pin.AnalogPin1);
            GroveLight = DeviceFactory.Build.LightSensor(Pin.AnalogPin2);

            GroveRelay = DeviceFactory.Build.Relay(Pin.DigitalPin2);
            GroveTempHumi = DeviceFactory.Build.DHTTemperatureAndHumiditySensor(Pin.DigitalPin3, DHTModel.Dht11);
            GroveRanger = DeviceFactory.Build.UltraSonicSensor(Pin.DigitalPin4);
            GroveLedBar = DeviceFactory.Build.BuildLedBar(Pin.DigitalPin5);
            GroveBuzzer = DeviceFactory.Build.Buzzer(Pin.DigitalPin6);
            GroveButton = DeviceFactory.Build.ButtonSensor(Pin.DigitalPin7);
            GroveLCD = DeviceFactory.Build.RgbLcdDisplay();

            GroveLedBar.Initialize(GrovePi.Sensors.Orientation.GreenToRed);
            GroveLCD.SetBacklightRgb(255, 50, 255);

            DeviceFactory.Build.GrovePi().PinMode(Pin.DigitalPin2, PinMode.Output);
            Delay.Milliseconds(10);
            DeviceFactory.Build.GrovePi().Flush();

            DeviceFactory.Build.GrovePi().PinMode(Pin.DigitalPin6, PinMode.Output);
            Delay.Milliseconds(10);
            DeviceFactory.Build.GrovePi().Flush();
        }