this is program how build nRF51822 to get ADXL345 data

Dependencies:   BLE_API mbed nRF51822

Fork of ADXL345_I2C by Peter Swanson

Revision:
2:99e00e9c5035
Parent:
0:d0adb548714f
Child:
3:1749778af065
--- a/main.cpp	Thu May 12 01:19:36 2011 +0000
+++ b/main.cpp	Sat Dec 02 00:56:53 2017 +0000
@@ -1,10 +1,48 @@
 #include "ADXL345_I2C.h"
+#include "mbed.h"
+#include "ble/BLE.h"
+
+#include "ble/services/UARTService.h"
+
+#define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
+                               * it will have an impact on code-size and power consumption. */
+
+#if NEED_CONSOLE_OUTPUT
+#define DEBUG(...) { printf(__VA_ARGS__); }
+#else
+#define DEBUG(...) /* nothing */
+#endif /* #if NEED_CONSOLE_OUTPUT */
 
- ADXL345_I2C accelerometer(p28, p27);
+ ADXL345_I2C accelerometer(p7, p30);
  Serial pc(USBTX, USBRX);
+ BLEDevice  ble;
+ DigitalOut led1(LED1);
+ UARTService *uartServicePtr;
+ 
+ void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+    DEBUG("Disconnected!\n\r");
+    DEBUG("Restarting the advertising process\n\r");
+    ble.startAdvertising();
+}
+
+void onDataWritten(const GattWriteCallbackParams *params)
+{
+    if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
+        uint16_t bytesRead = params->len;
+        DEBUG("received %u bytes\n\r", bytesRead);
+        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
+    }
+}
+
+void periodicCallback(void)
+{
+    led1 = !led1;
+}
+
 
  int main() {
-       pc.baud(115200);
+       pc.baud(9600);
      int readings[3] = {0, 0, 0};
      
      pc.printf("Starting ADXL345 test...\n");
@@ -12,38 +50,46 @@
      pc.printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID());
     wait(.001);
     
-     // These are here to test whether any of the initialization fails. It will print the failure
-    if (accelerometer.setPowerControl(0x00)){
-         pc.printf("didn't intitialize power control\n"); 
-         return 0;  }
-     //Full resolution, +/-16g, 4mg/LSB.
-     wait(.001);
-     
-     if(accelerometer.setDataFormatControl(0x0B)){
-        pc.printf("didn't set data format\n");
-        return 0;  }
-     wait(.001);
-     
-     //3.2kHz data rate.
-     if(accelerometer.setDataRate(ADXL345_3200HZ)){
-        pc.printf("didn't set data rate\n");
-        return 0;    }
-     wait(.001);
-     
-     //Measurement mode.
-     
-     if(accelerometer.setPowerControl(MeasurementMode)) {
-        pc.printf("didn't set the power control to measurement\n"); 
-        return 0;   } 
+    //Go into standby mode to configure the device.
+    accelerometer.setPowerControl(0x00);
+
+    //Full resolution, +/-16g, 4mg/LSB.
+    accelerometer.setDataFormatControl(0x0B);
+    
+    //3.2kHz data rate.
+    accelerometer.setDataRate(ADXL345_3200HZ);
+
+    //Measurement mode.
+    accelerometer.setPowerControl(0x08);
+    
+    led1 = 1;
+    Ticker ticker;
+    ticker.attach(periodicCallback, 1);
+
+    DEBUG("Initialising the nRF51822\n\r");
+    ble.init();
+    ble.onDisconnection(disconnectionCallback);
+    ble.onDataWritten(onDataWritten);
+
+    /* setup advertising */
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
+    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
+                                     (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
+                                     (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
+
+    ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
+    ble.startAdvertising();
+
+    UARTService uartService(ble);
+    uartServicePtr = &uartService;
  
      while (1) {
-     
+         ble.waitForEvent();
          wait(0.1);
-         
          accelerometer.getOutput(readings);
-         
-
-        pc.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
+         pc.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
      }
  
  }