Пример #1
0
        // Display value on 8x8 LED Matrix using TinyFont
        private void displayDataOn8x8()
        {
            double value = sensordata.temperature;
            Color  color = Colors.Blue;

            switch (count)
            {
            case 1:
                value = double.Parse(senseHat.Sensors.Humidity.ToString());
                color = Colors.Red;
                break;

            default:
                value = sensordata.temperature;
                color = Colors.Blue;
                break;
            }

            display = senseHat.Display;
            display.Clear();
            tinyFont.Write(display, ((int)Math.Round(value)).ToString(), color);
            display.Update();

            count++;

            // Reset counter to 0 so that the LED matrix can show temperature value
            if (count > 1)
            {
                count = 0;
            }
        }
Пример #2
0
        private void AskTempState()
        {
            if (!askTempInitialized)
            {
                if (isDayTime())
                {
                    tempToConfirm = forecastHighTemp;
                }
                else
                {
                    tempToConfirm = forecastLowTemp;
                }
                userHasMadeTempChoice = false;
                userAgreeWithTemp     = false;
                display.Clear();
                display.Fill(Colors.Black);
                tinyFont.Write(display, tempToConfirm, textColor);
                display.Update();
                askTempInitialized = true;
            }
            else
            {
                if (keyStateChanged)
                {
                    // ask user to agree (up/down with green) or disagree(left/right with red) temperature forecast
                    if ((lastButtonState == ButtonStates.LEFT) || (lastButtonState == ButtonStates.RIGHT))
                    {
                        userAgreeWithTemp     = false;
                        userHasMadeTempChoice = true;
                        display.Clear();
                        display.Fill(Colors.Red);
                        tinyFont.Write(display, tempToConfirm, textColor);
                        display.Update();
                    }
                    else if ((lastButtonState == ButtonStates.UP) || (lastButtonState == ButtonStates.DOWN))
                    {
                        userAgreeWithTemp     = true;
                        userHasMadeTempChoice = true;
                        display.Clear();
                        display.Fill(Colors.Green);
                        tinyFont.Write(display, tempToConfirm, textColor);
                        display.Update();
                    }
                    else
                    {
                        if (!userHasMadeTempChoice)
                        {
                            return;
                        }

                        currentState           = States.ASK_FORECAST;
                        askForecastInitialized = false;
                        stateChanged           = true;
                        LogStateChange();
                    }
                }
            }
        }
Пример #3
0
        private async void Timer_TickAsync(ThreadPoolTimer timer)
        {
            // Update and Initialize all Sensors
            UpdateAllSensors();

            if (_senseHat.Sensors.Temperature.HasValue)
            {
                // Read and Send Telemetry
                var telemetry = GetDeviceTelemetry();
                await _conn.SendEvent(JsonConvert.SerializeObject(telemetry));

                Debug.WriteLine($"Message send to IoTHub:  {JsonConvert.SerializeObject(telemetry)}");

                var result = await _conn.ReceiveEvent();

                if (!String.IsNullOrEmpty(result))
                {
                    Debug.WriteLine("Message received from IoTHub: " + result);
                    Random r = new Random();
                    _color = Color.FromArgb((byte)r.Next(), (byte)r.Next(), (byte)r.Next(), (byte)r.Next());
                }

                _senseHat.Display.Clear();
                _tinyFont.Write(_senseHat.Display, telemetry.Temperature.ToString().Substring(0, 2), _color);
                _senseHat.Display.Update();
            }
            else
            {
                Debug.WriteLine("Unable to Initialize SenseHat!");
            }
        }
Пример #4
0
        public async Task LogAction(double obj)
        {
            await Task.Run(() =>
            {
                var tinyFont = new TinyFont();

                ISenseHatDisplay display = _senseHat.Display;

                double humidityValue = obj;

                int humidity = (int)Math.Round(humidityValue);

                string text = humidity.ToString();

                if (text.Length > 2)
                {
                    // Too long to fit the display!
                    text = "**";
                }

                display.Clear();
                Color color;
                color = GetTextColor(humidity);

                tinyFont.Write(display, text, color);
                display.Update();
            });
        }
