#!/usr/bin/env python
-# -*- coding: iso-8859-1 -*-
+# -*- coding: utf-8 -*-
# vim: set expandtab tabstop=4 shiftwidth=4 :
#----------------------------------------------------------------------------
# Etherled
# 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 <llucare@fi.uba.ar>
#----------------------------------------------------------------------------
__all__ = ('SendError', 'RecvError', 'Client', 'NetworkedDevice', 'DummyServer')
-# Tamaño del buffer
+# Tamaño del buffer
_BUFSIZ = 65536
# Cantidad de bytes de la columna de leds
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)
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:
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
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):
class NetworkedDevice(Client):
+ def turn_off(self):
+ self.set(packet.VAR_OFF)
+
def _getMatrix(self):
stream = self.get(packet.VAR_MATRIX)
return _stream2Matrix(stream)
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)
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:
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
_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: