C/C++ Question need help with wrapping a pointer

Siam

Active Member
Licensed User
Longtime User
hello,

I've been stuck for a few days and i cant find the right solution

i need to wrap this :

B4X:
   * Load a character from the font data into a user buffer.
   *
   * Copy the bitmap for a library font character (current font set by setFont()) and
   * return it in the data area passed by the user. If the user buffer is not large
   * enough, only the first size elements are copied to the buffer.
   *
   * NOTE: This function is only available if the library defined value
   * USE_LOCAL_FONT is set to 1.
   *
   * \param c     the character to retrieve.
   * \param size  the size of the user buffer in unit8_t units.
   * \param buf   address of the user buffer supplied.
   * \return width (in columns) of the character, 0 if parameter errors.
   */
uint8_t getChar(uint8_t c, uint8_t size, uint8_t *buf);

This is the part from the demo code

B4X:
void scrollText(char *p)
{
  uint8_t charWidth;
  uint8_t cBuf[8];  // this should be ok for all built-in fonts

  PRINTS("\nScrolling text");
  mx.clear();

  while (*p != '\0')
  {
    charWidth = mx.getChar(*p++, sizeof(cBuf) / sizeof(cBuf[0]), cBuf);

    for (uint8_t i=0; i<=charWidth; i++)    // allow space between characters
    {
      mx.transform(MD_MAX72XX::TSL);
      if (i < charWidth)
        mx.setColumn(0, cBuf[i]);
      delay(DELAYTIME);
    }
  }
}


what i know is that
*p = a single char
sizeof(cBuf) / sizeof(cBuf[0])= buffer size
cBuf = i dont know from where comes this information

i dont know if i m right:

from b4j side i have to create a byte array and i send every single char to this function but how i came to the cbuf ?

there are other types of data except:
uint8_t is the same as Byte.
uint16_t is the same as UInt.
uint8_t* is the same as ?????

I can not get rid of this error:
error: invalid conversion from 'byte {aka unsigned char}' to 'uint8_t* {aka unsigned char*}'

regards

Andy
 
Last edited:

zero9178

New Member
cBuf is the buffer that is written to by the getChar method. With sizeof(cBuf)/sizeof(cBuf[0]) you specified the size of this buffer, which is needed so that getChar does not write passed the buffer. uint8_t is an 8 bit integer, uint16_t is a 16 bit integer. uint8_t* is a Pointer to an 8 bit integer. I don't know where the error you have is coming from as as far as I can see it can't be coming from any of the code above but judging by the error message you just need to put a & infront of the variable you are passing, to take the address of the variable, not the variable itsself (therefore creating a pointer to your variable aka a uint8_t*)
 

Siam

Active Member
Licensed User
Longtime User
so far i have this understood but my problem is how i can wrap this funktion:

charWidth = mx.getChar(*p++, sizeof(cBuf) / sizeof(cBuf[0]), cBuf);

my idea for the b4a code:

B4X:
Dim lauftext() As Byte = "hallo welt"
Dim charwidth As Byte
Dim cbuf(8) As Byte

cbuf = how i get the pointer from the cbuf?

led.getchar(lauftext(t), sizeof(cBuf) / sizeof(cBuf[0]),cbuf)
...
...
...

my idea for the wrapper in the .h filde:
B4X:
byte getChar(byte c, byte size, byte *buf);

my idea for the wrapper in the .cpp filde:

B4X:
byte B4RMDMAX72xxLedMatrix::getChar(byte c, byte size, byte *buf){
return led->getChar(c, size, *buf);
 }
 

Siam

Active Member
Licensed User
Longtime User
Hello,

first a small demo about this library all you can see is programmed with b4r


to finalize this library i need realy help i find no solution !:(:(:(


here is my B4r Code (im not shure but i think here is my thinking error)

B4X:
Sub scrolltext(unused As Byte)
    Dim charWidth As Byte
    Dim buf(9) As Byte

   mdledmatrix.clear() ' clear all  display segments
   
    Dim ledtext() As Byte="hallo da draussen 1 2 3 4 5 6 7 8 9 0         " ' scrolltext
    For t= 0 To ledtext.Length-1

        charWidth = mdledmatrix.getChar1(ledtext(t),8,buf(0)) '<- i think this part is wrong
        For i =0 To charWidth
            mdledmatrix.transform("TSL")
            If i < charWidth Then

                mdledmatrix.setColum(0,buf(0))
            End If
            mdledmatrix.wait (100)
        Next
    Next

here the wrapped code from my .h file

B4X:
byte getChar1(byte c, byte size, byte buf);

and the wrapped code from my .cpp file

B4X:
    byte B4RMDMAX72xxLedMatrix::getChar1(byte c, byte size, byte buf){
    return led->getChar(c, size, &buf);
    }

here the code from the lib.h

B4X:
   * Load a character from the font data into a user buffer.
   *
   * Copy the bitmap for a library font character (current font set by setFont()) and
   * return it in the data area passed by the user. If the user buffer is not large
   * enough, only the first size elements are copied to the buffer.
   *
   * NOTE: This function is only available if the library defined value
   * USE_LOCAL_FONT is set to 1.
   *
   * \param c     the character to retrieve.
   * \param size  the size of the user buffer in unit8_t units.
   * \param buf   address of the user buffer supplied.
   * \return width (in columns) of the character, 0 if parameter errors.
   */
uint8_t getChar(uint8_t c, uint8_t size, uint8_t *buf);

and the lib.cpp

B4X:
uint8_t MD_MAX72XX::getChar(uint8_t c, uint8_t size, uint8_t *buf)
{
  PRINT("\ngetChar: '", (char)c);
  PRINT("' ASC ", c);
  PRINT(" - bufsize ", size);

  if (buf == nullptr)
    return(0);

  int16_t offset = getFontCharOffset(c);
  if (offset == -1)
  {
    memset(buf, 0, size);
    size = 0;
  }
  else
  {
    size = min(size, pgm_read_byte(_fontData+offset));

    offset++; // skip the size byte

    for (uint8_t i=0; i<size; i++)
      *buf++ = pgm_read_byte(_fontData+offset+i);
  }
 
  return(size);
}


please help me
 
Top