forked from dotnet/iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
88 lines (69 loc) · 2.39 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Drawing;
using System.Threading;
using Iot.Device.Common;
using Iot.Device.SenseHat;
using UnitsNet;
// set this to the current sea level pressure in the area for correct altitude readings
var defaultSeaLevelPressure = WeatherHelper.MeanSeaLevel;
using SenseHat sh = new();
int n = 0;
int x = 3, y = 3;
while (true)
{
Console.Clear();
(int dx, int dy, bool holding) = JoystickState(sh);
if (holding)
{
n++;
}
x = (x + 8 + dx) % 8;
y = (y + 8 + dy) % 8;
sh.Fill(n % 2 == 0 ? Color.DarkBlue : Color.DarkRed);
sh.SetPixel(x, y, Color.Yellow);
var tempValue = sh.Temperature;
var temp2Value = sh.Temperature2;
var preValue = sh.Pressure;
var humValue = sh.Humidity;
var accValue = sh.Acceleration;
var angValue = sh.AngularRate;
var magValue = sh.MagneticInduction;
var altValue = WeatherHelper.CalculateAltitude(preValue, defaultSeaLevelPressure, tempValue);
Console.WriteLine($"Temperature Sensor 1: {tempValue.DegreesCelsius:0.#}\u00B0C");
Console.WriteLine($"Temperature Sensor 2: {temp2Value.DegreesCelsius:0.#}\u00B0C");
Console.WriteLine($"Pressure: {preValue.Hectopascals:0.##} hPa");
Console.WriteLine($"Altitude: {altValue.Meters:0.##} m");
Console.WriteLine($"Acceleration: {sh.Acceleration} g");
Console.WriteLine($"Angular rate: {sh.AngularRate} DPS");
Console.WriteLine($"Magnetic induction: {sh.MagneticInduction} gauss");
Console.WriteLine($"Relative humidity: {humValue.Percent:0.#}%");
// WeatherHelper supports more calculations, such as saturated vapor pressure, actual vapor pressure and absolute humidity.
Console.WriteLine($"Heat index: {WeatherHelper.CalculateHeatIndex(tempValue, humValue).DegreesCelsius:0.#}\u00B0C");
Console.WriteLine($"Dew point: {WeatherHelper.CalculateDewPoint(tempValue, humValue).DegreesCelsius:0.#}\u00B0C");
Thread.Sleep(1000);
}
(int, int, bool) JoystickState(SenseHat sh)
{
sh.ReadJoystickState();
int dx = 0;
int dy = 0;
if (sh.HoldingUp)
{
dy--; // y goes down
}
if (sh.HoldingDown)
{
dy++;
}
if (sh.HoldingLeft)
{
dx--;
}
if (sh.HoldingRight)
{
dx++;
}
return (dx, dy, sh.HoldingButton);
}