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