#!/usr/bin/env python
-# -*- coding: iso-8859-1 -*-
+# -*- coding: utf-8 -*-
# vim: set expandtab tabstop=4 shiftwidth=4 :
#----------------------------------------------------------------------------
# Etherled
import gtk
from simplegladeapp import SimpleGladeApp
from simplegladeapp import bindtextdomain
+from optparse import OptionParser
#from dispatcher import Dispatcher
from led import Led
import etherled
bindtextdomain(app_name, locale_dir)
+ROWS = 16
+
class MainWindow(SimpleGladeApp):
def __init__(self, path="cetherled.glade", root="main_window",
domain=app_name, **kwargs):
+ self.columns = kwargs.get('columns', 16)
+ self.host = kwargs.get('host', 'localhost')
+ self.port = kwargs.get('port', 9876)
#notificar = Dispatcher(self.actualizar)
- self.device = etherled.NetworkedDevice()
+ self.device = etherled.NetworkedDevice(self.host, self.port)
path = os.path.join(glade_dir, path)
SimpleGladeApp.__init__(self, path, root, domain, **kwargs)
def new(self):
self.tabla = {}
- for i in xrange(16):
- for j in xrange(16):
+ for i in xrange(ROWS):
+ for j in xrange(self.columns):
led = Led()
self.table_leds.attach(led, j, j+1, i, i+1)
led.show()
self.device.matrix = self.leds2matrix()
def on_btn_recibir_clicked(self, widget, *args):
- self.device.matrix = self.leds2matrix()
+ matrix = self.device.matrix
+ for row in xrange(ROWS):
+ for col in xrange(self.columns):
+ self.tabla[row,col].prendido = matrix[row,col]
+ self.tabla[row,col].queue_draw()
+ etherled.protocol._print_matrix(matrix)
def on_main_window_delete_event(self, widget, event, *args):
self.quit()
def leds2matrix(self):
matrix = {}
- for row in xrange(16):
- for col in xrange(16):
+ for row in xrange(ROWS):
+ for col in xrange(self.columns):
matrix[row,col] = int(self.tabla[row,col].prendido)
etherled.protocol._print_matrix(matrix)
return matrix
- def matrix2leds(self, matrix):
- for row in xrange(16):
- for col in xrange(16):
- self.tabla[row,col].prendido = matrix[row,col]
- etherled.protocol._print_matrix(matrix)
- return matrix
+
+def parse_options():
+ parser = OptionParser(description="Cliente de etherled",
+ version="%prog " + app_version, prog='cetherled')
+ parser.add_option("-s", "--server", default='localhost', dest="host",
+ metavar="HOSTNAME", help="Nombre/IP del host del dispositivo "
+ "[default: localhost]")
+ parser.add_option("-p", "--port", default=9876, metavar="PORT",
+ type="int", help="Puerto UDP del dispositivo [default: 9876].")
+ parser.add_option("-c", "--columns", default=16, metavar="COLS",
+ type="int", help="Cantidad de columnas de la matriz [default: 16].")
+ parser.add_option("-g", "--gui", default=False, action="store_true",
+ help="Levanta la interfaz gráfica para dibujar la matriz")
+ (opts, args) = parser.parse_args()
+ return (parser, opts, args)
+
def main():
- gtk.threads_init()
- main_window = MainWindow()
- gtk.threads_enter()
- main_window.run()
- gtk.threads_leave()
+ (parser, opts, args) = parse_options()
+ if (opts.columns < 8) or (opts.columns > 32):
+ parser.error("El número de columnas debe estar entre 8 y 32.")
+ if opts.gui:
+ gtk.threads_init()
+ main_window = MainWindow(columns=opts.columns, host=opts.host,
+ port=opts.port)
+ gtk.threads_enter()
+ main_window.run()
+ gtk.threads_leave()
+ else:
+ if len(args) < 2:
+ parser.error("Debe especificarse un comando si no se usa la GUI.")
+ type = args[0]
+ var = args[1]
+ dev = etherled.NetworkedDevice(opts.host, opts.port)
+ if type == 'get':
+ if len(args) > 2:
+ parser.error("El comando get no puede llevar argumentos.")
+ if var == 'matrix':
+ etherled.protocol._print_matrix(dev.matrix)
+ elif var == 'pause':
+ print dev.paused
+ elif var == 'delay':
+ print dev.delay
+ else:
+ parser.error("Variable desconocida, debe ser una de: "
+ "matrix, pause, delay.")
+ elif type == 'set':
+ if var == 'off':
+ dev.turn_off()
+ elif var == 'matrix':
+ parser.error("Use la GUI para enviar la matriz.")
+ elif var == 'pause':
+ dev.paused = True
+ elif var == 'continue':
+ dev.paused = False
+ elif var == 'delay':
+ if len(args) != 3:
+ parser.error("Delay lleva 1 argumento.")
+ dev.delay = int(args[2])
+ else:
+ parser.error("Variable desconocida, debe ser una de: "
+ "off, pause, continue, delay.")
+
if __name__ == '__main__':
main()