Na najnowszym NRF lib

Dependencies:   BLE_API MMA8452Q mbed nRF51822

Fork of AAAatest_copy by Praktyki

Committer:
Radoj
Date:
Sun Apr 24 17:36:57 2016 +0000
Revision:
3:0f080eec3f0c
Parent:
2:e78a5ce9f1d7
Ta jest na tych najnowszych

Who changed what in which revision?

UserRevisionLine numberNew 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 2:e78a5ce9f1d7 42 ServicePtr->updateALLState(x,y,z);
Radoj 2:e78a5ce9f1d7 43 printf("%f;%f;%f; \r\n",x,y,z);
Radoj 0:6bd1a61571d0 44 }
Radoj 0:6bd1a61571d0 45
Radoj 0:6bd1a61571d0 46
Radoj 0:6bd1a61571d0 47 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
Radoj 0:6bd1a61571d0 48 {
Radoj 0:6bd1a61571d0 49 BLE::Instance().gap().startAdvertising();
Radoj 0:6bd1a61571d0 50 }
Radoj 0:6bd1a61571d0 51
Radoj 0:6bd1a61571d0 52 /**
Radoj 0:6bd1a61571d0 53 * This function is called when the ble initialization process has failled
Radoj 0:6bd1a61571d0 54 */
Radoj 0:6bd1a61571d0 55 void onBleInitError(BLE &ble, ble_error_t error)
Radoj 0:6bd1a61571d0 56 {
Radoj 0:6bd1a61571d0 57 /* Initialization error handling should go here */
Radoj 0:6bd1a61571d0 58 }
Radoj 0:6bd1a61571d0 59
Radoj 0:6bd1a61571d0 60 /**
Radoj 0:6bd1a61571d0 61 * Callback triggered when the ble initialization process has finished
Radoj 0:6bd1a61571d0 62 */
Radoj 0:6bd1a61571d0 63 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
Radoj 0:6bd1a61571d0 64 {
Radoj 0:6bd1a61571d0 65 BLE& ble = params->ble;
Radoj 0:6bd1a61571d0 66 ble_error_t error = params->error;
Radoj 0:6bd1a61571d0 67
Radoj 0:6bd1a61571d0 68 if (error != BLE_ERROR_NONE) {
Radoj 0:6bd1a61571d0 69 /* In case of error, forward the error handling to onBleInitError */
Radoj 0:6bd1a61571d0 70 onBleInitError(ble, error);
Radoj 0:6bd1a61571d0 71 return;
Radoj 0:6bd1a61571d0 72 }
Radoj 0:6bd1a61571d0 73
Radoj 0:6bd1a61571d0 74 /* Ensure that it is the default instance of BLE */
Radoj 0:6bd1a61571d0 75 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
Radoj 0:6bd1a61571d0 76 return;
Radoj 0:6bd1a61571d0 77 }
Radoj 0:6bd1a61571d0 78
Radoj 0:6bd1a61571d0 79 ble.gap().onDisconnection(disconnectionCallback);
Radoj 0:6bd1a61571d0 80
Radoj 0:6bd1a61571d0 81 /* Setup primary service */
Radoj 0:6bd1a61571d0 82 ServicePtr = new Service(ble);
Radoj 0:6bd1a61571d0 83
Radoj 0:6bd1a61571d0 84 /* setup advertising */
Radoj 0:6bd1a61571d0 85 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
Radoj 0:6bd1a61571d0 86 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
Radoj 0:6bd1a61571d0 87 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
Radoj 0:6bd1a61571d0 88 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Radoj 0:6bd1a61571d0 89 ble.gap().setAdvertisingInterval(100); /* 1000ms. */
Radoj 0:6bd1a61571d0 90 ble.gap().startAdvertising();
Radoj 0:6bd1a61571d0 91
Radoj 0:6bd1a61571d0 92 }
Radoj 0:6bd1a61571d0 93
Radoj 0:6bd1a61571d0 94 int main(void)
Radoj 0:6bd1a61571d0 95 {
Radoj 0:6bd1a61571d0 96 BLE &ble = BLE::Instance();
Radoj 0:6bd1a61571d0 97 ble.init(bleInitComplete);
Radoj 0:6bd1a61571d0 98
Radoj 0:6bd1a61571d0 99 accel.init();
Radoj 0:6bd1a61571d0 100 ticker.attach(f,1);
Radoj 0:6bd1a61571d0 101
Radoj 0:6bd1a61571d0 102 /* SpinWait for initialization to complete. This is necessary because the
Radoj 0:6bd1a61571d0 103 * BLE object is used in the main loop below. */
Radoj 0:6bd1a61571d0 104 while (ble.hasInitialized() == false) { /* spin loop */ }
Radoj 0:6bd1a61571d0 105
Radoj 0:6bd1a61571d0 106 while (true) {
Radoj 0:6bd1a61571d0 107 ble.waitForEvent();
Radoj 0:6bd1a61571d0 108 }
Radoj 0:6bd1a61571d0 109 }