Search code examples
carduinolcduint8tuint16

C - Arduino - cannot convert 'uint8_t*' to 'uint16_t*'


I am fairly new to C / Arduino

I am trying to simplify my code so that when I am writing my loop I will only require one function.

The code is for an SPI LCD display, i have two codes that I want to combine into one, the first code is to write 32 chars of txt to the screen, picking the 32chars from an array - this works perfect

void writeLCD(char LcdText[33]){
unsigned char TextRow, CharacterRow, TextColumn, CharNum ;
unsigned int InPointerString ; 
    InPointerString = 1 ;
    SPI.transfer(0x48) ; 
    SPI.transfer(0);SPI.transfer(0);SPI.transfer(0);
    for(TextColumn = 8 ; TextColumn > 0 ; TextColumn--){
        for(CharacterRow = 8 ; CharacterRow > 0 ; CharacterRow --){
            for(TextRow = 0 ; TextRow < 4; TextRow++){
                CharNum = LcdText[TextRow*8 + TextColumn-1] ;
                if(CharNum > 0x7F){
                    SPI.transfer(Font8x8[(CharNum-0x80)*8 + CharacterRow - 1] & 0x0F ) ;
                    SPI.transfer(((Font8x8[(CharNum-0x80)*8 + CharacterRow - 1] & 0xF0) >> 4) & 0x0f   ) ;
                } else {
                    SPI.transfer(Font8x8[CharNum*8 + CharacterRow - 1] & 0x0F ) ;
                    SPI.transfer(((Font8x8[CharNum*8 + CharacterRow - 1] & 0xF0) >> 4) & 0x0f   ) ;
                }
            }
        }
    }
    SPI.transfer(0x43) ;
    delay(1);
}

The second code is to pick 512 bytes from an array and write an image to the screen - this works perfect

// Data is a pointer to 512 bytes of image data to display
void BMP(uint16_t * data){
uint16_t i;
SPI.transfer(0x49); // Start Bitmap CMD
for (i=0; i<512; i++){
SPI.transfer(data[i]);
} // Send BMP Array
SPI.transfer(0x43); // End Bitmap CMD           
delay(1); // wait 1ms
}

Currently the arrays are stored in seperate libraries to make life easier BMPlibrary and TXTlibrary

Just to confirm to everyone - both codes work perfectly independantly

I came up with the code below to join the two into one, using an if statement to decide what to do with the array based on its size, if its small = write txt, if its large = write image. The code below works for BMP but fails to compile because

void LCD(char RGBXXX, uint16_t str[]){
  RGB(RGBXXX);
  if(sizeof(str) > 33){
  char data[33];
  memcpy(data,(uint8_t*)str,33);
 writeLCD(data);
  }
  else {
  BMP(str);
  }
} 

However the issue I have now is that when stating the typecast, if i set it as uint8_t, then the txt part of the code works fine, but the image code wont,

cannot convert 'uint8_t*' to 'uint16_t*' for argument '2' to 'void LCD(char, uint16_t*)'

and if i set as uint16_t the image works fine but the txt doesn't!

I'm not expecting charity just a little helping hand to figure out where im going wrong would be much appreciated! I spent hours to find out why the image wouldn't work at first until i realised its to do with byte size, but now i cant work out how to combine the two!


Solution

  • void* will solve your cast problem and sizeof a pointer is never going to be > 33 so try this instead:

    void LCD(char RGBXXX, void *str, int is_data) {  
      RGB(RGBXXX);   
      if (is_data != 0) {   
        char data[33];   
        memcpy(data,(uint8_t*)str,33);  
        writeLCD(data);   
     }   
     else {   
       BMP((uint16_t *)str);
     } 
    

    }