Test program for the Nordic Semi nRF24L01 Transceiver Module (http://www.sparkfun.com/products/691), talking to another module connected to SparkFun\'s Nordic Serial Interface Board (http://www.sparkfun.com/products/9019).

Dependencies:   mbed nRF24L01P

main.cpp

Committer:
jeroenkoster
Date:
2018-12-12
Revision:
2:766f2f117453
Parent:
1:5be2682710c6
Child:
3:37c053b30ca0

File content as of revision 2:766f2f117453:

#include "mbed.h"
#include "nRF24L01P.h"

Serial pc(USBTX, USBRX); // tx, rx

SPI spi(D11, D12, D13);
DigitalOut cs(D10, 1);
nRF24L01P my_nrf24l01p(D11, D12, D13, D8, D9, D7);

int led = 0;
int newLed = 0;

void setLed(int i) {
    led = i;
    cs = 0;
    spi.write(0x46);
    spi.write(0x09);
    
    int mask = 1;
    int writeValue = 255;
    writeValue ^= mask << i;
    spi.write(writeValue);
    cs = 1;
}


int readButton() {
    cs = 0;
    spi.write(0x47);
    spi.write(0x09);
    int misoOutput = spi.write(0x00);
    cs = 1;
    
    misoOutput = misoOutput & 192;
    if (misoOutput == 128) { // S1 pressed
        return 1;    
    } else if (misoOutput == 64) { // S2 pressed
        return 5;
    }
    return 0;
}

int main() {

// The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's
//  "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019)
//  only handles 4 byte transfers in the ATMega code.
#define TRANSFER_SIZE   2

    cs = 0;
 
    spi.write(0x46); // Select IODIR
    spi.write(0x00); // 0000 = Set all to output
    spi.write(0x00);
  
    // Deselect the device
    cs = 1;

    char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
    
    bool ackknowledged = false;

    my_nrf24l01p.powerUp();

    // Display the (default) setup of the nRF24L01+ chip
    pc.printf( "nRF24L01+ Frequency    : %d MHz\r\n",  my_nrf24l01p.getRfFrequency() );
    pc.printf( "nRF24L01+ Output power : %d dBm\r\n",  my_nrf24l01p.getRfOutputPower() );
    pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
    pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
    pc.printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );

    pc.printf( "Type keys to test transfers:\r\n  (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE );

    my_nrf24l01p.setTransferSize( TRANSFER_SIZE );

    my_nrf24l01p.setReceiveMode();
    my_nrf24l01p.enable();

    setLed(led);
    while (1) {
        if (ackknowledged == false) {
            my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, TRANSFER_SIZE );
        }
        // If we've received anything in the nRF24L01+...
        if ( my_nrf24l01p.readable() ) {
            led = rxData[1];
            // ...read the data into the receive buffer
            my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) );

            if (rxData[0] == 0) { // Request
                setLed(led);
                pc.printf("Received request: %d \n\r",rxData[1]);
                txData[0] = 1;
                txData[1] = led;
                my_nrf24l01p.write( NRF24L01P_PIPE_P0
                , txData, TRANSFER_SIZE );
                
            } else if (rxData[0] == 1) { // Ack
                ackknowledged = true;
                pc.printf("Received Ack: %d \n\r",rxData[1]);
                setLed(led);
                
                // Set light
            } else {
                pc.printf("rxData[0] not recognized \n\r");
            }
        }
        newLed = (led + readButton()) % 6;
        while (newLed != led && !ackknowledged) {
            printf("Should request %d \n\r", newLed);
            txData[0] = 0;
            txData[1] = newLed;
            my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, TRANSFER_SIZE );
            wait(0.2);
        }
    }
}