SmartMesh QSL for STM32F4 version

Fork of COG-AD4050_QSL by APS Lab

main.cpp

Committer:
APS_Lab
Date:
2018-07-12
Revision:
1:b909b8399252
Parent:
0:8ca1e814a851

File content as of revision 1:b909b8399252:

#include "mbed.h"
/*
Copyright (c) 2016, Dust Networks. All rights reserved.

SimplePublish example application for the mbed OS.

\license See attached DN_LICENSE.txt.
*/

#include "mbed.h"
#include "millis.h"
#include "dn_qsl_api.h"     // Only really need this include from QSL
#include "dn_debug.h"       // Included to borrow debug macros
#include "dn_endianness.h"  // Included to borrow array copying
#include "dn_time.h"        // Included to borrow sleep function
//#include "ADXL362.h"

#define NETID           0       // Factory default value used if zero (1229)
#define JOINKEY         NULL    // Factory default value used if NULL (44 55 53 54 4E 45 54 57 4F 52 4B 53 52 4F 43 4B)
#define BANDWIDTH_MS    500    // Not changed if zero (default base bandwidth given by manager is 9 s)
#define SRC_PORT        60000   // Default port used if zero (0xf0b8)
#define DEST_PORT       0       // Default port used if zero (0xf0b8)
#define DATA_PERIOD_MS  500    // Should be longer than (or equal to) bandwidth

// We can use debug macros from dn_debug, as stdio defaults to this serial
Serial serialDebug(USBTX, USBRX);
#define log_info serialDebug.printf
 
// LED2 is the green LED on the NUCLEO-L053R8; might change for other boards
DigitalOut myled(LED1);
DigitalOut status(LED2);
//ADXL362 Acc(P1_07, P1_08, P1_06, P1_10);

static uint16_t randomWalk(void);
static void parsePayload(const uint8_t *payload, uint8_t size);

//void init_acc(void)
//{    
//    Acc.init_spi();
//    Acc.init_adxl362();
//}
 
int main()
{
    uint8_t payload[4];
    uint8_t inboxBuf[DN_DEFAULT_PAYLOAD_SIZE_LIMIT];
    uint8_t bytesRead;
    uint8_t i,e;
    //int8_t x0, y0, z0;
    
    myled=1;
    status=1;     
    
    for(e=0;e<10;e++)
    {
        myled=!myled;
        status=!status;
        wait(0.5);
    }
    
    myled=1;
    status=1;     

    //init_acc();
    // Configure SysTick for timing
    millisStart();
    
    // Set PC debug serial baudrate
    serialDebug.baud(115200);
    serialDebug.printf("STM32F4 SmartMesh Demo\n");
    
    log_info("Initializing...");
    dn_qsl_init();
    
    // Flash LED to indicate start-up complete
    for (i = 0; i < 10; i++)
    {
        myled = !myled;
        dn_sleep_ms(50);
    }
    myled=0;

    while(TRUE) {
        if (dn_qsl_isConnected())
        {
            //Acc.ACC_GetXYZ8(&x0, &y0, &z0);
            //payload[0]=x0;
            //payload[1]=y0;
            //payload[2]=z0;
            uint16_t val = randomWalk();
            static uint8_t count = 0;
            myled = 1; // Turn off LED during send/read
            status = 0;
            dn_write_uint16_t(payload, val);
            payload[0] = count;
            
            if (dn_qsl_send(payload, sizeof (payload), DEST_PORT))
            {
                log_info("Sent message nr %u: %u", count, val);
                count++;
                status=1;
            } else
            {
                log_info("Send failed");
                status=0;
            }

            do
            {
                bytesRead = dn_qsl_read(inboxBuf);
                parsePayload(inboxBuf, bytesRead);
            } while (bytesRead > 0);
            
            myled = 1; // Turn on LED
            dn_sleep_ms(DATA_PERIOD_MS);
        } else
        {
            log_info("Connecting...");
            myled = 1; // Not connected; turn off LED
            if (dn_qsl_connect(NETID, JOINKEY, SRC_PORT, BANDWIDTH_MS))
            {
                myled = 0; // Connected; turn on LED
                status=1;//log_info("Connected to network");
            } else
            {
                log_info("Failed to connect");
            }
        }
    status=0;
    wait(0.5);
    status=1;
    }
}

static uint16_t randomWalk(void)
{
    static bool first = TRUE;
    static uint16_t lastValue = 0x7fff; // Start in middle of uint16 range
    const int powerLevel = 9001;
    
    // Seed random number generator on first call
    if (first)
    {
        first = FALSE;
        srand(dn_time_ms());
    }
    
    // Random walk within +/- powerLevel
    lastValue += rand() / (RAND_MAX / (2*powerLevel) + 1) - powerLevel;
    return lastValue;
}

static void parsePayload(const uint8_t *payload, uint8_t size)
{
    uint8_t i;
    char msg[size + 1];
    
    if (size == 0)
    {
        // Nothing to parse
        return;
    }
    
    // Parse bytes individually as well as together as a string
    //log_info("Received downstream payload of %u bytes:", size);
    for (i = 0; i < size; i++)
    {
        msg[i] = payload[i];
        log_info("\tByte# %03u: %#.2x (%u)", i, payload[i], payload[i]);
    }
    msg[size] = '\0';
    log_info("\tMessage: %s", msg);
}