Advertisement
Guest User

Untitled

a guest
Oct 17th, 2010
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.72 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Written By kenkeiras <kenkeiras@gmail.com>
  4. # This program is free software. It comes without any warranty, to
  5. # the extent permitted by applicable law. You can redistribute it
  6. # and/or modify it under the terms of the Do What The Fuck You Want
  7. # To Public License, Version 2, as published by Sam Hocevar. See
  8. # http://sam.zoy.org/wtfpl/COPYING for more details.
  9.  
  10. import sys, time, os
  11. from pymote import *
  12.  
  13. # Muestra los dispositivos disponibles
  14. def show_devices():
  15.     # Escanea buscando dispositivos nuevos
  16.     nearby_devices = bluetooth.discover_devices()
  17.  
  18.     # Dispositivos encontrados
  19.     print len(nearby_devices), "device(s) found"
  20.  
  21.     # Por cada uno encontrado
  22.     for bdaddr in nearby_devices:
  23.         print "-", bluetooth.lookup_name(bdaddr ), "[", bdaddr, "]"
  24.  
  25. # Obtiene la dirección de un dispositivo
  26. def get_addr(name):
  27.     address = None
  28.  
  29.     # Escanea buscando dispositivos
  30.     nearby_devices = bluetooth.discover_devices()
  31.  
  32.     # Por cada uno encontrado
  33.     for bdaddr in nearby_devices:
  34.         # Y se encontró, ya está
  35.         if (name == bluetooth.lookup_name( bdaddr )):
  36.             address = bdaddr
  37.             break
  38.  
  39.     return address
  40.  
  41. # Obtiene la próxima remesa de datos
  42. def next_bunch(f, l):
  43.     return f.read(l)
  44.  
  45. tapper = 0
  46. # Da un poco de color con los leds
  47. def tapLeds(dev):
  48.     global tapper
  49.     dev.toogle_led(tapper)
  50.     tapper = (tapper + 1) % 4
  51.  
  52.  
  53. if (len(sys.argv) < 3):
  54.     print >>sys.stderr, "Uso:", sys.argv[0],"<dispositivo> <pista.wav>"
  55. else:
  56.     f = open(sys.argv[2], "rb")
  57.     print "Obteniendo dirección..."
  58.     bdaddr = get_addr(sys.argv[1])    
  59.     if (bdaddr == None):
  60.         print >>sys.stderr,"Dispositivo no encontrado"
  61.         exit(1)
  62.     else:
  63.         print "Conectando..."
  64.         ctrl = pymote(bdaddr)
  65.         print "Reproduciendo..."
  66.         # Cantidad de frames que se pueden enviar de una vez
  67.         width = 20
  68.  
  69.         i = 0
  70.         j = os.path.getsize(sys.argv[2])
  71.         framerate = 1500.
  72.         while True:
  73.             # Se muestran los segundos reproducidos / totales
  74.             sys.stdout.write("\r" + str(int(i / framerate)) + "/" + \
  75.              str(int(j / framerate)))
  76.             sys.stdout.flush()
  77.  
  78.             # Se obtienen los próximos datos
  79.             b = next_bunch(f, width)
  80.             if (len(b) < 1):
  81.                 break
  82.             # Se envían
  83.             ctrl.send_audio(b)
  84.  
  85.             # Cada segundo cambian los leds :)
  86.             if ((i % int(framerate)) == 0 ):
  87.                tapLeds(ctrl)
  88.             i += width
  89.             # Se espera a que sea el momento de envíar los próximos frames
  90.             time.sleep(width / framerate)
  91.  
  92.         print ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement