X-Git-Url: https://git.llucax.com/z.facultad/66.09/etherled.git/blobdiff_plain/7abe2bbcea5cea86b3c47d6bd16fd69c9a16b17e..6997eb096892845d7c7608767e731cfc2dc0f146:/cliente/etherled/protocol.py?ds=sidebyside 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: