-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
181 lines (158 loc) · 3.71 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"log"
"github.com/eleniums/tello-engine/engine"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
)
const (
minSpeed = 0
maxSpeed = 100
defaultMoveSpeed = 50
defaultRotateSpeed = 80
)
var (
moveSpeed = defaultMoveSpeed
rotateSpeed = defaultRotateSpeed
moveXAxis = false
moveYAxis = false
moveZAxis = false
moveRotate = false
)
func main() {
flag.BoolVar(&engine.Debug, "debug", false, "true to enable debug logging")
flag.Parse()
// prepare to handle input and start the drone
e := engine.NewEngine()
e.Start(false)
defer e.Stop()
e.StartVideoStream()
pixelgl.Run(func() {
// open window for receiving keyboard input
win, err := run()
if err != nil {
panic(err)
}
// main loop to handle keyboard input
for !win.Closed() {
win.UpdateInput()
processKeys(e, win)
if win.Pressed(pixelgl.KeyEscape) {
break
}
}
})
}
func run() (*pixelgl.Window, error) {
cfg := pixelgl.WindowConfig{
Title: "tello-engine",
Bounds: pixel.R(0, 0, 480, 360),
VSync: true,
}
win, err := pixelgl.NewWindow(cfg)
if err != nil {
return nil, fmt.Errorf("Error creating window: %v", err)
}
win.SetSmooth(true)
return win, nil
}
func processKeys(e *engine.Engine, win *pixelgl.Window) {
if win.JustPressed(pixelgl.KeyX) {
e.TakeOff()
} else if win.JustPressed(pixelgl.KeyZ) {
e.Land()
}
if win.JustPressed(pixelgl.KeyO) {
moveSpeed -= 10
if moveSpeed < minSpeed {
moveSpeed = minSpeed
}
if engine.Debug {
log.Printf("Move speed decreased to: %v", moveSpeed)
}
} else if win.JustPressed(pixelgl.KeyP) {
moveSpeed += 10
if moveSpeed > maxSpeed {
moveSpeed = maxSpeed
}
if engine.Debug {
log.Printf("Move speed increased to: %v", moveSpeed)
}
}
if win.JustPressed(pixelgl.KeyU) {
rotateSpeed -= 10
if rotateSpeed < minSpeed {
rotateSpeed = minSpeed
}
if engine.Debug {
log.Printf("Rotate speed decreased to: %v", rotateSpeed)
}
} else if win.JustPressed(pixelgl.KeyI) {
rotateSpeed += 10
if rotateSpeed > maxSpeed {
rotateSpeed = maxSpeed
}
if engine.Debug {
log.Printf("Rotate speed increased to: %v", rotateSpeed)
}
}
if win.JustPressed(pixelgl.KeyLeft) {
moveXAxis = true
e.Left(moveSpeed)
} else if win.JustPressed(pixelgl.KeyRight) {
moveXAxis = true
e.Right(moveSpeed)
} else if moveXAxis && !win.Pressed(pixelgl.KeyLeft) && !win.Pressed(pixelgl.KeyRight) {
moveXAxis = false
e.Left(0)
}
if win.JustPressed(pixelgl.KeyUp) {
moveYAxis = true
e.Forward(moveSpeed)
} else if win.JustPressed(pixelgl.KeyDown) {
moveYAxis = true
e.Backward(moveSpeed)
} else if moveYAxis && !win.Pressed(pixelgl.KeyUp) && !win.Pressed(pixelgl.KeyDown) {
moveYAxis = false
e.Forward(0)
}
if win.JustPressed(pixelgl.KeyA) {
moveRotate = true
e.RotateLeft(rotateSpeed)
} else if win.JustPressed(pixelgl.KeyD) {
moveRotate = true
e.RotateRight(rotateSpeed)
} else if moveRotate && !win.Pressed(pixelgl.KeyA) && !win.Pressed(pixelgl.KeyD) {
moveRotate = false
e.RotateLeft(0)
}
if win.JustPressed(pixelgl.KeyW) {
moveZAxis = true
e.Up(moveSpeed)
} else if win.JustPressed(pixelgl.KeyS) {
moveZAxis = true
e.Down(moveSpeed)
} else if moveZAxis && !win.Pressed(pixelgl.KeyW) && !win.Pressed(pixelgl.KeyS) {
moveZAxis = false
e.Up(0)
}
if win.JustPressed(pixelgl.KeyQ) {
e.LeftFlip()
} else if win.JustPressed(pixelgl.KeyE) {
e.RightFlip()
} else if win.JustPressed(pixelgl.KeyF) {
e.FrontFlip()
} else if win.JustPressed(pixelgl.KeyR) {
e.BackFlip()
}
if win.JustPressed(pixelgl.KeyB) {
e.Bounce()
}
if win.JustPressed(pixelgl.KeySpace) {
moveSpeed = defaultMoveSpeed
rotateSpeed = defaultRotateSpeed
e.ResetMovement()
}
}