to cai
Dependencies: BLE_API nRF51822
Fork of BLE_NODE_TEST by
Diff: main.cpp
- Revision:
- 9:05f0b5a3a70a
- Parent:
- 6:e0fc9072e853
- Child:
- 10:808a025c9b4f
- Child:
- 11:22480ac31879
--- a/main.cpp Tue Sep 30 02:20:59 2014 +0000 +++ b/main.cpp Wed Oct 29 06:23:47 2014 +0000 @@ -16,27 +16,47 @@ #include "mbed.h" #include "BLEDevice.h" - +#include "DFUService.h" #include "UARTService.h" +#include "nrf_delay.h" +#include "battery.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 DEBUG 0 + -#if NEED_CONSOLE_OUTPUT -#define DEBUG(...) { printf(__VA_ARGS__); } -#else -#define DEBUG(...) /* nothing */ -#endif /* #if NEED_CONSOLE_OUTPUT */ +#define LOG(...) +#define BUTTON_DOWN 0 +#define LED_ON 0 +#define LED_OFF 1 + +DigitalOut blue(p18); +DigitalOut green(p17); +InterruptIn button(p30); +Battery battery(p5); BLEDevice ble; -DigitalOut led1(LED1); +UARTService *uartServicePtr; +Ticker ticker; + +BusOut outputGrove(p3, p4); +BusIn inputGrove(p1, p2); +BusIn charge(p6, p7); + +volatile bool button_event = false; -UARTService *uartServicePtr; +static const uint8_t SIZEOF_TX_RX_BUFFER = 32; +uint8_t rxPayload[SIZEOF_TX_RX_BUFFER] = {0,}; + +extern "C" void power_on(); +extern "C" void power_off(); + + +int button_detect(); void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) { - DEBUG("Disconnected!\n\r"); - DEBUG("Restarting the advertising process\n\r"); + LOG("Disconnected!\n"); + LOG("Restarting the advertising process\n"); ble.startAdvertising(); } @@ -44,23 +64,59 @@ { if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) { uint16_t bytesRead = params->len; - DEBUG("received %u bytes\n\r", bytesRead); - ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); + LOG("received %u bytes\n\r", bytesRead); + if (bytesRead < sizeof(rxPayload)) { + memcpy(rxPayload, params->data, bytesRead); + rxPayload[bytesRead] = '\0'; + } + + LOG("%s\n", (char *)rxPayload); + } } -void periodicCallback(void) +void tick(void) { - led1 = !led1; + green = !green; + + static uint8_t output = 0x01; + uint8_t input; + + outputGrove = output; + + button_event = true; + uartServicePtr->printf("battery:%3.2fV\n", battery.read()); + + + input = inputGrove; + uartServicePtr->printf("o->i:%d->%d\n", output, input); + + output = 3 - output; + + uint8_t chargeStatus = charge; + uartServicePtr->printf("charge:%d\n", chargeStatus); +} + +void button_down(void) +{ + button_event = true; } int main(void) { - led1 = 1; - Ticker ticker; - ticker.attach(periodicCallback, 1); + power_on(); + blue = LED_ON; + green = LED_ON; + +#if BUTTON_DOWN + button.mode(PullDown); + button.rise(button_down); +#else + button.mode(PullUp); + button.fall(button_down); +#endif - DEBUG("Initialising the nRF51822\n\r"); + LOG("Initialising the nRF51822\n"); ble.init(); ble.onDisconnection(disconnectionCallback); ble.onDataWritten(onDataWritten); @@ -69,17 +125,98 @@ ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, - (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); + (const uint8_t *)"NODE TEST", sizeof("NODE TEST")); ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); - ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ - ble.startAdvertising(); + //DFUService dfu(ble); UARTService uartService(ble); + uartService.retargetStdout(); uartServicePtr = &uartService; + + ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ + ble.startAdvertising(); + + blue = LED_OFF; + + ticker.attach(tick, 3); while (true) { - ble.waitForEvent(); + if (button_event) { + int click; + + blue = LED_ON; + click = button_detect(); + blue = LED_OFF; + LOG("click type: %d\n\r", click); + + button_event = false; + + if (1 == click) { + blue = !blue; + } else if (2 == click) { + //green = LED_ON; + } else if (-1 == click) { + ticker.detach(); + green = LED_OFF; + blue = LED_OFF; + while (BUTTON_DOWN == button.read()) { + + } + nrf_delay_us(3000); + + power_off(); + } else { + continue; + } + + } else { + ble.waitForEvent(); + } } } + +int button_detect(void) +{ + int t = 0; + + while (1) { + if (button.read() != BUTTON_DOWN) { + if (t < 30) { + return 0; // for anti shake + } else { + break; + } + } + + if (t > 30000) { // More than 3 seconds + return -1; // long click + } + + t++; + nrf_delay_us(100); + } + + if (t > 4000) { // More than 0.4 seconds + return 1; // single click + } + + while (true) { + if (button.read() == BUTTON_DOWN) { + nrf_delay_us(1000); + if (button.read() == BUTTON_DOWN) { + return 2; // double click + } + + t += 10; + } + + if (t > 4000) { + return 1; // The interval of double click should less than 0.4 seconds, so it's single click + } + + t++; + nrf_delay_us(100); + } +}