示例#1
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());
                }
            }
        }
 private void Timer_Tick(ThreadPoolTimer timer)
 {
     try
     {
         if (button.CurrentState != buttonState)
         {
             buttonState = button.CurrentState;
             blueLed.ChangeState(buttonState);
             buzzer.ChangeState(buttonState);
         }
         actualAmbientLight = lightSensor.SensorValue();
         if (actualAmbientLight < ambientLightThreshold)
         {
             brightness = Map(ambientLightThreshold - actualAmbientLight, 0, ambientLightThreshold, 0, 255);
         }
         else
         {
             brightness = 0;
         }
         redLed.AnalogWrite(Convert.ToByte(brightness));
         byte rgbVal = Convert.ToByte(brightness);
         display.SetBacklightRgb(rgbVal, rgbVal, 255);
         display.SetText(String.Format("Thingy\nLight: {0}", actualAmbientLight));
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.Write("Something happened: " + ex.ToString());
         throw;
     }
 }
 private void TimerCallBack(object state)
 {
     if (GroveButton.CurrentState == SensorStatus.On)
     {
         if (flag == 0)
         {
             flag = 1;
             GroveBuzzer.ChangeState(SensorStatus.Off);
         }
         else
         {
             flag = 0;
             GroveBuzzer.ChangeState(SensorStatus.On);
         }
         Task.Delay(100).Wait();
     }
 }
示例#4
0
        public async Task Buzz(int milliseconds = 1000)
        {
            _buzzer.ChangeState(SensorStatus.On);
            await Task.Delay(100);

            _buzzer.ChangeState(SensorStatus.Off);

            await Task.Delay(200);

            _buzzer.ChangeState(SensorStatus.On);
            await Task.Delay(100);

            _buzzer.ChangeState(SensorStatus.Off);

            await Task.Delay(200);

            _buzzer.ChangeState(SensorStatus.On);
            await Task.Delay(500);

            _buzzer.ChangeState(SensorStatus.Off);
        }
示例#5
0
        private void Timer_Tick(ThreadPoolTimer timer)
        {
            try {
                // Capture the current ambient noise level
                soundLevel = soundSensor.SensorValue();

                // Check the button state
                if (button.CurrentState == SensorStatus.On)
                {
                    // If the button is depressed, turn on the blue LED
                    // and activate the buzzer
                    buzzer.ChangeState(SensorStatus.On);
                    blueLed.ChangeState(SensorStatus.On);
                    // For debugging purposes, log a console message
                    System.Diagnostics.Debug.WriteLine("**** BUTTON ON ****");
                }
                else if (buzzer.CurrentState == SensorStatus.On || blueLed.CurrentState == SensorStatus.On)
                {
                    // Turn the buzzer and LED off
                    buzzer.ChangeState(SensorStatus.Off);
                    blueLed.ChangeState(SensorStatus.Off);
                }

                // Capture the current value from the Light Sensor
                actualAmbientLight = lightSensor.SensorValue();

                // If the actual light measurement is lower than the defined threshold
                // then define the LED brightness based on the delta between the actual
                // ambient light and the threshold value
                if (actualAmbientLight < ambientLightThreshold)
                {
                    // Use a range mapping method to conver the difference between the
                    // actual ambient light and the threshold to a value between 0 and 255
                    // (the 8-bit range of the LED on D6 - a PWM pin).
                    // If actual ambient light is low, the differnce between it and the threshold will be
                    // high resulting in a high brightness value.
                    brightness = Map(ambientLightThreshold - actualAmbientLight, 0, ambientLightThreshold, 0, 255);
                }
                else
                {
                    // If the actual ambient light value is above the threshold then
                    // the LED should be completely off. Set the brightness to 0
                    brightness = 0;
                }

                // AnalogWrite uses Pulse Width Modulation (PWM) to
                // control the brightness of the digital LED on pin D6.
                redLed.AnalogWrite(Convert.ToByte(brightness));

                // Use the brightness value to control the brightness of the RGB LCD backlight
                byte rgbVal = Convert.ToByte(brightness);
                display.SetBacklightRgb(rgbVal, rgbVal, rgbVal);

                // Updae the RGB LCD with the light and sound levels
                display.SetText(String.Format("Thingy\nL:{0} S:{1}", actualAmbientLight, soundLevel));
            }
            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());
            }
        }
