-
Notifications
You must be signed in to change notification settings - Fork 0
/
oprboard.py
127 lines (117 loc) · 4.64 KB
/
oprboard.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
126
127
from pgui import UIBase, Button, Label, InputBox, Root
from pgui.uiconsts import *
from pgui.root import warn, hint
import pygame as pg
class OprBoard(UIBase):
def init(self):
margin = 4
btnSize=(50, 25)
# home, play/pause, end, sync
# button home
btn_home = Button(self, caption='<<',
pos=(margin , margin), size=(btnSize))
btn_home.bind_command(self.cb_home)
btn_home.bind_key(K_6, self.cb_home, [KMOD_SHIFT]) # key ^
# button play
self.btn_play = btn_play = Button(self, caption='play',
pos=(margin + btnSize[0] + 1, margin), size=(btnSize))
btn_play.bind_command(self.cb_play)
# button pause
self.btn_pause = btn_pause = Button(self, caption='pause',
bgcolor=(0x8f, 0, 0, 0xff), hovercolor=(0xff, 0, 0, 0xff),
pos=(margin + btnSize[0] + 1, margin), size=(btnSize))
btn_pause.hide()
btn_pause.bind_command(self.cb_pause)
self.bind_key(pg.K_SPACE, self.cb_toggle)
# button end
btn_end = Button(self, caption='>>',
pos=(margin + 2*(btnSize[0] + 1), margin), size=(btnSize))
btn_end.bind_command(self.cb_end)
btn_end.bind_key(K_4, self.cb_end, [KMOD_SHIFT]) # key $
# button sync
self.btn_sync = btn_sync = Button(self, caption='sync',
pos=(margin + 3*(btnSize[0] + 1), margin), size=(btnSize))
btn_sync.bind_command(self.cb_sync)
btn_sync.bind_key(K_o, self.cb_sync)
# button unsync
self.btn_unsync = btn_unsync = Button(self, caption='unsync',
bgcolor=(0x8f, 0, 0, 0xff), hovercolor=(0xff, 0, 0, 0xff),
pos=(margin + 3*(btnSize[0] + 1), margin), size=(btnSize))
btn_unsync.bind_command(self.cb_unsync)
btn_unsync.bind_key(K_o, self.cb_unsync, [KMOD_SHIFT])
# the next line
# angle: [ ]
label_angle = Label(self, text='angle:',
pos=(margin, margin+btnSize[1]), size=btnSize)
self.input_angle = InputBox(self,
pos=(margin + btnSize[0] + 1, margin+btnSize[1]),
size=btnSize)
self.input_angle.bind_on_confirm(self.cb_angle)
# other values
self.servoc = None
def cb_home(self, *args):
self.servoc.goto_frame(0)
def cb_end(self, *args):
servoc = self.servoc
servo = servoc.servos[servoc.actived]
servoc.goto_frame(servo.total_frame())
def cb_play(self, *args):
if self.btn_play.is_visible():
hint('start playing')
self.servoc.play()
self.btn_play.hide()
self.btn_pause.show()
def cb_toggle(self, *args):
if self.btn_pause.is_visible():
hint('paused')
self.servoc.pause()
self.btn_play.show()
self.btn_pause.hide()
else:
hint('start playing')
self.servoc.play()
self.btn_play.hide()
self.btn_pause.show()
def cb_pause(self, *args):
if self.btn_pause.is_visible():
hint('paused')
self.servoc.pause()
self.btn_play.show()
self.btn_pause.hide()
def cb_angle(self):
# v: the new input value
v = self.input_angle.text
servoc = self.servoc
servo = servoc.servos[servoc.actived]
try:
v = int(v)
if servo.selected is not None:
i, dti = servo.find_ti_pos(servo.selected)
if i < len(servo.keyFrames) and dti == 0:
if servo.min <= v <= servo.max:
servo.keyFrames[i].a = v
servo.mark_redraw()
servo.select(servo.selected)
else:
warn("angle=%d out of range(%s)" %(v, (servo.min, servo.max)))
else:
warn("cannot set a non-key frame")
except Exception as ex:
warn(str(ex))
self.input_angle.text = ''
def cb_sync(self, *args):
if self.btn_sync.is_visible():
self.btn_sync.hide()
self.btn_unsync.show()
self.servoc.connect_robot()
def cb_unsync(self, *args):
if self.btn_unsync.is_visible():
self.btn_unsync.hide()
self.btn_sync.show()
self.servoc.disconnect_robot()
def update_info(self):
servoc = self.servoc
servo = servoc.servos[servoc.actived]
if servo.selected is not None:
self.input_angle.text = str(servo.get_a_at(servo.selected))
self.input_angle.mark_redraw()