0

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!

Rob Brown
  • 13
  • 2
  • 3
    You are trying to hammer a square uint16_t peg into a round char hole. Pretty unclear why you thought that was a good idea, writeLcd() takes a char[]. Maybe you got paralyzed by the difference between a string and a bitmap. Don't let LCD() do both. – Hans Passant Jan 28 '15 at 00:24
  • I provided an answer but I agree with Hans that this "combination function: of yours is not very pretty and probably best avoided, – GroovyDotCom Jan 28 '15 at 00:38
  • Thanks for the honest answer, sorry my bmp is a string of 512 bytes, i thought I had made that clear. the answer below from groovydotcom works perfectly. – Rob Brown Jan 28 '15 at 00:49

1 Answers1

0

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);
 } 

}

GroovyDotCom
  • 1,304
  • 2
  • 15
  • 29
  • My method behind the madness is that i need to show it to someone with less knowledge than me and them not manage to have a problem, regarding the size of pointer, its odd because if i stated uint8_t and commented out the bmp, then the code would send a text array to the lcd, and vice versa, so i thought i had it correct. However when I tried to modify your code to do the same thing it still didnt work, the size of my char array is 32 characters +1 null character, and my BMP is 512 bytes exactly. Is there anyway to create automation and miss having to use the is_data part of the code? – Rob Brown Jan 28 '15 at 00:55
  • Thank you for your answer, I appreciate it's a messy way of doing it, but your solution did the job! – Rob Brown Jan 28 '15 at 00:56