Working lib for qq v2
Diff: RF12B.cpp
- Revision:
- 1:09e016eea608
- Parent:
- 0:048b717be6c0
--- a/RF12B.cpp Fri Mar 11 18:02:05 2011 +0000 +++ b/RF12B.cpp Wed Feb 08 16:51:49 2012 +0000 @@ -1,8 +1,7 @@ #include "RF12B.h" #include "RF_defs.h" - -DigitalOut rfled(LED3); +#include <algorithm> RF12B::RF12B(PinName _SDI, PinName _SDO, @@ -58,6 +57,7 @@ /* Sends a packet of data to the RF module for transmission TODO: Make asych*/ void RF12B::write(unsigned char *data, unsigned char length) { unsigned char crc = 0; + /* Transmitter mode */ changeMode(TX); @@ -92,6 +92,48 @@ write(&data, 1); } +void RF12B::write(queue<char> &data, int length) { + char crc = 0; + char length_byte = 0; + + /* -1 means try to transmit everything in the queue */ + if(length == -1) { + length = data.size(); + } + + /* max length of packet is 255 */ + length_byte = min(length, 255); + + /* Transmitter mode */ + changeMode(TX); + + writeCmd(0x0000); + send(0xAA); // PREAMBLE + send(0xAA); + send(0xAA); + send(0x2D); // SYNC + send(0xD4); + /* Packet Length */ + send(length_byte); + crc = crc8(crc, length_byte); + send(crc); + crc = crc8(crc, crc); + /* Packet Data */ + for (char i=0; i<length_byte; i++) { + send(data.front()); + crc = crc8(crc, data.front()); + data.pop(); + } + send(crc); + send(0xAA); // DUMMY BYTES + send(0xAA); + send(0xAA); + + /* Back to receiver mode */ + changeMode(RX); + status(); +} + /********************************************************************** * PRIVATE FUNCTIONS *********************************************************************/ @@ -194,6 +236,7 @@ // I think this will slow down the pll's reaction time. Not sure, check with someone! ); + changeMode(RX); resetRX(); status(); } @@ -213,8 +256,9 @@ } /* Change the mode of the RF module to Transmitting or Receiving */ -void RF12B::changeMode(rfmode_t mode) { - if (mode == TX) { +void RF12B::changeMode(rfmode_t _mode) { + mode = _mode; + if (_mode == TX) { writeCmd(0x8239); //!er,!ebb,ET,ES,EX,!eb,!ew,DC } else { /* mode == RX */ writeCmd(0x8299); //er,!ebb,ET,ES,EX,!eb,!ew,DC @@ -230,7 +274,7 @@ static queue<unsigned char> temp; //Loop while interrupt is asserted - while (!NIRQ_in) { + while (!NIRQ_in && mode == RX) { /* Grab the packet's length byte */ if (i == -2) {