example firmware for nRF51-Dongle to use with Opentrigger
Dependencies: BLE_API mbed nRF51822
Fork of BLE_Button by
Diff: main.cpp
- Revision:
- 15:9f9f5d82743d
- Parent:
- 14:09f208b24460
- Child:
- 16:7a9fde930f8a
--- a/main.cpp Mon Jun 13 17:09:43 2016 +0000 +++ b/main.cpp Tue Jun 14 14:02:15 2016 +0000 @@ -1,5 +1,9 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2013 ARM Limited +/* nRF51 mbed opentrigger example + * Copyright (c) 2016 Florian Fida + * + * based on 'Demo for an Input Service' + * https://developer.mbed.org/teams/Bluetooth-Low-Energy/code/BLE_Button/ + * * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,10 +20,11 @@ #include "mbed.h" #include "ble/BLE.h" -#include "ButtonService.h" DigitalOut led1(LED1); DigitalOut led2(LED2); +DigitalOut led3(LED3); + InterruptIn button0(P0_15); InterruptIn button1(P0_16); InterruptIn button2(P0_17); @@ -28,8 +33,7 @@ InterruptIn button5(P0_20); const uint16_t MIN_ADVERT_INTERVAL = 50; /* msec */ -const static char DEVICE_NAME[] = "OT"; -static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID}; +const uint16_t MAX_ADVERT_INTERVAL = 5000; /* msec */ static uint8_t gpio_states[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; enum { @@ -39,8 +43,6 @@ }; static uint8_t buttonState = IDLE; -static ButtonService *buttonServicePtr; - void blinkLed(void){ led2 = !led2; } @@ -88,6 +90,8 @@ void onBleInitError(BLE &ble, ble_error_t bt_error) { /* Initialization error handling should go here */ + + printf("bt_error=%i\n\r",bt_error); error("Panic!"); } @@ -134,34 +138,27 @@ ble.gap().onDisconnection(disconnectionCallback); - /* Setup primary service */ - buttonServicePtr = new ButtonService(ble, false /* initial value for button pressed */); - /* setup advertising */ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); - ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); - ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); - uint16_t minInterval = ble.gap().getMinAdvertisingInterval(); - if(minInterval < MIN_ADVERT_INTERVAL) minInterval = MIN_ADVERT_INTERVAL; - ble.gap().setAdvertisingInterval(minInterval); /* ms. */ + ble.gap().setAdvertisingInterval(MIN_ADVERT_INTERVAL); setPir(0x00); - ble.gap().startAdvertising(); + error = ble.gap().startAdvertising(); + if (error != BLE_ERROR_NONE) onBleInitError(ble, error); // Print Interval and MAC Address - printf("advertisingInterval = %i\n\r", minInterval); + uint16_t minInterval = ble.gap().getMinAdvertisingInterval(); + printf("MinAdvertisingInterval = %i\n\r", minInterval); printf("MAC Address = %02X:%02X:%02X:%02X:%02X:%02X\n\r", macAddress[5], macAddress[4], macAddress[3], macAddress[2], macAddress[1], macAddress[0]); } - - int main(void) { Ticker ticker; - led1 = !0; led2 = !0; + led1 = !0; led2 = !0; led3 = !0; ticker.attach(periodicCallback, 1); button0.fall(button0PressedCallback); @@ -180,18 +177,37 @@ BLE &ble = BLE::Instance(); ble.init(bleInitComplete); - /* SpinWait for initialization to complete. This is necessary because the - * BLE object is used in the main loop below. */ + /* SpinWait for initialization to complete. This is necessary because the BLE object is used in the main loop below. */ while (ble.hasInitialized() == false) { /* spin loop */ } + uint8_t cnt = 0; while (true) { if (buttonState != IDLE) { - buttonServicePtr->updateButtonState(buttonState); - if(buttonState == PRESSED) {led1=!1; setPir(0x01);} - if(buttonState == RELEASED) {led1=!0; setPir(0x00);} + if(buttonState == PRESSED) { + led1=!1; + setPir(0x01); + } + if(buttonState == RELEASED) { + led1=!0; + setPir(0x00); + } + cnt = 0; buttonState = IDLE; } + + if(cnt < 1){ + ble.gap().startAdvertising(); + led3 = !1; + } + if(cnt > 5){ + ble.gap().stopAdvertising(); + led3 = !0; + }else{ + cnt++; + } + ble.waitForEvent(); + //printf("cnt=%i\n\r",cnt); } }