X-Git-Url: https://git.llucax.com/z.facultad/66.09/etherled.git/blobdiff_plain/113ca660652d693a5cd06c258e8fed2104d9fff0..HEAD:/cliente/etherled/protocol.py diff --git a/cliente/etherled/protocol.py b/cliente/etherled/protocol.py index 63c537d..e8a1703 100644 --- a/cliente/etherled/protocol.py +++ b/cliente/etherled/protocol.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -# -*- coding: iso-8859-1 -*- +# -*- coding: utf-8 -*- # vim: set expandtab tabstop=4 shiftwidth=4 : #---------------------------------------------------------------------------- # Etherled @@ -20,7 +20,7 @@ # with etherled; if not, write to the Free Software Foundation, Inc., 59 # Temple Place, Suite 330, Boston, MA 02111-1307 USA #---------------------------------------------------------------------------- -# Creado: sáb oct 29 00:45:52 ART 2005 +# Creado: sáb oct 29 00:45:52 ART 2005 # Autores: Leandro Lucarella #---------------------------------------------------------------------------- @@ -29,7 +29,7 @@ import packet __all__ = ('SendError', 'RecvError', 'Client', 'NetworkedDevice', 'DummyServer') -# Tamaño del buffer +# Tamaño del buffer _BUFSIZ = 65536 # Cantidad de bytes de la columna de leds @@ -43,7 +43,7 @@ class RecvError(socket.error): class Client(object): - def __init__(self, host='localhost', port=38437, timeout=3.0): + def __init__(self, host='localhost', port=9876, timeout=3.0): self._host = host self._port = port self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) @@ -59,7 +59,7 @@ class Client(object): except socket.timeout: raise SendError, "Tiempo de espera agotado" if sent != len(pkt): - raise SendError, "Sólo se enviaron %d bytes de %d" \ + raise SendError, "Sólo se enviaron %d bytes de %d" \ % (sent, len(pkt)) while True: try: @@ -67,6 +67,7 @@ class Client(object): except socket.timeout: raise RecvError, "Tiempo de espera agotado" pkt_r = packet.ClientPacket(msg) + # Verifica ACK if pkt == pkt_r: break # paquete ok self._drop += 1 @@ -75,12 +76,12 @@ class Client(object): def get(self, var): return self.send(packet.TYPE_GET, var) - def set(self, var, data): + def set(self, var, data=None): self.send(packet.TYPE_SET, var, data) def _getId(self, type, var): id = self._ids.get((type, var), 0) - self._ids[type, var] = (id + 1) % packet.MAX_ID + self._ids[type, var] = (id + 1) % (packet.MAX_ID + 1) return id def _getHost(self): @@ -102,6 +103,9 @@ class Client(object): class NetworkedDevice(Client): + def turn_off(self): + self.set(packet.VAR_OFF) + def _getMatrix(self): stream = self.get(packet.VAR_MATRIX) return _stream2Matrix(stream) @@ -110,11 +114,27 @@ class NetworkedDevice(Client): stream = _matrix2Stream(matrix) self.set(packet.VAR_MATRIX, stream) + def _getPaused(self): + return bool(ord(self.get(packet.VAR_PAUSE))) + + def _setPaused(self, paused): + self.set(packet.VAR_PAUSE, chr(paused)) + + def _getDelay(self): + return ord(self.get(packet.VAR_DELAY)) + + def _setDelay(self, delay): + self.set(packet.VAR_DELAY, chr(delay & 0xFF)) + matrix = property(_getMatrix, _setMatrix, doc='Matriz de leds') + paused = property(_getPaused, _setPaused, + doc='Indica si el dispositivo está en pausa') + delay = property(_getDelay, _setDelay, + doc='Timpo de retardo del dibujado') class DummyServer: - def __init__(self, host='localhost', port=38437, timeout=3.0): + def __init__(self, host='localhost', port=9876, timeout=3.0): self._host = host self._port = port self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) @@ -136,7 +156,7 @@ class DummyServer: 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" \ + 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: @@ -156,26 +176,24 @@ class DummyServer: addr = property(_getAddr, doc='Tupla (host, port)') def _stream2Matrix(stream): - cols = ord(stream[0]) # Obtiene tamaño + 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 col in xrange(cols): for row_byte in xrange(_LED_BYTES): - byte = ord(stream[(cols-col-1)*_LED_BYTES+row_byte]) + byte = ord(stream[col*_LED_BYTES+_LED_BYTES-row_byte-1]) for i in xrange(8): - shift = 8 - i - 1 - matrix[row_byte*8+i,col] = (byte >> shift) & 1 + matrix[row_byte*8+i,col] = (byte >> i) & 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): + stream = chr(cols) # primero va el tamaño + for col in xrange(cols): + for i in xrange(_LED_BYTES-1, -1, -1): byte = 0 - for row in xrange(8): - shift = 8 - row - 1 - byte += matrix[row+i*8,col] << shift + for row in xrange(7, -1, -1): + byte += matrix[row+i*8,col] << row stream += chr(byte) return stream @@ -271,6 +289,15 @@ if __name__ == '__main__': _print_matrix(dev.matrix) # Verifico resultado assert matrix == dev.matrix + # Probamos con otras cositas + dev.paused = True + assert dev.paused == True + dev.paused = False + assert dev.paused == False + dev.delay = 0x40 + assert dev.delay == 0x40 + dev.delay = 0xff + assert dev.delay == 0xff # Matamos al servidor os.kill(pid, 15) else: