현성 김 / Mbed 2 deprecated 181202_Castone_design_master

Dependencies:   mbed nRF24L01P

main.cpp

Committer:
hyunsungkim
Date:
2018-11-03
Revision:
2:4d2d1b988c14
Parent:
1:450bd5ee8cdc
Child:
3:690740ab3394

File content as of revision 2:4d2d1b988c14:

#include "mbed.h"
#include "RF24.h"
#include "tone.h"

Serial pc(USBTX, USBRX);
Serial lidar(D1, D0);
PwmOut motor_RA(D9);
PwmOut motor_RB(D10);
PwmOut motor_LA(D3);
PwmOut motor_LB(D6);
PwmOut led_B(A1);
PwmOut led_G(A2);
PwmOut led_R(A5);
PwmOut buzzer(D5);
RF24 nrf(D11,D12,D13,D2,A3);
AnalogIn batteryCheck(A0);

void startBeep();
void endBeep();
void goForward();

uint8_t id = 0;   // Listener
//uint8_t rxAddr[10][6] = {"BOT0R","BOT1R","BOT2R","BOT3R","BOT4R","BOT5R","BOT6R","BOT7R","BOT8R","BOT9R"};
//uint8_t txAddr[10][6] = {"BOT0T","BOT1T","BOT2T","BOT3T","BOT4T","BOT5T","BOT6T","BOT7T","BOT8T","BOT9T"};

uint8_t txAddr[6] = "BTOA0";
uint8_t rxAddr[6] = "ATOB0";

int main() {
    unsigned char outByte='Q';
    
    led_R=1;
    led_G=1;
    led_B=1;
    startBeep();
    pc.baud(115200);
    
    nrf.begin();
    nrf.stopListening();
    nrf.openWritingPipe(txAddr);
    nrf.openReadingPipe(1,rxAddr);
    nrf.setChannel(101);
    
    // Display the (default) setup of the nRF24L01+ chip
    pc.printf( "My ID : %d", id);
    pc.printf( "nRF24L01+ Frequency    : %d MHz\r\n",  2000+nrf.getChannel() );
    pc.printf( "nRF24L01+ Output power : %d dBm\r\n",  nrf.getPALevel() );
    pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", nrf.getDataRate() );
    pc.printf( "nRF24L01+ TX Address   : 0x%X%X%X%X%X\r\n", rxAddr[0], rxAddr[1], rxAddr[2], rxAddr[3], rxAddr[4]);
    pc.printf( "nRF24L01+ RX Address   : 0x%X%X%X%X%X\r\n", txAddr[0], txAddr[1], txAddr[2], txAddr[3], txAddr[4]);
    pc.printf( "nRF24L01+ CRCWidth   : %d Byte\r\n", nrf.getCRCLength() );
    
    //goForward();
    
    nrf.startListening();
    
    while(1) {
        wait(0.3f);
        pc.printf("Transmitted\r\n");
        nrf.write(&outByte, 1);
    }
}

void startBeep()
{
    buzzer.write(0.5);
    buzzer.period_us(1000000/Do5);
    wait(0.1);
    buzzer.period_us(1000000/Mi5);
    wait(0.1);
    buzzer.period_us(1000000/So5);
    wait(0.1);
    buzzer.write(0);
}

void endBeep()
{
    buzzer.write(0.5);
    buzzer.period_us(1000000/So5);
    wait(0.1);
    buzzer.period_us(1000000/Mi5);
    wait(0.1);
    buzzer.period_us(1000000/Do5);
    wait(0.1);
    buzzer.write(0);
}

void goForward()
{
    motor_RA = 0.5;
    motor_RB = 0;
    motor_LA = 0;
    motor_LB = 0.5;
}

void onLED(int r, int g, int b)
{
    
}


/*

// 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   4

    wait(2.0);
 
    char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
    int txDataCnt = 0;
    int rxDataCnt = 0;
 
    nrf.powerUp();
    nrf.setTxAddress(0xE7E7E7EF, 4);
 
    // Display the (default) setup of the nRF24L01+ chip
    pc.printf( "nRF24L01+ Frequency    : %d MHz\r\n",  nrf.getRfFrequency() );
    pc.printf( "nRF24L01+ Output power : %d dBm\r\n",  nrf.getRfOutputPower() );
    pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", nrf.getAirDataRate() );
    pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", nrf.getTxAddress() );
    pc.printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", nrf.getRxAddress() );
    pc.printf( "nRF24L01+ CRCWidth   : %d \r\n", nrf.getCrcWidth() );
 
    pc.printf( "Type keys to test transfers:\r\n  (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE );
 
    nrf.setTransferSize( TRANSFER_SIZE );
 
    nrf.setReceiveMode();
    nrf.enable();
 
    while (1) {
 
        // If we've received anything over the host serial link...
        if ( pc.readable() ) {
 
            // ...add it to the transmit buffer
            txData[txDataCnt++] = pc.getc();
 
            // If the transmit buffer is full
            if ( txDataCnt >= sizeof( txData ) ) {
 
                // Send the transmitbuffer via the nRF24L01+
                nrf.write( NRF24L01P_PIPE_P0, txData, txDataCnt );
 
                txDataCnt = 0;
            }
            endBeep();
        }
 
        // If we've received anything in the nRF24L01+...
        if ( nrf.readable() ) {
 
            // ...read the data into the receive buffer
            rxDataCnt = nrf.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) );
 
            // Display the receive buffer contents via the host serial link
            for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) {
                pc.putc( rxData[i] );
            }
            startBeep();
        }
    }
    */