0

i'm trying to check/display which button i'm pressing, i've been told to use specifically a double char array.

With the code bellow i'm getting this output : 1 7 8 9 10 11 MuteRec LockPlay and the lcd doesn't refresh itself to write nothing.

int displayButton(int buttonActivated)
{
    int parse = 0;
    char buttonNameArray[21][4] = {
        {"1   "},{"2   "},{"3   "},{"4   "},
        {"5   "},{"6   "},{"7   "},{"8   "},
        {"9   "},{"10  "},{"11  "},{"12  "},
        {"13  "},{"14  "},{"15  "},{"16  "},
        {"Mute"},{"Rec "},{"Lock"},{"Play"},
        {"    "}
    };

    for(parse = 0; parse < 3; parse++)
    {
        DRV_HD44780_putString(16 + parse, 1, &buttonNameArray[buttonActivated][parse]);
    }
    return(21);
}

void testKeyboardTask()
{
    int nbButton = 0;
    int buttonActivated = 21;

    while(1)
    {
        DRV_KEYBOARD_readKeyboard();
        int buttonId;
        for (buttonId=0;buttonId<20;buttonId++)
        {
            if (buttonStateArray[buttonId] == 1 && nbButton < 20)
            {
                buttonActivated = buttonId;
                nbButton++;
            }
        }
        if(nbButton > 1)
        {
            DRV_HD44780_putString(0, 3, "Error Multiple Press");
            osDelay(1000);
            DRV_HD44780_putString(0, 2, "                    ");
        }
        else
            buttonActivated = displayButton(buttonActivated);

        nbButton = 0;
    }
 }

It seems buttonActivated change over the time (adding 6 to itself). I can't grasp why i get this output (i'm pretty new to c) and with a switch it work just fine,

If anyone can see why

Matt.L
  • 21
  • 3
  • 7
    4 chars is too small to hold those strings – tkausl Jun 26 '18 at 09:04
  • 3
    They will hold the 4 characters but not the NUL terminators, so they can't be handled as strings. – Weather Vane Jun 26 '18 at 09:06
  • 1
    It should be just `DRV_HD44780_putString(16, 1, buttonNameArray[buttonActivated]);` and `buttonNameArray[buttonActivated]` should be a string. – melpomene Jun 26 '18 at 09:06
  • Thanks to a [stupid, secret rule](https://stackoverflow.com/questions/31296727/inconsistent-gcc-diagnostic-for-string-initialization) in C allowing initialization of char arrays that are not null terminated, the compiler doesn't give a warning here. This probably goes back to the ancient Unix days when they used some manner of "fixed width strings". – Lundin Jun 26 '18 at 11:17

1 Answers1

2

As everyone says i've simply forget about the NULL terminator as 4 chars weren't enough to hold it.

Thanks you everyone.

Matt.L
  • 21
  • 3