# HG changeset patch # User Tim MB # Date 1298982526 0 # Node ID e1b88e546699784f5a8a616391602c375aa5d3f6 # Parent 7286e5d455a758db0434e8725b950b92b93d6549 Made Python ask you for IP address and save it within a cache. diff -r 7286e5d455a7 -r e1b88e546699 tim_grid_mapper/grid_mapper.py --- a/tim_grid_mapper/grid_mapper.py Tue Mar 01 11:32:02 2011 +0000 +++ b/tim_grid_mapper/grid_mapper.py Tue Mar 01 12:28:46 2011 +0000 @@ -19,8 +19,15 @@ #### OSC OPTIONS - THESE NEED TO BE SET MANUALLY #### my_port = 12344 # to receive OSC messages from kinect + +# Both joe and ableton ip addresses are now read from the file ip_addresses.txt +# which is written to by the set_ip_addresses() function. + #joe = ('localhost', 12346)#changed by andrew - python sending to joe -ableton = ('localhost', 12345)#changed by andrew - max receiving from Joe +#ableton = ('localhost', 12345)#changed by andrew - max receiving from Joe +ip_address_file = 'ip_addresses.txt' +ableton = None ### THESE WILL BE SET AUTOMATICALLY BELOW +joe = None ### DO NOT CHANGE THEM HERE ### Constants for grid mapping: # The range of values that the input coordinates and output values may take: @@ -217,15 +224,8 @@ def start(): '''Set up OSC servers and start the program running. ''' - global joe, ableton, server - if joe[1] == "THIS_MUST_BE_SET": - joe_port = input("Enter port number on %s for Joe's synth software: " % joe[0]) - joe = (joe[0], joe_port) - - if ableton[1] == "THIS_MUST_BE_SET": - ableton_port = input("Enter port number on %s for Ableton: " % ableton[0]) - ableton = (ableton[0], ableton_port) - + global server + set_ip_addresses() server = ThreadingOSCServer(('localhost', my_port)) # Register OSC callbacks: server.addMsgHandler('/person', person_handler) @@ -250,6 +250,69 @@ else: print('Error: server has been told to close but is still running.') + +def get_ip_addresses_from_file(filename=ip_address_file): + '''Load up ip addresses from a file in the format saved by the + set_ip_addresses function. + ''' + f = open(filename, 'r') + lines = f.readlines() + global ableton, joe + ableton = eval(lines[1].split('#')[0].strip()) # set ableton + joe = eval(lines[2].split('#')[0].strip()) # set joe + if not ableton: + print('Failed to set IP for Ableton from file.') + + if not joe: + print('Failed to set IP for Joe from file.') + f.close() + + + +def set_ip_addresses(save_file=ip_address_file): + '''Manually set IP addresses based on input from user. IPs are then saved to + a file. + ''' + global joe, ableton + if not joe: joe = ('localhost', 12346) + if not ableton: ableton = ('localhost', 12345) + # set joe + try: + new_joe = eval(raw_input("Enter IP address and port for Joe in form: "+str(joe)+' or leave blank to keep that address.\n')) + except: + new_joe = None + if new_joe: joe = new_joe + print("Address for Joe: "+str(joe)+"\n") + # set ableton + try: + new_ableton = eval(raw_input('Enter IP address and port for Ableton in form: '+str(ableton)+' or leave blank to keep that address.\n')) + except: + new_ableton = None + if new_ableton: ableton = new_ableton + print('Address for Ableton: '+str(ableton)+'\n') + # save to file + if new_ableton or new_joe: + print('Writing new addresses to file %s.' % save_file) + try: + f = open(save_file, 'w') + f.write( +'''# This is a cache. These addresses will be overridden by Python if you set something different. Do not leave comments here they will be overridden +ableton = %s # set Ableton IP and port +joe = %s # set Joe's processing IP and port +''' % (str(ableton),str(joe)) + ) + f.flush() + f.close() + except IOError: + print('SAVE FAILED.') + + + + + +get_ip_addresses_from_file(ip_address_file) + + if __name__=='__main__': start() while True: @@ -260,3 +323,4 @@ exception = e trace = traceback.format_exc() print('Exception saved as `exception`. Stack trace saved as `trace`.') +