Test program for FF1705.

Dependencies:   libxDot-dev-mbed5-deprecated ISL29011

Example/src/peer_to_peer_example.cpp

Committer:
jernej_vrscaj
Date:
2017-11-09
Revision:
2:9b44950b84af
Parent:
1:12a289bd6b82

File content as of revision 2:9b44950b84af:

#include "dot_util.h"
#include "RadioEvent.h"
#include "mDotEvent.h"
#include "C12832.h"

#if ACTIVE_EXAMPLE == PEER_TO_PEER_EXAMPLE

#define TEST_board
//#define REFERENCE_board


/////////////////////////////////////////////////////////////////////////////
// -------------------- DOT LIBRARY REQUIRED ------------------------------//
// * Because these example programs can be used for both mDot and xDot     //
//     devices, the LoRa stack is not included. The libmDot library should //
//     be imported if building for mDot devices. The libxDot library       //
//     should be imported if building for xDot devices.                    //
// * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/    //
// * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/        //
// * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/    //
// * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/        //
/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////
// * these options must match between the two devices in   //
//   order for communication to be successful
/////////////////////////////////////////////////////////////

/*============================= INIT =========================================*/

static uint8_t network_address[] = { 0x01, 0x02, 0x03, 0x04 };
static uint8_t network_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 };
static uint8_t data_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 };

mDot* dot = NULL;
lora::ChannelPlan* plan = NULL;

Serial pc(USBTX, USBRX);

#ifdef TEST_board
C12832 lcd(PB_15, PB_13, PB_14, PA_12, PB_12);                                  // init LCD

DigitalIn joystick_up(PB_2);                                                    // init button to start the send routine
DigitalOut led(PB_8, 1);                                                        // init LED to indicate sending messages

DigitalIn BTN_1(PA_5);                                                          // init LoRa button
DigitalOut LED_1(PA_4, 0);                                                      // init LoRa LED


uint8_t trx = 0x2F;                                                             // initial sending value (0x2F dec 47)
uint8_t sent = 0; 
uint8_t sent_rx = 0; 

#elif defined(REFERENCE_board)
DigitalOut led(PA_4, 0);                                                        // init LED to indicate sending messages

uint8_t received = 0;                                                           // check for RadioEvent

uint16_t *ptr_rx;                                                               // init pointer to RxBuffer

#endif

/*============================ END INIT ======================================*/


/*============================== MAIN ========================================*/

int main()
{
    // Custom event handler for automatically displaying RX data
    RadioEvent events;
    uint32_t tx_frequency;
    uint8_t tx_datarate;
    uint8_t tx_power;
    uint8_t frequency_band;

    // sending data

    pc.baud(115200);

    mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);

#if CHANNEL_PLAN == CP_US915
    plan = new lora::ChannelPlan_US915();
#elif CHANNEL_PLAN == CP_AU915
    plan = new lora::ChannelPlan_AU915();
#elif CHANNEL_PLAN == CP_EU868
    plan = new lora::ChannelPlan_EU868();
#elif CHANNEL_PLAN == CP_KR920
    plan = new lora::ChannelPlan_KR920();
#elif CHANNEL_PLAN == CP_AS923
    plan = new lora::ChannelPlan_AS923();
#elif CHANNEL_PLAN == CP_AS923_JAPAN
    plan = new lora::ChannelPlan_AS923_Japan();
#elif CHANNEL_PLAN == CP_IN865
    plan = new lora::ChannelPlan_IN865();
