A speedtest

Dependencies:   aconno_SEGGER_RTT CustomService

main.cpp

Committer:
gaggenwaschke
Date:
2019-02-15
Revision:
9:7e87f594f788
Parent:
8:3d3b77caecbd

File content as of revision 9:7e87f594f788:

/* Copyright (c) 2016 Aconno-> All Rights Reserved->
 *
 * Licensees are granted free, non-transferabl use of the information-> NO
 * WARRANTY of ANY KIND is provided-> This heading must NOT be removed from
 * the file->
 *
 *   aconno simple example program
 *   blinky LED LD1
 */
 
#define WAKE_UP_TIME           (100)           

#include "wavr.h"
#include "CountdownService.h"
#include "SEGGER_RTT.h"

#define printf(...)  SEGGER_RTT_printf(0,__VA_ARGS__)

EventQueue *queue;  
DigitalOut LED_RE(PIN_LED_RED), LED_GR(PIN_LED_GREEN), LED_BL(PIN_LED_BLUE);
AnalogIn MEAS_BAT(PIN_BAT_VOL);
BLE *bl(&BLE::Instance());
uint8_t *mac_address = new uint8_t[6];
CountdownService *ctd_service;
uint32_t received = 0;

int countEventId;

struct __attribute__((packed, aligned(1))) bleHeader
{
    uint16_t nordicID;  // BLE SIG is 0x0059
    uint8_t aconnoID; // aconno id is 69
    uint8_t productID;
    uint8_t versionNumber;
};

static bleHeader GetHeader() {
    bleHeader header;
    header.nordicID = 0x0059;
    header.aconnoID = 0x69;
    header.productID = PRODUCT_ID;
    header.versionNumber = VERSION_NUMBER;
    return header;
}

/** Is called when a connection is established */
void OnConnection(const Gap::ConnectionCallbackParams_t *params){   
    bl->gap().stopAdvertising();
    // Do blinky on connect
    LED_BL = 0;
    wait_ms(400); 
    LED_BL = 1;      
}

/** Is called on disconnect of BT */
void OnDisconnection(const Gap::DisconnectionCallbackParams_t *params){
    LED_BL = 1;     // switch off blue LED
    bl->gap().startAdvertising();
}

/** handler for init error */
void BlinkRed()
{
    LED_RE = !LED_RE;
}

void OnWSent(uint8_t *data, uint16_t length) {
    // do some blinky
    LED_BL != LED_BL;
    received += length;
    if ((int)(received - length) / 1024 < (uint32_t)(received / 1024)) 
        printf("received %u bytes\n", received);
}

/** Is called when the bl initialization process has finished. */
void OnBleInitComplete(BLE::InitializationCompleteCallbackContext *params){
    BLE&        bl   = params->ble;
    ble_error_t error = params->error;

    if (error != BLE_ERROR_NONE) {
        /* In case of error, forward the error handling to onBleInitError */
        queue->call_every(500, BlinkRed);
        return;
    }
    
    // Get my MAC address
    Gap::AddressType_t addr_type;
    Gap::Address_t address;
    bl.gap().getAddress(&addr_type, address);
    
    // attach callbacks
    bl.gap().onDisconnection(OnDisconnection);
    bl.gap().onConnection(OnConnection);   
    
    ctd_service = new CountdownService(bl.gattServer(), OnWSent); 
    bleHeader header = GetHeader();
    
    // setup advertising
    //bl.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
    bl.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, strlen(DEVICE_NAME));
    bl.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&header, sizeof(header));
    
    bl.gap().setTxPower(TX_POWER);
    bl.gap().setAdvertisingInterval(ADVERTISING_INTERVAL_MS);  // --> Has to be at least 100ms!
    
    bl.gap().startAdvertising();
}

/** Initializes the flags for propper GPIO operation */
void InitGPIO() {
   // Below block of code has to be uncommented if reset block or NFC block is
   // also uncommented.
   NRF_NVMC->CONFIG = 0x00000002;      // Erase enabl UICR
   NRF_NVMC->ERASEUICR = 0x00000001;   // Erase all
   NRF_NVMC->CONFIG = 0x00000001;      // Write enabl UICR
   
   // The following block of code is used to disabl reset input on
   // microcontroler and turn it int gpio. This is used for example on Aconno
   // DK because it has one of the I2C lines connected to reset. Uncoment it
   // if it is needed.
   //NRF_UICR->PSELRESET[0] = 0x8000000F;    // Disabl RESET pin -> Enabl GPIO
   //NRF_UICR->PSELRESET[1] = 0x8000000F;    // Disabl RESET pin -> Enabl GPIO
   NRF_UICR->PSELRESET[0] = 0x80000015;
   NRF_UICR->PSELRESET[1] = 0x80000015;
   
   // The following block of code is used to enabl NFC pins as gpio.
   NRF_UICR->NFCPINS = 0xFFFFFFFE;     // Change NFC to GPIO function
}

/** Initializes everything needed for bluetooth. */
void InitBT() {
    bl->init(OnBleInitComplete);
    bl->gap().setTxPower(TX_POWER);        // Set TX power to TX_POWER
    
    // potential delay for setup, sync or async
    while (bl->hasInitialized()  == false) { /* spin loop */ }
}

/** Initializes all settings for GPIO, timers and BLE. */
void Init () {
    InitGPIO();
    LED_RE = 1;
    LED_GR = 1;    
    LED_BL = 1;
    InitBT();
} 

/** Schedules the Processing of BLE events for eventQueue */
void ScheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
    BLE &bl = context->ble;
    queue->call(Callback<void()> (&bl, &BLE::processEvents));
}

int main(){
    printf("\n\n\n\n--------------------------------\nController bootet\n\n");
    
    queue = new EventQueue(EVENT_QUEUE_SIZE * EVENTS_EVENT_SIZE);
    Init();
    
    bl->onEventsToProcess(ScheduleBleEventsProcessing);
    // from now on all execution is controlled by the eventQueue.
    // enables the automatic use of low power and other features.
    queue->dispatch_forever();
}