-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathProgram.cs
265 lines (225 loc) · 9.48 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// 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.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Iot.Device.LEDMatrix;
using Iot.Device.Graphics;
using System.Drawing;
using System.Numerics;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
namespace LedMatrixWeather
{
internal partial class Program
{
private static Action<RGBLedMatrix>? s_scenario = WeatherDemo;
private static Stopwatch? s_showLocalIp = null;
private static string[]? s_ips;
private static bool s_networkAvailable = false;
private static Weather? s_client;
private static OpenWeatherResponse s_weatherResponse;
private static readonly TimeZoneInfo s_timeZonePst = TimeZoneInfo.FindSystemTimeZoneById("America/Los_Angeles");
private static void Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("Usage: led-matrix-weather <openweathermap.org API key>");
Console.WriteLine();
Console.WriteLine("If wrong key is used or there is no network connection");
Console.WriteLine("example data will be displayed.");
Console.WriteLine();
Console.WriteLine("For the first 30 seconds of execution");
Console.WriteLine("IP addresses will be displayed instead of data.");
return;
}
s_client = new Weather(args[0]);
UpdateWeather();
Task.Run(WeatherUpdater);
Task.Run(IpsGetter);
PinMapping mapping = PinMapping.MatrixBonnetMapping32;
RGBLedMatrix matrix = new RGBLedMatrix(mapping, 64, 64, 2, 2);
Task drawing = Task.Run(() =>
{
matrix.StartRendering();
while (s_scenario is object)
{
Action<RGBLedMatrix> scenario = s_scenario;
Stopwatch sw = Stopwatch.StartNew();
scenario(matrix);
if (sw.ElapsedMilliseconds < 100)
{
Debug.WriteLine("Scenario execution finished in less than 100ms. This is likely due to bug.");
}
}
});
try
{
if (!Console.IsOutputRedirected)
{
while (s_scenario is object && Console.ReadKey(intercept: true).Key != ConsoleKey.Q)
{
Thread.Sleep(10);
}
s_scenario = null;
}
drawing.Wait();
}
finally
{
matrix.Dispose();
}
}
private static void WeatherUpdater()
{
while (true)
{
UpdateWeather();
Thread.Sleep(60000);
}
}
private static void IpsGetter()
{
try
{
s_ips = GetLocalNetworkIPAddresses().ToArray();
}
catch
{
// Choke all errors.
// This is function is used for diagnostics
// we don't want it to interrupt the process in any way
s_ips = new string[1] { "Cannot get IPs" };
}
s_showLocalIp = Stopwatch.StartNew();
}
private static void UpdateWeather()
{
try
{
s_weatherResponse = s_client!.GetWeatherFromOpenWeather();
s_networkAvailable = true;
}
catch (Exception e)
{
s_networkAvailable = false;
Console.WriteLine($"UpdateWeather error: {e}");
// So that we don't crash and cam display something reasonable
s_weatherResponse = OpenWeatherResponse.GetExampleResponse();
}
}
private static IEnumerable<string> GetLocalNetworkIPAddresses()
{
var networks = NetworkInterface
.GetAllNetworkInterfaces()
.Where((network) => network.OperationalStatus == OperationalStatus.Up);
foreach (var network in networks)
{
var properties = network.GetIPProperties();
if (properties.GatewayAddresses.Count == 0)
continue;
foreach (var address in properties.UnicastAddresses)
{
if (address.Address.AddressFamily != AddressFamily.InterNetwork)
continue;
if (IPAddress.IsLoopback(address.Address))
continue;
yield return address.Address.ToString();
}
}
}
private static IEnumerable<string> WeatherFeed()
{
if (s_showLocalIp != null && s_ips != null && s_ips.Length > 0 && s_showLocalIp.ElapsedMilliseconds < 30000)
{
foreach (var ip in s_ips)
{
yield return ip;
}
}
else
{
yield return ".NET IoT";
if (!s_networkAvailable)
{
yield return "Network not available. Displaying example data.";
}
yield return s_weatherResponse.Weather[0].Description;
yield return $"{s_weatherResponse.Main.Temp:0}\u00B0F";
float pressureHPa = s_weatherResponse.Main.Pressure;
yield return $"{pressureHPa:0}hPa";
yield return $"Humidity: {s_weatherResponse.Main.Humidity:0}%";
yield return $"Wind: {s_weatherResponse.Wind.Speed:1}MPH {s_weatherResponse.Wind.Deg}\u00B0";
}
}
private static void WeatherDemo(RGBLedMatrix matrix)
{
BdfFont font = BdfFont.Load(@"fonts/10x20.bdf");
BdfFont font1 = BdfFont.Load(@"fonts/8x13B.bdf");
matrix.Fill(0, 0, 0);
Thread.Sleep(100);
int textLeft = 0;
const int feedUpdateMs = 500;
const int drawIntervalMs = 25;
const int iterations = feedUpdateMs / drawIntervalMs;
byte Col(float x)
{
return (byte)Math.Clamp(x * 255, 0, 255);
}
string text = string.Empty;
int fullTextWidth = 0;
Stopwatch sw = Stopwatch.StartNew();
while (s_scenario != null)
{
text = " * " + string.Join(" * ", WeatherFeed());
fullTextWidth = text.Length * font.Width;
for (int i = 0; i < iterations && s_scenario != null; i++, textLeft --)
{
matrix.DrawText(textLeft, -2, text, font, 0, 0, 255, 0, 0, 0);
if (textLeft + fullTextWidth < matrix.Width)
{
matrix.DrawText(textLeft + fullTextWidth, -2, text, font, 0, 0, 255, 0, 0, 0);
}
if (textLeft + fullTextWidth <= 0)
{
textLeft += fullTextWidth;
}
DateTimeOffset localNow = DateTimeOffset.Now;
DateTimeOffset pstNow = TimeZoneInfo.ConvertTime(localNow, s_timeZonePst);
string d = pstNow.ToString("hh:mm:ss");
matrix.DrawText(0, font.Height + 1 - 2, d, font1, 0, 255, 0, 0, 0, 0);
int halfHeight = matrix.Height / 2;
int halfWidth = matrix.Width / 2;
float time = sw.ElapsedMilliseconds / 1000f;
for (int x = 0; x < matrix.Width; x++)
{
if (x < halfWidth)
{
for (int y = halfHeight; y < matrix.Height; y++)
{
Vector3 col3 = Clock(new Vector2((float)x / halfWidth, (float)(y - halfHeight) / halfHeight), pstNow);
Color color = Color.FromArgb(Col(col3.X), Col(col3.Y), Col(col3.Z));
matrix.SetPixel(x, y, color.R, color.G, color.B);
}
}
else
{
for (int y = halfHeight; y < matrix.Height; y++)
{
Vector2 uv = new Vector2((float)(x - halfWidth) / halfWidth, (float)(y - halfHeight) / halfHeight);
Vector3 col3 = OpenWeatherIcon(uv, s_weatherResponse.Weather[0].Icon, time);
Color color = Color.FromArgb(Col(col3.X), Col(col3.Y), Col(col3.Z));
matrix.SetPixel(x, y, color.R, color.G, color.B);
}
}
}
Thread.Sleep(drawIntervalMs);
}
}
}
}
}