AKM Development Platform. This is the D7.014 version.

Dependencies:   AK09970 AK099XX AK7401 AK7451 AK8963X AK9750 AK9752 AkmSensor BLE_API I2CNano MCP342x SerialNano SpiNano TCA9554A mbed nRF51822

Fork of AKDP by Masahiko Fukasawa

main.cpp

Committer:
masahikofukasawa
Date:
2016-04-28
Revision:
0:c240899240e7
Child:
1:0914af311974

File content as of revision 0:c240899240e7:

#include <stdio.h>
#include <stdlib.h>
#include "mbed.h"
#include "ble/BLE.h"
#include "ble/services/UARTService.h"
#include "SerialNano.h"
#include "akmsensormanager.h"
#include "debug.h"

#define BLE_UUID_TXRX_SERVICE            0x0000 /**< The UUID of the Nordic UART Service. */
#define BLE_UUID_TX_CHARACTERISTIC       0x0002 /**< The UUID of the TX Characteristic. */
#define BLE_UUIDS_RX_CHARACTERISTIC      0x0003 /**< The UUID of the RX Characteristic. */

//#define TXRX_LEN                        UARTService::BLE_UART_SERVICE_MAX_DATA_LEN+1
#define TXRX_LEN                        50  // Temp

#define CR                              '\r'
#define LF                              '\n'

BLE                 ble;
UARTService*        uartService;
SerialNano          serial(P0_28, P0_29);
AkmSensorManager*   manager;

static char commandStr[TXRX_LEN];
static uint16_t rx_len=0;

/*
void WrittenHandler(const GattWriteCallbackParams *Handler)
{   
    uint8_t buf[TXRX_LEN];
    uint16_t bytesRead;
    if (Handler->handle == uartService->getTXCharacteristicHandle()) 
    {
        ble.gattServer().read(uartService->getTXCharacteristicHandle(), buf, &bytesRead);
        int len = 0;
        for(int i=0; i<bytesRead; i++){
            commandStr[len++] = buf[i];
            if(i>=TXRX_LEN || buf[i]==LF )
            {
                manager->commandReceived(commandStr);
                return;
            }
        }
    }
}
*/

void WrittenHandler(const GattWriteCallbackParams *Handler)
{   
    uint8_t buf[TXRX_LEN];
    uint16_t bytesRead;

    if (Handler->handle == uartService->getTXCharacteristicHandle()) 
    {
        ble.gattServer().read(uartService->getTXCharacteristicHandle(), buf, &bytesRead);
        char command[TXRX_LEN];
        int len = 0;
        for(int i=0; i<bytesRead; i++){
            if(i>=TXRX_LEN || buf[i]==CR )
            {
                break;
            }
            else
            {
                if(buf[i] != LF)
                {
                    command[len++] = (char)buf[i];
                }
            }
        }
        manager->commandReceived(command);
    }
}


/*
void usbUartCallback(void)
{   
    if(serial.readable())    
    {
        uint8_t c = serial.getc();
        if(rx_len>=TXRX_LEN || c == CR )
        {
            manager->commandReceived(commandStr);
            rx_len = 0;
        }
        else{
            if(c != LF)
            {
                commandStr[rx_len++] = c;
            }            
        }
    }
}
*/
void usbUartCallback(void)
{   
    if(serial.readable())    
    {
        uint8_t c = serial.getc();

        // ignore CR
        if(c==CR) return;
        
        commandStr[rx_len++] = c;
        if(rx_len>=TXRX_LEN || c == LF)
        {
            manager->commandReceived(commandStr);
            rx_len = 0;
        }
    }
}

void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
{
    manager->setEventConnected();
    MSG("#Connected\r\n");
}

void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{
    manager->setEventDisconnected();
    MSG("#Disconnected \r\n");
    ble.gap().startAdvertising();
} 

int main(void)
{   
    // USB serial
    serial.baud(115200);
    
#ifdef DEBUG    
    Debug::setSerial(&serial);
    MSG("#Debug Mode.\n");
#endif
    
    ble.init();
    // setup advertising 
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
                                    (const uint8_t *)"AKDP Rev001", sizeof("AKDP Rev001") - 1);
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
                                    (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
    ble.gap().onConnection(connectionCallback);
    ble.gap().onDisconnection(disconnectionCallback);
    ble.gattServer().onDataWritten(WrittenHandler);  
    
    // 100ms; in multiples of 0.625ms. 
    ble.gap().setAdvertisingInterval(80);
    ble.gap().startAdvertising(); 

    // BLE UART service
    uartService = new UARTService(ble);
    
    // serial port RX event
    serial.attach(&usbUartCallback);
    
    // create sensor manager
    manager = new AkmSensorManager(&serial, uartService);
    if( manager->init() == AkmSensorManager::ERROR){
        MSG("#Error: sensor is NULL\n");
    }
    
    // main loop
    while(1)
    {
        if(manager->isEvent()){
            manager->processEvent();
        }else{
            ble.waitForEvent();
        }
    }
}