uart rx receive data then send data from UARTSERVICE

Dependencies:   BLE_API mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "ble/BLE.h"
00019 
00020 #include "ble/services/UARTService.h"
00021 #include "Serial.h"
00022 
00023 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
00024                                * it will have an impact on code-size and power consumption. */
00025 
00026 #if NEED_CONSOLE_OUTPUT
00027 #define DEBUG(...) { printf(__VA_ARGS__); }
00028 #else
00029 #define DEBUG(...) /* nothing */
00030 #endif /* #if NEED_CONSOLE_OUTPUT */
00031 
00032 BLEDevice  ble;
00033 DigitalOut led1(LED1);
00034 
00035 Serial uart1(USBTX,USBRX);
00036 
00037 UARTService *uartServicePtr;
00038 
00039 uint8_t sFlag = 0;
00040 
00041 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00042 {
00043     DEBUG("Disconnected!\n\r");
00044     DEBUG("Restarting the advertising process\n\r");
00045     ble.startAdvertising();
00046 }
00047 
00048 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
00049    
00050     DEBUG("Connected!\n\r");
00051     
00052 }
00053 
00054 
00055 uint8_t b[40]={'a','d','q','w'};
00056 void onDataWritten1(const GattWriteCallbackParams *params)
00057 {
00058     
00059     
00060     if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
00061         uint16_t bytesRead = params->len;
00062         DEBUG("received %u bytes %s\n\r", bytesRead,params->data);
00063        // if(sFlag == 1)
00064        //     ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)b/*params->data*/, 4/*bytesRead*/);
00065     }
00066 }
00067 
00068 
00069 void periodicCallback(void)
00070 {
00071     led1 = !led1;
00072     
00073 }
00074 uint8_t c;
00075 void uartRx(void)
00076 {
00077     
00078     c = uart1.getc();
00079  
00080     if(sFlag < 39)
00081         b[sFlag++] = c;    
00082         
00083     if(c == '\r' || c == '\n' || sFlag == 15)
00084     {
00085         ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)b/*params->data*/, sFlag/*bytesRead*/);
00086         sFlag = 0;
00087     }
00088 }
00089 
00090 int main(void)
00091 {
00092     led1 = 1;
00093     uart1.baud(9600);
00094     Ticker ticker;
00095     ticker.attach(periodicCallback, 1);
00096     
00097     uart1.attach(uartRx,Serial::RxIrq);
00098 
00099     DEBUG("Initialising the nRF51822\n\r");
00100     ble.init();
00101     ble.onDisconnection(disconnectionCallback);
00102     ble.onConnection(connectionCallback);
00103     ble.onDataWritten(onDataWritten1);
00104 
00105     /* setup advertising */
00106     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00107     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00108     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00109                                      (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
00110     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00111                                      (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
00112 
00113     ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
00114     ble.startAdvertising();
00115 
00116     UARTService uartService(ble);
00117     uartServicePtr = &uartService;
00118 
00119     while (true) {
00120         ble.waitForEvent();
00121     }
00122 }