Library to send and receive data using RF12B transceiver modules Big thanks to the tutorial at https://loee.jottit.com/rfm12b_and_avr_-_quick_start and madcowswe
Dependents: Measure_system Quadcopter_copy
RF12B.h@4:2a295db9ba1a, 2011-03-10 (annotated)
- Committer:
- harryeakins
- Date:
- Thu Mar 10 17:26:42 2011 +0000
- Revision:
- 4:2a295db9ba1a
- Parent:
- 3:e72ad65868ab
- Child:
- 5:a92c3f6d1711
Added variable length packets
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
harryeakins | 0:bd1232f200be | 1 | #ifndef _RF12B_H |
harryeakins | 0:bd1232f200be | 2 | #define _RF12B_H |
harryeakins | 0:bd1232f200be | 3 | |
harryeakins | 0:bd1232f200be | 4 | #include "mbed.h" |
harryeakins | 4:2a295db9ba1a | 5 | #include <queue> |
harryeakins | 4:2a295db9ba1a | 6 | |
harryeakins | 4:2a295db9ba1a | 7 | #define PACKET_LEN 16 |
harryeakins | 4:2a295db9ba1a | 8 | |
harryeakins | 4:2a295db9ba1a | 9 | enum rfmode_t{RX, TX}; |
harryeakins | 0:bd1232f200be | 10 | |
harryeakins | 0:bd1232f200be | 11 | class RF12B { |
harryeakins | 0:bd1232f200be | 12 | public: |
harryeakins | 4:2a295db9ba1a | 13 | /* Constructor */ |
harryeakins | 0:bd1232f200be | 14 | RF12B(PinName SDI, |
harryeakins | 2:99cf337cd23e | 15 | PinName SDO, |
harryeakins | 2:99cf337cd23e | 16 | PinName SCK, |
harryeakins | 2:99cf337cd23e | 17 | PinName NCS, |
harryeakins | 2:99cf337cd23e | 18 | PinName NIRQ); |
harryeakins | 2:99cf337cd23e | 19 | |
harryeakins | 4:2a295db9ba1a | 20 | /* Reads a byte of data from the receive buffer |
harryeakins | 4:2a295db9ba1a | 21 | Returns 0xFF if there is no data */ |
harryeakins | 0:bd1232f200be | 22 | unsigned char read(); |
harryeakins | 2:99cf337cd23e | 23 | |
harryeakins | 4:2a295db9ba1a | 24 | /* Transmits a packet of data */ |
harryeakins | 4:2a295db9ba1a | 25 | void write(unsigned char * data, unsigned char length); |
harryeakins | 4:2a295db9ba1a | 26 | |
harryeakins | 4:2a295db9ba1a | 27 | /* Transmits a 1-byte packet of data */ |
harryeakins | 4:2a295db9ba1a | 28 | void write(unsigned char data); |
harryeakins | 4:2a295db9ba1a | 29 | |
harryeakins | 4:2a295db9ba1a | 30 | /* Returns true if data is available in the receive buffer*/ |
harryeakins | 4:2a295db9ba1a | 31 | bool available(); |
harryeakins | 0:bd1232f200be | 32 | |
harryeakins | 0:bd1232f200be | 33 | private: |
harryeakins | 4:2a295db9ba1a | 34 | /* Receive FIFO buffer */ |
harryeakins | 4:2a295db9ba1a | 35 | queue<unsigned char> fifo; |
harryeakins | 4:2a295db9ba1a | 36 | |
harryeakins | 4:2a295db9ba1a | 37 | /* SPI module */ |
harryeakins | 0:bd1232f200be | 38 | SPI spi; |
harryeakins | 4:2a295db9ba1a | 39 | |
harryeakins | 4:2a295db9ba1a | 40 | /* Other digital pins */ |
harryeakins | 0:bd1232f200be | 41 | DigitalOut NCS; |
harryeakins | 4:2a295db9ba1a | 42 | InterruptIn NIRQ; |
harryeakins | 4:2a295db9ba1a | 43 | DigitalIn NIRQ_in; |
harryeakins | 3:e72ad65868ab | 44 | |
harryeakins | 2:99cf337cd23e | 45 | /* Initialises the RF12B module */ |
harryeakins | 3:e72ad65868ab | 46 | void init(); |
harryeakins | 4:2a295db9ba1a | 47 | |
harryeakins | 4:2a295db9ba1a | 48 | /* Write a command to the RF Module */ |
harryeakins | 4:2a295db9ba1a | 49 | unsigned int writeCmd(unsigned int cmd); |
harryeakins | 4:2a295db9ba1a | 50 | |
harryeakins | 4:2a295db9ba1a | 51 | /* Sends a byte of data across RF */ |
harryeakins | 4:2a295db9ba1a | 52 | void send(unsigned char data); |
harryeakins | 4:2a295db9ba1a | 53 | |
harryeakins | 3:e72ad65868ab | 54 | /* Switch module between receive and transmit modes */ |
harryeakins | 4:2a295db9ba1a | 55 | void changeMode(rfmode_t mode); |
harryeakins | 4:2a295db9ba1a | 56 | |
harryeakins | 4:2a295db9ba1a | 57 | /* Interrupt routine for data reception */ |
harryeakins | 4:2a295db9ba1a | 58 | void rxISR(); |
harryeakins | 4:2a295db9ba1a | 59 | |
harryeakins | 4:2a295db9ba1a | 60 | /* Tell the RF Module this packet is received and wait for the next */ |
harryeakins | 4:2a295db9ba1a | 61 | void resetRX(); |
harryeakins | 0:bd1232f200be | 62 | }; |
harryeakins | 0:bd1232f200be | 63 | |
harryeakins | 0:bd1232f200be | 64 | #endif /* _RF12B_H */ |