this is program how build nRF51822 to get ADXL345 data

Dependencies:   BLE_API mbed nRF51822

Fork of ADXL345_I2C by Peter Swanson

Committer:
asyrofi
Date:
Sun Dec 03 10:58:11 2017 +0000
Revision:
3:1749778af065
Parent:
2:99e00e9c5035
Child:
4:a57b495be9fa
have been revision using pc console

Who changed what in which revision?

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