Uart serie example, loopback
Dependencies: mbed BLE_API nRF51822
Diff: main.cpp
- Revision:
- 0:5b60e4b36aea
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Jun 09 13:10:59 2020 +0000 @@ -0,0 +1,148 @@ + +#include "mbed.h" +#include "nrf51.h" +#include "nrf51_bitfields.h" + +#include "BLE.h" +#include "UARTService.h" + + +#define LOG(...) { pc.printf(__VA_ARGS__); } + +#define LED_GREEN p21 +#define LED_RED p22 +#define LED_BLUE p23 +#define BUTTON_PIN p17 +#define BATTERY_PIN p1 + +#define MPU6050_SDA p12 +#define MPU6050_SCL p13 + +#define UART_TX p9 +#define UART_RX p11 +#define UART_CTS p8 +#define UART_RTS p10 + + + +DigitalOut blue(LED_BLUE); +DigitalOut green(LED_GREEN); +DigitalOut red(LED_RED); + +InterruptIn button(BUTTON_PIN); +AnalogIn battery(BATTERY_PIN); +Serial pc(UART_TX, UART_RX); + +InterruptIn motion_probe(p14); + +int read_none_count = 0; + +BLEDevice ble; +UARTService *uartServicePtr; + +volatile bool bleIsConnected = false; +volatile uint8_t tick_event = 0; + + + +void check_i2c_bus(void); +unsigned short inv_orientation_matrix_to_scalar( const signed char *mtx); + + +void connectionCallback(const Gap::ConnectionCallbackParams_t *params) +{ + LOG("Connected!\n"); + bleIsConnected = true; +} + +void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *cbParams) +{ + LOG("Disconnected!\n"); + LOG("Restarting the advertising process\n"); + ble.startAdvertising(); + bleIsConnected = false; +} + +void tick(void) +{ + static uint32_t count = 0; + + LOG("%d\r\n", count++); + green = !green; +} +void detect(void) +{ + static uint32_t count = 0; + + LOG("%d\r\n", count++); + blue = !blue; +} +void flush_uart_ble(){ + //envia un \n para forzar el envio del buffer y vaciarlo, en hex 0x0a + char end='\n'; + uartServicePtr->write(&end,1); + } + +void onDataWrittenCallback(const GattWriteCallbackParams *params) {//ocurre cuando llega un dato por bluetooth + if (params->handle == uartServicePtr->getTXCharacteristicHandle()) { + + for (int16_t x=0;x<params->len;x++) + pc.printf("%02X ", params->data[x]); + uartServicePtr->write(params->data,params->len); + flush_uart_ble(); + //uartServicePtr->flush(); + + } +} + + +int main(void) +{ + blue = 1; + green = 1; + red = 1; + + pc.baud(115200); + + wait(1); + + LOG("---- Seeed Tiny BLE ----\n"); + + + Ticker ticker; + ticker.attach(tick, 3); + + button.fall(detect); + + LOG("Initialising the nRF51822\n"); + ble.init(); + ble.gap().onDisconnection(disconnectionCallback); + ble.gap().onConnection(connectionCallback); + ble.onDataWritten(onDataWrittenCallback); + + + /* setup advertising */ + ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); + ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); + ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, + (const uint8_t *)"smurfs", sizeof("smurfs")); + ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, + (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); + + UARTService uartService(ble); + uartServicePtr = &uartService; + //uartService.retargetStdout(); + + ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ + ble.gap().startAdvertising(); + + while (true) { + //hacer algo + ble.waitForEvent(); + } +} + + + + +