Mercurial > ~darius > hgwebdir.cgi > EG-ZigBee
comparison __init__.py @ 2:1e65aca0f39c
- Refactor use of struct.unpack() for greater clarity.
- Fix logging of checksum errors.
author | darius@midget.dons.net.au |
---|---|
date | Mon, 29 Oct 2007 10:44:26 +1030 |
parents | c978dbb7e4b8 |
children | afd270964f5b |
comparison
equal
deleted
inserted
replaced
1:93b375e93fe1 | 2:1e65aca0f39c |
---|---|
83 self.serial.close() | 83 self.serial.close() |
84 self.serial = None | 84 self.serial = None |
85 | 85 |
86 | 86 |
87 def HandleChar(self, ch): | 87 def HandleChar(self, ch): |
88 chint = struct.unpack('B', ch)[0] | |
88 if (self.state == 'init'): | 89 if (self.state == 'init'): |
89 if (ch != '\x7e'): | 90 if (ch != '\x7e'): |
90 return | 91 return |
91 | 92 |
92 self.state = 'sizemsb' | 93 self.state = 'sizemsb' |
93 return | 94 return |
94 | 95 |
95 elif (self.state == 'sizemsb'): | 96 elif (self.state == 'sizemsb'): |
96 self.bufszmsb = struct.unpack('B', ch)[0] | 97 self.bufszmsb = chint |
97 self.state = 'sizelsb' | 98 self.state = 'sizelsb' |
98 return | 99 return |
99 | 100 |
100 elif (self.state == 'sizelsb'): | 101 elif (self.state == 'sizelsb'): |
101 self.bufszlsb = struct.unpack('B', ch)[0] | 102 self.bufszlsb = chint |
102 self.dataleft = self.bufszmsb << 8 | self.bufszlsb | 103 self.dataleft = self.bufszmsb << 8 | self.bufszlsb |
103 | 104 |
104 self.state = 'data' | 105 self.state = 'data' |
105 return | 106 return |
106 | 107 |
107 elif (self.state == 'data'): | 108 elif (self.state == 'data'): |
108 self.buffer.append(struct.unpack('B', ch)[0]) | 109 self.buffer.append(chint) |
109 self.dataleft = self.dataleft - 1 | 110 self.dataleft = self.dataleft - 1 |
110 if (self.dataleft == 0): | 111 if (self.dataleft == 0): |
111 self.state = 'cksum' | 112 self.state = 'cksum' |
112 return | 113 return |
113 | 114 |
114 elif (self.state == 'cksum'): | 115 elif (self.state == 'cksum'): |
115 cksum = reduce(lambda x, y: x + y, self.buffer) & 0xff | 116 pktsum = reduce(lambda x, y: x + y, self.buffer) & 0xff |
116 rxcksum = struct.unpack('B', ch)[0] | 117 rxcksum = chint |
117 if (cksum + rxcksum != 0xff): | 118 if (pktsum + rxcksum != 0xff): |
118 self.PrintError("Bad checksum, got 0x%02x, expected 0x%02x" % | 119 self.PrintError("Bad checksum, got 0x%02x, expected 0x%02x" % |
119 cksum, 0xff - cksum) | 120 rxcksum, 0xff - pktsum) |
120 else: | 121 else: |
121 self.PrintError("Triggered event: " + str(self.buffer)) | 122 self.PrintError("Triggered event: " + str(self.buffer)) |
122 self.TriggerEvent(str(self.buffer)) | 123 self.TriggerEvent(str(self.buffer)) |
123 self.buffer = [] | 124 self.buffer = [] |
124 self.state = 'init' | 125 self.state = 'init' |