football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

main.cpp

Committer:
AntonLS
Date:
2015-04-16
Revision:
4:17b8edf264c3
Parent:
3:388e441be8df
Child:
5:1b9734e68327

File content as of revision 4:17b8edf264c3:

/*
 * TA test
 *
 */

/* mbed Microcontroller Library
 * Copyright (c) 2006-2013 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mbed.h"
#include "BLEDevice.h"

#include "DFUService.h"
#include "UARTService.h"
#include "DeviceInformationService.h"

#include "MTSSerialFlowControl.h"

#define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
                               * it will have an impact on code-size and power consumption. */

#define LOOPBACK_MODE         // Loopback mode

#if NEED_CONSOLE_OUTPUT
#define DEBUG(...) { printf(__VA_ARGS__); }
#else
#define DEBUG(...) /* nothing */
#endif /* #if NEED_CONSOLE_OUTPUT */

BLEDevice  ble;

#define TXRX_BUF_LEN  20

extern "C"
{
//    serial_t _my_serial;
    void pin_mode( PinName, PinMode );
}

// Note:  From the datasheet:
//  PSELRXD, PSELRTS, PSELTRTS and PSELTXD must only be configured when the UART is disabled.
// But serial_init() erroneously enables the uart before the setting of those,
//  which messes up flow control.  Apparently the setting is ONCE per ON mode.  ARGH!
//  So we will make our own versions of Serial and SerialBase to (MySerial and MySerialBase)
//  to not use serial_init() in serial_api.c, so flow control is setup correctly.
//  MTSSerial now uses our MySerial instead of Serial.
//  Since MTSSerial now uses hardware flow control by default, MTSSerialFlowControl isn't needed
//  unless the hardware doesn't have hardware flow control capability or sw is always fast enough.
//
// mts::MTSSerialFlowControl pcfc( USBTX, USBRX, RTS_PIN_NUMBER, CTS_PIN_NUMBER, 1280, 1280 );
mts::MTSSerial pcfc( USBTX, USBRX, 1280, 1280 );

uint8_t txPayload[TXRX_BUF_LEN] = { 0 };

static uint8_t rx_buf[TXRX_BUF_LEN];
static uint8_t rx_len = 0;


DigitalOut led1( LED1 );
DigitalOut rts( RTS_PIN_NUMBER );


const char *deviceName = "TAF00";

const uint8_t DevInfoServiceUUID_rev[] =
{ 
    (uint8_t)(GattService::UUID_DEVICE_INFORMATION_SERVICE & 0xFF), (uint8_t)(GattService::UUID_DEVICE_INFORMATION_SERVICE >> 8)
};

UARTService *uartServicePtr;

void disconnectionCallback( Gap::Handle_t handle, Gap::DisconnectionReason_t reason )
{
    DEBUG( "Disconnected!\n\r" );
    DEBUG( "Restarting the advertising process\n\r" );
    ble.startAdvertising();
}

void onDataWritten( const GattCharacteristicWriteCBParams *params )
{
    if( (uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle()) )
    {
        uint8_t buf[TXRX_BUF_LEN];
        uint16_t bytesRead = params->len;

        DEBUG( "received %u bytes\n\r", bytesRead );

#ifdef LOOPBACK_MODE
        // Loopback data from Central.
        ble.updateCharacteristicValue( uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead );  // Notifies.
#endif

        // Also write to serial port...
        ble.readCharacteristicValue( uartServicePtr->getTXCharacteristicHandle(), buf, &bytesRead );
        memset( txPayload, 0, TXRX_BUF_LEN );
        memcpy( txPayload, buf, TXRX_BUF_LEN );
//        pcfc.printf( "From app: " );
        pcfc.write( (char *)txPayload, bytesRead );
//        pcfc.printf( "\r\n" );
    }
}

// bool notReady;

void onDataSent( unsigned count )
{
//    notReady = false;
}

void periodicCallback( void )
{
    led1 = !led1;
//    rts  = !rts;
}

void uartCB( void )
{
//    if( notReady )  return;

    if( 0 != rts.read() )  pcfc.puts( "\r\n!RTS disengaged.\r\n" );  // Can we read rts when in HW fc mode?


    // Set line from serial port to RX characteristic (From cone)
    while( pcfc.readable() )
    {
        char ch;
        pcfc.read( ch );
        rx_buf[rx_len++] = ch;
        if( rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\r' )
        {
//            notReady = true;

            ble.updateCharacteristicValue( uartServicePtr->getRXCharacteristicHandle(), rx_buf, rx_len );  // Notifies.
//            pcfc.printf( "RecHandler \r\n" );
            rx_len = 0;
            break;
        }
    }
}

int main( void )
{
    led1 = 1;
    Ticker ticker;
    ticker.attach( periodicCallback, 1 );


    pcfc.baud( 57600 );
    pcfc.rxClear();
    // pc.attach( uartCB, pc.RxIrq );
    pcfc.printf( "\r\nNano nano!\r\n" );
#ifdef LOOPBACK_MODE
    pcfc.printf( "\r\nIn BLE Loopback mode.\r\n" );
#endif


    DEBUG( "Initialising the nRF51822\n\r" );


    ble.init();
    ble.onDisconnection( disconnectionCallback );
    ble.onDataWritten( onDataWritten );
    ble.onDataSent( onDataSent );

    /* setup advertising */
    ble.accumulateAdvertisingPayload( GapAdvertisingData::BREDR_NOT_SUPPORTED );
    ble.setAdvertisingType( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED );
    ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME,
                                      (const uint8_t *)deviceName, strlen(deviceName) );
    ble.accumulateAdvertisingPayload( GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS,
                                      (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed) );

    ble.accumulateScanResponse( GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
                                      (const uint8_t *)DevInfoServiceUUID_rev, sizeof(DevInfoServiceUUID_rev) );

    ble.setAdvertisingInterval( Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS( 1000 ) );
    ble.startAdvertising();

    DeviceInformationService deviceInfo( ble, "TRX", "TrueAgility", "SN0001", "hw-rev1", "fw-rev1" );

    /* Enable over-the-air firmware updates. Instantiating DFUSservice introduces a
     * control characteristic which can be used to trigger the application to
     * handover control to a resident bootloader. */
    DFUService dfu( ble );

    UARTService uartService( ble );
    uartServicePtr = &uartService;

    while( true )
    {
        ble.waitForEvent();

        uartCB();
    }
}

/* EOF */