Radio communication with NRF24

Dependents:   F030

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ssRadio.h Source File

ssRadio.h

00001 #ifndef __SSRADIO_H__
00002 #define __SSRADIO_H__
00003 
00004 //#define SERIAL_DEBUG 1
00005 
00006 #include "RF24.h"
00007 #include "NodeConfig.h"
00008 
00009 #define QUEUELEN 3
00010 
00011 typedef union _Packet {
00012   struct __attribute__((packed)) {
00013     uint16_t address;     // Source address
00014     uint8_t type;         // Type. MSB signals extended type (+1 byte). Not implemented yet.
00015     uint8_t counter;      // Packet counter
00016     uint8_t payload[28]; 
00017   } fields;
00018   uint8_t raw[32];
00019 } Packet;
00020 
00021 class SSRadio {
00022 
00023 protected:
00024     NodeConfig *config;
00025     RF24 rf24;
00026     
00027     void (*onReceiveData)(uint8_t *data, uint16_t type, uint8_t len);
00028     void (*onConnect)();
00029     void (*onDisconnect)();
00030 
00031     uint16_t txCounter;
00032     uint16_t rxCounter;
00033     
00034     uint8_t packetRx[32];
00035     long lastSync;  // millis when the last Sync was received
00036     int8_t sendSlot;    // Transmission slot, when synced
00037     int8_t freeSlot;    // Free (for everyone) slot, when synced
00038     uint8_t slot_ms;    // Slot size in ms
00039     
00040     void receiveSyncFrame(uint8_t *frame, uint8_t size);
00041     void receiveDataFrame(uint8_t *frame, uint8_t size);
00042     
00043     uint16_t myAddress;
00044     uint64_t gatewayAddress;
00045     
00046     uint8_t txQueue[QUEUELEN * 30]; // 1 length + 1 type + 28 payload
00047     uint8_t rxQueue[QUEUELEN * 30];
00048     uint8_t txQueueLength;
00049     uint8_t rxQueueLength;
00050     
00051 public:
00052     SSRadio(SPI *spi, NodeConfig *nc);
00053     
00054     void init();
00055     void loop();
00056     
00057     void useIRQ(uint16_t irqPin);
00058     
00059     bool isRunning();   // RF24 is connected or not
00060     bool isConnected(); // There is a GW nearby
00061     bool isScheduled(); // Sending is scheduled by the GW
00062     bool isAvailableData(); // Available data
00063     
00064     bool sendData(uint8_t *data, uint16_t type, uint8_t len); // Send data to GW
00065     bool instantData(uint8_t *data, uint16_t type, uint8_t len); // Send data to GW immediately
00066     bool receiveData(uint8_t *data, uint16_t &type, uint8_t &len); // Receive data from GW
00067     void setSlotSize(uint8_t slot_ms);
00068     
00069     // Callbacks
00070     void setOnReceiveData(void (*onReceiveData)(uint8_t *data, uint16_t type, uint8_t len));
00071     void setOnConnect(void (*onConnect)());
00072     void setOnDisconnect(void (*onDisconnect)());
00073 };
00074 
00075 #endif // __SSRADIO_H__