Connect the BLE_UART Console to a PC USB/Serial Console. Data written to PC console is sent over BLE UART Data received from BLE UART is displayed in PC console
Dependencies: BLE_API mbed nRF51822
Fork of BLE_LED_Controller by
Diff: main.cpp
- Revision:
- 16:3faefe15a4a4
- Parent:
- 15:016434184568
- Child:
- 17:4aa151182398
--- a/main.cpp Thu Dec 18 21:14:19 2014 +0000 +++ b/main.cpp Fri Feb 13 15:14:27 2015 +0000 @@ -19,24 +19,18 @@ #include "UARTService.h" -#define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; +#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; * it will have an impact on code-size and power consumption. */ #if NEED_CONSOLE_OUTPUT -#define DEBUG(...) { printf(__VA_ARGS__); } +#define DEBUG(...) { printf(__VA_ARGS__); } //Defaults to stdio without having to wirte pcUart explicitly #else #define DEBUG(...) /* nothing */ #endif /* #if NEED_CONSOLE_OUTPUT */ -#define LED2CMD "led2" -#define CMD_LENGTH 4 - - -//char rxPayload[CMD_SIZE]; +Serial pcUart(USBTX, USBRX); // tx, rx BLEDevice ble; // Create Bluetooth object -DigitalOut led1(LED1); // Set the pin attached to LED1 as an output -PwmOut led2(LED2); // Set the pin attached to LED2 as an output UARTService *uartServicePtr; @@ -54,31 +48,31 @@ uint16_t bytesRead = params->len; DEBUG("received %u bytes\n\r", bytesRead); DEBUG("Received string: '"); - DEBUG((const char *)params->data); //Note the size of data expands to the largest string received. Need to use bytesRead to resize. + for (int i=0;i<bytesRead; i++) { + DEBUG("%c",params->data[i]); + } + //DEBUG((const char *)params->data); //Note the size of data expands to the largest string received. Need to use bytesRead to resize. DEBUG("'\n\r"); - if (!strncmp(LED2CMD,(const char *)params->data,CMD_LENGTH-1)){ - float value; - char cmd[CMD_LENGTH]; - sscanf((const char *)params->data, "%s %f", cmd, &value ); - led2 = value; - DEBUG("Cmd: %s LED Level = %f\n\r", cmd, value); - } - ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data,bytesRead); // Echo received characters back over BLE + + // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data,bytesRead); // Echo received characters back over BLE } } /* Periodic Ticker callback */ -void periodicCallback(void) +void rxInterrupt(void) { - led1 = !led1; // Toggle LED 1 + if (pcUart.readable()) { + char s[2]; + s[1]=0; + s[0]=pcUart.getc(); + // DEBUG("-%c_",s[0]); + uartServicePtr->write(s,1); + } } int main(void) { - led1 = 1; - led2 = 0.5; - Ticker ticker; // Create period timer - ticker.attach(periodicCallback, 1); // Attach ticker callback function with a period of 1 second + pcUart.attach(&rxInterrupt,Serial::RxIrq); DEBUG("Initialising the nRF51822\n\r"); ble.init();