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 10:38:26 2017 +0000
Revision:
2:89f008ca5911
Parent:
1:cfb4bb8f33e9
Child:
3:3ee58f31a637
coba aja lagi

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