Пример #5
0
        public override void Run()
        {
            var tinyFont = new TinyFont();

            ISenseHatDisplay display = SenseHat.Display;

            while (true)
            {
                SenseHat.Sensors.HumiditySensor.Update();

                if (SenseHat.Sensors.Temperature.HasValue)
                {
                    int    temperature = (int)Math.Round(SenseHat.Sensors.Temperature.Value);
                    string text        = temperature.ToString();

                    if (text.Length > 2)
                    {
                        // Too long to fit the display!
                        text = "**";
                    }

                    display.Clear();
                    tinyFont.Write(display, text, Colors.White);
                    display.Update();

                    // Sleep quite some time; the temperature usually change quite slowly...
                    Sleep(TimeSpan.FromSeconds(5));
                }
                else
                {
                    // Rapid update until there is a temperature reading.
                    Sleep(TimeSpan.FromSeconds(0.5));
                }
            }
        }
        public override void Run()
        {
            var    tinyFont = new TinyFont();
            Cpu    cpu      = new Cpu();
            double temp_calibrated;

            ISenseHatDisplay display = SenseHat.Display;

            TemperatureUnit unit = TemperatureUnit.Celcius; // The wanted temperature unit.

            string unitText = GetUnitText(unit);            // Get the unit as a string.

            while (true)
            {
                SenseHat.Sensors.HumiditySensor.Update();

                if (SenseHat.Sensors.Temperature.HasValue)
                {
                    double temperatureValue = ConvertTemperatureValue(unit, SenseHat.Sensors.Temperature.Value);
                    double?cpu_temp         = cpu.GetTemperature();
                    if (cpu_temp != null)
                    {
                        temp_calibrated = temperatureValue - (((double)cpu_temp - temperatureValue) / 0.79);
                    }
                    else
                    {
                        temp_calibrated = temperatureValue;
                    }

                    int temperature = (int)Math.Round((double)temp_calibrated);

                    string text = temperature.ToString();

                    if (text.Length > 2)
                    {
                        // Too long to fit the display!
                        text = "**";
                    }

                    display.Clear();
                    tinyFont.Write(display, text, Color.Blue);
                    display.Update();

                    SetScreenText?.Invoke($"{temperatureValue:0.0} {unitText}"); // Update the MainPage (if it's utilized; i.e. not null).

                    // Sleep quite some time; the temperature usually change quite slowly...
                    Sleep(TimeSpan.FromSeconds(5));
                }
                else
                {
                    // Rapid update until there is a temperature reading.
                    Sleep(TimeSpan.FromSeconds(0.5));
                }
            }
        }
Пример #7
0
        private async Task CollectSensorDataAsync(IProgress <Measurement> progress)
        {
            using (ISenseHat senseHat = await SenseHatFactory.GetSenseHat())
            {
                while (Closing == false)
                {
                    try
                    {
                        senseHat.Sensors.HumiditySensor.Update();
                        senseHat.Sensors.PressureSensor.Update();

                        var measurement = new Measurement()
                        {
                            Temperature = (senseHat.Sensors.Temperature ?? 0) - 12.5,
                            Humidity    = senseHat.Sensors.Humidity ?? 0,
                            Pressure    = senseHat.Sensors.Pressure ?? 0,
                        };

                        #region SenseHAT LED
                        senseHat.Display.Clear();

                        int    temperature     = (int)Math.Round(measurement.Temperature);
                        string temperatureText = temperature.ToString();
                        tinyFont.Write(senseHat.Display, temperatureText, Colors.Aqua);

                        senseHat.Display.Screen[2, 5] = Colors.Orange;

                        senseHat.Display.Screen[4, 5] = Colors.Orange;
                        senseHat.Display.Screen[5, 5] = Colors.Orange;
                        senseHat.Display.Screen[4, 6] = Colors.Orange;
                        senseHat.Display.Screen[4, 7] = Colors.Orange;
                        senseHat.Display.Screen[5, 7] = Colors.Orange;

                        senseHat.Display.Update();
                        #endregion

                        progress.Report(measurement);

                        //await AzureIoTHub.SendDeviceToCloudMessageAsync(measurement);
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine("Exception: " + e.Message);
                    }
                    await Task.Delay(5000);
                }
            }
        }
