#!/usr/bin/env python
+# -*- coding: utf-8 -*-
# vim: set expandtab tabstop=4 shiftwidth=4 :
#----------------------------------------------------------------------------
# Etherled
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 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)
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):
+ 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: