No changes

Dependencies:   BLE_API mbed nRF51822

Fork of SDP_Version3_Abdul by Michael Galis

Committer:
Radoj
Date:
Sun Apr 10 19:05:36 2016 +0000
Revision:
3:7dc284221369
Parent:
2:e78a5ce9f1d7
Child:
4:caab577334f0
BLE Accel

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Radoj 0:6bd1a61571d0 1 #include "mbed.h"
Radoj 0:6bd1a61571d0 2 #include "ble/BLE.h"
Radoj 0:6bd1a61571d0 3 #include "MMA8452Q.h"
Radoj 0:6bd1a61571d0 4 #include "Service.h"
Radoj 0:6bd1a61571d0 5
Radoj 3:7dc284221369 6 MMA8452Q accel(P0_0, P0_1, 0x1D); //deklaracja obiektu akcelerometru (P0_0 -> SDA, P0_1 -> SCL, 0x1D -> ID urzadzenia)
Radoj 0:6bd1a61571d0 7 Ticker ticker;
Radoj 3:7dc284221369 8 float x,y,z; //zmienne do których przypisywane są wartosci odczytu x y z z akcelerometru
Radoj 0:6bd1a61571d0 9
Radoj 3:7dc284221369 10 const static char DEVICE_NAME[] = "BLE_Accel";
Radoj 0:6bd1a61571d0 11 static const uint16_t uuid16_list[] = {Service::SERVICE_UUID};
Radoj 0:6bd1a61571d0 12
Radoj 0:6bd1a61571d0 13
Radoj 0:6bd1a61571d0 14 static Service *ServicePtr;
Radoj 0:6bd1a61571d0 15
Radoj 3:7dc284221369 16 void f(){
Radoj 3:7dc284221369 17 /*
Radoj 3:7dc284221369 18 * Odczyt x y z
Radoj 3:7dc284221369 19 */
Radoj 3:7dc284221369 20 x=accel.readX();
Radoj 3:7dc284221369 21 y=accel.readY();
Radoj 3:7dc284221369 22 z=accel.readZ();
Radoj 0:6bd1a61571d0 23
Radoj 3:7dc284221369 24 /*
Radoj 3:7dc284221369 25 * Update wartosci w charakterystykach x y z i all
Radoj 3:7dc284221369 26 */
Radoj 3:7dc284221369 27 ServicePtr->updateXState(x);
Radoj 3:7dc284221369 28 ServicePtr->updateYState(y);
Radoj 3:7dc284221369 29 ServicePtr->updateZState(z);
Radoj 3:7dc284221369 30 ServicePtr->updateALLState(x,y,z);
Radoj 3:7dc284221369 31 }
Radoj 0:6bd1a61571d0 32
Radoj 0:6bd1a61571d0 33
Radoj 0:6bd1a61571d0 34 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
Radoj 0:6bd1a61571d0 35 {
Radoj 0:6bd1a61571d0 36 BLE::Instance().gap().startAdvertising();
Radoj 0:6bd1a61571d0 37 }
Radoj 0:6bd1a61571d0 38
Radoj 0:6bd1a61571d0 39 /**
Radoj 0:6bd1a61571d0 40 * This function is called when the ble initialization process has failled
Radoj 0:6bd1a61571d0 41 */
Radoj 0:6bd1a61571d0 42 void onBleInitError(BLE &ble, ble_error_t error)
Radoj 0:6bd1a61571d0 43 {
Radoj 0:6bd1a61571d0 44 /* Initialization error handling should go here */
Radoj 0:6bd1a61571d0 45 }
Radoj 0:6bd1a61571d0 46
Radoj 0:6bd1a61571d0 47 /**
Radoj 0:6bd1a61571d0 48 * Callback triggered when the ble initialization process has finished
Radoj 0:6bd1a61571d0 49 */
Radoj 0:6bd1a61571d0 50 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
Radoj 0:6bd1a61571d0 51 {
Radoj 0:6bd1a61571d0 52 BLE& ble = params->ble;
Radoj 0:6bd1a61571d0 53 ble_error_t error = params->error;
Radoj 0:6bd1a61571d0 54
Radoj 0:6bd1a61571d0 55 if (error != BLE_ERROR_NONE) {
Radoj 0:6bd1a61571d0 56 /* In case of error, forward the error handling to onBleInitError */
Radoj 0:6bd1a61571d0 57 onBleInitError(ble, error);
Radoj 0:6bd1a61571d0 58 return;
Radoj 0:6bd1a61571d0 59 }
Radoj 0:6bd1a61571d0 60
Radoj 0:6bd1a61571d0 61 /* Ensure that it is the default instance of BLE */
Radoj 0:6bd1a61571d0 62 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
Radoj 0:6bd1a61571d0 63 return;
Radoj 0:6bd1a61571d0 64 }
Radoj 0:6bd1a61571d0 65
Radoj 0:6bd1a61571d0 66 ble.gap().onDisconnection(disconnectionCallback);
Radoj 0:6bd1a61571d0 67
Radoj 0:6bd1a61571d0 68 /* Setup primary service */
Radoj 0:6bd1a61571d0 69 ServicePtr = new Service(ble);
Radoj 0:6bd1a61571d0 70
Radoj 0:6bd1a61571d0 71 /* setup advertising */
Radoj 0:6bd1a61571d0 72 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
Radoj 0:6bd1a61571d0 73 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
Radoj 0:6bd1a61571d0 74 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
Radoj 0:6bd1a61571d0 75 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Radoj 0:6bd1a61571d0 76 ble.gap().setAdvertisingInterval(100); /* 1000ms. */
Radoj 0:6bd1a61571d0 77 ble.gap().startAdvertising();
Radoj 0:6bd1a61571d0 78
Radoj 0:6bd1a61571d0 79 }
Radoj 0:6bd1a61571d0 80
Radoj 0:6bd1a61571d0 81 int main(void)
Radoj 0:6bd1a61571d0 82 {
Radoj 0:6bd1a61571d0 83 BLE &ble = BLE::Instance();
Radoj 0:6bd1a61571d0 84 ble.init(bleInitComplete);
Radoj 0:6bd1a61571d0 85
Radoj 0:6bd1a61571d0 86 accel.init();
Radoj 0:6bd1a61571d0 87 ticker.attach(f,1);
Radoj 0:6bd1a61571d0 88
Radoj 0:6bd1a61571d0 89 /* SpinWait for initialization to complete. This is necessary because the
Radoj 0:6bd1a61571d0 90 * BLE object is used in the main loop below. */
Radoj 0:6bd1a61571d0 91 while (ble.hasInitialized() == false) { /* spin loop */ }
Radoj 0:6bd1a61571d0 92
Radoj 0:6bd1a61571d0 93 while (true) {
Radoj 0:6bd1a61571d0 94 ble.waitForEvent();
Radoj 0:6bd1a61571d0 95 }
Radoj 0:6bd1a61571d0 96 }