From: Leandro Lucarella Date: Tue, 1 Nov 2005 06:32:19 +0000 (+0000) Subject: Bugfixes e integración del protocolo con la interfaz gráfica. X-Git-Tag: 0.1-recibe-matriz-raw-por-udp~57 X-Git-Url: https://git.llucax.com/z.facultad/66.09/etherled.git/commitdiff_plain/113ca660652d693a5cd06c258e8fed2104d9fff0?ds=sidebyside Bugfixes e integración del protocolo con la interfaz gráfica. El cliente está terminado en cuanto a la funcionalidad pricipal (funcionalidad vital básica =), pero faltan pulir varios detalles para que sea agradable de usar. --- diff --git a/cliente/cetherled.py b/cliente/cetherled.py index dfc15f7..936d9b7 100755 --- a/cliente/cetherled.py +++ b/cliente/cetherled.py @@ -30,6 +30,7 @@ from simplegladeapp import SimpleGladeApp from simplegladeapp import bindtextdomain from dispatcher import Dispatcher from led import Led +import etherled app_name = "cetherled" app_version = "1.0" @@ -38,12 +39,12 @@ locale_dir = "" bindtextdomain(app_name, locale_dir) - class MainWindow(SimpleGladeApp): def __init__(self, path="cetherled.glade", root="main_window", domain=app_name, **kwargs): #notificar = Dispatcher(self.actualizar) + self.device = etherled.NetworkedDevice() path = os.path.join(glade_dir, path) SimpleGladeApp.__init__(self, path, root, domain, **kwargs) @@ -52,7 +53,7 @@ class MainWindow(SimpleGladeApp): for i in xrange(16): for j in xrange(16): led = Led() - self.table_leds.attach(led, i, i+1, j, j+1) + self.table_leds.attach(led, j, j+1, i, i+1) led.show() self.tabla[i,j] = led @@ -60,38 +61,22 @@ class MainWindow(SimpleGladeApp): self.quit() def on_btn_enviar_clicked(self, widget, *args): - print self.tabla_a_stream() + self.device.matrix = self.leds2matrix() def on_main_window_delete_event(self, widget, event, *args): self.quit() - def imprimir_tabla(self, rotada=False): - rango = xrange(16) - if rotada: - rango = xrange(15, -1, -1) - for col in rango: - for row in xrange(16): - print int(self.tabla[col,row].prendido), + def leds2matrix(self): + matrix = {} + for row in xrange(16): + for col in xrange(16): + matrix[row,col] = int(self.tabla[row,col].prendido) + #print int(self.tabla[row,col].prendido), + print (row,col), print print - - def imprimir_stream(self, stream): - for i in stream: - print '0x%02x' % i - - def tabla_a_stream(self): - stream = [] - for col in xrange(15, -1, -1): - i = 0 - for row in xrange(8): - i += int(self.tabla[col,row].prendido) << row - stream.append(i) - i = 0 - for row in xrange(8, 16): - i += int(self.tabla[col,row].prendido) << row - stream.append(i) - return stream - + etherled.protocol._print_matrix(matrix) + return matrix def main(): gtk.threads_init() @@ -100,7 +85,6 @@ def main(): main_window.run() gtk.threads_leave() - if __name__ == '__main__': main() diff --git a/cliente/etherled/protocol.py b/cliente/etherled/protocol.py index 56bb34a..63c537d 100644 --- a/cliente/etherled/protocol.py +++ b/cliente/etherled/protocol.py @@ -32,6 +32,9 @@ __all__ = ('SendError', 'RecvError', 'Client', 'NetworkedDevice', 'DummyServer') # Tamaño del buffer _BUFSIZ = 65536 +# Cantidad de bytes de la columna de leds +_LED_BYTES = 2 + class SendError(socket.error): pass @@ -99,40 +102,14 @@ class Client(object): class NetworkedDevice(Client): - LED_BYTES = 2 - def _getMatrix(self): stream = self.get(packet.VAR_MATRIX) - return self._stream2Matrix(stream) + return _stream2Matrix(stream) def _setMatrix(self, matrix): - stream = self._matrix2Stream(matrix) + stream = _matrix2Stream(matrix) self.set(packet.VAR_MATRIX, stream) - def _stream2Matrix(self, stream): - cols = ord(stream[0]) # Obtiene tamaño - stream = stream[1:1+cols*self.LED_BYTES] # me quedo con el resto - matrix = {} - for col in xrange(cols-1, -1, -1): - for row_byte in xrange(self.LED_BYTES): - byte = ord(stream[(cols-col-1)*self.LED_BYTES+row_byte]) - for i in xrange(8): - shift = 8 - i - 1 - matrix[col, row_byte*8+i] = (byte >> shift) & 1 - return matrix - - def _matrix2Stream(self, matrix): - cols = len(matrix) / (self.LED_BYTES*8) - stream = chr(cols) # primero va el tamaño - for col in xrange(cols-1, -1, -1): - for i in xrange(self.LED_BYTES): - byte = 0 - for row in xrange(8): - shift = 8 - row - 1 - byte += matrix[col,row] << shift - stream += chr(byte) - return stream - matrix = property(_getMatrix, _setMatrix, doc='Matriz de leds') class DummyServer: @@ -149,15 +126,21 @@ class DummyServer: while True: (msg, addr) = self._sock.recvfrom(_BUFSIZ) pkt = packet.ServerPacket(msg) + print 'Recibido:', repr(pkt) if pkt.type == packet.TYPE_GET: pkt.data = self._vars[pkt.var] elif pkt.type == packet.TYPE_SET: + if pkt.var == packet.VAR_MATRIX: + _print_matrix(_stream2Matrix(pkt.data)) self._vars[pkt.var] = pkt.data pkt = packet.ServerPacket(pkt.type, pkt.var, pkt.id) sent = self._sock.sendto(str(pkt), addr) if sent != len(pkt): raise SendError, "Sólo se enviaron %d bytes de %d" \ % (sent, len(packet)) + print 'Enviado:', repr(pkt) + if pkt.type == packet.TYPE_GET and pkt.var == packet.VAR_MATRIX: + _print_matrix(_stream2Matrix(pkt.data)) def _getHost(self): return self._host @@ -172,9 +155,33 @@ class DummyServer: port = property(_getPort, doc='Puerto al cual enviar datos') addr = property(_getAddr, doc='Tupla (host, port)') +def _stream2Matrix(stream): + cols = ord(stream[0]) # Obtiene tamaño + stream = stream[1:1+cols*_LED_BYTES] # me quedo con el resto + matrix = {} + for col in xrange(cols-1, -1, -1): + for row_byte in xrange(_LED_BYTES): + byte = ord(stream[(cols-col-1)*_LED_BYTES+row_byte]) + for i in xrange(8): + shift = 8 - i - 1 + matrix[row_byte*8+i,col] = (byte >> shift) & 1 + return matrix + +def _matrix2Stream(matrix): + cols = len(matrix) / (_LED_BYTES*8) + stream = chr(cols) # primero va el tamaño + for col in xrange(cols-1, -1, -1): + for i in xrange(_LED_BYTES): + byte = 0 + for row in xrange(8): + shift = 8 - row - 1 + byte += matrix[row+i*8,col] << shift + stream += chr(byte) + return stream + def _print_matrix(matrix): - for row in xrange(NetworkedDevice.LED_BYTES*8): - for col in xrange(len(matrix)/(NetworkedDevice.LED_BYTES*8)): + for row in xrange(_LED_BYTES*8): + for col in xrange(len(matrix)/(_LED_BYTES*8)): print matrix[row,col], print print @@ -234,6 +241,36 @@ if __name__ == '__main__': _print_matrix(dev.matrix) # Verifico resultado assert matrix == dev.matrix + ########################### + # Creo matriz + matrix = {} + for col in xrange(16): + for row in xrange(16): + matrix[row,col] = 0 + matrix[0,0] = 1 + matrix[0,14] = 1 + matrix[0,15] = 1 + matrix[1,15] = 1 + matrix[13,0] = 1 + matrix[14,0] = 1 + matrix[15,0] = 1 + matrix[15,1] = 1 + matrix[15,2] = 1 + matrix[12,15] = 1 + matrix[13,15] = 1 + matrix[14,15] = 1 + matrix[15,15] = 1 + matrix[15,14] = 1 + matrix[15,13] = 1 + matrix[15,12] = 1 + # Mando matriz + print 'Matriz enviada:' + _print_matrix(matrix) + dev.matrix = matrix + print 'Matriz recibida:' + _print_matrix(dev.matrix) + # Verifico resultado + assert matrix == dev.matrix # Matamos al servidor os.kill(pid, 15) else: