Is it possible to build a HID device (like a keyboard) using an Arduino uno?
yes it is! And there's a great tutorial on how to do that over here
At the time being, I have button inputs on the Arduino giving outputs on the
serial line. So, how can I transform my current firmware into something that
can behave like a HID keyboard?
As the tutorial shows you, you need to transform your firmware code so instead of
printing the events, you actually trigger events. What you need to do, is add to your
code:
void triggerKey(uint8_t mod, uint8_t chr) {
uint8_t buf[8] = {mod, chr, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
// press key
Serial.write(buf, 8);
// emulate key press delay
delay(10);
buf[1] = 0x00;
// release key
Serial.write(buf, 8);
}
What you're doing here, is send from your main Arduino microcontroller events over
the serial line to the smaller microcontroller that handles USB communication. That
microcontroller will take those key "buffers" and transfer them as USB HID Keyboard
key presses over USB.
Beware that the key character (chr
in the code here), is actually a keycode sent
over USB that gets interpreted by your USB HID driver, which then translates it into
a real character using the current layout you've setup for your keyboard.
The mod
character is one of the modifier keys you have on the keyboard (shift,
control, alt...), 0x00
being no modifier.
- Shall I write a new window device driver?
No, you don't need! With the DFU upgrade, your arduino will be seen as a USB keyboard
by windows.
- Shall I create a software layer that could take my serial data and have windows read it as HID input?
You neither need to do that, and it's actually a very bad idea for many reasons:
- your solution would not be portable, and thus you'd need to hack something for every platform you may use,
- your solution would imply a lot of new code ; the more code one write, the more bugs is likely to be in your system,
- your solution would be really hackish, as it would behave as a device driver (i.e. kernel land) taking input from
a user program (i.e. user land) and is likely to not work, be a hell to debug, and prone for security issues...
If that's even possible to do!
I'm using an Uno, but I'll look at what you posted because last time we considered the DFU update but we're kinda scared of bricking stuff
Finally you're having concerns, in a comment, about bricking your arduino. Please
believe me and the author of the article that you cannot brick your arduino
doing a firmware flash:
NOTE: There is no chance of ‘bricking’ the device using this method. The Arduino bootloader firmware can always be updated using the DFU protocol!
In case something gets really badly wrong, and you get a bootloader corruption in the
little AVR (atmega8u2), you still can reflash it using another arduino or an
atmel programmer using the 6 pins next to the USB plug.
In the end, know that the only way to brick an AVR, thus an Arduino as well, is
to play with the fuses and reset the programming fuses. And even then, you may use
HVSP to save your device, though it's often less trouble to just take a new chip.