Uno -> HID Many questions about Scancodes and Byte* Variables

Hey Guys,
i flashed the Atmega16U2 from the Uno with the arduino-keyboard-0.3.hex. I downloaded an Example File and everything workfs fine! But i don´t understand the code and thats make me going crazy! The code looks like this:

uint8_t buf[8] = { 0 };	/* Keyboard report buffer */

#define KEY_LEFT_SHIFT	0x02

void setup() 
{
    Serial.begin(9600);
    delay(10000); //some Time to wait
    pinMode(8,INPUT);
    digitalWrite(8,HIGH); //external button
    pinMode(13,OUTPUT);
    digitalWrite(13,LOW); //just to see that the 10 Seconds are over
}

char *str = "arduino";

void loop() 
{
    char *chp = str;
    int button = digitalRead(8);   //only do that if the Button is pushed
    if (button == LOW)
    {
    while (*chp) {
	    
	if ((*chp >= 'a') && (*chp <= 'z')) {
	    buf[2] = *chp - 'a' + 4;
	} else if ((*chp >= 'A') && (*chp <= 'Z')) {
	    buf[0] = KEY_LEFT_SHIFT;	/* Caps */
	    buf[2] = *chp - 'A' + 4;
	} else {
	    switch (*chp) {
	    case ' ':
	    	buf[2] = 0x2c;	// Space
		break;
	    default:
	        /* Character not handled. To do: add rest of chars from HUT1_11.pdf */
		buf[2] = 0x37;	// Period
		break;
	    }
	}

	Serial.write(buf, 8);	// Send keypress
        delay(25); //some Time for the Keyboard Controller
	buf[0] = 0;
	buf[2] = 0;
	Serial.write(buf, 8);	// Release key
	chp++;
    }
        buf[2]= 0x28;
        Serial.write(buf, 8);	// Send keypress
	buf[0] = 0;
	buf[2] = 0;
        delay(25); //dem Controller Zeit geben!
	Serial.write(buf, 8);	// Release key
    delay(100);
    }
}

My questions are:

  1. uint8_t buf[8] = { 0 }; uint8_t buf[8] is the same like byte buf[8] isn´t it? But Why make an array of 8 if we need in the code only 2 byte. Hope you understand me!? In the code we only use buf[0] or buf[2]
    Is there any reason for it? I don´t understand that....

  2. whats about the * before the str where the string is declared? what means *str ?

  3. I don´t understand why "while (*chp)" work. I would make an for loop with textlength or something, but the while loop is much shorter and looks more professional but i have never seen this before, so i just want to ask if that have any handicaps in comparsion with a "textlengt for loop"

Maybe someone knows the answers...
Thanks a lot, Ruediger

But Why make an array of 8 if we need in the code only 2 byte. Hope you understand me!? In the code we only use buf[0] or buf[2]

But that's a span of three bytes.

whats about the * before the str where the string is declared?

It makes the variable a pointer

I don´t understand why "while (*chp)" work.

Fetch the value that chip points to, test to see if it is zero

Hi Awol,
thanks for that very fast answer!
I understand answer 2 + 3 and will know aks google some things :slight_smile:
But i don´t understand answer 1 !

If I replaye buf[8] with
byte byte1 = 0;
byte byte2 = 0;
and then replaye every buf[0] with byte1 and every buf[2] with byte2 then the code don´t work! But Why??

Thanks a lot for your patience and sorry for that worse english!

Ruediger

I don't understand why you're using buf[0] and buf[2] instead of buf[0] and buf[1] if you only need 2 bytes.

:slight_smile:
that´s excactly what i also don´t understand! The Code from my first post is the code from the Example i found in the Internet!
And know im wondering why in the code the variable uint8_t buf[8] ist declare with an 8 and not with an 2
Is there something in the code i don´t see??

Greetz, Ruediger

And know im wondering why in the code the variable uint8_t buf[8] ist declare with an 8 and not with an 2

because they're writing eight bytes every time to the serial port.
This is presumably because whatever is on the other end of the serial connection expects eight bytes, not two.

Doh...
I think your absolutly right! Now i´m al little bit ashamed, because that was very logical
However, thank you very much for the Answers!!
Greetings, Ruediger