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:
-
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.... -
whats about the * before the str where the string is declared? what means *str ?
-
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