Test program for magnetometer
Dependencies: mbed AES BLE_API nRF51822 smallAES
main.cpp@2:4871b5ad7938, 2019-09-30 (annotated)
- Committer:
- f3d
- Date:
- Mon Sep 30 10:38:33 2019 +0000
- Revision:
- 2:4871b5ad7938
- Parent:
- 1:b10a8955c2fc
- Child:
- 3:b9783107a8c4
Code for the LSM303 version of the BBC Microbit;
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 | 2:4871b5ad7938 | 20 | #include "ButtonAService.h" |
f3d | 2:4871b5ad7938 | 21 | #include "accelService.h" |
f3d | 0:21352e62003f | 22 | |
f3d | 0:21352e62003f | 23 | /* |
f3d | 0:21352e62003f | 24 | * All the LEDs on the micro:bit are part of the LED Matrix, |
f3d | 0:21352e62003f | 25 | * In order to get simple blinking behaviour, we set column 0 |
f3d | 0:21352e62003f | 26 | * to be permanently at ground. If you want to use the LEDs as |
f3d | 0:21352e62003f | 27 | * a screen, there is a display driver in the micro:bit 'DAL', |
f3d | 0:21352e62003f | 28 | */ |
f3d | 0:21352e62003f | 29 | DigitalOut col1(P0_4, 0); |
f3d | 0:21352e62003f | 30 | DigitalOut alivenessLED(P0_13, 0); |
f3d | 0:21352e62003f | 31 | DigitalOut actuatedLED(P0_14, 0); |
f3d | 2:4871b5ad7938 | 32 | |
f3d | 2:4871b5ad7938 | 33 | const static char DEVICE_NAME[] = "front1"; |
f3d | 2:4871b5ad7938 | 34 | //static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID,ACCELService::ACCEL_SERVICE_UUID,ButtonAService::BUTTONA_SERVICE_UUID}; |
f3d | 2:4871b5ad7938 | 35 | static const uint16_t uuid16_list[] = {ACCELService::ACCEL_SERVICE_UUID}; |
f3d | 0:21352e62003f | 36 | |
f3d | 0:21352e62003f | 37 | LEDService *ledServicePtr; |
f3d | 2:4871b5ad7938 | 38 | ButtonAService * btnAServicePtr; |
f3d | 2:4871b5ad7938 | 39 | ACCELService *AccelServicePtr; |
f3d | 2:4871b5ad7938 | 40 | |
f3d | 0:21352e62003f | 41 | Ticker ticker; |
f3d | 0:21352e62003f | 42 | |
f3d | 0:21352e62003f | 43 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
f3d | 0:21352e62003f | 44 | { |
f3d | 0:21352e62003f | 45 | BLE::Instance().gap().startAdvertising(); |
f3d | 0:21352e62003f | 46 | } |
f3d | 0:21352e62003f | 47 | |
f3d | 0:21352e62003f | 48 | void periodicCallback(void) |
f3d | 0:21352e62003f | 49 | { |
f3d | 2:4871b5ad7938 | 50 | |
f3d | 2:4871b5ad7938 | 51 | btnAServicePtr->poll(); |
f3d | 2:4871b5ad7938 | 52 | AccelServicePtr->poll(); |
f3d | 2:4871b5ad7938 | 53 | if (btnAServicePtr->GetButtonAState()) |
f3d | 2:4871b5ad7938 | 54 | alivenessLED = 1; |
f3d | 2:4871b5ad7938 | 55 | else |
f3d | 2:4871b5ad7938 | 56 | alivenessLED = 0; |
f3d | 2:4871b5ad7938 | 57 | |
f3d | 0:21352e62003f | 58 | } |
f3d | 0:21352e62003f | 59 | |
f3d | 0:21352e62003f | 60 | /** |
f3d | 0:21352e62003f | 61 | * This callback allows the LEDService to receive updates to the ledState Characteristic. |
f3d | 0:21352e62003f | 62 | * |
f3d | 0:21352e62003f | 63 | * @param[in] params |
f3d | 0:21352e62003f | 64 | * Information about the characterisitc being updated. |
f3d | 0:21352e62003f | 65 | */ |
f3d | 0:21352e62003f | 66 | void onDataWrittenCallback(const GattWriteCallbackParams *params) { |
f3d | 0:21352e62003f | 67 | if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) { |
f3d | 0:21352e62003f | 68 | actuatedLED = *(params->data); |
f3d | 0:21352e62003f | 69 | } |
f3d | 0:21352e62003f | 70 | } |
f3d | 0:21352e62003f | 71 | |
f3d | 0:21352e62003f | 72 | void onDataReadCallback(const GattReadCallbackParams *params) { |
f3d | 2:4871b5ad7938 | 73 | |
f3d | 2:4871b5ad7938 | 74 | |
f3d | 0:21352e62003f | 75 | } |
f3d | 0:21352e62003f | 76 | /** |
f3d | 0:21352e62003f | 77 | * This function is called when the ble initialization process has failed |
f3d | 0:21352e62003f | 78 | */ |
f3d | 0:21352e62003f | 79 | void onBleInitError(BLE &ble, ble_error_t error) |
f3d | 0:21352e62003f | 80 | { |
f3d | 0:21352e62003f | 81 | /* Initialization error handling should go here */ |
f3d | 0:21352e62003f | 82 | } |
f3d | 0:21352e62003f | 83 | |
f3d | 0:21352e62003f | 84 | /** |
f3d | 0:21352e62003f | 85 | * Callback triggered when the ble initialization process has finished |
f3d | 0:21352e62003f | 86 | */ |
f3d | 0:21352e62003f | 87 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
f3d | 0:21352e62003f | 88 | { |
f3d | 0:21352e62003f | 89 | BLE& ble = params->ble; |
f3d | 0:21352e62003f | 90 | ble_error_t error = params->error; |
f3d | 0:21352e62003f | 91 | |
f3d | 0:21352e62003f | 92 | if (error != BLE_ERROR_NONE) { |
f3d | 0:21352e62003f | 93 | /* In case of error, forward the error handling to onBleInitError */ |
f3d | 0:21352e62003f | 94 | onBleInitError(ble, error); |
f3d | 0:21352e62003f | 95 | return; |
f3d | 0:21352e62003f | 96 | } |
f3d | 0:21352e62003f | 97 | |
f3d | 0:21352e62003f | 98 | /* Ensure that it is the default instance of BLE */ |
f3d | 0:21352e62003f | 99 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
f3d | 0:21352e62003f | 100 | return; |
f3d | 0:21352e62003f | 101 | } |
f3d | 0:21352e62003f | 102 | |
f3d | 0:21352e62003f | 103 | ble.gap().onDisconnection(disconnectionCallback); |
f3d | 0:21352e62003f | 104 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
f3d | 2:4871b5ad7938 | 105 | // ble.gattServer().onDataRead(onDataReadCallback); // Nordic Soft device will not call this so have to poll instead |
f3d | 0:21352e62003f | 106 | |
f3d | 0:21352e62003f | 107 | bool initialValueForLEDCharacteristic = false; |
f3d | 0:21352e62003f | 108 | ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic); |
f3d | 2:4871b5ad7938 | 109 | btnAServicePtr = new ButtonAService(ble); |
f3d | 2:4871b5ad7938 | 110 | int16_t Tst=0; |
f3d | 2:4871b5ad7938 | 111 | AccelServicePtr = new ACCELService(ble,Tst); |
f3d | 0:21352e62003f | 112 | /* setup advertising */ |
f3d | 0:21352e62003f | 113 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
f3d | 0:21352e62003f | 114 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
f3d | 0:21352e62003f | 115 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
f3d | 0:21352e62003f | 116 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
f3d | 0:21352e62003f | 117 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
f3d | 0:21352e62003f | 118 | ble.gap().startAdvertising(); |
f3d | 0:21352e62003f | 119 | } |
f3d | 0:21352e62003f | 120 | |
f3d | 0:21352e62003f | 121 | int main(void) |
f3d | 0:21352e62003f | 122 | { |
f3d | 2:4871b5ad7938 | 123 | ticker.attach(periodicCallback, 1); /* Poll devices every 100ms */ |
f3d | 0:21352e62003f | 124 | |
f3d | 0:21352e62003f | 125 | BLE &ble = BLE::Instance(); |
f3d | 0:21352e62003f | 126 | ble.init(bleInitComplete); |
f3d | 0:21352e62003f | 127 | |
f3d | 0:21352e62003f | 128 | /* SpinWait for initialization to complete. This is necessary because the |
f3d | 0:21352e62003f | 129 | * BLE object is used in the main loop below. */ |
f3d | 0:21352e62003f | 130 | while (ble.hasInitialized() == false) { /* spin loop */ } |
f3d | 0:21352e62003f | 131 | while (true) { |
f3d | 0:21352e62003f | 132 | ble.waitForEvent(); |
f3d | 0:21352e62003f | 133 | } |
f3d | 0:21352e62003f | 134 | } |