15
15
# number of channels to read
18
# number of motor channels
18
21
# maximum channel value
19
22
MAX_CHANNEL_VALUE = 1000
21
24
# serial device to use
22
SERIAL_DEVICE = '/dev/ttyACM0'
25
SERIAL_DEVICE = '/dev/ttyUSB0'
25
28
def __init__( self ):
26
self.link = serial.Serial( self.SERIAL_DEVICE, 9600 )
30
self.link = serial.Serial( self.SERIAL_DEVICE, 9600 )
31
self.link_error = None
32
except serial.serialutil.SerialException as e:
27
35
self.got_one = False
28
self.channel_values = []
36
self.motor_values = [ 0 ] * self.NUM_MOTORS
30
38
self.buffer = bytearray()
41
def add_info( self, vbox, heading = False, text = '' ):
44
label.set_markup( "<b>%s</b>" % text )
46
label.set_label( text )
47
# label.set_justify( gtk.JUSTIFY_LEFT )
48
label.set_alignment( 0, 0 )
49
vbox.pack_start( label, False, False )
33
51
def setup_window( self ):
35
53
# create and set up window
41
59
self.window.set_modal( True )
42
60
self.window.connect( "key-press-event", self.key_press_event )
45
table = gtk.Table( self.NUM_CHANNELS, 2 )
46
table.set_row_spacings( 20 )
47
table.set_col_spacings( 10 )
48
self.window.add( table )
62
hbox = gtk.HBox( False, 10 )
63
self.window.add( hbox )
66
frame = gtk.Frame( "Channels" )
67
frame.set_shadow_type( gtk.SHADOW_IN )
68
hbox.pack_start( frame )
70
table = gtk.Table( 2, self.NUM_CHANNELS )
71
table.set_border_width( 25 )
72
table.set_row_spacings( 10 )
73
table.set_col_spacings( 15 )
52
for a in range( 0, self.NUM_CHANNELS ):
78
for a in range( self.NUM_CHANNELS ):
54
label = gtk.Label( "Channel %d" % ( a + 1 ) )
55
table.attach( label, 0, 1, a, a + 1 )
80
label = gtk.Label( a + 1 )
81
table.attach( label, a, a + 1, 1, 2 )
57
83
adj = gtk.Adjustment( 0, 0, self.MAX_CHANNEL_VALUE, 1, 0, 0 )
58
control = gtk.HScale( adj )
84
control = gtk.VScale( adj )
59
85
control.set_draw_value( False )
60
control.set_property( 'width-request', 300 )
86
control.set_inverted( True )
87
control.set_size_request( -1, 200 )
61
88
control.set_sensitive( False )
62
table.attach( control, 1, 2, a, a + 1 )
89
table.attach( control, a, a + 1, 0, 1 )
64
91
self.controls.append( control )
94
frame = gtk.Frame( "Motors" )
95
frame.set_shadow_type( gtk.SHADOW_IN )
96
hbox.pack_start( frame )
98
table = gtk.Table( 2, self.NUM_MOTORS )
99
table.set_border_width( 25 )
100
table.set_row_spacings( 10 )
101
table.set_col_spacings( 15 )
106
for a in range( self.NUM_MOTORS ):
108
label = gtk.Label( a + 1 )
109
table.attach( label, a, a + 1, 1, 2 )
111
adj = gtk.Adjustment( 0, 0, self.MAX_CHANNEL_VALUE, 1, 0, 0 )
112
control = gtk.VScale( adj )
113
control.set_draw_value( False )
114
control.set_inverted( True )
115
control.set_size_request( -1, 200 )
116
table.attach( control, a, a + 1, 0, 1 )
118
control.connect( "value-changed", self.on_motor_change, a )
120
self.motors.append( control )
123
frame = gtk.Frame( "Info" )
124
hbox.pack_start( frame )
127
vbox.set_border_width( 10 )
130
self.add_info( vbox, True, "USB Device:" )
131
self.add_info( vbox, False, self.SERIAL_DEVICE )
132
self.add_info( vbox )
134
if( self.link_error is not None ):
135
self.add_info( vbox, True, "USB Error:" )
136
label = gtk.Label( self.link_error )
137
label.set_line_wrap( True )
138
label.set_size_request( 150, -1 )
139
vbox.pack_start( label, False )
143
vbox.pack_start( gtk.Label( "" ) )
67
146
self.window.show_all()
149
def on_motor_change( self, widget, motor ):
150
self.motor_values[ motor ] = int( widget.get_value() )
70
154
def key_press_event( self, widget, event ):
71
155
if event.keyval == gtk.keysyms.Escape:
72
156
self.window.destroy()