Home | Trees | Indices | Help |
|
---|
|
1 # nxt.telegram module -- LEGO Mindstorms NXT telegram formatting and parsing 2 # Copyright (C) 2006 Douglas P Lau 3 # Copyright (C) 2009 Marcus Wanner 4 # 5 # This program is free software: you can redistribute it and/or modify 6 # it under the terms of the GNU General Public License as published by 7 # the Free Software Foundation, either version 3 of the License, or 8 # (at your option) any later version. 9 # 10 # This program is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU General Public License for more details. 14 15 'Used by nxt.system for sending telegrams to the NXT' 16 17 from cStringIO import StringIO 18 from struct import pack, unpack 19 import nxt.error 20 23 2628 29 TYPE = 0 # type byte offset 30 CODE = 1 # code byte offset 31 DATA = 2 # data byte offset 32 33 TYPE_NOT_DIRECT = 0x01 # system vs. direct type 34 TYPE_REPLY = 0x02 # reply type (from NXT brick) 35 TYPE_REPLY_NOT_REQUIRED = 0x80 # reply not required flag 36113 114 import nxt.direct 115 import nxt.system 116 117 OPCODES = dict(nxt.system.OPCODES) 118 OPCODES.update(nxt.direct.OPCODES) 11938 if pkt: 39 self.pkt = StringIO(pkt) 40 self.typ = self.parse_u8() 41 self.opcode = self.parse_u8() 42 if not self.is_reply(): 43 raise InvalidReplyError 44 if self.opcode != opcode: 45 raise InvalidOpcodeError, self.opcode 46 else: 47 self.pkt = StringIO() 48 typ = 0 49 if not direct: 50 typ |= Telegram.TYPE_NOT_DIRECT 51 if not reply_req: 52 typ |= Telegram.TYPE_REPLY_NOT_REQUIRED 53 self.add_u8(typ) 54 self.add_u8(opcode)55 58 6163 self.pkt.write(pack('%ds' % n_bytes, v))6466 self.pkt.write(pack('20s', fname))6769 self.pkt.write(pack('<b', v))7072 self.pkt.write(pack('<B', v))7375 self.pkt.write(pack('<h', v))7678 self.pkt.write(pack('<H', v))7981 self.pkt.write(pack('<i', v))8284 self.pkt.write(pack('<I', v))8587 if n_bytes: 88 return unpack('%ss' % n_bytes, 89 self.pkt.read(n_bytes))[0] 90 else: 91 return self.pkt.read()9294 return unpack('<b', self.pkt.read(1))[0]9597 return unpack('<B', self.pkt.read(1))[0]98100 return unpack('<h', self.pkt.read(2))[0]101103 return unpack('<H', self.pkt.read(2))[0]104106 return unpack('<i', self.pkt.read(4))[0]107109 return unpack('<I', self.pkt.read(4))[0]110
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Fri Jan 11 12:17:38 2013 | http://epydoc.sourceforge.net |