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-06-11
Revision:
10:a710e8c3311c
Parent:
9:90a7650db802
Child:
11:53e52f5f1051

File content as of revision 10:a710e8c3311c:

#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"
#include "tca9554a.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 BLE_BUF_LEN                     UARTService::BLE_UART_SERVICE_MAX_DATA_LEN+1
#define TXRX_LEN                        50

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

BLE                 ble;
UARTService*        uartService;
#ifndef REV_D
SerialNano          serial(P0_28, P0_29); // Rev.C pin configuration
#else
SerialNano          serial(P0_4, P0_5);  // Rev.D pin configuration
#endif

AkmSensorManager*   manager;

void WrittenHandler(const GattWriteCallbackParams *Handler)
{   
    static char command[TXRX_LEN]="";
    static uint16_t len=0;

    uint8_t buf[BLE_BUF_LEN];
    uint16_t bytesRead;
    
    if (Handler->handle == uartService->getTXCharacteristicHandle()) 
    {
        ble.gattServer().read(uartService->getTXCharacteristicHandle(), buf, &bytesRead);

        for(uint16_t i=0; i<bytesRead; i++){
            if(buf[i] == CR)
            {
                ;   // ignore CR
            }
            else if(buf[i] == LF || len > TXRX_LEN)
            {
                manager->commandReceived(command);
                for(int j=0; j<TXRX_LEN; j++){
                    command[j] = 0;
                }
                len = 0;
            }
            else
            {
                command[len++] = (char)buf[i];
            }
        }
    }
}

void usbUartCallback(void)
{   
    static char command[TXRX_LEN] = "";
    static uint16_t len=0;

    if(serial.readable())    
    {
        uint8_t c = serial.getc();

        // ignore CR
        if(c==CR) return;
        
        command[len++] = c;
        if(len>=TXRX_LEN || c == LF)
        {
            manager->commandReceived(command);
            for(int j=0; j<TXRX_LEN; j++){
                command[j] = 0;
            }
            len = 0;
        }
    }
}

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

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

int main(void)
{   
    // USB serial
    serial.baud(115200);
            
    // serial port RX event
    serial.attach(&usbUartCallback);

#ifdef DEBUG    
    Debug::setSerial(&serial);
    MSG("#Debug Mode.\n");
#endif

#ifdef REV_D
    /* Rev.D */
    {
        MSG("#I2C GPIO Expander.\n");
        const int TIME_FOR_OE_MS = 100;
        const TCA9554A::Port PORT_OE_LVS1 = TCA9554A::PORT_7;
        const TCA9554A::Port PORT_OE_LVS2 = TCA9554A::PORT_6;
        I2C i2c(I2C_SDA, I2C_SCL);
        TCA9554A tca9554a(&i2c, TCA9554A::SLAVE_ADDRESS_38H);
        // Initializes TCA9554A (I2C GPIO Expander)
        tca9554a.configurePort(PORT_OE_LVS1, TCA9554A::DIR_OUTPUT);
        tca9554a.configurePort(PORT_OE_LVS2, TCA9554A::DIR_OUTPUT);
        // Makes sure that the OE is low first.
//        tca9554a.setPortLevel(PORT_OE_LVS1, TCA9554A::LOW);
//        tca9554a.setPortLevel(PORT_OE_LVS2, TCA9554A::LOW);
        wait_ms(TIME_FOR_OE_MS);
        // Sets the OE pins high.
        tca9554a.setPortLevel(PORT_OE_LVS1, TCA9554A::HIGH);
        tca9554a.setPortLevel(PORT_OE_LVS2, TCA9554A::HIGH);
        MSG("#Start main loop.\n");
//        manager->releaseTWI();
    }
#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 RevD 001", sizeof("AKDP RevD 001") - 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);

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