#endif
    assert(plan);

    dot = mDot::getInstance(plan);
    assert(dot);

    logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);

    // start from a well-known state
    logInfo("defaulting Dot configuration");
    dot->resetConfig();

    // make sure library logging is turned on
    dot->setLogLevel(mts::MTSLog::INFO_LEVEL);

    // attach the custom events handler
    dot->setEvents(&events);

    // update configuration if necessary
    if (dot->getJoinMode() != mDot::PEER_TO_PEER) {
        logInfo("changing network join mode to PEER_TO_PEER");
        if (dot->setJoinMode(mDot::PEER_TO_PEER) != mDot::MDOT_OK) {
            logError("failed to set network join mode to PEER_TO_PEER");
        }
    }
    frequency_band = dot->getFrequencyBand();
    switch (frequency_band) {
        case lora::ChannelPlan::EU868_OLD:
        case lora::ChannelPlan::EU868:
            // 250kHz channels achieve higher throughput
            // DR_6 : SF7 @ 250kHz
            // DR_0 - DR_5 (125kHz channels) available but much slower
            tx_frequency = 869850000;
            tx_datarate = lora::DR_6;
            // the 869850000 frequency is 100% duty cycle if the total power is under 7 dBm - tx power 4 + antenna gain 3 = 7
            tx_power = 4;
            break;

        case lora::ChannelPlan::US915_OLD:
        case lora::ChannelPlan::US915:
        case lora::ChannelPlan::AU915_OLD:
        case lora::ChannelPlan::AU915:
            // 500kHz channels achieve highest throughput
            // DR_8 : SF12 @ 500kHz
            // DR_9 : SF11 @ 500kHz
            // DR_10 : SF10 @ 500kHz
            // DR_11 : SF9 @ 500kHz
            // DR_12 : SF8 @ 500kHz
            // DR_13 : SF7 @ 500kHz
            // DR_0 - DR_3 (125kHz channels) available but much slower
            tx_frequency = 915500000;
            tx_datarate = lora::DR_13;
            // 915 bands have no duty cycle restrictions, set tx power to max
            tx_power = 20;
            break;

        case lora::ChannelPlan::AS923:
        case lora::ChannelPlan::AS923_JAPAN:
            // 250kHz channels achieve higher throughput
            // DR_6 : SF7 @ 250kHz
            // DR_0 - DR_5 (125kHz channels) available but much slower
            tx_frequency = 924800000;
            tx_datarate = lora::DR_6;
            tx_power = 16;
            break;

        case lora::ChannelPlan::KR920:
            // DR_5 : SF7 @ 125kHz
            tx_frequency = 922700000;
            tx_datarate = lora::DR_5;
            tx_power = 14;
            break;

        default:
            while (true) {
                logFatal("no known channel plan in use - extra configuration is needed!");
                wait(5);
            }
            break;
    }
    // in PEER_TO_PEER mode there is no join request/response transaction
    // as long as both Dots are configured correctly, they should be able to communicate
    update_peer_to_peer_config(network_address, network_session_key, data_session_key, tx_frequency, tx_datarate, tx_power);

    // save changes to configuration
    logInfo("saving configuration");
    if (!dot->saveConfig()) {
        logError("failed to save configuration");
    }

    // display configuration
    display_config();

#ifdef TEST_board

    while (true) {

        lcd.locate(0,0);
        lcd.printf("Zacni test s pritiskom na   JOYSTICK tipko...");

                    // join network if not joined
        if (!dot->getNetworkJoinStatus()) {
            join_network();
        }
            
        while(!BTN_1) LED_1 = 1;                                                // press btn to light up led
        LED_1 = 0;                                                              // clear LED

        
        if(joystick_up) {                                                       // wait for user input
        
            sent = 1;

            lcd.cls();
            led = 0;

            std::vector<uint8_t> tx_data;

            trx++;                                                              // +1 to value to be send
            tx_data.push_back((trx >> 8) & 0xFF);                               // shift data eg. 0x0031 -> 0x3100
            tx_data.push_back(trx & 0xFF);
            logInfo("send_data: 0x%x", trx);
            
            lcd.locate(0,0);
            lcd.printf("Poslano: %u", trx);
            
            send_data(tx_data);                                                 // send data

            wait(1);
            
            if(!(sent && sent_rx)) 
            {
                lcd.locate(0,10);
                lcd.printf("...TIMEOUT - FAIL!"); 
                lcd.locate(0,20);
                lcd.printf("Preveri REFERENCNO platico."); 
            }          
            
            wait(4);     
            
            sent = 0;
            sent_rx = 0;                    

            lcd.cls();
            led = 1;

        }
    }
    
#elif defined(REFERENCE_board)

    while (true) {
        
        std::vector<uint8_t> tx_data;

            // join network if not joined
            if (!dot->getNetworkJoinStatus()) {
                join_network();
            }
        
        if(received)                                                            // RadioEvent happened
        {
            
        led = 1;
        
        received = 0;                                                           // clear event flag
        ++*ptr_rx;                                                              // +1 to received value
        tx_data.push_back((*ptr_rx >> 8) & 0xFF);                               // shift data eg. 0x0031 -> 0x3100
        tx_data.push_back(*ptr_rx & 0xFF);
        logInfo("Sent *ptr_rx: %#x", *ptr_rx);
        send_data(tx_data);                                                     // then send back
        
        wait(1);
        
        led = 0;        
        
        NVIC_SystemReset();                                                     // reset Reference board
        
        }
    }

#endif

    return 0;
}

#endif

/*============================ END MAIN =======================================*/