francescomecca.eu/output/categories/arduino-uno.xml
2023-08-30 18:10:14 +02:00

46 lines
No EOL
6.6 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Caught in the Net (Posts about arduino uno)</title><link>francescomecca.eu</link><description></description><atom:link href="francescomecca.eu/categories/arduino-uno.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><copyright>Contents © 2023 &lt;a href="mailto:francescomecca.eu"&gt;Francesco Mecca&lt;/a&gt; </copyright><lastBuildDate>Wed, 30 Aug 2023 15:57:17 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Arduino Uno as HID keyboard</title><link>francescomecca.eu/blog/2016/7/5/arduino_keyboard/</link><dc:creator>Francesco Mecca</dc:creator><description>&lt;p&gt;Turin is the hometown of Arduino. I have been at the &lt;a href="http://fablabtorino.org/"&gt;fablab&lt;/a&gt; multiple times but I had to come all the way to America to get my hands on a simple Arduino Uno.&lt;/p&gt;
&lt;p&gt;For 60$ I bought a cheap (but still good!) mechanical keyboard by Qisan, a clone of the Arduino Uno and a USB host shield.&lt;/p&gt;
&lt;p&gt;Given that is 3 years since I have been using a dvorak layout and it's a pain to change layout on every machine that you have to use.
You can imagine that given this three pieces of hardware together I put together an hardware key mapper for the keyboard.&lt;/p&gt;
&lt;p&gt;I have never had experience with Arduino before but it was not that difficult to make it do simple things like blinking the led or send signal through to a serial monitor.&lt;/p&gt;
&lt;p&gt;It took me half an hour to wear down all my excitement: the USB Host Shield library broke all the compatibility with the similar project I found wandering online.&lt;/p&gt;
&lt;p&gt;In particular &lt;a href="http://hunt.net.nz/users/darran/"&gt;this blog&lt;/a&gt; has the most precious information and the guy wrote a HID driver that allows the Uno to be seen as a HID device.&lt;/p&gt;
&lt;p&gt;It was a noob error but I didn't checked the various arduino alternatives and I discovered late that just a few have the HID capabilities that would make this work easier. I should have bought and Arduino Due or Leonardo maybe.&lt;/p&gt;
&lt;p&gt;Also, the various guides about flashing with a dfu tool are specific to older models of the Uno and it took me some time to figure the name of the new components so that I could flash a new firmware.&lt;/p&gt;
&lt;h3&gt;A small journey in the Arduino world&lt;/h3&gt;
&lt;p&gt;It feels pretentious to write a little guide for this kind of work, given also the fact that I have roughly 10 hours of experience with the Arduino. But the other resources are really outdated so I hope this piece can be useful to someone out there.&lt;/p&gt;
&lt;p&gt;All the files I have used today are on &lt;a href="http://francescomecca.eu:3000/pesceWanda/arduino_HID_keyboard"&gt;my repos&lt;/a&gt; and I included also an outdated version of the USB Host Shield library that I used.&lt;/p&gt;
&lt;p&gt;The original code from this &lt;a href="http://hunt.net.nz/users/darran/weblog/c6f35/Arduino_USB_Keyboard_Passthrough.html"&gt;blog post&lt;/a&gt; works like a charm but just as a simple passthrough.&lt;/p&gt;
&lt;p&gt;It was not difficult at all to examine the code:
during each loop of the iteration a char array gets read from the shield and if it is contains information Arduino with the Serial.Write method send the data to the host.&lt;/p&gt;
&lt;p&gt;The buffer array is a simple array of length 8 and the first two positions are reserved. In particular the first one represent the various modifier keys.&lt;/p&gt;
&lt;p&gt;The dvorak layout has the same pairs as the US layout but eventually I got used to having the '@' where at the same place of 'Q' (qwerty) and '"' over the '2'.
Also, I am an avid vim user (I should thank Simone Basso for that) and I swapped some keys on the new 65 keys keyboard.
The modifier bit at the beginning of the array came in handy for my code.&lt;/p&gt;
&lt;p&gt;An hardware key remapper is a simple but long switch C statement but I decided to consider also the modifier bit: in this way certain keys like the Window (UGH!) key is mapped to a different layer of keys.
I got all the codes for the HID events &lt;a href="http://www.freebsddiary.org/APC/usb_hid_usages.php"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The process of flashing the code on the Uno goes like this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;write the looping code;&lt;/li&gt;
&lt;li&gt;push it to the Arduino using the IDE;&lt;/li&gt;
&lt;li&gt;shortcircuit the board so that it goes in DFU mode;&lt;/li&gt;
&lt;li&gt;flash the .hex HID firmware;&lt;/li&gt;
&lt;li&gt;try your code;&lt;/li&gt;
&lt;li&gt;repeat until it's right.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Everything fits in one picture" src="francescomecca.eu/wp-content/uploads/2016/IMG_20160706_011304.jpg"&gt;&lt;/p&gt;
&lt;h3&gt;Flashing the firmware&lt;/h3&gt;
&lt;p&gt;The firmware is in my repo but I got it from (here)[http://hunt.net.nz/users/darran/weblog/a6d52/Arduino_UNO_Keyboard_HID_version_02.html].
The tool I used to flash it is dfu-programmer (version 0.62).
Every time you want to flash a new firmware the Arduino must be put in DFU mode (you can see the difference with lsusb).
To do that simply create a shortcircuit using a small metal wire on the two pins near the reset button and a led will blink.
This &lt;a href="https://www.youtube.com/watch?v=E8XyRwXQr8Q"&gt;video&lt;/a&gt; shows the method briefly (no real need for a jumper).
The commands are the following and there is no risk to brick the Uno:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;dfu-programmer atmega16u2 erase
dfu-programmer atmega16u2 flash Arduino-keyboard-0.2.hex
dfu-programmer atmega16u2 reset
&lt;/pre&gt;
&lt;p&gt;After each flashing the device needs to be disconnected once. Of course you can flash the original firmware back. It is included in my repo or on the official ones.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Arduino and the shield" src="francescomecca.eu/wp-content/uploads/2016/IMG_20160706_011143.jpg"&gt;&lt;/p&gt;
&lt;p&gt;That's it, as you can see is not difficult at all. The worst part is gathering the various info that are left dormant in blogs or forums.&lt;/p&gt;</description><category>Arduino</category><category>arduino uno</category><category>HID arduino</category><category>mechanical keyboard</category><category>PesceWanda</category><category>programming</category><guid>francescomecca.eu/blog/2016/7/5/arduino_keyboard/</guid><pubDate>Tue, 05 Jul 2016 00:00:00 GMT</pubDate></item></channel></rss>