Пример #8
0
        public override void Run()
        {
            temperaturestate = true;

            var tinyFont = new TinyFont();

            ISenseHatDisplay display = SenseHat.Display;

            TemperatureUnit unit = TemperatureUnit.Celcius; //we want celcius :v

            string unitText = GetUnitText(unit);

            while (temperaturestate == true)
            {
                SenseHat.Sensors.HumiditySensor.Update();

                if (SenseHat.Sensors.Temperature.HasValue)
                {
                    double temperatureValue = ConvertTemperatureValue(unit, SenseHat.Sensors.Temperature.Value);

                    int    temperature = (int)Math.Round(temperatureValue);
                    string text        = temperature.ToString();

                    if (text.Length > 2)
                    {
                        // too long to display :'v
                        text = "**";
                    }

                    display.Clear();
                    tinyFont.Write(display, text, Colors.White);
                    display.Update();

                    SetScreenText?.Invoke($"{temperatureValue:0:1} {unitText}");

                    Sleep(TimeSpan.FromSeconds(2));

                    temperaturestate = false;
                }
                else
                {
                    //rapid update until value is available
                    Sleep(TimeSpan.FromSeconds(0.5));
                }
                ActionRunner.Run(senseHat => HomeSelector.GetAction(senseHat, SetScreenText));
            }
        }
        private void state(string text, ISenseHat _senseHat)
        {
            var tinyFont = new TinyFont();

            ISenseHatDisplay display = _senseHat.Display;

            if (text.Length > 2)
            {
                // Too long to fit the display!
                text = "**";
            }

            display.Clear();

            tinyFont.Write(display, text, Colors.White);
            display.Update();
        }
        public void Write(string text, Color color, DisplayDirection rotation = DisplayDirection.Deg0)
        {
            SenseHat.Display.Reset();
            SenseHat.Display.Direction = rotation;

            if (text.Length > 2)
            {
                // Too long to fit the display!
                text = text.Substring(0, 2);
            }

            var tinyFont = new TinyFont();

            SenseHat.Display.Clear();
            tinyFont.Write(SenseHat.Display, text, color);
            SenseHat.Display.Update();
        }
Пример #11
0
        public override void Run()
        {
            pressurestate = true;

            var tinyFont = new TinyFont();

            ISenseHatDisplay display = SenseHat.Display;

            while (pressurestate == true)
            {
                SenseHat.Sensors.PressureSensor.Update();

                if (SenseHat.Sensors.Pressure.HasValue)
                {
                    double pressurevalue = SenseHat.Sensors.Pressure.Value;

                    int    pressure = (int)Math.Round(pressurevalue / 1000);
                    string text     = pressure.ToString();

                    if (text.Length > 2)
                    {
                        // too long to display :'v
                        text = "**";
                    }

                    display.Clear();
                    tinyFont.Write(display, text, Colors.IndianRed);
                    display.Update();

                    SetScreenText?.Invoke($"{pressure:0:1}");

                    Sleep(TimeSpan.FromSeconds(2));

                    pressurestate = false;
                }
                else
                {
                    Sleep(TimeSpan.FromSeconds(0.5));
                }
                ActionRunner.Run(senseHat => HomeSelector.GetAction(senseHat, SetScreenText));
            }
        }
