Shuto Naruse
/
Eurobot2012_Beacons
Eurobot2012_Beacons
RF12B/RF12B.h@0:b8be0a00c4f8, 2012-04-18 (annotated)
- Committer:
- narshu
- Date:
- Wed Apr 18 21:08:20 2012 +0000
- Revision:
- 0:b8be0a00c4f8
complete beacon code;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
narshu | 0:b8be0a00c4f8 | 1 | #ifndef _RF12B_H |
narshu | 0:b8be0a00c4f8 | 2 | #define _RF12B_H |
narshu | 0:b8be0a00c4f8 | 3 | |
narshu | 0:b8be0a00c4f8 | 4 | #include "mbed.h" |
narshu | 0:b8be0a00c4f8 | 5 | #include <queue> |
narshu | 0:b8be0a00c4f8 | 6 | |
narshu | 0:b8be0a00c4f8 | 7 | enum rfmode_t{RX, TX}; |
narshu | 0:b8be0a00c4f8 | 8 | |
narshu | 0:b8be0a00c4f8 | 9 | class RF12B { |
narshu | 0:b8be0a00c4f8 | 10 | public: |
narshu | 0:b8be0a00c4f8 | 11 | /* Constructor */ |
narshu | 0:b8be0a00c4f8 | 12 | RF12B(PinName SDI, |
narshu | 0:b8be0a00c4f8 | 13 | PinName SDO, |
narshu | 0:b8be0a00c4f8 | 14 | PinName SCK, |
narshu | 0:b8be0a00c4f8 | 15 | PinName NCS, |
narshu | 0:b8be0a00c4f8 | 16 | PinName NIRQ, |
narshu | 0:b8be0a00c4f8 | 17 | PinName TRIG); |
narshu | 0:b8be0a00c4f8 | 18 | |
narshu | 0:b8be0a00c4f8 | 19 | |
narshu | 0:b8be0a00c4f8 | 20 | |
narshu | 0:b8be0a00c4f8 | 21 | /* Reads a packet of data. Returns false if read failed. Use available() to check how much space to allocate for buffer */ |
narshu | 0:b8be0a00c4f8 | 22 | bool read(unsigned char* data, unsigned int size); |
narshu | 0:b8be0a00c4f8 | 23 | |
narshu | 0:b8be0a00c4f8 | 24 | /* Reads a byte of data from the receive buffer |
narshu | 0:b8be0a00c4f8 | 25 | Returns 0xFF if there is no data */ |
narshu | 0:b8be0a00c4f8 | 26 | unsigned char read(); |
narshu | 0:b8be0a00c4f8 | 27 | |
narshu | 0:b8be0a00c4f8 | 28 | /* Transmits a packet of data */ |
narshu | 0:b8be0a00c4f8 | 29 | void write(unsigned char* data, unsigned char length); |
narshu | 0:b8be0a00c4f8 | 30 | void write(unsigned char data); /* 1-byte packet */ |
narshu | 0:b8be0a00c4f8 | 31 | void write(queue<char> &data, int length = -1); /* sends a whole queue */ |
narshu | 0:b8be0a00c4f8 | 32 | |
narshu | 0:b8be0a00c4f8 | 33 | /* Returns the packet length if data is available in the receive buffer, 0 otherwise*/ |
narshu | 0:b8be0a00c4f8 | 34 | unsigned int available(); |
narshu | 0:b8be0a00c4f8 | 35 | |
narshu | 0:b8be0a00c4f8 | 36 | /* RF code setting */ |
narshu | 0:b8be0a00c4f8 | 37 | void setCode(unsigned char code); |
narshu | 0:b8be0a00c4f8 | 38 | |
narshu | 0:b8be0a00c4f8 | 39 | protected: |
narshu | 0:b8be0a00c4f8 | 40 | /* Receive FIFO buffer */ |
narshu | 0:b8be0a00c4f8 | 41 | queue<unsigned char> fifo; |
narshu | 0:b8be0a00c4f8 | 42 | |
narshu | 0:b8be0a00c4f8 | 43 | /* SPI module */ |
narshu | 0:b8be0a00c4f8 | 44 | SPI spi; |
narshu | 0:b8be0a00c4f8 | 45 | |
narshu | 0:b8be0a00c4f8 | 46 | /* Other digital pins */ |
narshu | 0:b8be0a00c4f8 | 47 | DigitalOut NCS; |
narshu | 0:b8be0a00c4f8 | 48 | InterruptIn NIRQ; |
narshu | 0:b8be0a00c4f8 | 49 | DigitalIn NIRQ_in; |
narshu | 0:b8be0a00c4f8 | 50 | DigitalOut rfled; |
narshu | 0:b8be0a00c4f8 | 51 | DigitalOut trigLED; |
narshu | 0:b8be0a00c4f8 | 52 | DigitalOut TRIG; |
narshu | 0:b8be0a00c4f8 | 53 | |
narshu | 0:b8be0a00c4f8 | 54 | rfmode_t mode; |
narshu | 0:b8be0a00c4f8 | 55 | |
narshu | 0:b8be0a00c4f8 | 56 | /* Initialises the RF12B module */ |
narshu | 0:b8be0a00c4f8 | 57 | void init(); |
narshu | 0:b8be0a00c4f8 | 58 | |
narshu | 0:b8be0a00c4f8 | 59 | /* Write a command to the RF Module */ |
narshu | 0:b8be0a00c4f8 | 60 | unsigned int writeCmd(unsigned int cmd); |
narshu | 0:b8be0a00c4f8 | 61 | |
narshu | 0:b8be0a00c4f8 | 62 | /* Sends a byte of data across RF */ |
narshu | 0:b8be0a00c4f8 | 63 | void send(unsigned char data); |
narshu | 0:b8be0a00c4f8 | 64 | |
narshu | 0:b8be0a00c4f8 | 65 | /* Switch module between receive and transmit modes */ |
narshu | 0:b8be0a00c4f8 | 66 | void changeMode(rfmode_t mode); |
narshu | 0:b8be0a00c4f8 | 67 | |
narshu | 0:b8be0a00c4f8 | 68 | /* Interrupt routine for data reception */ |
narshu | 0:b8be0a00c4f8 | 69 | void rxISR(); |
narshu | 0:b8be0a00c4f8 | 70 | |
narshu | 0:b8be0a00c4f8 | 71 | /* Tell the RF Module this packet is received and wait for the next */ |
narshu | 0:b8be0a00c4f8 | 72 | void resetRX(); |
narshu | 0:b8be0a00c4f8 | 73 | |
narshu | 0:b8be0a00c4f8 | 74 | /* Return the RF Module Status word */ |
narshu | 0:b8be0a00c4f8 | 75 | unsigned int status(); |
narshu | 0:b8be0a00c4f8 | 76 | |
narshu | 0:b8be0a00c4f8 | 77 | /* Calculate CRC8 */ |
narshu | 0:b8be0a00c4f8 | 78 | unsigned char crc8(unsigned char crc, unsigned char data); |
narshu | 0:b8be0a00c4f8 | 79 | |
narshu | 0:b8be0a00c4f8 | 80 | |
narshu | 0:b8be0a00c4f8 | 81 | unsigned char rfCode; |
narshu | 0:b8be0a00c4f8 | 82 | }; |
narshu | 0:b8be0a00c4f8 | 83 | |
narshu | 0:b8be0a00c4f8 | 84 | #endif /* _RF12B_H */ |