this is implement of uart adxl345 ble nrf51

Dependencies:   BLE_API adxl345 mbed nRF51822

Fork of ADXL345_HelloWorld by Aaron Berk

Files at this revision

API Documentation at this revision

Comitter:
asyrofi
Date:
Thu Dec 07 06:31:23 2017 +0000
Parent:
0:9e92575dece6
Child:
2:89f008ca5911
Commit message:
zxcvasdqw1;

Changed in this revision

ADXL345_lib.lib Show annotated file Show diff for this revision Revisions of this file
BLE_API.lib Show annotated file Show diff for this revision Revisions of this file
adxl345.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
nRF51822.lib Show annotated file Show diff for this revision Revisions of this file
--- a/ADXL345_lib.lib	Tue Aug 03 08:31:00 2010 +0000
+++ b/ADXL345_lib.lib	Thu Dec 07 06:31:23 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/aberk/programs/ADXL345_lib/latest
\ No newline at end of file
+http://mbed.org
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BLE_API.lib	Thu Dec 07 06:31:23 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#d494ad3e87bd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/adxl345.lib	Thu Dec 07 06:31:23 2017 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/asyrofi/code/adxl345/#cd40b5e776b0
--- a/main.cpp	Tue Aug 03 08:31:00 2010 +0000
+++ b/main.cpp	Thu Dec 07 06:31:23 2017 +0000
@@ -1,14 +1,68 @@
+#include "mbed.h"
+#include "ble/BLE.h"
+#include "ble/services/UARTService.h"
+#include "Serial.h"
 #include "ADXL345.h"
+#include "mbed.h"
+
+#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
+#define DEBUG(...) { printf(__VA_ARGS__); }
+#else
+#define DEBUG(...) /* nothing */
+#endif /* #if NEED_CONSOLE_OUTPUT */
+
+BLEDevice  ble;
+DigitalOut led1(LED1);
+Serial uart1(USBTX,USBRX);
+UARTService *uartServicePtr;
+ADXL345 accelerometer(p5, p6, p7, p8); // (SDA, SDO, SCL, CS);
+
+uint8_t sFlag = 0;
 
-ADXL345 accelerometer(p5, p6, p7, p8);
-Serial pc(USBTX, USBRX);
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+    DEBUG("Disconnected!\n\r");
+    DEBUG("Restarting the advertising process\n\r");
+    ble.startAdvertising();
+}
+
+void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
+   
+    DEBUG("Connected!\n\r");
+    
+}
+
+
+uint8_t b[40]={'a','d','q','w'};
+void onDataWritten1(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);
+       // if(sFlag == 1)
+       //     ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)b/*params->data*/, 4/*bytesRead*/);
+    }
+}
+
+
+void periodicCallback(void)
+{
+    led1 = !led1;
+    
+}
+
 
 int main() {
 
     int readings[3] = {0, 0, 0};
     
-    pc.printf("Starting ADXL345 test...\n");
-    pc.printf("Device ID is: 0x%02x\n", accelerometer.getDevId());
+    uart1.printf("Starting ADXL345 test...\n");
+    uart1.printf("Device ID is: 0x%02x\n", accelerometer.getDevId());
 
     //Go into standby mode to configure the device.
     accelerometer.setPowerControl(0x00);
@@ -21,15 +75,42 @@
 
     //Measurement mode.
     accelerometer.setPowerControl(0x08);
+    
+    led1 = 1;
+    uart1.baud(9600);
+    Ticker ticker;
+    ticker.attach(periodicCallback, 1);
+    
+    DEBUG("Initialising the nRF51822\n\r");
+    ble.init();
+    ble.onDisconnection(disconnectionCallback);
+    ble.onConnection(connectionCallback);
+    ble.onDataWritten(onDataWritten1);
+
+    /* 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);
         
         //13-bit, sign extended values.
-        pc.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
+        uart1.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
+        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)readings[0]/*params->data*/, sizeof(readings),false/*bytesRead*/);
+        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)readings[1]/*params->data*/, sizeof(readings),false/*bytesRead*/);
+        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)readings[2]/*params->data*/, sizeof(readings),false/*bytesRead*/);
 
     }
 
--- a/mbed.bld	Tue Aug 03 08:31:00 2010 +0000
+++ b/mbed.bld	Thu Dec 07 06:31:23 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/9114680c05da
+http://mbed.org/users/mbed_official/code/mbed/builds/34e6b704fe68
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nRF51822.lib	Thu Dec 07 06:31:23 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#088f5738bf18