+
+ def MCPrts(self,TXB0=False,TXB1=False,TXB2=False):
+ """Requests to send one of the transmit buffers."""
+ flags=0;
+ if TXB0: flags=flags|1;
+ if TXB1: flags=flags|2;
+ if TXB2: flags=flags|4;
+
+ if flags==0:
+ print "Warning: Requesting to send no buffer.";
+
+ self.SPItrans([0x80|flags]);
+ def readrxbuffer(self,packbuf=0):
+ """Reads the RX buffer. Might have old data."""
+ data=self.SPItrans([0x90|(packbuf<<2),
+ 0x00,0x00, #SID
+ 0x00,0x00, #EID
+ 0x00, #DLC
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00
+ ]);
+ return data[1:len(data)];
+ def writetxbuffer(self,packet,packbuf=0):
+ """Writes the transmit buffer."""
+ self.SPItrans([0x40|(packbuf<<1)]+packet);
+
+ def rxpacket(self):
+ """Reads the next incoming packet from either buffer.
+ Returns None immediately if no packet is waiting."""
+ status=self.MCPrxstatus()&0xC0;
+ if status&0x40:
+ #Buffer 0 has higher priority.
+ return self.readrxbuffer(0);
+ elif status&0x80:
+ #Buffer 1 has lower priority.
+ return self.readrxbuffer(1);
+ else:
+ return None;
+ def txpacket(self,packet):
+ """Transmits a packet through one of the outbound buffers.
+ As usual, the packet should begin with SIDH.
+ For now, only TXB0 is supported."""
+ self.writetxbuffer(packet,0);
+ self.MCPrts(TXB0=True);
+ def packet2str(self,packet):
+ """Converts a packet from the internal format to a string."""
+ toprint="";
+ for bar in packet:
+ toprint=toprint+("%02x "%ord(bar))
+ return toprint;