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 14:00:40 2017 +0000
Revision:
4:a57b495be9fa
Parent:
3:1749778af065
Child:
5:8242f251bcac
mantapp

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 4:a57b495be9fa 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 3:1749778af065 53
peterswanson87 0:d0adb548714f 54
peterswanson87 0:d0adb548714f 55 int main() {
asyrofi 4:a57b495be9fa 56 uart1.baud(9600);
peterswanson87 0:d0adb548714f 57 int readings[3] = {0, 0, 0};
asyrofi 4:a57b495be9fa 58 uart1.printf("Starting ADXL345 test...\n");
peterswanson87 0:d0adb548714f 59 wait(.001);
asyrofi 4:a57b495be9fa 60 uart1.printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID());
asyrofi 3:1749778af065 61 wait(.001);
peterswanson87 0:d0adb548714f 62
asyrofi 2:99e00e9c5035 63 //Go into standby mode to configure the device.
asyrofi 2:99e00e9c5035 64 accelerometer.setPowerControl(0x00);
asyrofi 2:99e00e9c5035 65
asyrofi 2:99e00e9c5035 66 //Full resolution, +/-16g, 4mg/LSB.
asyrofi 2:99e00e9c5035 67 accelerometer.setDataFormatControl(0x0B);
asyrofi 2:99e00e9c5035 68
asyrofi 2:99e00e9c5035 69 //3.2kHz data rate.
asyrofi 2:99e00e9c5035 70 accelerometer.setDataRate(ADXL345_3200HZ);
asyrofi 2:99e00e9c5035 71
asyrofi 2:99e00e9c5035 72 //Measurement mode.
asyrofi 2:99e00e9c5035 73 accelerometer.setPowerControl(0x08);
asyrofi 2:99e00e9c5035 74
asyrofi 2:99e00e9c5035 75 led1 = 1;
asyrofi 3:1749778af065 76 uart1.baud(9600);
asyrofi 2:99e00e9c5035 77 Ticker ticker;
asyrofi 2:99e00e9c5035 78 ticker.attach(periodicCallback, 1);
asyrofi 4:a57b495be9fa 79
asyrofi 4:a57b495be9fa 80
asyrofi 2:99e00e9c5035 81 DEBUG("Initialising the nRF51822\n\r");
asyrofi 2:99e00e9c5035 82 ble.init();
asyrofi 2:99e00e9c5035 83 ble.onDisconnection(disconnectionCallback);
asyrofi 3:1749778af065 84 ble.onConnection(connectionCallback);
asyrofi 2:99e00e9c5035 85 ble.onDataWritten(onDataWritten);
asyrofi 2:99e00e9c5035 86
asyrofi 2:99e00e9c5035 87 /* setup advertising */
asyrofi 2:99e00e9c5035 88 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
asyrofi 2:99e00e9c5035 89 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
asyrofi 2:99e00e9c5035 90 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
asyrofi 2:99e00e9c5035 91 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
asyrofi 2:99e00e9c5035 92 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
asyrofi 2:99e00e9c5035 93 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
asyrofi 2:99e00e9c5035 94
asyrofi 2:99e00e9c5035 95 ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
asyrofi 2:99e00e9c5035 96 ble.startAdvertising();
asyrofi 2:99e00e9c5035 97
asyrofi 2:99e00e9c5035 98 UARTService uartService(ble);
asyrofi 2:99e00e9c5035 99 uartServicePtr = &uartService;
peterswanson87 0:d0adb548714f 100
asyrofi 4:a57b495be9fa 101 while (1)
asyrofi 4:a57b495be9fa 102 {
asyrofi 2:99e00e9c5035 103 ble.waitForEvent();
peterswanson87 0:d0adb548714f 104 wait(0.1);
peterswanson87 0:d0adb548714f 105 accelerometer.getOutput(readings);
asyrofi 4:a57b495be9fa 106 uart1.printf("%i, %i, %i\n", (uint8_t)readings[0], (uint8_t)readings[1], (uint8_t)readings[2]);
asyrofi 4:a57b495be9fa 107 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)readings[0]/*params->data*/, 4/*bytesRead*/);
asyrofi 4:a57b495be9fa 108 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)readings[1]/*params->data*/, 4/*bytesRead*/);
asyrofi 4:a57b495be9fa 109 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)readings[2]/*params->data*/, 4/*bytesRead*/);
peterswanson87 0:d0adb548714f 110 }
peterswanson87 0:d0adb548714f 111
peterswanson87 0:d0adb548714f 112 }