LED and Button for BLE workshop (TinyBLE)
Dependencies: BLE_API mbed nRF51822
Fork of BLE_LED by
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "mbed.h" 00018 #include "ble/BLE.h" 00019 #include "LEDService.h" 00020 #include "ButtonService.h" 00021 00022 #define LED_ON 0 00023 #define LED_OFF 1 00024 00025 DigitalOut alivenessLED(LED1, LED_OFF); // green 00026 DigitalOut actuatedLED(LED2, LED_OFF); // red 00027 InterruptIn button(BUTTON1); 00028 00029 const static char DEVICE_NAME[] = "MY_BLE_DEVICE"; 00030 static const uint16_t uuid16_list[] = { 00031 LEDService::LED_SERVICE_UUID, 00032 ButtonService::BUTTON_SERVICE_UUID 00033 }; 00034 00035 LEDService *ledServicePtr; 00036 ButtonService *buttonServicePtr; 00037 00038 Ticker ticker; 00039 00040 void periodicCallback(void) 00041 { 00042 alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */ 00043 } 00044 00045 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00046 { 00047 BLE::Instance().gap().startAdvertising(); 00048 00049 ticker.attach(periodicCallback, 1); 00050 } 00051 00052 void connectionCallback(const Gap::ConnectionCallbackParams_t * params) 00053 { 00054 // stop blinking when we connect 00055 ticker.detach(); 00056 // also put the led off 00057 alivenessLED = LED_OFF; 00058 } 00059 00060 /** 00061 * This callback allows the LEDService to receive updates to the ledState Characteristic. 00062 * 00063 * @param[in] params 00064 * Information about the characterisitc being updated. 00065 */ 00066 void onDataWrittenCallback(const GattWriteCallbackParams *params) { 00067 // handle corresponds to the characteristic being written 00068 // then we can read data to get a buffer of the actual data 00069 if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) { 00070 // When writing 1 -> turn LED on, 0 -> turn LED off 00071 char val = params->data[0]; 00072 actuatedLED = val == 1 ? LED_ON : LED_OFF; 00073 } 00074 } 00075 00076 /** 00077 * This function is called when the ble initialization process has failed 00078 */ 00079 void onBleInitError(BLE &ble, ble_error_t error) 00080 { 00081 // blink fast when we encountered an error 00082 ticker.attach(periodicCallback, 0.2); 00083 } 00084 00085 /** 00086 * Callback triggered when the ble initialization process has finished 00087 */ 00088 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00089 { 00090 BLE& ble = params->ble; 00091 ble_error_t error = params->error; 00092 00093 if (error != BLE_ERROR_NONE) { 00094 /* In case of error, forward the error handling to onBleInitError */ 00095 onBleInitError(ble, error); 00096 return; 00097 } 00098 00099 /* Ensure that it is the default instance of BLE */ 00100 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { 00101 return; 00102 } 00103 00104 ble.gap().onDisconnection(disconnectionCallback); 00105 ble.gap().onConnection(connectionCallback); 00106 ble.gattServer().onDataWritten(onDataWrittenCallback); 00107 00108 // Begin - If you add a new service, add it here! 00109 ledServicePtr = new LEDService(ble, false /* inital value */); 00110 buttonServicePtr = new ButtonService(ble, false /* initial value */); 00111 // End - If you add a new service, add it here! 00112 00113 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00114 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00115 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00116 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00117 ble.gap().setAdvertisingInterval(50); /* 50ms. Hack for EvoThings. */ 00118 ble.gap().startAdvertising(); 00119 } 00120 00121 void button_down() { 00122 if (!buttonServicePtr) return; 00123 buttonServicePtr->updateButtonState(true); 00124 } 00125 void button_up() { 00126 if (!buttonServicePtr) return; 00127 buttonServicePtr->updateButtonState(false); 00128 } 00129 00130 int main(void) 00131 { 00132 // Blink the green LED! 00133 ticker.attach(periodicCallback, 1); 00134 00135 button.fall(&button_down); 00136 button.rise(&button_up); 00137 00138 BLE &ble = BLE::Instance(); 00139 ble.init(bleInitComplete); 00140 00141 /* SpinWait for initialization to complete. This is necessary because the 00142 * BLE object is used in the main loop below. */ 00143 while (ble.hasInitialized() == false) { /* spin loop */ } 00144 00145 while (true) { 00146 ble.waitForEvent(); 00147 } 00148 }
Generated on Tue Jul 12 2022 23:11:20 by 1.7.2