j
Dependencies: BLE_API mbed nRF51822
main.cpp@0:56899988652b, 2016-03-07 (annotated)
- Committer:
- Gulio
- Date:
- Mon Mar 07 11:23:36 2016 +0000
- Revision:
- 0:56899988652b
ghj
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Gulio | 0:56899988652b | 1 | /* mbed Microcontroller Library |
Gulio | 0:56899988652b | 2 | * Copyright (c) 2006-2013 ARM Limited |
Gulio | 0:56899988652b | 3 | * |
Gulio | 0:56899988652b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Gulio | 0:56899988652b | 5 | * you may not use this file except in compliance with the License. |
Gulio | 0:56899988652b | 6 | * You may obtain a copy of the License at |
Gulio | 0:56899988652b | 7 | * |
Gulio | 0:56899988652b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Gulio | 0:56899988652b | 9 | * |
Gulio | 0:56899988652b | 10 | * Unless required by applicable law or agreed to in writing, software |
Gulio | 0:56899988652b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Gulio | 0:56899988652b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Gulio | 0:56899988652b | 13 | * See the License for the specific language governing permissions and |
Gulio | 0:56899988652b | 14 | * limitations under the License. |
Gulio | 0:56899988652b | 15 | */ |
Gulio | 0:56899988652b | 16 | |
Gulio | 0:56899988652b | 17 | #include "mbed.h" |
Gulio | 0:56899988652b | 18 | #include "ble/BLE.h" |
Gulio | 0:56899988652b | 19 | #include "LEDService.h" |
Gulio | 0:56899988652b | 20 | |
Gulio | 0:56899988652b | 21 | DigitalOut led1(LED1, 0); |
Gulio | 0:56899988652b | 22 | DigitalOut led2(LED2, 0); |
Gulio | 0:56899988652b | 23 | DigitalOut pin1(P0_1, 0); |
Gulio | 0:56899988652b | 24 | DigitalOut pin2(P0_2, 0); |
Gulio | 0:56899988652b | 25 | |
Gulio | 0:56899988652b | 26 | const static char DEVICE_NAME[] = "LED"; |
Gulio | 0:56899988652b | 27 | static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID}; |
Gulio | 0:56899988652b | 28 | |
Gulio | 0:56899988652b | 29 | LEDService *ledServicePtr; |
Gulio | 0:56899988652b | 30 | |
Gulio | 0:56899988652b | 31 | Ticker ticker; |
Gulio | 0:56899988652b | 32 | |
Gulio | 0:56899988652b | 33 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
Gulio | 0:56899988652b | 34 | { |
Gulio | 0:56899988652b | 35 | BLE::Instance().gap().startAdvertising(); |
Gulio | 0:56899988652b | 36 | } |
Gulio | 0:56899988652b | 37 | |
Gulio | 0:56899988652b | 38 | void periodicCallback(void) |
Gulio | 0:56899988652b | 39 | { |
Gulio | 0:56899988652b | 40 | //led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */ |
Gulio | 0:56899988652b | 41 | } |
Gulio | 0:56899988652b | 42 | |
Gulio | 0:56899988652b | 43 | /** |
Gulio | 0:56899988652b | 44 | * This callback allows the LEDService to receive updates to the ledState Characteristic. |
Gulio | 0:56899988652b | 45 | * |
Gulio | 0:56899988652b | 46 | * @param[in] params |
Gulio | 0:56899988652b | 47 | * Information about the characterisitc being updated. |
Gulio | 0:56899988652b | 48 | */ |
Gulio | 0:56899988652b | 49 | void onDataWrittenCallback(const GattWriteCallbackParams *params) { |
Gulio | 0:56899988652b | 50 | if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) { |
Gulio | 0:56899988652b | 51 | //printf("led1"); |
Gulio | 0:56899988652b | 52 | pin1 = *(params->data); |
Gulio | 0:56899988652b | 53 | led1=pin1; |
Gulio | 0:56899988652b | 54 | } |
Gulio | 0:56899988652b | 55 | if ((params->handle == ledServicePtr->getValueHandle2()) && (params->len == 1)) { |
Gulio | 0:56899988652b | 56 | //printf("led2"); |
Gulio | 0:56899988652b | 57 | pin2 = *(params->data); |
Gulio | 0:56899988652b | 58 | if(pin2==1){ |
Gulio | 0:56899988652b | 59 | led2=1; |
Gulio | 0:56899988652b | 60 | } |
Gulio | 0:56899988652b | 61 | else{ |
Gulio | 0:56899988652b | 62 | led2=0; |
Gulio | 0:56899988652b | 63 | } |
Gulio | 0:56899988652b | 64 | } |
Gulio | 0:56899988652b | 65 | } |
Gulio | 0:56899988652b | 66 | |
Gulio | 0:56899988652b | 67 | /** |
Gulio | 0:56899988652b | 68 | * This function is called when the ble initialization process has failed |
Gulio | 0:56899988652b | 69 | */ |
Gulio | 0:56899988652b | 70 | void onBleInitError(BLE &ble, ble_error_t error) |
Gulio | 0:56899988652b | 71 | { |
Gulio | 0:56899988652b | 72 | /* Initialization error handling should go here */ |
Gulio | 0:56899988652b | 73 | } |
Gulio | 0:56899988652b | 74 | |
Gulio | 0:56899988652b | 75 | /** |
Gulio | 0:56899988652b | 76 | * Callback triggered when the ble initialization process has finished |
Gulio | 0:56899988652b | 77 | */ |
Gulio | 0:56899988652b | 78 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
Gulio | 0:56899988652b | 79 | { |
Gulio | 0:56899988652b | 80 | BLE& ble = params->ble; |
Gulio | 0:56899988652b | 81 | ble_error_t error = params->error; |
Gulio | 0:56899988652b | 82 | |
Gulio | 0:56899988652b | 83 | if (error != BLE_ERROR_NONE) { |
Gulio | 0:56899988652b | 84 | /* In case of error, forward the error handling to onBleInitError */ |
Gulio | 0:56899988652b | 85 | onBleInitError(ble, error); |
Gulio | 0:56899988652b | 86 | return; |
Gulio | 0:56899988652b | 87 | } |
Gulio | 0:56899988652b | 88 | |
Gulio | 0:56899988652b | 89 | /* Ensure that it is the default instance of BLE */ |
Gulio | 0:56899988652b | 90 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
Gulio | 0:56899988652b | 91 | return; |
Gulio | 0:56899988652b | 92 | } |
Gulio | 0:56899988652b | 93 | |
Gulio | 0:56899988652b | 94 | ble.gap().onDisconnection(disconnectionCallback); |
Gulio | 0:56899988652b | 95 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
Gulio | 0:56899988652b | 96 | |
Gulio | 0:56899988652b | 97 | bool initialValueForLEDCharacteristic = false; |
Gulio | 0:56899988652b | 98 | ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic); |
Gulio | 0:56899988652b | 99 | |
Gulio | 0:56899988652b | 100 | /* setup advertising */ |
Gulio | 0:56899988652b | 101 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
Gulio | 0:56899988652b | 102 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
Gulio | 0:56899988652b | 103 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
Gulio | 0:56899988652b | 104 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Gulio | 0:56899988652b | 105 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
Gulio | 0:56899988652b | 106 | ble.gap().startAdvertising(); |
Gulio | 0:56899988652b | 107 | } |
Gulio | 0:56899988652b | 108 | |
Gulio | 0:56899988652b | 109 | int main(void) |
Gulio | 0:56899988652b | 110 | { |
Gulio | 0:56899988652b | 111 | //ticker.attach(periodicCallback, 1); /* Blink LED every second */ |
Gulio | 0:56899988652b | 112 | |
Gulio | 0:56899988652b | 113 | BLE &ble = BLE::Instance(); |
Gulio | 0:56899988652b | 114 | ble.init(bleInitComplete); |
Gulio | 0:56899988652b | 115 | |
Gulio | 0:56899988652b | 116 | /* SpinWait for initialization to complete. This is necessary because the |
Gulio | 0:56899988652b | 117 | * BLE object is used in the main loop below. */ |
Gulio | 0:56899988652b | 118 | while (ble.hasInitialized() == false) { /* spin loop */ } |
Gulio | 0:56899988652b | 119 | |
Gulio | 0:56899988652b | 120 | while (true) { |
Gulio | 0:56899988652b | 121 | ble.waitForEvent(); |
Gulio | 0:56899988652b | 122 | } |
Gulio | 0:56899988652b | 123 | } |