changeset 5:5d5963d542bc

More tidyups & fixes. - Remove ability to set stuff like RSSI - it is pointless. - Rejig class hierarchy so it makes more sense. - Convert to list of ordinals earlier.
author darius@inchoate.localdomain
date Wed, 31 Oct 2007 20:47:32 +1030
parents ed7abe6f59c2
children 9c499d923544
files zb.py
diffstat 1 files changed, 35 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/zb.py	Wed Oct 31 20:01:54 2007 +1030
+++ b/zb.py	Wed Oct 31 20:47:32 2007 +1030
@@ -61,28 +61,19 @@
     PKT_DESC = "RX Packet: 16 bit address"
     ADDR_SIZE = 2
     
-    def setsender(self, value):
-        self.resize(self.ADDR_SIZE)
-        for i, j in zip(reversed(range(self.ADDR_SIZE)), range(0, self.ADDR_SIZE * 8, 8)):
-            self.data[i] = (value & (0xff << j)) >> j
-
     def getsender(self):
         value = 0
         for i, j in zip(reversed(range(self.ADDR_SIZE)), range(0, self.ADDR_SIZE * 8, 8)):
             value |= self.data[i] << j
         return value
-    sender = property(getsender, setsender)
+    sender = property(getsender, None)
+
+    rssi = property(lambda s: -1 * s.data[s.ADDR_SIZE], None)
 
-    def setrssi(self, value):
-        self.resize(self.ADDR_SIZE + 1)
-        self.data[self.ADDR_SIZE] = -1 * value
-    rssi = property(lambda s: -1 * s.data[s.ADDR_SIZE], setrssi)
-    
-    def setflags(self, value):
-        self.resize(self.ADDR_SIZE + 2)
-        self.data[self.ADDR_SIZE + 1] = value
-    flags = property(lambda s: s.data[s.ADDR_SIZE + 1], setflags)
+    flags = property(lambda s: s.data[s.ADDR_SIZE + 1], None)
 
+    payload = property(lambda s: s.data[s.ADDR_SIZE + 2:], None)
+                       
 class RX_64_Bit(RX_16_Bit):
     PKT_TYPE = 0x80
     PKT_DESC = "RX Packet: 64 bit address"
@@ -95,7 +86,10 @@
     def setnsamples(self, value):
         self.resize(self.ADDR_SIZE + 3)
         self.data[self.ADDR_SIZE + 2] = value
-    nsamples = property(lambda s: s.data[s.ADDR_SIZE + 2], setnsamples)
+
+    def getnsamples(self):
+        return self.data[self.ADDR_SIZE + 2]
+    nsamples = property(getnsamples, setnsamples)
 
     def setmask(self, value):
         self.resize(self.ADDR_SIZE + 5)
@@ -124,16 +118,22 @@
 
         return rtn
 
-class RXIO_64_Bit(RX_16_Bit):
+class RXIO_64_Bit(RXIO_16_Bit):
     PKT_TYPE = 0x82
     PKT_DESC = "RXIO Packet: 64 bit address"
     ADDR_SIZE = 8
     
-class TX_16_Bit(PktBase):
+class TX_16_Bit(RX_64_Bit):
     PKT_TYPE = 0x00
     PKT_DESC = "TX Packet: 16 bit address"
 
-class TX_64_Bit(PktBase):
+    def setsender(self, value):
+        self.resize(self.ADDR_SIZE)
+        for i, j in zip(reversed(range(self.ADDR_SIZE)), range(0, self.ADDR_SIZE * 8, 8)):
+            self.data[i] = (value & (0xff << j)) >> j
+    sender = property(RX_64_Bit.getsender, setsender)
+
+class TX_64_Bit(RX_64_Bit):
     PKT_TYPE = 0x00
     PKT_DESC = "TX Packet: 64 bit address"
 
@@ -183,14 +183,17 @@
         self.rx_cnt = 0 # Packet count
         
         self.pktq = []
-    
+
+    def writedata(self, s, data):
+        s.write("".join(map(str, data)))
+                
     def getdata(self, s):
         l = []
         while (1):
             a = s.read()
             if (a == ''):
                 break
-            l.append(a)
+            l.append(ord(a))
     
         return self.process(l)
     
@@ -198,27 +201,27 @@
         pktcount = 0
         for d in data:
             if (self.state == 'init'):
-                if (d != '\x7e'):
-                    print "Framing error, got 0x%02x, expected 0x7f" % (ord(d))
+                if (d != 0x7e):
+                    print "Framing error, got 0x%02x, expected 0x7e" % (d)
                     self.fr_err += 1
                     continue
                 
                 self.state = 'sizemsb'
             elif (self.state == 'sizemsb'):
-                self.bufmsb = ord(d)
+                self.bufmsb = d
                 self.state = 'sizelsb'
             elif (self.state == 'sizelsb'):
                 self.dataleft = self.bufmsb << 8 | \
-                                ord(d)
+                                d
                 self.state = 'data'
             elif (self.state == 'data'):
-                self.buffer.append(ord(d))
+                self.buffer.append(d)
                 self.dataleft = self.dataleft - 1
                 if (self.dataleft == 0):
                     self.state = 'cksum'
             elif (self.state == 'cksum'):
                 pktsum = reduce(lambda x, y: x + y, self.buffer) & 0xff
-                rxcksum = ord(d)
+                rxcksum = d
                 self.state = 'init'
                 if (pktsum + rxcksum != 0xff):
                     self.buffer = []
@@ -226,7 +229,7 @@
                     print "Checksum error, got 0x%02x, expected 0x%02x" % \
                           (rxcksum, 0xff - pktsum)
                 else:
-                    print "Got a packet - " + str(self.buffer)
+                    #print "Got a packet - " + str(self.buffer)
                     p = Packets.Build(self.buffer)
                     self.pktq.append(p)
                     self.buffer = []
@@ -250,16 +253,16 @@
 
 
 # 0x0001 (-36dBm) -> 1 samples, mask 0x000f, DIO - 0x00f
-goodtest = ['~', '\x00', '\n', '\x83', '\x00', '\x01', '$', '\x00', '\x01', '\x00', '\x0f', '\x00', '\x0f', '8']
+goodtest = [126, 0, 10, 131, 0, 1, 36, 0, 1, 0, 15, 0, 15, 56]
 
 # Checksum error
-badtest = ['~', '\x00', '\n', '\x83', '\x00', '\x01', '$', '\x00', '\x01', '\x00', '\x0f', '\x00', '\x0e', '8']
+badtest = [126, 0, 10, 131, 0, 1, 36, 0, 1, 0, 15, 0, 14, 56]
 
 #0x0005 (-36dBm) -> 1 samples, mask 0x020e, DIO - 0x00e, ADC0 - 0x3ff
-adctest = ['~', '\x00', '\x0c', '\x83', '\x00', '\x05', '$', '\x00', '\x01', '\x02', '\x0e', '\x00', '\x0e', '\x03', '\xff', '2' ]
+adctest = [126, 0, 12, 131, 0, 5, 36, 0, 1, 2, 14, 0, 14, 3, 255, 50]
 
 # Exception
-badpkttypetest = ['~', '\x00', '\x03', '\x0a', 'V', 'L', 'S']
+badpkttypetest = [126, 0, 3, 10, 86, 76, 83]
 
 up = Packets()
 up.process(goodtest)