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