micro:bit мигалка

Dependencies:   mbed BLE_API nRF51822

Программа для исполнительного устройства на micro:bit (nRF51822), включение\выключение светодиода по команде с управляющего устройства на nRF51822.

Committer:
Alexgerni
Date:
Tue Aug 08 20:38:27 2017 +0000
Revision:
0:8ae63d3cc188
Child:
1:222379aed289
Simple LED example

Who changed what in which revision?

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