this is implement of uart adxl345 ble nrf51

Dependencies:   BLE_API adxl345 mbed nRF51822

Fork of ADXL345_HelloWorld by Aaron Berk

Committer:
asyrofi
Date:
Thu Dec 07 06:31:23 2017 +0000
Revision:
1:cfb4bb8f33e9
Parent:
0:9e92575dece6
Child:
2:89f008ca5911
zxcvasdqw1;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
asyrofi 1:cfb4bb8f33e9 1 #include "mbed.h"
asyrofi 1:cfb4bb8f33e9 2 #include "ble/BLE.h"
asyrofi 1:cfb4bb8f33e9 3 #include "ble/services/UARTService.h"
asyrofi 1:cfb4bb8f33e9 4 #include "Serial.h"
aberk 0:9e92575dece6 5 #include "ADXL345.h"
asyrofi 1:cfb4bb8f33e9 6 #include "mbed.h"
asyrofi 1:cfb4bb8f33e9 7
asyrofi 1:cfb4bb8f33e9 8 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
asyrofi 1:cfb4bb8f33e9 9 * it will have an impact on code-size and power consumption. */
asyrofi 1:cfb4bb8f33e9 10
asyrofi 1:cfb4bb8f33e9 11 #if NEED_CONSOLE_OUTPUT
asyrofi 1:cfb4bb8f33e9 12 #define DEBUG(...) { printf(__VA_ARGS__); }
asyrofi 1:cfb4bb8f33e9 13 #else
asyrofi 1:cfb4bb8f33e9 14 #define DEBUG(...) /* nothing */
asyrofi 1:cfb4bb8f33e9 15 #endif /* #if NEED_CONSOLE_OUTPUT */
asyrofi 1:cfb4bb8f33e9 16
asyrofi 1:cfb4bb8f33e9 17 BLEDevice ble;
asyrofi 1:cfb4bb8f33e9 18 DigitalOut led1(LED1);
asyrofi 1:cfb4bb8f33e9 19 Serial uart1(USBTX,USBRX);
asyrofi 1:cfb4bb8f33e9 20 UARTService *uartServicePtr;
asyrofi 1:cfb4bb8f33e9 21 ADXL345 accelerometer(p5, p6, p7, p8); // (SDA, SDO, SCL, CS);
asyrofi 1:cfb4bb8f33e9 22
asyrofi 1:cfb4bb8f33e9 23 uint8_t sFlag = 0;
aberk 0:9e92575dece6 24
asyrofi 1:cfb4bb8f33e9 25 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
asyrofi 1:cfb4bb8f33e9 26 {
asyrofi 1:cfb4bb8f33e9 27 DEBUG("Disconnected!\n\r");
asyrofi 1:cfb4bb8f33e9 28 DEBUG("Restarting the advertising process\n\r");
asyrofi 1:cfb4bb8f33e9 29 ble.startAdvertising();
asyrofi 1:cfb4bb8f33e9 30 }
asyrofi 1:cfb4bb8f33e9 31
asyrofi 1:cfb4bb8f33e9 32 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
asyrofi 1:cfb4bb8f33e9 33
asyrofi 1:cfb4bb8f33e9 34 DEBUG("Connected!\n\r");
asyrofi 1:cfb4bb8f33e9 35
asyrofi 1:cfb4bb8f33e9 36 }
asyrofi 1:cfb4bb8f33e9 37
asyrofi 1:cfb4bb8f33e9 38
asyrofi 1:cfb4bb8f33e9 39 uint8_t b[40]={'a','d','q','w'};
asyrofi 1:cfb4bb8f33e9 40 void onDataWritten1(const GattWriteCallbackParams *params)
asyrofi 1:cfb4bb8f33e9 41 {
asyrofi 1:cfb4bb8f33e9 42
asyrofi 1:cfb4bb8f33e9 43
asyrofi 1:cfb4bb8f33e9 44 if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
asyrofi 1:cfb4bb8f33e9 45 uint16_t bytesRead = params->len;
asyrofi 1:cfb4bb8f33e9 46 DEBUG("received %u bytes %s\n\r", bytesRead,params->data);
asyrofi 1:cfb4bb8f33e9 47 // if(sFlag == 1)
asyrofi 1:cfb4bb8f33e9 48 // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)b/*params->data*/, 4/*bytesRead*/);
asyrofi 1:cfb4bb8f33e9 49 }
asyrofi 1:cfb4bb8f33e9 50 }
asyrofi 1:cfb4bb8f33e9 51
asyrofi 1:cfb4bb8f33e9 52
asyrofi 1:cfb4bb8f33e9 53 void periodicCallback(void)
asyrofi 1:cfb4bb8f33e9 54 {
asyrofi 1:cfb4bb8f33e9 55 led1 = !led1;
asyrofi 1:cfb4bb8f33e9 56
asyrofi 1:cfb4bb8f33e9 57 }
asyrofi 1:cfb4bb8f33e9 58
aberk 0:9e92575dece6 59
aberk 0:9e92575dece6 60 int main() {
aberk 0:9e92575dece6 61
aberk 0:9e92575dece6 62 int readings[3] = {0, 0, 0};
aberk 0:9e92575dece6 63
asyrofi 1:cfb4bb8f33e9 64 uart1.printf("Starting ADXL345 test...\n");
asyrofi 1:cfb4bb8f33e9 65 uart1.printf("Device ID is: 0x%02x\n", accelerometer.getDevId());
aberk 0:9e92575dece6 66
aberk 0:9e92575dece6 67 //Go into standby mode to configure the device.
aberk 0:9e92575dece6 68 accelerometer.setPowerControl(0x00);
aberk 0:9e92575dece6 69
aberk 0:9e92575dece6 70 //Full resolution, +/-16g, 4mg/LSB.
aberk 0:9e92575dece6 71 accelerometer.setDataFormatControl(0x0B);
aberk 0:9e92575dece6 72
aberk 0:9e92575dece6 73 //3.2kHz data rate.
aberk 0:9e92575dece6 74 accelerometer.setDataRate(ADXL345_3200HZ);
aberk 0:9e92575dece6 75
aberk 0:9e92575dece6 76 //Measurement mode.
aberk 0:9e92575dece6 77 accelerometer.setPowerControl(0x08);
asyrofi 1:cfb4bb8f33e9 78
asyrofi 1:cfb4bb8f33e9 79 led1 = 1;
asyrofi 1:cfb4bb8f33e9 80 uart1.baud(9600);
asyrofi 1:cfb4bb8f33e9 81 Ticker ticker;
asyrofi 1:cfb4bb8f33e9 82 ticker.attach(periodicCallback, 1);
asyrofi 1:cfb4bb8f33e9 83
asyrofi 1:cfb4bb8f33e9 84 DEBUG("Initialising the nRF51822\n\r");
asyrofi 1:cfb4bb8f33e9 85 ble.init();
asyrofi 1:cfb4bb8f33e9 86 ble.onDisconnection(disconnectionCallback);
asyrofi 1:cfb4bb8f33e9 87 ble.onConnection(connectionCallback);
asyrofi 1:cfb4bb8f33e9 88 ble.onDataWritten(onDataWritten1);
asyrofi 1:cfb4bb8f33e9 89
asyrofi 1:cfb4bb8f33e9 90 /* setup advertising */
asyrofi 1:cfb4bb8f33e9 91 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
asyrofi 1:cfb4bb8f33e9 92 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
asyrofi 1:cfb4bb8f33e9 93 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
asyrofi 1:cfb4bb8f33e9 94 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
asyrofi 1:cfb4bb8f33e9 95 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
asyrofi 1:cfb4bb8f33e9 96 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
asyrofi 1:cfb4bb8f33e9 97
asyrofi 1:cfb4bb8f33e9 98 ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
asyrofi 1:cfb4bb8f33e9 99 ble.startAdvertising();
asyrofi 1:cfb4bb8f33e9 100
asyrofi 1:cfb4bb8f33e9 101 UARTService uartService(ble);
asyrofi 1:cfb4bb8f33e9 102 uartServicePtr = &uartService;
aberk 0:9e92575dece6 103
aberk 0:9e92575dece6 104 while (1) {
asyrofi 1:cfb4bb8f33e9 105 ble.waitForEvent();
aberk 0:9e92575dece6 106 wait(0.1);
aberk 0:9e92575dece6 107 accelerometer.getOutput(readings);
aberk 0:9e92575dece6 108
aberk 0:9e92575dece6 109 //13-bit, sign extended values.
asyrofi 1:cfb4bb8f33e9 110 uart1.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
asyrofi 1:cfb4bb8f33e9 111 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)readings[0]/*params->data*/, sizeof(readings),false/*bytesRead*/);
asyrofi 1:cfb4bb8f33e9 112 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)readings[1]/*params->data*/, sizeof(readings),false/*bytesRead*/);
asyrofi 1:cfb4bb8f33e9 113 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)readings[2]/*params->data*/, sizeof(readings),false/*bytesRead*/);
aberk 0:9e92575dece6 114
aberk 0:9e92575dece6 115 }
aberk 0:9e92575dece6 116
aberk 0:9e92575dece6 117 }