example firmware for nRF51-Dongle to use with Opentrigger
Dependencies: BLE_API mbed nRF51822
Fork of BLE_Button by
Diff: main.cpp
- Revision:
- 13:0f26a3fc1f32
- Parent:
- 12:2cb903df877d
- Child:
- 14:09f208b24460
--- a/main.cpp Tue Jun 07 14:40:36 2016 +0000 +++ b/main.cpp Tue Jun 07 18:20:12 2016 +0000 @@ -20,8 +20,9 @@ DigitalOut led1(LED1); DigitalOut led2(LED2); -InterruptIn button(P0_15); +InterruptIn button(P0_15); /* The Pin where your Button is Connected */ +const uint16_t MIN_ADVERT_INTERVAL = 50; /* msec */ const static char DEVICE_NAME[] = "Button"; static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID}; @@ -68,11 +69,17 @@ void onBleInitError(BLE &ble, ble_error_t error) { /* Initialization error handling should go here */ + error("Panic!"); } void setPir(uint8_t pir){ - uint8_t data[] = { 0xee, 0xff, 0x04, 0x01, 0x11, 0x07, 0x00 }; - data[6] = pir; + uint8_t data[] = { + 0xee, 0xff, // Tokencube Manufacturer ID + 0x04, // Token Module Version + 0x01, // Firmware Version + 0x11, // Page 1 of 1 + 0x07, pir // pir value + }; BLE::Instance().gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, data, sizeof(data)); } @@ -95,8 +102,13 @@ return; } - const uint8_t address1[] = {0xe7,0xAA,0xAA,0xAA,0xAA,0xAA}; - ble.gap().setAddress(BLEProtocol::AddressType::PUBLIC, address1); + uint8_t macAddress[6] = {0x00}; + BLEProtocol::AddressType_t pubAddr = BLEProtocol::AddressType::PUBLIC; + ble.gap().getAddress(&pubAddr, macAddress); + + /* change device MAC address */ + //const uint8_t address1[] = {0xe7,0xAA,0xAA,0xAA,0xAA,0xAA}; + //ble.gap().setAddress(pubAddr, address1); ble.gap().onDisconnection(disconnectionCallback); @@ -108,9 +120,16 @@ 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); - ble.gap().setAdvertisingInterval(300); /* ms. */ + + uint16_t minInterval = ble.gap().getMinAdvertisingInterval(); + if(minInterval < 50) minInterval = 50; + ble.gap().setAdvertisingInterval(minInterval); /* ms. */ setPir(0x00); ble.gap().startAdvertising(); + + // Print Interval and MAC Address + printf("advertisingInterval = %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]); } @@ -135,9 +154,8 @@ 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);} buttonState = IDLE; }