fsdfds
Dependencies: BLE_API mbed-dev-bin nRF51822
Fork of microbit-dal by
MicroBitButtonService.cpp
00001 /* 00002 The MIT License (MIT) 00003 00004 Copyright (c) 2016 British Broadcasting Corporation. 00005 This software is provided by Lancaster University by arrangement with the BBC. 00006 00007 Permission is hereby granted, free of charge, to any person obtaining a 00008 copy of this software and associated documentation files (the "Software"), 00009 to deal in the Software without restriction, including without limitation 00010 the rights to use, copy, modify, merge, publish, distribute, sublicense, 00011 and/or sell copies of the Software, and to permit persons to whom the 00012 Software is furnished to do so, subject to the following conditions: 00013 00014 The above copyright notice and this permission notice shall be included in 00015 all copies or substantial portions of the Software. 00016 00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00023 DEALINGS IN THE SOFTWARE. 00024 */ 00025 00026 /** 00027 * Class definition for the custom MicroBit Button Service. 00028 * Provides a BLE service to remotely read the state of each button, and configure its behaviour. 00029 */ 00030 #include "MicroBitConfig.h" 00031 #include "ble/UUID.h" 00032 00033 #include "MicroBitButtonService.h" 00034 #include "MicroBitButton.h" 00035 00036 /** 00037 * Constructor. 00038 * Create a representation of the ButtonService 00039 * @param _ble The instance of a BLE device that we're running on. 00040 */ 00041 MicroBitButtonService::MicroBitButtonService(BLEDevice &_ble) : 00042 ble(_ble) 00043 { 00044 // Create the data structures that represent each of our characteristics in Soft Device. 00045 GattCharacteristic buttonADataCharacteristic(MicroBitButtonAServiceDataUUID, (uint8_t *)&buttonADataCharacteristicBuffer, 0, 00046 sizeof(buttonADataCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); 00047 00048 GattCharacteristic buttonBDataCharacteristic(MicroBitButtonBServiceDataUUID, (uint8_t *)&buttonBDataCharacteristicBuffer, 0, 00049 sizeof(buttonBDataCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); 00050 00051 00052 // Initialise our characteristic values. 00053 buttonADataCharacteristicBuffer = 0; 00054 buttonBDataCharacteristicBuffer = 0; 00055 00056 // Set default security requirements 00057 buttonADataCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); 00058 buttonBDataCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); 00059 00060 GattCharacteristic *characteristics[] = {&buttonADataCharacteristic, &buttonBDataCharacteristic}; 00061 GattService service(MicroBitButtonServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); 00062 00063 ble.addService(service); 00064 00065 buttonADataCharacteristicHandle = buttonADataCharacteristic.getValueHandle(); 00066 buttonBDataCharacteristicHandle = buttonBDataCharacteristic.getValueHandle(); 00067 00068 ble.gattServer().write(buttonADataCharacteristicHandle,(uint8_t *)&buttonADataCharacteristicBuffer, sizeof(buttonADataCharacteristicBuffer)); 00069 ble.gattServer().write(buttonBDataCharacteristicHandle,(uint8_t *)&buttonBDataCharacteristicBuffer, sizeof(buttonBDataCharacteristicBuffer)); 00070 00071 if (EventModel::defaultEventBus) 00072 { 00073 EventModel::defaultEventBus->listen(MICROBIT_ID_BUTTON_A, MICROBIT_EVT_ANY, this, &MicroBitButtonService::buttonAUpdate, MESSAGE_BUS_LISTENER_IMMEDIATE); 00074 EventModel::defaultEventBus->listen(MICROBIT_ID_BUTTON_B, MICROBIT_EVT_ANY, this, &MicroBitButtonService::buttonBUpdate, MESSAGE_BUS_LISTENER_IMMEDIATE); 00075 } 00076 } 00077 00078 00079 /** 00080 * Button B update callback 00081 */ 00082 void MicroBitButtonService::buttonAUpdate(MicroBitEvent e) 00083 { 00084 if (ble.getGapState().connected) 00085 { 00086 if (e.value == MICROBIT_BUTTON_EVT_UP) 00087 { 00088 buttonADataCharacteristicBuffer = 0; 00089 ble.gattServer().notify(buttonADataCharacteristicHandle,(uint8_t *)&buttonADataCharacteristicBuffer, sizeof(buttonADataCharacteristicBuffer)); 00090 } 00091 00092 if (e.value == MICROBIT_BUTTON_EVT_DOWN) 00093 { 00094 buttonADataCharacteristicBuffer = 1; 00095 ble.gattServer().notify(buttonADataCharacteristicHandle,(uint8_t *)&buttonADataCharacteristicBuffer, sizeof(buttonADataCharacteristicBuffer)); 00096 } 00097 00098 if (e.value == MICROBIT_BUTTON_EVT_HOLD) 00099 { 00100 buttonADataCharacteristicBuffer = 2; 00101 ble.gattServer().notify(buttonADataCharacteristicHandle,(uint8_t *)&buttonADataCharacteristicBuffer, sizeof(buttonADataCharacteristicBuffer)); 00102 } 00103 } 00104 } 00105 00106 /** 00107 * Button A update callback 00108 */ 00109 void MicroBitButtonService::buttonBUpdate(MicroBitEvent e) 00110 { 00111 if (ble.getGapState().connected) 00112 { 00113 if (e.value == MICROBIT_BUTTON_EVT_UP) 00114 { 00115 buttonBDataCharacteristicBuffer = 0; 00116 ble.gattServer().notify(buttonBDataCharacteristicHandle,(uint8_t *)&buttonBDataCharacteristicBuffer, sizeof(buttonBDataCharacteristicBuffer)); 00117 } 00118 00119 if (e.value == MICROBIT_BUTTON_EVT_DOWN) 00120 { 00121 buttonBDataCharacteristicBuffer = 1; 00122 ble.gattServer().notify(buttonBDataCharacteristicHandle,(uint8_t *)&buttonBDataCharacteristicBuffer, sizeof(buttonBDataCharacteristicBuffer)); 00123 } 00124 00125 if (e.value == MICROBIT_BUTTON_EVT_HOLD) 00126 { 00127 buttonBDataCharacteristicBuffer = 2; 00128 ble.gattServer().notify(buttonBDataCharacteristicHandle,(uint8_t *)&buttonBDataCharacteristicBuffer, sizeof(buttonBDataCharacteristicBuffer)); 00129 } 00130 } 00131 } 00132 00133 const uint8_t MicroBitButtonServiceUUID[] = { 00134 0xe9,0x5d,0x98,0x82,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 00135 }; 00136 00137 const uint8_t MicroBitButtonAServiceDataUUID[] = { 00138 0xe9,0x5d,0xda,0x90,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 00139 } 00140 ; 00141 const uint8_t MicroBitButtonBServiceDataUUID[] = { 00142 0xe9,0x5d,0xda,0x91,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 00143 };
Generated on Wed Jul 13 2022 00:58:03 by 1.7.2