This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

how to convert 2's complement ADXL345 data to nRF51 UART? needed help

finally I have done to implement that ADXL345 can throw data over BLE using nRF UART app service with this code. now my problem is how to change 2's complement ADXL345 data to array of string/character.

here it is the data using nRF51 uart app uart app

so how to fix random character to array of string number. like the number of sensor measurenment.

here this code:

#include "ADXL345_I2C.h"
#include "mbed.h"
#include "ble/BLE.h"
#include "ble/services/UARTService.h"
#include "Serial.h"

#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
                       * it will have an impact on code-size and power consumption. */

#if NEED_CONSOLE_OUTPUT
#define DEBUG(...) { printf(__VA_ARGS__); }
#else
#define DEBUG(...) /* nothing */
#endif /* #if NEED_CONSOLE_OUTPUT */

ADXL345_I2C accelerometer(p30, p7);
BLEDevice  ble;
DigitalOut led1(LED1);
Serial uart1(USBTX,USBRX);
UARTService *uartServicePtr;


void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{
  DEBUG("Disconnected!\n\r");
  DEBUG("Restarting the advertising process\n\r");
  ble.startAdvertising();
}

void connectionCallback(const Gap::ConnectionCallbackParams_t *params) 
 {

   DEBUG("Connected!\n\r");

 }

  uint8_t b[40]={'a','d','q','w'};
  void onDataWritten(const GattWriteCallbackParams *params)
  {
    if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) 
    {
     uint16_t bytesRead = params->len;
     DEBUG("received %u bytes %s\n\r", bytesRead,params->data);
     /*uint16_t bytesRead = params->len;
     DEBUG("received %u bytes\n\r", bytesRead);
     ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);*/
    }
}

  void periodicCallback(void)
  {
     led1 = !led1;
  }

 
    int main() {
    uart1.baud(9600);
    int readings[3] = {0, 0, 0};

    pc.printf("Starting ADXL345 test...\n");
    wait(.001);
    pc.printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID());
    wait(.001);

    //Go into standby mode to configure the device.
    accelerometer.setPowerControl(0x00);

   //Full resolution, +/-16g, 4mg/LSB.
   accelerometer.setDataFormatControl(0x0B);

   //3.2kHz data rate.
   accelerometer.setDataRate(ADXL345_3200HZ);

   //Measurement mode.
   accelerometer.setPowerControl(0x08);

   led1 = 1;
   uart1.baud(9600);
   Ticker ticker;
   ticker.attach(periodicCallback, 1);
   
   DEBUG("Initialising the nRF51822\n\r");
   ble.init();
   ble.onDisconnection(disconnectionCallback);
   ble.onConnection(connectionCallback);
   ble.onDataWritten(onDataWritten);

   /* setup advertising */
   ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
   ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
   ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
                             (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
   ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
                             (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));

   ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
   ble.startAdvertising();

   UARTService uartService(ble);
   uartServicePtr = &uartService;

    while (1) 
    {
      ble.waitForEvent();
      wait(0.1);
      accelerometer.getOutput(readings);
      uart1.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
      ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)readings[0], sizeof(readings),false);
      ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)readings[1], sizeof(readings),false);
      ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)readings[2], sizeof(readings),false);
    }

  }
  • You can fill a char* buffer using sprintf(), then send this buffer via nus

  • thank you for your answer, can you explained with the code please.. where is should I take fill a char buffer using sprint to code..

    are take it to here?

     while (1) 
    {
      ble.waitForEvent();
      wait(0.1);
      accelerometer.getOutput(readings);
      uart1.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
      ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)readings[0], sizeof(readings),false);
      ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)readings[1], sizeof(readings),false);
      ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t*)readings[2], sizeof(readings),false);
    }
    

    because send over BLE should have method to change 2's complement ADXL345 (16 bit) to nrf51 (10 bit) that change that complement to string of array. I still confuse

  • char tests[BUFFER_LEN] = {0}

    snprintf((char *)tests,BUFFER_LEN,"%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);     
    

    send a tests array with your function as 20 B chunks

  • still error bro, can you make it code simple.. write your example code that fix char buffer..

  • Try something like this:

     char buffer [20];
    
     // Test data
     memset(&buffer, 0, sizeof(buffer));
    int16_t reading_1 = 54;
    int16_t reading_2 =42;
    int16_t reading_3 = 32;
    snprintf(buffer,20 ,"data: %d, %d, %d\n", reading_1, reading_2, reading_3);
     
     while (1) 
     {
         ble.waitForEvent();
         wait(0.1);
         accelerometer.getOutput(readings);
         uart1.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
         
         memset(&buffer, 0, sizeof(buffer));
         snprintf(buffer,20 ,"data: %d, %d, %d\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
    
     ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t*)buffer,sizeof(buffer),false/*bytesRead*/);
    
    }
    
Related