No changes
Dependencies: BLE_API mbed nRF51822
Fork of SDP_Version3_Abdul by
main.cpp@0:6bd1a61571d0, 2016-04-07 (annotated)
- Committer:
- Radoj
- Date:
- Thu Apr 07 16:44:08 2016 +0000
- Revision:
- 0:6bd1a61571d0
- Child:
- 2:e78a5ce9f1d7
code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Radoj | 0:6bd1a61571d0 | 1 | /* mbed Microcontroller Library |
Radoj | 0:6bd1a61571d0 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Radoj | 0:6bd1a61571d0 | 3 | * |
Radoj | 0:6bd1a61571d0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Radoj | 0:6bd1a61571d0 | 5 | * you may not use this file except in compliance with the License. |
Radoj | 0:6bd1a61571d0 | 6 | * You may obtain a copy of the License at |
Radoj | 0:6bd1a61571d0 | 7 | * |
Radoj | 0:6bd1a61571d0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Radoj | 0:6bd1a61571d0 | 9 | * |
Radoj | 0:6bd1a61571d0 | 10 | * Unless required by applicable law or agreed to in writing, software |
Radoj | 0:6bd1a61571d0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Radoj | 0:6bd1a61571d0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Radoj | 0:6bd1a61571d0 | 13 | * See the License for the specific language governing permissions and |
Radoj | 0:6bd1a61571d0 | 14 | * limitations under the License. |
Radoj | 0:6bd1a61571d0 | 15 | */ |
Radoj | 0:6bd1a61571d0 | 16 | |
Radoj | 0:6bd1a61571d0 | 17 | #include "mbed.h" |
Radoj | 0:6bd1a61571d0 | 18 | #include "ble/BLE.h" |
Radoj | 0:6bd1a61571d0 | 19 | #include "MMA8452Q.h" |
Radoj | 0:6bd1a61571d0 | 20 | #include "Service.h" |
Radoj | 0:6bd1a61571d0 | 21 | |
Radoj | 0:6bd1a61571d0 | 22 | MMA8452Q accel(P0_0, P0_1, 0x1D); |
Radoj | 0:6bd1a61571d0 | 23 | Ticker ticker; |
Radoj | 0:6bd1a61571d0 | 24 | float x,y,z; |
Radoj | 0:6bd1a61571d0 | 25 | |
Radoj | 0:6bd1a61571d0 | 26 | const static char DEVICE_NAME[] = "BLE_Accel"; |
Radoj | 0:6bd1a61571d0 | 27 | static const uint16_t uuid16_list[] = {Service::SERVICE_UUID}; |
Radoj | 0:6bd1a61571d0 | 28 | |
Radoj | 0:6bd1a61571d0 | 29 | |
Radoj | 0:6bd1a61571d0 | 30 | static Service *ServicePtr; |
Radoj | 0:6bd1a61571d0 | 31 | |
Radoj | 0:6bd1a61571d0 | 32 | void f(){ |
Radoj | 0:6bd1a61571d0 | 33 | //printf("cykabliat\n"); |
Radoj | 0:6bd1a61571d0 | 34 | |
Radoj | 0:6bd1a61571d0 | 35 | x=accel.readX(); |
Radoj | 0:6bd1a61571d0 | 36 | y=accel.readY(); |
Radoj | 0:6bd1a61571d0 | 37 | z=accel.readZ(); |
Radoj | 0:6bd1a61571d0 | 38 | |
Radoj | 0:6bd1a61571d0 | 39 | ServicePtr->updateXState(x); |
Radoj | 0:6bd1a61571d0 | 40 | ServicePtr->updateYState(y); |
Radoj | 0:6bd1a61571d0 | 41 | ServicePtr->updateZState(z); |
Radoj | 0:6bd1a61571d0 | 42 | printf("%f;%f;%f\r\n",x,y,z); |
Radoj | 0:6bd1a61571d0 | 43 | } |
Radoj | 0:6bd1a61571d0 | 44 | |
Radoj | 0:6bd1a61571d0 | 45 | |
Radoj | 0:6bd1a61571d0 | 46 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
Radoj | 0:6bd1a61571d0 | 47 | { |
Radoj | 0:6bd1a61571d0 | 48 | BLE::Instance().gap().startAdvertising(); |
Radoj | 0:6bd1a61571d0 | 49 | } |
Radoj | 0:6bd1a61571d0 | 50 | |
Radoj | 0:6bd1a61571d0 | 51 | /** |
Radoj | 0:6bd1a61571d0 | 52 | * This function is called when the ble initialization process has failled |
Radoj | 0:6bd1a61571d0 | 53 | */ |
Radoj | 0:6bd1a61571d0 | 54 | void onBleInitError(BLE &ble, ble_error_t error) |
Radoj | 0:6bd1a61571d0 | 55 | { |
Radoj | 0:6bd1a61571d0 | 56 | /* Initialization error handling should go here */ |
Radoj | 0:6bd1a61571d0 | 57 | } |
Radoj | 0:6bd1a61571d0 | 58 | |
Radoj | 0:6bd1a61571d0 | 59 | /** |
Radoj | 0:6bd1a61571d0 | 60 | * Callback triggered when the ble initialization process has finished |
Radoj | 0:6bd1a61571d0 | 61 | */ |
Radoj | 0:6bd1a61571d0 | 62 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
Radoj | 0:6bd1a61571d0 | 63 | { |
Radoj | 0:6bd1a61571d0 | 64 | BLE& ble = params->ble; |
Radoj | 0:6bd1a61571d0 | 65 | ble_error_t error = params->error; |
Radoj | 0:6bd1a61571d0 | 66 | |
Radoj | 0:6bd1a61571d0 | 67 | if (error != BLE_ERROR_NONE) { |
Radoj | 0:6bd1a61571d0 | 68 | /* In case of error, forward the error handling to onBleInitError */ |
Radoj | 0:6bd1a61571d0 | 69 | onBleInitError(ble, error); |
Radoj | 0:6bd1a61571d0 | 70 | return; |
Radoj | 0:6bd1a61571d0 | 71 | } |
Radoj | 0:6bd1a61571d0 | 72 | |
Radoj | 0:6bd1a61571d0 | 73 | /* Ensure that it is the default instance of BLE */ |
Radoj | 0:6bd1a61571d0 | 74 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
Radoj | 0:6bd1a61571d0 | 75 | return; |
Radoj | 0:6bd1a61571d0 | 76 | } |
Radoj | 0:6bd1a61571d0 | 77 | |
Radoj | 0:6bd1a61571d0 | 78 | ble.gap().onDisconnection(disconnectionCallback); |
Radoj | 0:6bd1a61571d0 | 79 | |
Radoj | 0:6bd1a61571d0 | 80 | /* Setup primary service */ |
Radoj | 0:6bd1a61571d0 | 81 | ServicePtr = new Service(ble); |
Radoj | 0:6bd1a61571d0 | 82 | |
Radoj | 0:6bd1a61571d0 | 83 | /* setup advertising */ |
Radoj | 0:6bd1a61571d0 | 84 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
Radoj | 0:6bd1a61571d0 | 85 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
Radoj | 0:6bd1a61571d0 | 86 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
Radoj | 0:6bd1a61571d0 | 87 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Radoj | 0:6bd1a61571d0 | 88 | ble.gap().setAdvertisingInterval(100); /* 1000ms. */ |
Radoj | 0:6bd1a61571d0 | 89 | ble.gap().startAdvertising(); |
Radoj | 0:6bd1a61571d0 | 90 | |
Radoj | 0:6bd1a61571d0 | 91 | } |
Radoj | 0:6bd1a61571d0 | 92 | |
Radoj | 0:6bd1a61571d0 | 93 | int main(void) |
Radoj | 0:6bd1a61571d0 | 94 | { |
Radoj | 0:6bd1a61571d0 | 95 | BLE &ble = BLE::Instance(); |
Radoj | 0:6bd1a61571d0 | 96 | ble.init(bleInitComplete); |
Radoj | 0:6bd1a61571d0 | 97 | |
Radoj | 0:6bd1a61571d0 | 98 | accel.init(); |
Radoj | 0:6bd1a61571d0 | 99 | ticker.attach(f,1); |
Radoj | 0:6bd1a61571d0 | 100 | |
Radoj | 0:6bd1a61571d0 | 101 | /* SpinWait for initialization to complete. This is necessary because the |
Radoj | 0:6bd1a61571d0 | 102 | * BLE object is used in the main loop below. */ |
Radoj | 0:6bd1a61571d0 | 103 | while (ble.hasInitialized() == false) { /* spin loop */ } |
Radoj | 0:6bd1a61571d0 | 104 | |
Radoj | 0:6bd1a61571d0 | 105 | while (true) { |
Radoj | 0:6bd1a61571d0 | 106 | ble.waitForEvent(); |
Radoj | 0:6bd1a61571d0 | 107 | } |
Radoj | 0:6bd1a61571d0 | 108 | } |