示例#6
0
        private void OnTick(object sender, object e)
        {
            try
            {
                // Check the value of the button.

                string buttonon = button.CurrentState.ToString();
                // bool buttonison = buttonon.Equals("On", StringComparison.OrdinalIgnoreCase);
                System.Diagnostics.Debug.WriteLine("Button is " + buttonon);
            }
            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());
            }

            try
            {
                System.Diagnostics.Debug.WriteLine("Brightness: " + brightness.ToString());

                // Check the brightness, if it's going to overflow, reset it.
                if (brightness > 250)
                {
                    brightness = 0;
                }

                // Increase the brightness by 5 points.
                brightness = brightness + 5;
                // Write the values to the three LEDs.
                // USA!  Red, White, and Blue!
                Led1.AnalogWrite(Convert.ToByte(brightness));
            }

            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());
            }
            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);
                }
                try
                {
                    // Check the value of the button, turn it into a string.
                    string sensorvalue = light1.SensorValue().ToString();
                    System.Diagnostics.Debug.WriteLine("light is " + sensorvalue);
                }
                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());
                }
            }
            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());
            }
            //Task.Delay(100).Wait();     // Delay 0.1 second
            // We need to make sure we delay here.  If we don't, we won't be able to read
            // the LCD Screen.
            try
            {
                // First, output to the LCD Display.
                display.SetText("Light:" + light1.SensorValue()).SetBacklightRgb(255, 50, 255);
                // Then output to the debug window.
                System.Diagnostics.Debug.WriteLine("Hello from Dexter Industries!");
            }
            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());
            }
            try
            {
                // Check the value of the Ultrasonic Sensor
                string sensorvalue = distance1.MeasureInCentimeters().ToString();
                System.Diagnostics.Debug.WriteLine("Ultrasonic reads " + sensorvalue);
            }
            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());
            }
        }
        private void InitWorker()
        {
            IAsyncAction asyncAction = Windows.System.Threading.ThreadPool.RunAsync(
                async(workItem) =>
            {
                int work_index = 0;

                int distance        = 0, sound = 0, light = 0, rotary = 0;
                SensorStatus button = SensorStatus.Off;
                SensorStatus buzzer = SensorStatus.Off;

                while (true)
                {
                    if (work_index < 5)
                    {
                        rotary = GroveRotary.SensorValue();
                        button = GroveButton.CurrentState;

                        //
                        RotaryValue = rotary;
                        int level   = RotaryValue / 100;
                        if (level > 10)
                        {
                            level = 10;
                        }
                        GroveLedBar.SetLevel((byte)level);

                        buzzer = GroveBuzzer.CurrentState;

                        if (RotaryValue > 1000)
                        {
                            if (buzzer != SensorStatus.On)
                            {
                                GroveBuzzer.ChangeState(SensorStatus.On);
                            }
                        }
                        else
                        {
                            if (buzzer != SensorStatus.Off)
                            {
                                GroveBuzzer.ChangeState(SensorStatus.Off);
                            }
                        }

                        if (button == SensorStatus.On)
                        {
                            RelayOnOff = 1;
                            GroveRelay.ChangeState(SensorStatus.On);
                        }
                        else
                        {
                            RelayOnOff = 0;
                            GroveRelay.ChangeState(SensorStatus.Off);
                        }

                        work_index++;
                    }
                    else
                    {
                        // Read temp & humidity
                        GroveTempHumi.Measure();
                        LastTemp = GroveTempHumi.TemperatureInCelsius;
                        LastHumi = GroveTempHumi.Humidity;

                        distance = GroveRanger.MeasureInCentimeters();
                        //System.Diagnostics.Debug.WriteLine(distance);

                        sound = GroveSound.SensorValue();
                        //System.Diagnostics.Debug.WriteLine(sound);

                        light = GroveLight.SensorValue();
                        //System.Diagnostics.Debug.WriteLine(light);

                        if (distance > 0 && distance < 100)
                        {
                            ShiftLeft(DistanceList, distance);
                        }

                        ShiftLeft(SoundList, sound);
                        ShiftLeft(LightList, light);

                        work_index = 0;
                    }

                    await Dispatcher.RunAsync(
                        CoreDispatcherPriority.High,
                        () =>
                    {
                        if (work_index == 0)
                        {
                            UpdataUISlow();
                        }
                        else
                        {
                            UpdateUIFast();
                        }
                    });

                    //Delay.Milliseconds(100);
                }
            }
                );
        }
 private static async Task SoundBuzzer()
 {
     Buzzer.ChangeState(SensorStatus.On);
     await Task.Delay(400).ContinueWith(_ => Buzzer.ChangeState(SensorStatus.Off));
 }