this is program how build nRF51822 to get ADXL345 data

Dependencies:   BLE_API mbed nRF51822

Fork of ADXL345_I2C by Peter Swanson

Revision:
3:1749778af065
Parent:
2:99e00e9c5035
Child:
4:a57b495be9fa
--- a/main.cpp	Sat Dec 02 00:56:53 2017 +0000
+++ b/main.cpp	Sun Dec 03 10:58:11 2017 +0000
@@ -1,10 +1,10 @@
 #include "ADXL345_I2C.h"
 #include "mbed.h"
 #include "ble/BLE.h"
+#include "ble/services/UARTService.h"
+#include "Serial.h"
 
-#include "ble/services/UARTService.h"
-
-#define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
+#define NEED_CONSOLE_OUTPUT 1 /* 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
@@ -17,7 +17,9 @@
  Serial pc(USBTX, USBRX);
  BLEDevice  ble;
  DigitalOut led1(LED1);
+ Serial uart1(USBTX,USBRX);
  UARTService *uartServicePtr;
+ uint8_t sFlag = 0;
  
  void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
@@ -26,12 +28,21 @@
     ble.startAdvertising();
 }
 
+void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
+   
+    DEBUG("Connected!\n\r");
+    
+}
+
+uint8_t b[40]={'a','d','q','w'};
 void onDataWritten(const GattWriteCallbackParams *params)
 {
     if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
         uint16_t bytesRead = params->len;
+        DEBUG("received %u bytes %s\n\r", bytesRead,params->data);
+        /*uint16_t bytesRead = params->len;
         DEBUG("received %u bytes\n\r", bytesRead);
-        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
+        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);*/
     }
 }
 
@@ -40,15 +51,31 @@
     led1 = !led1;
 }
 
+uint8_t c;
+void uartRx(void)
+{
+    
+    c = uart1.getc();
+ 
+    if(sFlag < 39)
+        b[sFlag++] = c;    
+        
+    if(c == '\r' || c == '\n' || sFlag == 15)
+    {
+        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)b/*params->data*/, sFlag/*bytesRead*/);
+        sFlag = 0;
+    }
+}
+
 
  int main() {
-       pc.baud(9600);
+     pc.baud(9600);
      int readings[3] = {0, 0, 0};
      
      pc.printf("Starting ADXL345 test...\n");
      wait(.001);
      pc.printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID());
-    wait(.001);
+     wait(.001);
     
     //Go into standby mode to configure the device.
     accelerometer.setPowerControl(0x00);
@@ -63,12 +90,15 @@
     accelerometer.setPowerControl(0x08);
     
     led1 = 1;
+    uart1.baud(9600);
     Ticker ticker;
     ticker.attach(periodicCallback, 1);
+    uart1.attach(uartRx,Serial::RxIrq);
 
     DEBUG("Initialising the nRF51822\n\r");
     ble.init();
     ble.onDisconnection(disconnectionCallback);
+    ble.onConnection(connectionCallback);
     ble.onDataWritten(onDataWritten);
 
     /* setup advertising */