현성 김 / Mbed 2 deprecated 181202_Castone_design_master

Dependencies:   mbed nRF24L01P

main.cpp

Committer:
hyunsungkim
Date:
2018-11-15
Revision:
4:ae81aeeed069
Parent:
3:690740ab3394
Child:
5:ad66d64c3d33

File content as of revision 4:ae81aeeed069:

#include "mbed.h"
#include <RF24Network.h>
#include <RF24.h>
#include "beep.h"

/* Set */
#define NODE_ADDR 00

#define nrf_CE      D2
#define nrf_CSN     A3
#define spi_SCK     D13
#define spi_MOSI    D11
#define spi_MISO    D12
RF24 radio(spi_MOSI, spi_MISO, spi_SCK, nrf_CE, nrf_CSN );
RF24Network network(radio);

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(A5);
PwmOut led_G(A2);
PwmOut led_R(A1);
PwmOut buzzer(D5);
AnalogIn batteryCheck(A0);

const uint16_t addr = NODE_ADDR;
struct payload_t 
{
    unsigned long ms;
    unsigned long counter;
};

void beepStart();
void endBeep();
void goForward();

int main() {
    led_R=0.5;
    led_G=0.4;
    led_B=1;
    beepStart();
    pc.baud(115200);
    radio.begin();
    network.begin(/*channel*/ 90, /*node address*/ addr);
    wait(2.0f);
    
 //   goForward();
    
    // Display the (default) setup this node
    pc.printf("My ADDR : %d\r\n", NODE_ADDR);
    pc.printf("Complete Setting\r\n");
 //   beepAfterset();
    
    led_B.write(0.5);
    while(1) {
        // Network should be updated regularly to keep layer going
        network.update();
        
        while (network.available()) {
            RF24NetworkHeader header_rx;
            payload_t payload_rx;
            network.read(header_rx,&payload_rx,sizeof(payload_rx));
            pc.printf("Received packet # %d at %d ms\n",payload_rx.counter,payload_rx.ms);
        }
    }
}


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

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] );
            }
            beepStart();
        }
    }
    */