7
pygtk.require( '2.0' );
8
import gtk, serial, struct
15
# number of channels to read
18
# maximum channel value
19
MAX_CHANNEL_VALUE = 1000
21
# serial device to use
22
SERIAL_DEVICE = '/dev/ttyUSB0'
26
self.link = serial.Serial( self.SERIAL_DEVICE, 9600 )
28
self.channel_values = []
30
self.buffer = bytearray()
33
def setup_window( self ):
35
# create and set up window
36
self.window = gtk.Window( gtk.WINDOW_TOPLEVEL )
37
self.window.connect( "delete_event", self.destroy )
38
self.window.connect( "destroy", self.destroy )
39
self.window.set_title( "Testing rc-interface serial comms" );
40
self.window.set_border_width( 30 )
41
self.window.set_modal( True )
42
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 )
52
for a in range( 0, self.NUM_CHANNELS ):
54
label = gtk.Label( "Channel %d" % ( a + 1 ) )
55
table.attach( label, 0, 1, a, a + 1 )
57
adj = gtk.Adjustment( 0, 0, self.MAX_CHANNEL_VALUE, 1, 0, 0 )
58
control = gtk.HScale( adj )
59
control.set_draw_value( False )
60
control.set_property( 'width-request', 300 )
61
control.set_sensitive( False )
62
table.attach( control, 1, 2, a, a + 1 )
64
self.controls.append( control )
67
self.window.show_all()
70
def key_press_event( self, widget, event ):
71
if event.keyval == gtk.keysyms.Escape:
77
def destroy( self, widget, data = None ):
81
def process_serial_data_at( self, pos ):
82
for a in range( 0, self.NUM_CHANNELS ):
84
value = struct.unpack( "h", self.buffer[ at : at + 2 ] )[ 0 ]
87
self.controls[ a ].set_value( value )
90
def read_serial( self ):
93
in_waiting = self.link.inWaiting()
95
self.buffer.extend( self.link.read( in_waiting ) )
97
# check buffer for double -1
100
while i < len( self.buffer ):
102
# check foir double -1
103
if i > 0 and self.buffer[ i ] == 255 and \
104
self.buffer[ i - 1 ] == 255:
106
# if we've got enough data, process it
107
start_index = i - ( self.NUM_CHANNELS * 2 + 1 )
109
self.process_serial_data_at( start_index )
111
# remove everything up to and including this double -1 from
113
self.buffer = self.buffer[ i + 1 : ]
125
while gtk.events_pending():
126
gtk.main_iteration( False )
128
# check serial interface
132
MainDialogue().main()