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:
Mon Dec 04 15:11:37 2017 +0000
Revision:
5:8242f251bcac
Parent:
4:a57b495be9fa
Child:
6:bc835d0f686f
sipp

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 4:a57b495be9fa 16 ADXL345_I2C accelerometer(p30, p7);
asyrofi 2:99e00e9c5035 17 BLEDevice ble;
asyrofi 2:99e00e9c5035 18 DigitalOut led1(LED1);
asyrofi 3:1749778af065 19 Serial uart1(USBTX,USBRX);
asyrofi 2:99e00e9c5035 20 UARTService *uartServicePtr;
asyrofi 5:8242f251bcac 21 uint8_t sFlag = 0;
asyrofi 2:99e00e9c5035 22
asyrofi 2:99e00e9c5035 23 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
asyrofi 2:99e00e9c5035 24 {
asyrofi 2:99e00e9c5035 25 DEBUG("Disconnected!\n\r");
asyrofi 2:99e00e9c5035 26 DEBUG("Restarting the advertising process\n\r");
asyrofi 2:99e00e9c5035 27 ble.startAdvertising();
asyrofi 2:99e00e9c5035 28 }
asyrofi 2:99e00e9c5035 29
asyrofi 3:1749778af065 30 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
asyrofi 3:1749778af065 31
asyrofi 3:1749778af065 32 DEBUG("Connected!\n\r");
asyrofi 3:1749778af065 33
asyrofi 3:1749778af065 34 }
asyrofi 3:1749778af065 35
asyrofi 3:1749778af065 36 uint8_t b[40]={'a','d','q','w'};
asyrofi 2:99e00e9c5035 37 void onDataWritten(const GattWriteCallbackParams *params)
asyrofi 2:99e00e9c5035 38 {
asyrofi 2:99e00e9c5035 39 if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
asyrofi 2:99e00e9c5035 40 uint16_t bytesRead = params->len;
asyrofi 3:1749778af065 41 DEBUG("received %u bytes %s\n\r", bytesRead,params->data);
asyrofi 3:1749778af065 42 /*uint16_t bytesRead = params->len;
asyrofi 2:99e00e9c5035 43 DEBUG("received %u bytes\n\r", bytesRead);
asyrofi 3:1749778af065 44 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);*/
asyrofi 2:99e00e9c5035 45 }
asyrofi 2:99e00e9c5035 46 }
asyrofi 2:99e00e9c5035 47
asyrofi 2:99e00e9c5035 48 void periodicCallback(void)
asyrofi 2:99e00e9c5035 49 {
asyrofi 2:99e00e9c5035 50 led1 = !led1;
asyrofi 2:99e00e9c5035 51 }
asyrofi 2:99e00e9c5035 52
asyrofi 5:8242f251bcac 53 uint8_t c;
asyrofi 5:8242f251bcac 54 void uartRx(void)
asyrofi 5:8242f251bcac 55 {
asyrofi 5:8242f251bcac 56
asyrofi 5:8242f251bcac 57 c = uart1.getc();
asyrofi 5:8242f251bcac 58
asyrofi 5:8242f251bcac 59 if(sFlag < 39)
asyrofi 5:8242f251bcac 60 b[sFlag++] = c;
asyrofi 5:8242f251bcac 61
asyrofi 5:8242f251bcac 62 if(c == '\r' || c == '\n' || sFlag == 15)
asyrofi 5:8242f251bcac 63 {
asyrofi 5:8242f251bcac 64 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)b/*params->data*/, sFlag/*bytesRead*/);
asyrofi 5:8242f251bcac 65 sFlag = 0;
asyrofi 5:8242f251bcac 66 }
asyrofi 5:8242f251bcac 67 }
asyrofi 5:8242f251bcac 68
asyrofi 3:1749778af065 69
peterswanson87 0:d0adb548714f 70
peterswanson87 0:d0adb548714f 71 int main() {
asyrofi 4:a57b495be9fa 72 uart1.baud(9600);
peterswanson87 0:d0adb548714f 73 int readings[3] = {0, 0, 0};
asyrofi 4:a57b495be9fa 74 uart1.printf("Starting ADXL345 test...\n");
peterswanson87 0:d0adb548714f 75 wait(.001);
asyrofi 4:a57b495be9fa 76 uart1.printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID());
asyrofi 3:1749778af065 77 wait(.001);
peterswanson87 0:d0adb548714f 78
asyrofi 2:99e00e9c5035 79 //Go into standby mode to configure the device.
asyrofi 2:99e00e9c5035 80 accelerometer.setPowerControl(0x00);
asyrofi 2:99e00e9c5035 81
asyrofi 2:99e00e9c5035 82 //Full resolution, +/-16g, 4mg/LSB.
asyrofi 2:99e00e9c5035 83 accelerometer.setDataFormatControl(0x0B);
asyrofi 2:99e00e9c5035 84
asyrofi 2:99e00e9c5035 85 //3.2kHz data rate.
asyrofi 2:99e00e9c5035 86 accelerometer.setDataRate(ADXL345_3200HZ);
asyrofi 2:99e00e9c5035 87
asyrofi 2:99e00e9c5035 88 //Measurement mode.
asyrofi 2:99e00e9c5035 89 accelerometer.setPowerControl(0x08);
asyrofi 2:99e00e9c5035 90
asyrofi 2:99e00e9c5035 91 led1 = 1;
asyrofi 3:1749778af065 92 uart1.baud(9600);
asyrofi 2:99e00e9c5035 93 Ticker ticker;
asyrofi 2:99e00e9c5035 94 ticker.attach(periodicCallback, 1);
asyrofi 5:8242f251bcac 95 uart1.attach(uartRx,Serial::RxIrq);
asyrofi 4:a57b495be9fa 96
asyrofi 4:a57b495be9fa 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
asyrofi 4:a57b495be9fa 118 while (1)
asyrofi 4:a57b495be9fa 119 {
asyrofi 2:99e00e9c5035 120 ble.waitForEvent();
peterswanson87 0:d0adb548714f 121 wait(0.1);
peterswanson87 0:d0adb548714f 122 accelerometer.getOutput(readings);
asyrofi 5:8242f251bcac 123 uart1.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
asyrofi 5:8242f251bcac 124 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)readings[0]/*params->data*/, sizeof(readings),false/*bytesRead*/);
asyrofi 5:8242f251bcac 125 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)readings[1]/*params->data*/, sizeof(readings),false/*bytesRead*/);
asyrofi 5:8242f251bcac 126 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)readings[2]/*params->data*/, sizeof(readings),false/*bytesRead*/);
peterswanson87 0:d0adb548714f 127 }
peterswanson87 0:d0adb548714f 128
peterswanson87 0:d0adb548714f 129 }