0

Bear with me as I'm new to C++, pointers, etc..

I'm sending over raw image data to a microcontroller using Arduino via BLE.

Currently, I'm using JS to send the raw data over as an ArrayBuffer. However (surprisingly), it looks like I can only receive the data on the Arduino side as a String and not raw Bytes.

I verified this by looking at the param the onWrite cb takes. It takes a BLECharacteristic Object. Doc here BLECharacteristic doesn't show any instance methods or anything to receive data...just the getValue fn which returns a String. Printing this String out on Serial shows weird symbols..guessing just something similar to ArrayBuffer.toString()...?

I'd then like to convert this String data to a Byte array so that I can display it on an epaper display.

Here is my BLE onWrite cb.

class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
    std::string value = pCharacteristic->getValue();

    int n = value.length();

    char const *cstr = new char [value.length()+1];
    std::strcpy (cstr, value.c_str());

    display.drawBitmap(0,0,*c,296,128,EPD_BLACK);
  }
}};

This doesn't work and I get invalid conversion from 'const char*' to 'char*'

Note: The format the drawBitmap function is expecting is const uint8_t (byte array). drawBitmap docs

However, I've gotten this to work by using a hardcoded array in the format of

const unsigned char foo [] = {
    0xff, 0xff, 0xff, 0xff, 0xff
};

So I'm confused on the difference between const char [], const uint8_t, and byte array. May someone please explain the differences? Then - how can I convert my String of raw data into that the drawBitmap fn is expecting?

user2402616
  • 1,434
  • 4
  • 22
  • 38

1 Answers1

1

Seems to be quite simple, std::strcpy() needs a pointer to writable (not const) memory, therefor the pointer cstr may not point to const char, leave out const and the following should work:

char *cstr = new char [value.length()+1];
std::strcpy (cstr, value.c_str());

If you feel fancy, I believe you could use a const pointer to char, meaning the content can be changed but not the pointer itself, this however seems unnecessary:

char * const cstr = new char [value.length()+1];
std::strcpy (cstr, value.c_str());
ZwergofPhoenix
  • 141
  • 1
  • 6
  • Hi! This solves my first error, but now I get `no matching function for call to 'drawBitmap(int, int, char&, int, int, )'` http://adafruit.github.io/Adafruit-GFX-Library/html/class_adafruit___g_f_x.html#a805a15f1b3ea9eff5d1666b8e6db1c56 shows that the fn expects `const uint8_t bitmap[]` type. and something in format of `const unsigned char foo [] = { 0xff, 0xff, 0xff, 0xff, 0xff };` does work. How can I convert what you have above into something the fn will take? Thanks! – user2402616 Aug 24 '22 at 19:26
  • The only problem seems to be that you just need cstr to be unsigned: `unsigned char *cstr = new char [value.length()+1]; std::strcpy (cstr, value.c_str());` – ZwergofPhoenix Aug 25 '22 at 10:45
  • 1
    The above comment might cause problems with `strcpy()`, if that is the case I would cast in the `drawBitmap()` call: `display.drawBitmap(0,0,(unsinged char *)cstr,296,128,EPD_BLACK);` – ZwergofPhoenix Aug 25 '22 at 10:50
  • The casting part worked. Ty! – user2402616 Aug 26 '22 at 16:16