-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleWorker.cs
180 lines (163 loc) · 6.75 KB
/
ConsoleWorker.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
using Microsoft.Extensions.Logging;
using RyzeTelloSDK.Core;
using RyzeTelloSDK.Enum;
using RyzeTelloSDK.Extensions;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;
using Console = Colorful.Console;
namespace TelloTestApp
{
public class ConsoleWorker
{
private static readonly Dictionary<ConsoleKey, MoveDirection> MoveMappings = new Dictionary<ConsoleKey, MoveDirection>()
{
{ ConsoleKey.W, MoveDirection.Forward },
{ ConsoleKey.S, MoveDirection.Back },
{ ConsoleKey.A, MoveDirection.Left },
{ ConsoleKey.D, MoveDirection.Right },
{ ConsoleKey.R, MoveDirection.Up },
{ ConsoleKey.F, MoveDirection.Down },
};
private readonly ILogger logger;
private readonly Core core;
private readonly TelloClient client;
private readonly GamePadController gamePad;
private bool gamePadEnabled;
public ConsoleWorker(Core core, TelloClient client, GamePadController gamePad, ILogger<ConsoleWorker> logger)
{
this.core = core;
this.logger = logger;
this.client = client;
this.gamePad = gamePad;
gamePadEnabled = false;
}
public async Task MainLoop()
{
await core.Init();
RenderConsoleLoop();
var shouldLoop = true;
while (shouldLoop)
{
try
{
var key = Console.ReadKey(true);
logger.LogInformation($"{key.Key} pressed");
switch (key.Key)
{
case ConsoleKey.W:
case ConsoleKey.S:
case ConsoleKey.A:
case ConsoleKey.D:
case ConsoleKey.R:
case ConsoleKey.F:
if (gamePadEnabled == true) break;
logger.LogInformation($"FlyDirection({MoveMappings[key.Key]}, 30)");
await client.FlyDirection(MoveMappings[key.Key], 30);
break;
case ConsoleKey.Q:
case ConsoleKey.E:
if (gamePadEnabled == true) break;
logger.LogInformation($"RotateDirection({(key.Key == ConsoleKey.E ? "cw" : "ccw")}, 20)");
await client.RotateDirection(key.Key == ConsoleKey.E, 20);
break;
case ConsoleKey.T:
logger.LogInformation("TakeOff()");
await client.TakeOff();
break;
case ConsoleKey.L:
logger.LogInformation("Land()");
await client.Land();
break;
case ConsoleKey.Spacebar:
logger.LogInformation("Emergency()");
await client.Emergency();
break;
case ConsoleKey.J:
if (gamePadEnabled)
{
gamePad.Close();
gamePadEnabled = false;
}
else
{
gamePadEnabled = gamePad.Listen();
}
logger.LogInformation($"Gamepad mode is {(gamePadEnabled ? "ON" : "OFF")}");
break;
case ConsoleKey.Escape:
logger.LogInformation("Closing all of the connections");
core.Close();
shouldLoop = false;
break;
}
}
catch (Exception ex)
{
logger.LogError(ex, "Exception while proccessing key press");
}
}
logger.LogInformation("Exiting loop");
}
private async void RenderConsoleLoop()
{
RenderConsole(true);
while (true)
{
await Task.Delay(250); // 0.25s
RenderConsole();
}
}
private void RenderConsole(bool firstTime = true)
{
var state = core.GetState();
if (firstTime)
{
Console.SetWindowSize(90, 9);
Console.SetCursorPosition(15, 0);
Console.Write(" X Y Z Low. High. °C Pitch Roll Yaw");
Console.SetCursorPosition(0, 1);
Console.Write("Acceleration: Temperature:");
Console.SetCursorPosition(0, 2);
Console.Write("Velocity: TOF Height Battery Barometer");
Console.SetCursorPosition(0, 3);
Console.Write("Time:");
Console.SetCursorPosition(0, 4);
Console.Write("Gamepad:");
Console.SetCursorPosition(0, 8);
Console.Write(">");
}
Console.SetCursorPosition(16, 1);
Console.Write($"{state.AccelerationX,6:0} {state.AccelerationY,6:0} {state.AccelerationZ,6:0}");
Console.SetCursorPosition(16, 2);
Console.Write($"{state.VelocityX,6:0} {state.VelocityY,6:0} {state.VelocityZ,6:0}");
Console.SetCursorPosition(57, 1);
Console.Write($"{state.TempLowest,3}", GetTempColor(state.TempLowest));
Console.Write($" {state.TempHighest,3}", GetTempColor(state.TempHighest));
Console.SetCursorPosition(74, 1);
Console.Write($"{state.Pitch,5} {state.Roll,4} {state.Yaw,3}");
Console.SetCursorPosition(44, 3);
Console.Write($"{state.TOF,3} {state.Height,4}cm {state.Battery,6}% {state.Barometer,9:0.000}");
Console.SetCursorPosition(6, 3);
Console.Write($"{state.Time}s");
Console.SetCursorPosition(9, 4);
if (gamePadEnabled)
{
Console.Write("ON ", Color.Green);
}
else
{
Console.Write("OFF", Color.Red);
}
Console.SetCursorPosition(2, 8);
}
private Color GetTempColor(int temp)
{
if (temp > 80) return Color.Red;
if (temp > 60) return Color.Orange;
if (temp > 50) return Color.Yellow;
return Color.Green;
}
}
}