diff options
author | Ebus-at-dockstar <ebus@dockstar> | 2013-03-25 10:24:28 +0100 |
---|---|---|
committer | Ebus-at-dockstar <ebus@dockstar> | 2013-03-25 10:24:43 +0100 |
commit | 862282ce99760832d3e9e5b4b1171b861105e004 (patch) | |
tree | 0e229418e391917b79d42a8bdee46fb7a8612895 /heap/ebus/model/__init__.py | |
parent | 9522cdffa94f278eb5e1894600535986e22c2890 (diff) | |
download | ebus-alt-862282ce99760832d3e9e5b4b1171b861105e004.tar.gz ebus-alt-862282ce99760832d3e9e5b4b1171b861105e004.zip |
move old stuff away
Diffstat (limited to 'heap/ebus/model/__init__.py')
-rw-r--r-- | heap/ebus/model/__init__.py | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/heap/ebus/model/__init__.py b/heap/ebus/model/__init__.py new file mode 100644 index 0000000..f6a44b9 --- /dev/null +++ b/heap/ebus/model/__init__.py @@ -0,0 +1,99 @@ +# -*- coding:utf8 -*- +class DataField(object): + def __init__(self, name, offset): + self.name = name + self.offset = offset + def value(self,data): + raise NotImplemented() + +class Data1b(DataField): + """ + Beispiel für die Berechnung: + if ((x & 80h) == 80h) // y negativ + y = - [dez(!x) + 1] + else + y = dez(x) + """ + def value(self,data): + x = ord(data[self.offset]) + if x & 0x80 == 0x80: + return (-1) * (0xff^x + 1) + else: + return x + +class Data1c(DataField): + def value(self,data): + return ord(data[self.offset])/2.0 + +class Data2c(DataField): + """ + Beispiel für die Berechnung: + if ((x & 8000h) == 8000h) // y negativ + y = - [dez(High_Byte(!x)) 16 + dez(High_Nibble (Low_Byte (!x))) + + (dez(Low_Nibble (Low_Byte (!x))) +1 ) / 16] + else // y positiv + y = dez(High_Byte(x)) 16 + dez(High_ Nibble (Low Byte (x))) + + dez(Low_ Nibble (Low Byte (x))) / 16 + """ + def value(self,data): + lowByte = ord(data[self.offset]) + highByte = ord(data[self.offset+1]) + n_highByte = 0xff ^ highByte + n_lowByte = 0xff ^ lowByte + n_lowByteHighNibble = n_lowByte >> 4 + n_lowByteLowNibble = 0x0f & n_lowByte + if (0x8000 & (highByte<<8 | lowByte)) == 0x8000: + return (-1) * ( (n_highByte<<4 + n_lowByteHighNibble + (n_lowByteLowNibble+1)) / 16.0 ) + else: + return highByte*16 + (lowByte>>4) + (0x0f & lowByte)/16.0 + +class Data2b(DataField): + """ + if ((x&8000h) == 8000h) // y negativ + y = - [dez(High_Byte(!x)) + (dez(Low_Byte(!x)) + 1) / 256] + else // y positiv + y = dez(High_Byte (x)) + dez(Low_Byte (x)) / 256 + """ + def value(self,data): + highByte = ord(data[self.offset+1]) + lowByte = ord(data[self.offset]) + n_highByte = 0xff ^ highByte + n_lowByte = 0xff ^ lowByte + if (0x8000 & (highByte<<8 | lowByte)) == 0x8000: + return (-1) * (n_highByte + (n_lowByte+1)/256.0) + else: + return highByte + lowByte/256.0 + +class Bit(DataField): + def value(self, data): + return ord(data[self.offset]) == 0x1 + +class Word(DataField): + def value(self, data): + lb, hb = data[self.offset], data[self.offset+1] + return ord(lb) + (ord(hb)<<8) + +class Bcd(DataField): + """ + y = dez(High_Nibble(x))*10 + dez(Low_Nibble(x)) + """ + def value(self, data): + byte = ord(data[self.offset]) + return (byte >> 4) * 10 + (byte & 0xf) + +class Byte(DataField): + def value(self, data): + return ord(data[self.offset]) + + +class ByteEnum(DataField): + def __init__(self, name, offset, values): + DataField.__init__(self, name, offset) + self.values = values + + def value(self, data): + value = ord(data[self.offset]) + if self.values.has_key(value): + return self.values[value] + else: + return None |