Mercurial > ~darius > hgwebdir.cgi > EG-ZigBee
comparison __init__.py @ 3:afd270964f5b default tip
Lots of changes.. Reformatting, culled unused stuff, fixed checksums, changed output
format (so RSSI isn't part of the trigger(!)), etc..
author | darius@inchoate.localdomain |
---|---|
date | Sat, 03 Nov 2007 10:56:05 +1030 |
parents | 1e65aca0f39c |
children |
comparison
equal
deleted
inserted
replaced
2:1e65aca0f39c | 3:afd270964f5b |
---|---|
23 | 23 |
24 import wx | 24 import wx |
25 import threading | 25 import threading |
26 import win32event | 26 import win32event |
27 import win32file | 27 import win32file |
28 import struct | |
29 | 28 |
30 class Serial(eg.RawReceiverPlugin): | 29 class Serial(eg.RawReceiverPlugin): |
31 canMultiLoad = True | 30 canMultiLoad = True |
32 text = Text | 31 text = Text |
33 | 32 |
83 self.serial.close() | 82 self.serial.close() |
84 self.serial = None | 83 self.serial = None |
85 | 84 |
86 | 85 |
87 def HandleChar(self, ch): | 86 def HandleChar(self, ch): |
88 chint = struct.unpack('B', ch)[0] | |
89 if (self.state == 'init'): | 87 if (self.state == 'init'): |
90 if (ch != '\x7e'): | 88 if (ch != '\x7e'): |
91 return | 89 return |
92 | 90 |
93 self.state = 'sizemsb' | 91 self.state = 'sizemsb' |
94 return | 92 return |
95 | 93 |
96 elif (self.state == 'sizemsb'): | 94 elif (self.state == 'sizemsb'): |
97 self.bufszmsb = chint | 95 self.bufszmsb = ord(ch) |
98 self.state = 'sizelsb' | 96 self.state = 'sizelsb' |
99 return | 97 return |
100 | 98 |
101 elif (self.state == 'sizelsb'): | 99 elif (self.state == 'sizelsb'): |
102 self.bufszlsb = chint | 100 self.bufszlsb = ord(ch) |
103 self.dataleft = self.bufszmsb << 8 | self.bufszlsb | 101 self.dataleft = self.bufszmsb << 8 | self.bufszlsb |
104 | 102 |
105 self.state = 'data' | 103 self.state = 'data' |
106 return | 104 return |
107 | 105 |
108 elif (self.state == 'data'): | 106 elif (self.state == 'data'): |
109 self.buffer.append(chint) | 107 self.buffer.append(ord(ch)) |
110 self.dataleft = self.dataleft - 1 | 108 self.dataleft = self.dataleft - 1 |
111 if (self.dataleft == 0): | 109 if (self.dataleft == 0): |
112 self.state = 'cksum' | 110 self.state = 'cksum' |
113 return | 111 return |
114 | 112 |
115 elif (self.state == 'cksum'): | 113 elif (self.state == 'cksum'): |
116 pktsum = reduce(lambda x, y: x + y, self.buffer) & 0xff | 114 pktsum = reduce(lambda x, y: x + y, self.buffer) & 0xff |
117 rxcksum = chint | 115 rxcksum = ord(ch) |
116 self.state = 'init' | |
118 if (pktsum + rxcksum != 0xff): | 117 if (pktsum + rxcksum != 0xff): |
118 self.buffer = [] | |
119 self.PrintError("Bad checksum, got 0x%02x, expected 0x%02x" % | 119 self.PrintError("Bad checksum, got 0x%02x, expected 0x%02x" % |
120 rxcksum, 0xff - pktsum) | 120 rxcksum, 0xff - pktsum) |
121 else: | 121 else: |
122 self.PrintError("Triggered event: " + str(self.buffer)) | 122 if (self.buffer[0] == 0x83): |
123 self.TriggerEvent(str(self.buffer)) | 123 src = self.buffer[1] << 8 | self.buffer[2] |
124 self.buffer = [] | 124 rssi = -1 * self.buffer[3] |
125 self.state = 'init' | 125 flags = self.buffer[4] |
126 nsamples = self.buffer[5] | |
127 mask = self.buffer[6] << 8 | self.buffer[7] | |
128 if (mask | 0x01ff): | |
129 dios = self.buffer[8] << 8 | self.buffer[9] | |
130 else: | |
131 dios = 0 | |
132 # ADC samples would follow, we ignore them, also | |
133 # ignore multiple samples | |
134 self.TriggerEvent("0x%02x -> 0x%03x" % (src, dios)) | |
135 self.PrintError("0x%02x %d dBm, flags 0x%02x, nsamples %d, mask 0x%03x -> 0x%03x" % | |
136 (src, rssi, flags, nsamples, mask, dios)) | |
137 else: | |
138 self.PrintError("Got unknown packet: " + str(self.buffer)) | |
139 | |
140 self.buffer = [] | |
141 | |
126 return | 142 return |
127 else: | 143 else: |
128 self.PrintError("Internal error: bad state: " + str(self.state)) | 144 self.PrintError("Internal error: bad state: " + str(self.state)) |
129 self.state = 'init' | 145 self.state = 'init' |
130 | 146 |
189 bytesize=8, | 205 bytesize=8, |
190 parity=0, | 206 parity=0, |
191 stopbits=0, | 207 stopbits=0, |
192 handshake=0, | 208 handshake=0, |
193 generateEvents=False, | 209 generateEvents=False, |
194 prefix="Serial", | 210 prefix="ZB", |
195 ): | 211 ): |
196 text = self.text | 212 text = self.text |
197 dialog = eg.ConfigurationDialog(self) | 213 dialog = eg.ConfigurationDialog(self) |
198 portCtrl = eg.SerialPortChoice(dialog, value=port) | 214 portCtrl = eg.SerialPortChoice(dialog, value=port) |
199 | 215 |