Search code examples
functionpointersvariablesprintfquotation-marks

Trying to get my own printf function to work with variables


I am programming a display and I am able to display characters on the display by using this function:

void printChar(char *tekst, uint16_t xPos, uint16_t yPos)
{
    //calculate the position the first byte should be placed on
    uint16_t startPos = yPos * width_disp_byte + (xPos/8);
    int i;

    //put all 16 bytes on the right place on the display based on the users' input
    for(i=0;i<16;i++)
    {
        test_image[startPos]=convertChar(*tekst,i);
        startPos += width_disp_byte;
    }
}

Basically I get a character and find its location in an array that I build. Than I take 16 bytes of data and put this in the display.

The next step is to display integer variables on the display. I have written a code that looks like this:

void printVariable(uint16_t integer, uint16_t xPos, uint16_t yPos)
{
    uint16_t value;
    uint16_t remainder;
    char printValue;

    //max value is 9999 that can be displayed, caused by the '4' in the for loop
    for(int i = 0; i < 4;i++)
    {
        value = integer;
        //int_power calculates the divisor. going from 3 to 0. cq 1000,100,10,1.
        value /= int_power(10,(3-i));

        //remove highest number from integer value (for 312, remove the 3 by substracting 300)
        integer -= (value) * (int_power(10,(3-i)));

        // add '0' to value to get the correct ASCII value.
        value += '0';

        // convert uint16_t value into char
        printValue = value;
        printChar(printValue,xPos,yPos);

        xPos += 8;
    }
}

I take a variable, lets say 3164. The first step is to divide this by 1000. The answer will be 3, since it's an integer. I display this character using the printChar function.

the next step removes 3000 from 3164 and divides the value by 100, resulting in 1. Again this value is printed using the printf function. Then 100 is removed from from 164 and then gets divided by 10 etc etc.

This code is quite limited in its use, but it fits perfectly in what I want to achieve. There is no need to print variables within a string.

The problem here is that the printChar function does not work like I have written in the code. Normally I would use the printChar function like this:

printChar("F",0,0);

This would print the character F in the topleft corner. If I want to use the printChar function like this, it doesn't work:

printChar(printValue,xPos,yPos);

The warning message says:

incompatible integer to pointer conversion passing 'char' to parameter of type 'char *'; take the address with &

If I take the address with & I don't get the correct value displayed on my display.

How can I fix this?


Solution

  • You only want to print ONE character, so you do not need a pointer as parameter. Your function would work like this:

    void printChar(char tekst, uint16_t xPos, uint16_t yPos){
    ...
        //depending on the Parameters of your convertChar- function either
        ... = convertChar(tekst,i); // if you can modify this function too
    
        ... = convertChar(&tekst,i); // if the function is to use as it is
    
    }
    

    The difference is in char tekst instead of char * text