Example #1
0
 private void rpiRun()
 {
     Grove_LedBar.SetLevel((byte)index);
     if (index++ == 10)
     {
         index = 0;
     }
 }
 // Set LedBar light up level
 private void setLedBar(byte level)
 {
     for (byte i = 1; i < level + 1; i++)
     {
         GroveLedBar.SetLevel(i);
         Task.Delay(1).Wait();
     }
 }
Example #3
0
        private async void Timer_Tick(ThreadPoolTimer timer)
        {
            // record the start time
            DateTime start = DateTime.Now;

            // update the display
            leds.Initialize(GrovePi.Sensors.Orientation.GreenToRed);
            leds.SetLevel((byte)0);


            int    lightValue = 0;
            string lightError = string.Empty;

            // read the light
            try
            {
                lightValue = lightSensor.SensorValue();
                // int between 0 (dark) and 1023 (bright)
            }
            catch (Exception ex)
            {
                lightError = ex.Message;
            }

            int    soundValue = 0;
            string soundError = String.Empty;

            try
            {
                soundValue = soundSensor.SensorValue();
            }
            catch (Exception ex)
            {
                soundError = ex.Message;
            }

            // check if token is still valid
            // get a new token every 12 minutes - no real need to since we'll usually keep the connection alive
            if (token == null || token == string.Empty)
            {
                token = await fmserver.Authenticate();

                tokenRecieved = DateTime.Now;
            }
            else if (DateTime.Now > tokenRecieved.AddMinutes(12))
            {
                int logoutResponse = await fmserver.Logout();

                token    = string.Empty;
                fmserver = GetFMSInstance();
                token    = await fmserver.Authenticate();

                tokenRecieved = DateTime.Now;
            }
            if (token != string.Empty)
            {
                // get some data from the RPI itself
                var processorName = Raspberry.Board.Current.ProcessorName;
                var rpiModel      = Raspberry.Board.Current.Model.ToString();

                // write it to FMS
                var request = fmserver.NewRecordRequest();

                if (processorName != null)
                {
                    request.AddField("rpiProcessor", processorName);
                }

                if (rpiModel != null)
                {
                    request.AddField("rpiModel", rpiModel);
                }

                request.AddField("when_start", start.ToString());
                request.AddField("sound", soundValue.ToString());
                request.AddField("sound_error", soundError);
                request.AddField("light", lightValue.ToString());
                request.AddField("light_error", lightError);
                request.AddField("when", DateTime.Now.ToString());

                // add a script call to calculate the time diff to the previous record
                request.AddScript(ScriptTypes.after, "calculate_gap_grove");

                var response = await request.Execute();

                if (fmserver.lastErrorCode != 0)
                {
                    leds.SetLevel((byte)2);
                }
                else
                {
                    leds.SetLevel((byte)10);
                }
                Thread.Sleep(TimeSpan.FromMilliseconds(250));
                // no longer logging out after each call
                // await fmserver.Logout();
                // token = string.empty;
            }
            // clear the display again
            leds.SetLevel((byte)1);
            Thread.Sleep(TimeSpan.FromMilliseconds(250));
        }
        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);
                }
            }
                );
        }