-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeliosCurses.py
executable file
·127 lines (108 loc) · 3.49 KB
/
HeliosCurses.py
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
#!/usr/bin/env python
import curses, time, sys
from curses import wrapper
from HeliosController import HeliosController
from subprocess import call
class HeliosCurses():
stdscr = None
controller = None
def __init__(self):
scr = curses.initscr(); curses.noecho(); curses.cbreak();
scr.keypad(1)
scr.clear()
self.stdscr = scr
self.controller = HeliosController("config.ini")
self.controller.subscribe(self.redraw)
def redraw(self, etype, src):
self.stdscr.refresh()
status = src.getStatus()
statLeft = status["statLeft"]
statRight = status["statRight"]
statVert = status["statVert"]
pwmValue = status["pwmValue"]
ssm = status["singleSteeringMode"]
alt = status["currentAltitude"]
ralt = status["requestedAltitude"]
autoalt = status["maintainAltitude"]
forcedescent = status["forceDescent"]
suffix = ""
if autoalt:
suffix = " - AUTO - requested %04.3fcm" % ralt
self.stdscr.addstr(1, 0, "Altitude: %04.3fcm%s" % (alt, suffix))
self.stdscr.clrtoeol()
self.stdscr.addstr(2, 0, "Forced auto-descent: %s" % forcedescent)
self.stdscr.clrtoeol()
self.stdscr.addstr(3, 0, "Steering mode: %s" % ssm)
self.stdscr.clrtoeol()
self.stdscr.addstr(4, 0, "PWM Value: %d / 1023" % pwmValue)
self.stdscr.clrtoeol()
guiopt = {
'L': statLeft == "forward",
'R': statRight == "forward",
'l': statLeft == "backward",
'r': statRight == "backward",
'D': statVert == "backward",
'U': statVert == "forward",
}
self.showArrows(6, 4, "layout.txt", guiopt)
self.showHelp(15, 0, "help.txt")
def showArrows(self, y, x, fn, opt):
dy = 0
for rawline in open(fn).readlines():
line = rawline.rstrip()
dx = 0
for c in list(line):
o = curses.A_REVERSE if (c in opt and opt[c]) else 0
l = " " if (c == " " or (c in opt and opt[c])) else "."
self.stdscr.addstr(y + dy, x + dx, l, o)
dx += 1
dy += 1
def showHelp(self, y, x, fn):
dy = 0
for rawline in open(fn).readlines():
line = rawline.rstrip()
self.stdscr.addstr(y + dy, x, line)
dy += 1
def handleKey(self, c):
if c == "q":
self.controller.shutdown()
curses.endwin()
sys.exit(0)
if c == "KEY_UP":
self.controller.forward()
if c == "KEY_DOWN":
self.controller.backward()
if c == "KEY_LEFT":
self.controller.turnLeft()
if c == "KEY_RIGHT":
self.controller.turnRight()
if c == " ":
self.controller.up()
self.controller.setAuto(False)
if c == "d":
self.controller.down()
self.controller.setAuto(False)
if c == "m":
self.controller.setHeight(self.controller.getStatus()["currentAltitude"])
self.controller.setAuto(not self.controller.getStatus()["maintainAltitude"])
if c == "f":
self.controller.setForceDescent(not self.controller.getStatus()["forceDescent"])
if c == "s":
self.controller.setSingleSteerMode(not self.controller.getStatus()["singleSteeringMode"])
if c == "+" or c == "=":
pwm = self.controller.getStatus()["pwmValue"]
self.controller.setSpeed(min(pwm + 20, 1023))
if c == "-" or c == "_":
pwm = self.controller.getStatus()["pwmValue"]
self.controller.setSpeed(max(pwm - 20, 0))
if c == "i":
#call(["/usr/bin/raspistill", "-n", "-o", "out.jpg", "-t", "100", "-w", "640", "-h", "480", "-e", "jpg"])
stdscr.addstr(0,0, "Saved image: %s" % "out.jpg")
# FIXME: increase image filename
def start(self):
while True:
c = self.stdscr.getkey()
self.handleKey(c)
if __name__ == '__main__':
hc = HeliosCurses()
hc.start()