Smart Button
Dependencies: BLE_API mbed nRF51822
Diff: main.cpp
- Revision:
- 1:795c0e3fbd4e
- Parent:
- 0:d7ba2329a303
diff -r d7ba2329a303 -r 795c0e3fbd4e main.cpp --- a/main.cpp Sun Oct 16 14:18:27 2016 +0000 +++ b/main.cpp Sun Oct 16 14:19:47 2016 +0000 @@ -1,105 +1,3 @@ -/* - -#define BUTTON_SERVICE_UUID 0xA000 -#define BUTTON_STATE_CHAR_UUID 0xA001 - -#define LED_SERVICE_UUID 0xB000 -#define LED_STATE_CHAR_UUID 0xB001 -#define LED_STATUS_CHAR_UUID 0xB002 - -const static char DEVICE_NAME[] = "Button"; -static const uint16_t uuid16_list[] = {BUTTON_SERVICE_UUID, LED_SERVICE_UUID}; - -bool buttonPressed = false; - -ReadOnlyGattCharacteristic<bool> buttonState(BUTTON_STATE_CHAR_UUID, &buttonPressed, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); - -//static uint8_t readValue[10] = {0}; -//ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(LED_STATUS_CHAR_UUID, readValue); - -static uint8_t writeValue[10] = {0}; -WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(LED_STATE_CHAR_UUID, writeValue); - - - - -void writeCharCallback(const GattWriteCallbackParams* params) -{ - // check to see what characteristic was written, by handle - if(params->handle == writeChar.getValueHandle()) { - // toggle LED if only 1 byte is written - if(params->len == 1) { - led2 = params->data[0] ? false : true; - } - // update the readChar with the value of writeChar - //ble.updateCharacteristicValue(readChar.getValueHandle(),params->data,params->len); - } -} - - - -void buttonPressedCallback(void) -{ - buttonPressed = true; - ble.updateCharacteristicValue(buttonState.getValueHandle(), (uint8_t *)&buttonPressed, sizeof(bool)); - //led2 = false; -} - -void buttonReleasedCallback(void) -{ - buttonPressed = false; - ble.updateCharacteristicValue(buttonState.getValueHandle(), (uint8_t *)&buttonPressed, sizeof(bool)); - //led2 = true; -} - -void disconnectionCallback(const Gap::DisconnectionCallbackParams_t* reason) -{ - ble.startAdvertising(); - led1 = 1; - led2 = 1; -} - -void periodicCallback(void) -{ - led1 = !led1; // Do blinky on LED1 to indicate system aliveness. -} - -int main(void) -{ - led1 = 1; - led2 = 1; - Ticker ticker; - ticker.attach(periodicCallback, 1); - button.fall(buttonPressedCallback); - button.rise(buttonReleasedCallback); - - ble.init(); - ble.onDisconnection(disconnectionCallback); - ble.onDataWritten(writeCharCallback); - - GattCharacteristic *btnChars[] = {&buttonState}; - GattService buttonService(BUTTON_SERVICE_UUID, btnChars, sizeof(btnChars) / sizeof(GattCharacteristic *)); - ble.addService(buttonService); - - // Set up custom service - GattCharacteristic *ledChars[] = { &writeChar}; //&readChar, - GattService ledService(LED_SERVICE_UUID, ledChars, sizeof(ledChars) / sizeof(GattCharacteristic *)); - ble.addService(ledService); - - // setup advertising - ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); - ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); - ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); - ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); - ble.setAdvertisingInterval(Gap::MSEC_TO_GAP_DURATION_UNITS(1000)); // 1000ms. - ble.startAdvertising(); - - while (true) { - ble.waitForEvent(); - } -} -*/ - #include "mbed.h" #include "BLE.h"