francescomecca.eu/_site/pescewanda/2016/07/05/arduino_keyboard/index.html

882 lines
16 KiB
HTML
Raw Normal View History

2016-07-06 07:49:02 +02:00
<!DOCTYPE html>
<html lang="en-us">
2017-05-09 11:29:54 +02:00
2016-07-06 07:49:02 +02:00
<head>
2017-05-09 11:29:54 +02:00
<meta charset="UTF-8">
<title>Caught in the Net</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#157878">
<link rel="stylesheet" href="/css/normalize.css">
<!--<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>-->
<link rel="stylesheet" href="/fonts/opensans.css">
<link rel="stylesheet" href="/css/cayman.css">
2016-07-06 07:49:02 +02:00
</head>
2017-05-09 11:29:54 +02:00
<body>
<section class="page-header">
<h1 class="project-name">Caught in the Net</h1>
<h2 class="project-tagline">La rete ti cattura ma libera il pensiero</h2>
<a class="btn" href="/">Home</a>
2017-05-09 12:17:48 +02:00
<a class="btn" href="/about/">About me</a>
2017-05-09 11:29:54 +02:00
<a class="btn" href="/contattami/">Contact me</a>
2017-05-10 20:26:52 +02:00
<a class="btn" href="/archive/">Archive</a>
<a class="btn" href="/feed.xml">RSS</a>
2017-09-22 14:07:26 +02:00
<a class="btn" href="http://francescomecca.eu/git/explore/repos">Personal Git</a>
2017-05-09 11:29:54 +02:00
<a class="btn" href="https://github.com/FraMecca">Github</a>
2017-05-09 11:38:51 +02:00
<a class="btn" href="/curriculum/CV_Mecca_Francesco.pdf">Curriculum</a>
2017-05-09 11:29:54 +02:00
</section>
2016-07-06 07:49:02 +02:00
2017-05-09 11:29:54 +02:00
<section class="main-content">
2016-07-06 07:49:02 +02:00
<div class="post">
<h1 class="post-title">Arduino Uno as HID keyboard</h1>
<span class="post-date">05 Jul 2016</span>
2016-07-07 02:05:21 +02:00
<p>Turin is the hometown of Arduino. I have been at the <a href="http://fablabtorino.org/">fablab</a> multiple times but I had to come all the way to America to get my hands on a simple Arduino Uno.</p>
2016-07-06 07:51:25 +02:00
<p>For 60$ I bought a cheap (but still good!) mechanical keyboard by Qisan, a clone of the Arduino Uno and a USB host shield.</p>
2016-07-06 07:49:02 +02:00
<p>Given that is 3 years since I have been using a dvorak layout and it&#39;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.</p>
<p>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.</p>
<p>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.</p>
<p>In particular <a href="http://hunt.net.nz/users/darran/">this blog</a> has the most precious information and the guy wrote a HID driver that allows the Uno to be seen as a HID device.</p>
<p>It was a noob error but I didn&#39;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.</p>
<p>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.</p>
2016-07-07 02:05:21 +02:00
<h2 id="a-small-journey-in-the-arduino-world">A small journey in the Arduino world</h2>
2016-07-06 07:49:02 +02:00
<p>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.</p>
<p>All the files I have used today are on <a href="http://francescomecca.eu:3000/pesceWanda/arduino_HID_keyboard">my repos</a> and I included also an outdated version of the USB Host Shield library that I used.</p>
<p>The original code from this <a href="http://hunt.net.nz/users/darran/weblog/c6f35/Arduino_USB_Keyboard_Passthrough.html">blog post</a> works like a charm but just as a simple passthrough.</p>
<p>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.</p>
<p>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.</p>
<p>The dvorak layout has the same pairs as the US layout but eventually I got used to having the &#39;@&#39; where at the same place of &#39;Q&#39; (qwerty) and &#39;&quot;&#39; over the &#39;2&#39;.
2016-07-07 02:05:21 +02:00
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.
2016-07-06 07:49:02 +02:00
The modifier bit at the beginning of the array came in handy for my code.</p>
<p>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 <a href="http://www.freebsddiary.org/APC/usb_hid_usages.php">here</a>.</p>
<p>The process of flashing the code on the Uno goes like this:</p>
<ul>
<li>write the looping code;</li>
<li>push it to the Arduino using the IDE;</li>
<li>shortcircuit the board so that it goes in DFU mode;</li>
<li>flash the .hex HID firmware;</li>
<li>try your code;</li>
<li>repeat until it&#39;s right.</li>
</ul>
<p><img src="/wp-content/uploads/2016/IMG_20160706_011304.jpg" alt="Everything fits in one picture"></p>
2016-07-07 02:05:21 +02:00
<h2 id="flashing-the-firmware">Flashing the firmware</h2>
2016-07-06 07:49:02 +02:00
2016-07-07 02:05:21 +02:00
<p>The firmware is in my repo but I got it from (here)[<a href="http://hunt.net.nz/users/darran/weblog/a6d52/Arduino_UNO_Keyboard_HID_version_02.html">http://hunt.net.nz/users/darran/weblog/a6d52/Arduino_UNO_Keyboard_HID_version_02.html</a>].
2016-07-06 07:49:02 +02:00
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 <a href="https://www.youtube.com/watch?v=E8XyRwXQr8Q">video</a> shows the method briefly (no real need for a jumper).
The commands are the following and there is no risk to brick the Uno:</p>
<div class="highlight"><pre><code class="language-" data-lang="">dfu-programmer atmega16u2 erase
dfu-programmer atmega16u2 flash Arduino-keyboard-0.2.hex
dfu-programmer atmega16u2 reset
</code></pre></div>
<p>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.</p>
<p><img src="/wp-content/uploads/2016/IMG_20160706_011143.jpg" alt="Arduino and the shield"></p>
<p>That&#39;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.</p>
</div>
2016-09-17 15:13:02 +02:00
<!--<div class="related">-->
<!--<related-posts />-->
<!--<h2>Related Posts</h2>-->
<!--<ul class="related-posts">-->
2017-10-05 06:35:49 +02:00
<!---->
<!--<li>-->
<!--<h3>-->
<!--<a href="/pescewanda/2017/10/02/minidoxguide/">-->
<!--Minidox, a guide for the Europeans and the Scrooges-->
<!--<small>02 Oct 2017</small>-->
<!--</a>-->
<!--</h3>-->
<!--</li>-->
2017-05-10 20:26:52 +02:00
<!---->
<!--<li>-->
<!--<h3>-->
<!--<a href="/pescewanda/2017/05/09/vaporwave/">-->
<!--Cyber-utopia and vaporwave-->
<!--<small>09 May 2017</small>-->
<!--</a>-->
<!--</h3>-->
<!--</li>-->
2017-05-07 12:21:36 +02:00
<!---->
<!--<li>-->
<!--<h3>-->
<!--<a href="/pescewanda/2017/05/07/latestage_handbrake/">-->
<!--Late Stage Capitalism meets FOSS-->
<!--<small>07 May 2017</small>-->
<!--</a>-->
<!--</h3>-->
<!--</li>-->
2017-03-22 13:07:09 +01:00
<!---->
<!--<li>-->
<!--<h3>-->
2017-03-22 19:38:23 +01:00
<!--<a href="/pescewanda/2017/03/20/spazio-digitale-rant-facebook__eng/">-->
2017-03-22 13:07:09 +01:00
<!--Some shallow thoughts from my tiny virtual space-->
<!--<small>20 Mar 2017</small>-->
<!--</a>-->
<!--</h3>-->
<!--</li>-->
2017-02-24 07:32:48 +01:00
<!---->
<!--<li>-->
<!--<h3>-->
2017-03-17 20:54:44 +01:00
<!--<a href="/pescewanda/2017/03/07/spazio-digitale-rant-facebook/">-->
<!--Breve riflessione dal mio piccolo mondo virtuale-->
<!--<small>07 Mar 2017</small>-->
2017-02-24 07:32:48 +01:00
<!--</a>-->
<!--</h3>-->
<!--</li>-->
2016-11-19 18:38:14 +01:00
<!---->
2016-09-17 15:13:02 +02:00
<!--</ul>-->
<!--</div>-->
2017-10-05 06:35:49 +02:00
<h4>Similar Posts</h4>
<ul>
<li class="relatedPost">
<a href="/pescewanda/2017/10/02/minidoxguide/">Minidox, a guide for the Europeans and the Scrooges
</a>
</li>
2016-09-17 15:13:02 +02:00
2017-03-22 19:38:23 +01:00
2016-09-17 15:13:02 +02:00
2017-02-24 07:32:48 +01:00
2017-05-10 20:26:52 +02:00
2017-02-24 07:32:48 +01:00
2016-09-17 15:13:02 +02:00
2017-05-10 20:26:52 +02:00
2016-09-17 15:13:02 +02:00
2017-05-10 20:26:52 +02:00
2016-09-17 15:13:02 +02:00
2016-11-19 18:38:14 +01:00
2016-09-17 15:13:02 +02:00
2016-11-19 18:38:14 +01:00
2016-09-17 15:13:02 +02:00
2016-11-18 20:45:39 +01:00
2017-03-22 13:07:09 +01:00
<li class="relatedPost">
2017-05-10 20:26:52 +02:00
<a href="/pescewanda/2016/11/15/machine-learning-PARTE3/">Capire il Machine Learning (parte 3)
2017-03-22 13:07:09 +01:00
</a>
</li>
2016-11-18 20:45:39 +01:00
2017-03-22 13:07:09 +01:00
2016-11-18 20:45:39 +01:00
2016-09-17 15:13:02 +02:00
2017-05-07 12:21:36 +02:00
<li class="relatedPost">
2017-05-10 20:26:52 +02:00
<a href="/pescewanda/2016/11/11/machine-learning-PARTE2/">Capire il Machine Learning (parte 2)
2017-05-07 12:21:36 +02:00
</a>
</li>
2016-09-17 15:13:02 +02:00
2017-05-07 12:21:36 +02:00
2016-09-17 15:13:02 +02:00
2016-11-19 18:38:14 +01:00
2017-05-07 12:21:36 +02:00
2017-03-22 13:07:09 +01:00
2016-11-19 18:38:14 +01:00
2016-11-18 20:49:28 +01:00
2016-09-17 15:13:02 +02:00
2016-11-18 20:45:39 +01:00
2016-09-17 15:13:02 +02:00
2017-05-10 20:26:52 +02:00
<li class="relatedPost">
<a href="/pescewanda/2016/11/10/machine-learning-intro/">Capire il Machine Learning (parte 1)
</a>
</li>
2016-09-17 15:13:02 +02:00
2017-05-10 20:26:52 +02:00
2016-09-17 15:13:02 +02:00
2017-05-10 20:26:52 +02:00
2016-09-17 15:13:02 +02:00
2016-11-18 20:45:39 +01:00
2017-03-22 13:07:09 +01:00
2016-11-19 18:38:14 +01:00
2017-05-07 12:21:36 +02:00
2016-09-17 15:13:02 +02:00
2017-05-07 12:21:36 +02:00
2017-03-22 13:07:09 +01:00
2016-09-17 15:13:02 +02:00
2016-11-18 20:45:39 +01:00
2017-05-07 12:21:36 +02:00
2017-05-10 20:26:52 +02:00
2016-11-18 20:45:39 +01:00
2016-09-17 15:13:02 +02:00
</ul>
2016-07-06 07:49:02 +02:00
2017-05-09 11:29:54 +02:00
2017-05-10 20:26:52 +02:00
2017-05-09 11:29:54 +02:00
<footer class="site-footer">
2017-05-10 20:26:52 +02:00
<!--<span class="site-footer-owner"><a href="http://francescomecca.eu">Caught in the Net</a> is maintained by <a href="contattami">Francesco Mecca</a>.</span>-->
2017-05-09 11:29:54 +02:00
<span> CC BY-SA 4.0 International.</br> </span>
<span class="site-footer-credits"><a href="https://jekyllrb.com">Jekyll</a>, <a href="https://github.com/jasonlong/cayman-theme">Cayman theme</a>.</span>
</footer>
</section>
2016-07-06 07:49:02 +02:00
</body>
</html>