comparison zb.py @ 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
comparison
equal deleted inserted replaced
4:ed7abe6f59c2 5:5d5963d542bc
59 class RX_16_Bit(PktBase): 59 class RX_16_Bit(PktBase):
60 PKT_TYPE = 0x81 60 PKT_TYPE = 0x81
61 PKT_DESC = "RX Packet: 16 bit address" 61 PKT_DESC = "RX Packet: 16 bit address"
62 ADDR_SIZE = 2 62 ADDR_SIZE = 2
63 63
64 def setsender(self, value):
65 self.resize(self.ADDR_SIZE)
66 for i, j in zip(reversed(range(self.ADDR_SIZE)), range(0, self.ADDR_SIZE * 8, 8)):
67 self.data[i] = (value & (0xff << j)) >> j
68
69 def getsender(self): 64 def getsender(self):
70 value = 0 65 value = 0
71 for i, j in zip(reversed(range(self.ADDR_SIZE)), range(0, self.ADDR_SIZE * 8, 8)): 66 for i, j in zip(reversed(range(self.ADDR_SIZE)), range(0, self.ADDR_SIZE * 8, 8)):
72 value |= self.data[i] << j 67 value |= self.data[i] << j
73 return value 68 return value
74 sender = property(getsender, setsender) 69 sender = property(getsender, None)
75 70
76 def setrssi(self, value): 71 rssi = property(lambda s: -1 * s.data[s.ADDR_SIZE], None)
77 self.resize(self.ADDR_SIZE + 1) 72
78 self.data[self.ADDR_SIZE] = -1 * value 73 flags = property(lambda s: s.data[s.ADDR_SIZE + 1], None)
79 rssi = property(lambda s: -1 * s.data[s.ADDR_SIZE], setrssi) 74
80 75 payload = property(lambda s: s.data[s.ADDR_SIZE + 2:], None)
81 def setflags(self, value): 76
82 self.resize(self.ADDR_SIZE + 2)
83 self.data[self.ADDR_SIZE + 1] = value
84 flags = property(lambda s: s.data[s.ADDR_SIZE + 1], setflags)
85
86 class RX_64_Bit(RX_16_Bit): 77 class RX_64_Bit(RX_16_Bit):
87 PKT_TYPE = 0x80 78 PKT_TYPE = 0x80
88 PKT_DESC = "RX Packet: 64 bit address" 79 PKT_DESC = "RX Packet: 64 bit address"
89 ADDR_SIZE = 8 80 ADDR_SIZE = 8
90 81
93 PKT_DESC = "RXIO Packet: 16 bit address" 84 PKT_DESC = "RXIO Packet: 16 bit address"
94 85
95 def setnsamples(self, value): 86 def setnsamples(self, value):
96 self.resize(self.ADDR_SIZE + 3) 87 self.resize(self.ADDR_SIZE + 3)
97 self.data[self.ADDR_SIZE + 2] = value 88 self.data[self.ADDR_SIZE + 2] = value
98 nsamples = property(lambda s: s.data[s.ADDR_SIZE + 2], setnsamples) 89
90 def getnsamples(self):
91 return self.data[self.ADDR_SIZE + 2]
92 nsamples = property(getnsamples, setnsamples)
99 93
100 def setmask(self, value): 94 def setmask(self, value):
101 self.resize(self.ADDR_SIZE + 5) 95 self.resize(self.ADDR_SIZE + 5)
102 self.data[self.ADDR_SIZE + 3] = (value & 0xff00) >> 8 96 self.data[self.ADDR_SIZE + 3] = (value & 0xff00) >> 8
103 self.data[self.ADDR_SIZE + 4] = value & 0xff 97 self.data[self.ADDR_SIZE + 4] = value & 0xff
122 self.data[offs + 1]) 116 self.data[offs + 1])
123 offs = offs + 2 117 offs = offs + 2
124 118
125 return rtn 119 return rtn
126 120
127 class RXIO_64_Bit(RX_16_Bit): 121 class RXIO_64_Bit(RXIO_16_Bit):
128 PKT_TYPE = 0x82 122 PKT_TYPE = 0x82
129 PKT_DESC = "RXIO Packet: 64 bit address" 123 PKT_DESC = "RXIO Packet: 64 bit address"
130 ADDR_SIZE = 8 124 ADDR_SIZE = 8
131 125
132 class TX_16_Bit(PktBase): 126 class TX_16_Bit(RX_64_Bit):
133 PKT_TYPE = 0x00 127 PKT_TYPE = 0x00
134 PKT_DESC = "TX Packet: 16 bit address" 128 PKT_DESC = "TX Packet: 16 bit address"
135 129
136 class TX_64_Bit(PktBase): 130 def setsender(self, value):
131 self.resize(self.ADDR_SIZE)
132 for i, j in zip(reversed(range(self.ADDR_SIZE)), range(0, self.ADDR_SIZE * 8, 8)):
133 self.data[i] = (value & (0xff << j)) >> j
134 sender = property(RX_64_Bit.getsender, setsender)
135
136 class TX_64_Bit(RX_64_Bit):
137 PKT_TYPE = 0x00 137 PKT_TYPE = 0x00
138 PKT_DESC = "TX Packet: 64 bit address" 138 PKT_DESC = "TX Packet: 64 bit address"
139 139
140 class TX_Status(PktBase): 140 class TX_Status(PktBase):
141 PKT_TYPE = 0x89 141 PKT_TYPE = 0x89
181 self.fr_err = 0 # Framing error 181 self.fr_err = 0 # Framing error
182 self.ck_err = 0 # Checksum error 182 self.ck_err = 0 # Checksum error
183 self.rx_cnt = 0 # Packet count 183 self.rx_cnt = 0 # Packet count
184 184
185 self.pktq = [] 185 self.pktq = []
186 186
187 def writedata(self, s, data):
188 s.write("".join(map(str, data)))
189
187 def getdata(self, s): 190 def getdata(self, s):
188 l = [] 191 l = []
189 while (1): 192 while (1):
190 a = s.read() 193 a = s.read()
191 if (a == ''): 194 if (a == ''):
192 break 195 break
193 l.append(a) 196 l.append(ord(a))
194 197
195 return self.process(l) 198 return self.process(l)
196 199
197 def process(self, data): 200 def process(self, data):
198 pktcount = 0 201 pktcount = 0
199 for d in data: 202 for d in data:
200 if (self.state == 'init'): 203 if (self.state == 'init'):
201 if (d != '\x7e'): 204 if (d != 0x7e):
202 print "Framing error, got 0x%02x, expected 0x7f" % (ord(d)) 205 print "Framing error, got 0x%02x, expected 0x7e" % (d)
203 self.fr_err += 1 206 self.fr_err += 1
204 continue 207 continue
205 208
206 self.state = 'sizemsb' 209 self.state = 'sizemsb'
207 elif (self.state == 'sizemsb'): 210 elif (self.state == 'sizemsb'):
208 self.bufmsb = ord(d) 211 self.bufmsb = d
209 self.state = 'sizelsb' 212 self.state = 'sizelsb'
210 elif (self.state == 'sizelsb'): 213 elif (self.state == 'sizelsb'):
211 self.dataleft = self.bufmsb << 8 | \ 214 self.dataleft = self.bufmsb << 8 | \
212 ord(d) 215 d
213 self.state = 'data' 216 self.state = 'data'
214 elif (self.state == 'data'): 217 elif (self.state == 'data'):
215 self.buffer.append(ord(d)) 218 self.buffer.append(d)
216 self.dataleft = self.dataleft - 1 219 self.dataleft = self.dataleft - 1
217 if (self.dataleft == 0): 220 if (self.dataleft == 0):
218 self.state = 'cksum' 221 self.state = 'cksum'
219 elif (self.state == 'cksum'): 222 elif (self.state == 'cksum'):
220 pktsum = reduce(lambda x, y: x + y, self.buffer) & 0xff 223 pktsum = reduce(lambda x, y: x + y, self.buffer) & 0xff
221 rxcksum = ord(d) 224 rxcksum = d
222 self.state = 'init' 225 self.state = 'init'
223 if (pktsum + rxcksum != 0xff): 226 if (pktsum + rxcksum != 0xff):
224 self.buffer = [] 227 self.buffer = []
225 self.ck_err += 1 228 self.ck_err += 1
226 print "Checksum error, got 0x%02x, expected 0x%02x" % \ 229 print "Checksum error, got 0x%02x, expected 0x%02x" % \
227 (rxcksum, 0xff - pktsum) 230 (rxcksum, 0xff - pktsum)
228 else: 231 else:
229 print "Got a packet - " + str(self.buffer) 232 #print "Got a packet - " + str(self.buffer)
230 p = Packets.Build(self.buffer) 233 p = Packets.Build(self.buffer)
231 self.pktq.append(p) 234 self.pktq.append(p)
232 self.buffer = [] 235 self.buffer = []
233 pktcount += 1 236 pktcount += 1
234 self.rx_cnt += 1 237 self.rx_cnt += 1
248 #s.write('+++') 251 #s.write('+++')
249 #s.readline(eol='\r') 252 #s.readline(eol='\r')
250 253
251 254
252 # 0x0001 (-36dBm) -> 1 samples, mask 0x000f, DIO - 0x00f 255 # 0x0001 (-36dBm) -> 1 samples, mask 0x000f, DIO - 0x00f
253 goodtest = ['~', '\x00', '\n', '\x83', '\x00', '\x01', '$', '\x00', '\x01', '\x00', '\x0f', '\x00', '\x0f', '8'] 256 goodtest = [126, 0, 10, 131, 0, 1, 36, 0, 1, 0, 15, 0, 15, 56]
254 257
255 # Checksum error 258 # Checksum error
256 badtest = ['~', '\x00', '\n', '\x83', '\x00', '\x01', '$', '\x00', '\x01', '\x00', '\x0f', '\x00', '\x0e', '8'] 259 badtest = [126, 0, 10, 131, 0, 1, 36, 0, 1, 0, 15, 0, 14, 56]
257 260
258 #0x0005 (-36dBm) -> 1 samples, mask 0x020e, DIO - 0x00e, ADC0 - 0x3ff 261 #0x0005 (-36dBm) -> 1 samples, mask 0x020e, DIO - 0x00e, ADC0 - 0x3ff
259 adctest = ['~', '\x00', '\x0c', '\x83', '\x00', '\x05', '$', '\x00', '\x01', '\x02', '\x0e', '\x00', '\x0e', '\x03', '\xff', '2' ] 262 adctest = [126, 0, 12, 131, 0, 5, 36, 0, 1, 2, 14, 0, 14, 3, 255, 50]
260 263
261 # Exception 264 # Exception
262 badpkttypetest = ['~', '\x00', '\x03', '\x0a', 'V', 'L', 'S'] 265 badpkttypetest = [126, 0, 3, 10, 86, 76, 83]
263 266
264 up = Packets() 267 up = Packets()
265 up.process(goodtest) 268 up.process(goodtest)
266 up.process(badtest) 269 up.process(badtest)
267 up.process(adctest) 270 up.process(adctest)