현성 김 / Mbed 2 deprecated 181202_Castone_design_master

Dependencies:   mbed nRF24L01P

main.cpp

Committer:
hyunsungkim
Date:
2018-12-01
Branch:
RF24_library_test_tx
Revision:
15:217b3e5a2275
Parent:
14:a4402559cd6e
Child:
16:7a78838b3b8a

File content as of revision 15:217b3e5a2275:

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

#define nrf_CE      D2
#define nrf_CSN     A3
#define spi_SCK     D13
#define spi_MOSI    D11
#define spi_MISO    D12
#define spi_IRQ     D4

#define TRANSFER_SIZE 15

nRF24L01P nrf(spi_MOSI, spi_MISO, spi_SCK, nrf_CSN, nrf_CE, spi_IRQ);    // mosi, miso, sck, csn, ce, irq

Serial pc(USBTX, USBRX);
PwmOut buzzer(D5);

void beepStart();
void endBeep();
void initNRF();
void dumpRFInfo();
void getPayload(int id, unsigned int count, int lspeed, int rspeed, char* txData);


int main() {
    int inByteCnt = 0;
    char buf[TRANSFER_SIZE+3];
    
    beepStart();
    pc.baud(115200);
    printf("I'm Master\r\n");
    initNRF();
    dumpRFInfo();
    
    while(1) {
        while(pc.readable()) {
            char inByte = pc.getc();
            if(inByteCnt > TRANSFER_SIZE+2) {
                printf("Data size overflow!\r\n");
                inByteCnt = 0;
                break;
            } else if(inByte == '\0') {
                buf[inByteCnt] = '\0';
                printf("Command: %s\r\n", buf);
                nrf.write(NRF24L01P_PIPE_P0, buf, TRANSFER_SIZE);
                inByteCnt=0;
            } else {
                buf[inByteCnt++] = inByte;
            }
        }
    }
}

void dumpRFInfo()
{
    printf( "nRF24L01+ Frequency    : %d MHz\r\n",  nrf.getRfFrequency() );
    printf( "nRF24L01+ Output power : %d dBm\r\n",  nrf.getRfOutputPower() );
    printf( "nRF24L01+ Data Rate    : %d kbps\r\n", nrf.getAirDataRate() );
    printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", nrf.getTxAddress() );
    printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", nrf.getRxAddress() );
}

void initNRF()
{
    nrf.setTxAddress(0xDEADBEEF0F);
    nrf.powerUp();
    nrf.setTransferSize( TRANSFER_SIZE );
    nrf.setTransmitMode();
    nrf.enable();

}

void getPayload(int id, unsigned int count, int lspeed, int rspeed, char* txData)
{
    *(txData+0) = '$';
    *(txData+1) = id/10+'0';
    *(txData+2) = id%10+'0';
    *(txData+3) = count/10000+'0';
    *(txData+4) = count/1000%10+'0';
    *(txData+5) = count/100%10+'0';
    *(txData+6) = count/10%10+'0';
    *(txData+7) = count%10+'0';
    *(txData+8) = lspeed>0?'+':'-';
    *(txData+9) = abs(lspeed)/10+'0';
    *(txData+10) = abs(lspeed)%10+'0';
    *(txData+11) = rspeed>0?'+':'-';
    *(txData+12) = abs(rspeed)/10+'0';
    *(txData+13) = abs(rspeed)%10+'0';
    *(txData+14) = '\0';
}

/*
xxyyyyyabbcddn

14 chars

xx: robot_id
yyyyy: packet_id
a: sign of lspeed
bb: lspeed
c: sign of rspeed
dd: rspeed
n: NULL 0

0100001+30+30
*/