BLE switch interface using micro:bit with 3 tact switches or 3 Makey Makey sensors

Dependencies:   microbit

Committer:
masakjm
Date:
Thu Jun 06 19:06:06 2019 +0000
Revision:
1:9d0e2e5b5d25

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
masakjm 1:9d0e2e5b5d25 1 /* mbed Microcontroller Library
masakjm 1:9d0e2e5b5d25 2 * Copyright (c) 2006-2013 ARM Limited
masakjm 1:9d0e2e5b5d25 3 *
masakjm 1:9d0e2e5b5d25 4 * Licensed under the Apache License, Version 2.0 (the "License");
masakjm 1:9d0e2e5b5d25 5 * you may not use this file except in compliance with the License.
masakjm 1:9d0e2e5b5d25 6 * You may obtain a copy of the License at
masakjm 1:9d0e2e5b5d25 7 *
masakjm 1:9d0e2e5b5d25 8 * http://www.apache.org/licenses/LICENSE-2.0
masakjm 1:9d0e2e5b5d25 9 *
masakjm 1:9d0e2e5b5d25 10 * Unless required by applicable law or agreed to in writing, software
masakjm 1:9d0e2e5b5d25 11 * distributed under the License is distributed on an "AS IS" BASIS,
masakjm 1:9d0e2e5b5d25 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
masakjm 1:9d0e2e5b5d25 13 * See the License for the specific language governing permissions and
masakjm 1:9d0e2e5b5d25 14 * limitations under the License.
masakjm 1:9d0e2e5b5d25 15 */
masakjm 1:9d0e2e5b5d25 16
masakjm 1:9d0e2e5b5d25 17 #ifndef __BLE_HID_DEVICE_INFORMATION_SERVICE_H__
masakjm 1:9d0e2e5b5d25 18 #define __BLE_HID_DEVICE_INFORMATION_SERVICE_H__
masakjm 1:9d0e2e5b5d25 19
masakjm 1:9d0e2e5b5d25 20 #include "ble/BLE.h"
masakjm 1:9d0e2e5b5d25 21
masakjm 1:9d0e2e5b5d25 22 #include "HID_types.h"
masakjm 1:9d0e2e5b5d25 23
masakjm 1:9d0e2e5b5d25 24 /**
masakjm 1:9d0e2e5b5d25 25 * @class HIDDeviceInformationService
masakjm 1:9d0e2e5b5d25 26 * @brief BLE Device Information Service <br>
masakjm 1:9d0e2e5b5d25 27 * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.device_information.xml <br>
masakjm 1:9d0e2e5b5d25 28 * Manufacturer Name String Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.manufacturer_name_string.xml
masakjm 1:9d0e2e5b5d25 29 */
masakjm 1:9d0e2e5b5d25 30 class HIDDeviceInformationService {
masakjm 1:9d0e2e5b5d25 31 public:
masakjm 1:9d0e2e5b5d25 32 /**
masakjm 1:9d0e2e5b5d25 33 * @brief Device Information Service Constructor.
masakjm 1:9d0e2e5b5d25 34 *
masakjm 1:9d0e2e5b5d25 35 * @param[ref] _ble
masakjm 1:9d0e2e5b5d25 36 * BLE object for the underlying controller.
masakjm 1:9d0e2e5b5d25 37 * @param[in] manufacturersName
masakjm 1:9d0e2e5b5d25 38 * This characteristic represents the name of the
masakjm 1:9d0e2e5b5d25 39 * manufacturer of the device. The name is copied into the
masakjm 1:9d0e2e5b5d25 40 * BLE stack during this constructor.
masakjm 1:9d0e2e5b5d25 41 * @param[in] modelNumber
masakjm 1:9d0e2e5b5d25 42 * This characteristic represents the model number that is
masakjm 1:9d0e2e5b5d25 43 * assigned by the device vendor. The value is copied into
masakjm 1:9d0e2e5b5d25 44 * the BLE stack during this constructor.
masakjm 1:9d0e2e5b5d25 45 * @param[in] serialNumber
masakjm 1:9d0e2e5b5d25 46 * This characteristic represents the serial number for a
masakjm 1:9d0e2e5b5d25 47 * particular instance of the device. The value is copied
masakjm 1:9d0e2e5b5d25 48 * into the BLE stack during this constructor.
masakjm 1:9d0e2e5b5d25 49 * @param[in] hardwareRevision
masakjm 1:9d0e2e5b5d25 50 * This characteristic represents the hardware revision for
masakjm 1:9d0e2e5b5d25 51 * the hardware within the device. The value is copied
masakjm 1:9d0e2e5b5d25 52 * into the BLE stack during this constructor.
masakjm 1:9d0e2e5b5d25 53 * @param[in] firmwareRevision
masakjm 1:9d0e2e5b5d25 54 * This characteristic represents the firmware revision for
masakjm 1:9d0e2e5b5d25 55 * the firmware within the device. The value is copied
masakjm 1:9d0e2e5b5d25 56 * into the BLE stack during this constructor.
masakjm 1:9d0e2e5b5d25 57 * @param[in] softwareRevision
masakjm 1:9d0e2e5b5d25 58 * This characteristic represents the software revision for
masakjm 1:9d0e2e5b5d25 59 * the software within the device. The value is copied
masakjm 1:9d0e2e5b5d25 60 * into the BLE stack during this constructor.
masakjm 1:9d0e2e5b5d25 61 * @param[in] pnpID
masakjm 1:9d0e2e5b5d25 62 * This characteristic represents HID-specific information,
masakjm 1:9d0e2e5b5d25 63 * such as vendor id, product id and version.
masakjm 1:9d0e2e5b5d25 64 */
masakjm 1:9d0e2e5b5d25 65 HIDDeviceInformationService(BLE &_ble,
masakjm 1:9d0e2e5b5d25 66 const char *manufacturersName = NULL,
masakjm 1:9d0e2e5b5d25 67 const char *modelNumber = NULL,
masakjm 1:9d0e2e5b5d25 68 const char *serialNumber = NULL,
masakjm 1:9d0e2e5b5d25 69 const char *hardwareRevision = NULL,
masakjm 1:9d0e2e5b5d25 70 const char *firmwareRevision = NULL,
masakjm 1:9d0e2e5b5d25 71 const char *softwareRevision = NULL,
masakjm 1:9d0e2e5b5d25 72 PnPID_t *PnPID = NULL) :
masakjm 1:9d0e2e5b5d25 73 ble(_ble),
masakjm 1:9d0e2e5b5d25 74 manufacturersNameStringCharacteristic(GattCharacteristic::UUID_MANUFACTURER_NAME_STRING_CHAR,
masakjm 1:9d0e2e5b5d25 75 (uint8_t *)manufacturersName,
masakjm 1:9d0e2e5b5d25 76 (manufacturersName != NULL) ? strlen(manufacturersName) : 0, /* minLength */
masakjm 1:9d0e2e5b5d25 77 (manufacturersName != NULL) ? strlen(manufacturersName) : 0, /* maxLength */
masakjm 1:9d0e2e5b5d25 78 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
masakjm 1:9d0e2e5b5d25 79 modelNumberStringCharacteristic(GattCharacteristic::UUID_MODEL_NUMBER_STRING_CHAR,
masakjm 1:9d0e2e5b5d25 80 (uint8_t *)modelNumber,
masakjm 1:9d0e2e5b5d25 81 (modelNumber != NULL) ? strlen(modelNumber) : 0, /* minLength */
masakjm 1:9d0e2e5b5d25 82 (modelNumber != NULL) ? strlen(modelNumber) : 0, /* maxLength */
masakjm 1:9d0e2e5b5d25 83 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
masakjm 1:9d0e2e5b5d25 84 serialNumberStringCharacteristic(GattCharacteristic::UUID_SERIAL_NUMBER_STRING_CHAR,
masakjm 1:9d0e2e5b5d25 85 (uint8_t *)serialNumber,
masakjm 1:9d0e2e5b5d25 86 (serialNumber != NULL) ? strlen(serialNumber) : 0, /* minLength */
masakjm 1:9d0e2e5b5d25 87 (serialNumber != NULL) ? strlen(serialNumber) : 0, /* maxLength */
masakjm 1:9d0e2e5b5d25 88 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
masakjm 1:9d0e2e5b5d25 89 hardwareRevisionStringCharacteristic(GattCharacteristic::UUID_HARDWARE_REVISION_STRING_CHAR,
masakjm 1:9d0e2e5b5d25 90 (uint8_t *)hardwareRevision,
masakjm 1:9d0e2e5b5d25 91 (hardwareRevision != NULL) ? strlen(hardwareRevision) : 0, /* minLength */
masakjm 1:9d0e2e5b5d25 92 (hardwareRevision != NULL) ? strlen(hardwareRevision) : 0, /* maxLength */
masakjm 1:9d0e2e5b5d25 93 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
masakjm 1:9d0e2e5b5d25 94 firmwareRevisionStringCharacteristic(GattCharacteristic::UUID_FIRMWARE_REVISION_STRING_CHAR,
masakjm 1:9d0e2e5b5d25 95 (uint8_t *)firmwareRevision,
masakjm 1:9d0e2e5b5d25 96 (firmwareRevision != NULL) ? strlen(firmwareRevision) : 0, /* minLength */
masakjm 1:9d0e2e5b5d25 97 (firmwareRevision != NULL) ? strlen(firmwareRevision) : 0, /* maxLength */
masakjm 1:9d0e2e5b5d25 98 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
masakjm 1:9d0e2e5b5d25 99 softwareRevisionStringCharacteristic(GattCharacteristic::UUID_SOFTWARE_REVISION_STRING_CHAR,
masakjm 1:9d0e2e5b5d25 100 (uint8_t *)softwareRevision,
masakjm 1:9d0e2e5b5d25 101 (softwareRevision != NULL) ? strlen(softwareRevision) : 0, /* minLength */
masakjm 1:9d0e2e5b5d25 102 (softwareRevision != NULL) ? strlen(softwareRevision) : 0, /* maxLength */
masakjm 1:9d0e2e5b5d25 103 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
masakjm 1:9d0e2e5b5d25 104 pnpIDCharacteristic(GattCharacteristic::UUID_PNP_ID_CHAR,
masakjm 1:9d0e2e5b5d25 105 PnPID)
masakjm 1:9d0e2e5b5d25 106 {
masakjm 1:9d0e2e5b5d25 107 static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */
masakjm 1:9d0e2e5b5d25 108 if (serviceAdded) {
masakjm 1:9d0e2e5b5d25 109 return;
masakjm 1:9d0e2e5b5d25 110 }
masakjm 1:9d0e2e5b5d25 111
masakjm 1:9d0e2e5b5d25 112 GattCharacteristic *charTable[] = {&manufacturersNameStringCharacteristic,
masakjm 1:9d0e2e5b5d25 113 &modelNumberStringCharacteristic,
masakjm 1:9d0e2e5b5d25 114 &serialNumberStringCharacteristic,
masakjm 1:9d0e2e5b5d25 115 &hardwareRevisionStringCharacteristic,
masakjm 1:9d0e2e5b5d25 116 &firmwareRevisionStringCharacteristic,
masakjm 1:9d0e2e5b5d25 117 &softwareRevisionStringCharacteristic,
masakjm 1:9d0e2e5b5d25 118 &pnpIDCharacteristic};
masakjm 1:9d0e2e5b5d25 119 GattService deviceInformationService(GattService::UUID_DEVICE_INFORMATION_SERVICE, charTable,
masakjm 1:9d0e2e5b5d25 120 sizeof(charTable) / sizeof(GattCharacteristic *));
masakjm 1:9d0e2e5b5d25 121
masakjm 1:9d0e2e5b5d25 122 /*
masakjm 1:9d0e2e5b5d25 123 * This is a hack to make things work on MacOSX 10.10. I don't have the details, but MacOSX
masakjm 1:9d0e2e5b5d25 124 * 10.10 gets confused when only characteristics from HID Service require security...
masakjm 1:9d0e2e5b5d25 125 */
masakjm 1:9d0e2e5b5d25 126 pnpIDCharacteristic.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM);
masakjm 1:9d0e2e5b5d25 127 ble.addService(deviceInformationService);
masakjm 1:9d0e2e5b5d25 128 serviceAdded = true;
masakjm 1:9d0e2e5b5d25 129 }
masakjm 1:9d0e2e5b5d25 130
masakjm 1:9d0e2e5b5d25 131 protected:
masakjm 1:9d0e2e5b5d25 132 BLE &ble;
masakjm 1:9d0e2e5b5d25 133 GattCharacteristic manufacturersNameStringCharacteristic;
masakjm 1:9d0e2e5b5d25 134 GattCharacteristic modelNumberStringCharacteristic;
masakjm 1:9d0e2e5b5d25 135 GattCharacteristic serialNumberStringCharacteristic;
masakjm 1:9d0e2e5b5d25 136 GattCharacteristic hardwareRevisionStringCharacteristic;
masakjm 1:9d0e2e5b5d25 137 GattCharacteristic firmwareRevisionStringCharacteristic;
masakjm 1:9d0e2e5b5d25 138 GattCharacteristic softwareRevisionStringCharacteristic;
masakjm 1:9d0e2e5b5d25 139 ReadOnlyGattCharacteristic<PnPID_t> pnpIDCharacteristic;
masakjm 1:9d0e2e5b5d25 140 };
masakjm 1:9d0e2e5b5d25 141
masakjm 1:9d0e2e5b5d25 142 #endif /* #ifndef __BLE_HID_DEVICE_INFORMATION_SERVICE_H__*/
masakjm 1:9d0e2e5b5d25 143
masakjm 1:9d0e2e5b5d25 144