1

I am using the following code:

#include "USBHostGiga.h"

//REDIRECT_STDOUT_TO(Serial)
Keyboard keyb;
HostSerial ser;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  while (!Serial);
  pinMode(PA_15, OUTPUT);
  keyb.begin();
  ser.begin();
}


void loop() {
  if (keyb.available()) {
    auto _key = keyb.read();
    Serial.println(keyb.getAscii(_key));
  }
  while (ser.available()) {
    auto _char = ser.read();
    Serial.write(_char);
  }
  //delay(1);
}

from here: https://docs.arduino.cc/tutorials/giga-r1-wifi/giga-usb

I am using Arduino GIGA R1 on which I have connected a USB keyboard and I press buttons. I see the results on the Serial. However, I face the following problem: When I press a button, apart of the letter I get also a square next to it. For instance, let's say I press the 'A' letter. In the serial I get 'A' letter + a square symbol. When I press 'B', I get 'B' + a square symbol, etc. I am trying for 2 days to fix it, I tried to read the button as a char array with two positions and delete the [1] where the square should be, but nothing worked. The auto type is very strange in order to make a type casting, because the Arduino IDE continuously shows me error. I cannot find a way to remove the strange 'square' ASCII character which I take with each letter pressed...The only things I noticed is that the square ASCII character is displayed the time I release the keyboard button, and also it delays to appear when I increase the delay ms in the delay(...); command. Has anyone any idea how to solve this?

1
  • does [SHIFT]+[A] look the same as [A]? what does Serial.println(_key, HEX) look like? Commented Nov 3, 2023 at 4:58

1 Answer 1

0

I don't have GIGA R1, so no practical way to test this. I also cannot explain why it's showing up as square for you. It is common behaviour for character codes that don't have a font glyph to render in that way, but the one in question (null character) does not do that for me under the 1.x 2.x IDEs.

However, the rest of what follows may help.

According to github, the type that is returned by .read() is HID_KEYBD_Info_TypeDef, so if you want you should be able to:

const HID_KEYBD_Info_TypeDef _key = keyb.read();

However, there's not much wrong with using auto here. So far as I can tell, what you're really receiving is the content of Boot mode HID reports. Not individual key information really, but rather the state which may include up to 6 pressed keys. If you're only pressing one key (one non-shift state key; as in not "shift", "control", "alt". etc, it shows up in array index 0, which is what getAscii is ultimately interpreting.

So when you release the key (all of them) you are getting a report with nothing (no key code; represented with a zero value) in keys[0], and so there's nothing there from which to get an ASCII code. What it seems to be doing is translating the 0 value that's there into another zero.

So, it would seem all you need to do is check to see if it's a zero after the call to getAscii, e.g.:

if (keyb.available()) {
  const auto keyboard_input_report /*not really a key*/ = keyb.read();
  const auto first_as_ascii = keyb.getAscii(keyboard_input_report);
  //  where getAscii really means getAsciiCodeOfFirstkeyIfThereIsOneInTheReport
  if (first_as_ascii != 0) {
    Serial.println(first_as_ascii);
  }
}

You could to check before translation with if (keyboard_input_report.keys[0] != 0) {. However, the former may make more sense. Because not every key on the keyboard will map unto may onto an ASCII code value anyway. E.g. ASCII does not represent the arrow keys, function keys, or page-up/page-down.

In any case, this code may have issues if multiple keys are pressed. It looks like doing so correctly would involve using the internals of getAscii to consult the translation tables for all six of the entries in the report's keys array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.