Пример #12
0
        public override void Run()
        {
            humiditystate = true;

            var tinyFont = new TinyFont();

            ISenseHatDisplay display = SenseHat.Display;

            while (humiditystate == true)
            {
                SenseHat.Sensors.HumiditySensor.Update();

                if (SenseHat.Sensors.Humidity.HasValue)
                {
                    double humidityValue = SenseHat.Sensors.Humidity.Value;
                    int    humidity      = (int)Math.Round(humidityValue);
                    string text          = humidity.ToString();

                    if (text.Length > 2)
                    {
                        text = "**";
                    }

                    display.Clear();
                    tinyFont.Write(display, text, Colors.Blue);
                    display.Update();

                    SetScreenText?.Invoke($"{humidityValue:0:1}");

                    Sleep(TimeSpan.FromSeconds(2));

                    humiditystate = false;
                }
                else
                {
                    Sleep(TimeSpan.FromSeconds(0.5));
                }
                ActionRunner.Run(senseHat => HomeSelector.GetAction(senseHat, SetScreenText));
            }
        }
        private async Task <List <string> > TheWholeThing(int theIndexNumberWhichIsCool)
        {
            StorageFile file = await storageFolder.CreateFileAsync(fileName,
                                                                   CreationCollisionOption.OpenIfExists);

            ISenseHat senseHat = await SenseHatFactory.GetSenseHat().ConfigureAwait(false);

            ISenseHatDisplay display = senseHat.Display;

            var exceptionSave = new List <string>();

            //Connect to the senseHat & Clear the display
            var    tinyFont = new TinyFont();
            string myText   = theIndexNumberWhichIsCool.ToString();

            if (myText.Length > 2)
            {
                myText = myText.Substring(0, 2);
            }
            for (int i = 0; i < 5; ++i)
            {
                display.Clear();
                tinyFont.Write(display, myText, Windows.UI.Colors.Green);
                display.Update();
                //_waitEvent.Wait(TimeSpan.FromMilliseconds(100));
                display.Clear();
                //display.Update();
                //_waitEvent.Wait(TimeSpan.FromMilliseconds(200));
            }

            //Reading Temperature
            CustomData myTemperatureReading = new CustomData
            {
                value     = "",
                timestamp = "",
                dataType  = "te",
                sensorID  = sensorID
            };

            senseHat.Sensors.HumiditySensor.Update();
            for (int i = 0; i <= 3; ++i)
            {
                if (senseHat.Sensors.Temperature.HasValue)
                {
                    myTemperatureReading.timestamp = getTimeNow().ToString(timeStampFormat);
                    var ttt = senseHat.Sensors.Temperature.Value;
                    myTemperatureReading.value = ttt.ToString();
                    break;
                }
                //else _waitEvent.Wait(TimeSpan.FromMilliseconds(100));
            }
            string temperatureReadingString = JsonConvert.SerializeObject(myTemperatureReading);

            //Read Acceleration
            CustomData myAccelerationReading = new CustomData
            {
                value     = "",
                timestamp = "",
                dataType  = "ac",
                sensorID  = sensorID
            };

            senseHat.Sensors.ImuSensor.Update();
            for (int i = 0; i <= 3; ++i)
            {
                if (senseHat.Sensors.Acceleration.HasValue)
                {
                    myAccelerationReading.timestamp = getTimeNow().ToString(timeStampFormat);
                    myAccelerationReading.value     = senseHat.Sensors.Acceleration.Value.ToString();
                    break;
                }
                //else _waitEvent.Wait(TimeSpan.FromMilliseconds(100));
            }
            string accelerationReadingString = JsonConvert.SerializeObject(myAccelerationReading);

            //send data to Azure
            var message = new Message(Encoding.ASCII.GetBytes(temperatureReadingString));

            try
            {
                await DeviceClient.SendEventAsync(message);
            }
            catch
            {
                Debug.WriteLine("Sending temperature data to cloud failed.");
                await FileIO.AppendTextAsync(file, temperatureReadingString);

                exceptionSave.Add(temperatureReadingString);
            }
            message = new Message(Encoding.ASCII.GetBytes(accelerationReadingString));

            try
            {
                await DeviceClient.SendEventAsync(message);
            }
            catch
            {
                Debug.WriteLine("Sending acceleration data to cloud failed.");
                await FileIO.AppendTextAsync(file, accelerationReadingString);

                exceptionSave.Add(accelerationReadingString);
            }

            //Reading
            using (IRandomAccessStream textStream = await file.OpenReadAsync())
            {
                using (DataReader textReader = new DataReader(textStream))
                {
                    uint textLength = (uint)textStream.Size;
                    await textReader.LoadAsync(textLength);

                    Debug.WriteLine(textReader.ReadString(textLength));
                }
            }

            return(exceptionSave);
        }