X-Git-Url: https://git.llucax.com/z.facultad/66.09/etherled.git/blobdiff_plain/cf711545839c42cd01d385f5d474099a590b7cfd..163891ab133ae8e210f3e4493ebbd6875a4e9327:/cliente/etherled/packet.py diff --git a/cliente/etherled/packet.py b/cliente/etherled/packet.py index 7bbd18a..7783458 100644 --- a/cliente/etherled/packet.py +++ b/cliente/etherled/packet.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- # vim: set expandtab tabstop=4 shiftwidth=4 : #---------------------------------------------------------------------------- # Etherled @@ -23,19 +24,23 @@ # Autores: Leandro Lucarella #---------------------------------------------------------------------------- +from sets import ImmutableSet as frozenset + # Tipos de operación TYPE_GET = 0 TYPE_SET = 1 # Variables -VAR_MATRIX = 0 +VAR_OFF = 0 +VAR_MATRIX = 1 +VAR_PAUSE = 2 +VAR_DELAY = 3 -# Limites -MAX_ID = 8 -MAX_VAR = 8 +# Variables soportadas +supported_vars = frozenset([VAR_OFF, VAR_MATRIX, VAR_PAUSE, VAR_DELAY]) -class ParityError(ValueError): - pass +# Limites +MAX_ID = 7 class Packet(object): @@ -50,12 +55,9 @@ class Packet(object): def fromStr(self, string): header = ord(string[0]) - self.type = header >> 7 - self.var = (header & 0x70) >> 4 - self.id = (header & 0x0E) >> 1 - par = header & 0x01 - if self.par != par: - raise ParityError + self.type = header >> 7 # bit 7 + self.var = (header & 0x78) >> 3 # bits 6 5 4 3 + self.id = (header & 0x07) # bits 2 1 0 self.data = string[1:] or None def __str__(self): @@ -76,16 +78,7 @@ class Packet(object): return self.type == p.type and self.var == p.var and self.id == p.id def _header_to_int(self): - res = (self.type << 7) + (self.var << 4) + (self.id << 1) - return res + self.par - - def _getPar(self): - par = self.type - for i in xrange(3): - par += int((self.var & (1 << i)) != 0) - for i in xrange(3): - par += int((self.id & (1 << i)) != 0) - return par % 2 + return (self.type << 7) + (self.var << 3) + self.id def _getType(self): return self._type @@ -99,22 +92,21 @@ class Packet(object): return self._var def _setVar(self, var): - if var < 0 and var >= self.MAX_VAR: - raise ValueError, "var debe estar entre 0 y %d" % self.MAX_VAR-1 + if var not in supported_vars: + raise ValueError, "var puede ser uno de %s" % tuple(supported_vars) self._var = var def _getId(self): return self._id def _setId(self, id): - if id < 0 and id >= self.MAX_ID: - raise ValueError, "id debe estar entre 0 y %d" % self.MAX_ID-1 + if id < 0 or id > MAX_ID: + raise ValueError, "id debe estar entre 0 y %d" % MAX_ID self._id = id type = property(_getType, _setType, doc="Tipo de operación") var = property(_getVar, _setVar, doc="Variable con la cual operar") id = property(_getId, _setId, doc="Identificador del paquete") - par = property(_getPar, doc="Paridad de la cabecera del paquete") class ClientPacket(Packet): @@ -152,13 +144,29 @@ class ServerPacket(Packet): # Prueba if __name__ == '__main__': - assert str(Packet(1, 7, 7)) == '\xFF' - assert str(Packet(0, 0, 0)) == '\x00' - assert str(Packet(1, 1, 1)) == '\x93' - assert str(Packet(TYPE_SET, VAR_MATRIX, 2)) == '\x84' - assert str(Packet(TYPE_GET, 4, 0)) == 'A' - assert str(Packet(TYPE_GET, 4, 0, 'hola')) == 'Ahola' - p = Packet(TYPE_GET, 4, 0, 'hola') + # GET + assert str(Packet(TYPE_GET, VAR_OFF, 0)) == chr(0x00) + assert str(Packet(TYPE_GET, VAR_OFF, 7)) == chr(0x07) + assert str(Packet(TYPE_GET, VAR_MATRIX, 1)) == chr(0x09) + assert str(Packet(TYPE_GET, VAR_MATRIX, 6)) == chr(0x0E) + assert str(Packet(TYPE_GET, VAR_PAUSE, 2)) == chr(0x12) + assert str(Packet(TYPE_GET, VAR_PAUSE, 5)) == chr(0x15) + assert str(Packet(TYPE_GET, VAR_DELAY, 3)) == chr(0x1B) + assert str(Packet(TYPE_GET, VAR_DELAY, 4)) == chr(0x1C) + assert str(Packet(TYPE_GET, VAR_DELAY, 7, 'hola')) == chr(0x1F) + 'hola' + p = Packet(TYPE_GET, VAR_MATRIX, 0, 'hola') + assert Packet(str(p)) == p + # SET + assert str(Packet(TYPE_SET, VAR_OFF, 0)) == chr(0x80) + assert str(Packet(TYPE_SET, VAR_OFF, 7)) == chr(0x87) + assert str(Packet(TYPE_SET, VAR_MATRIX, 1)) == chr(0x89) + assert str(Packet(TYPE_SET, VAR_MATRIX, 6)) == chr(0x8E) + assert str(Packet(TYPE_SET, VAR_PAUSE, 2)) == chr(0x92) + assert str(Packet(TYPE_SET, VAR_PAUSE, 5)) == chr(0x95) + assert str(Packet(TYPE_SET, VAR_DELAY, 3)) == chr(0x9B) + assert str(Packet(TYPE_SET, VAR_DELAY, 4)) == chr(0x9C) + assert str(Packet(TYPE_SET, VAR_DELAY, 7, 'hola')) == chr(0x9F) + 'hola' + p = Packet(TYPE_SET, VAR_MATRIX, 0, 'hola') assert Packet(str(p)) == p print "OK!"