{"id":1001,"date":"2017-02-10T17:08:50","date_gmt":"2017-02-10T17:08:50","guid":{"rendered":"http:\/\/ofalcao.pt\/blog\/?p=1001"},"modified":"2017-02-10T17:35:04","modified_gmt":"2017-02-10T17:35:04","slug":"using-ftdi-as-ir","status":"publish","type":"post","link":"https:\/\/ofalcao.pt\/blog\/2017\/using-ftdi-as-ir","title":{"rendered":"Using a FTDI adapter as an IR emitter"},"content":{"rendered":"<div class=\"seriesmeta\">This post is part 1 of 5 of \u00a0<a href=\"https:\/\/ofalcao.pt\/blog\/series\/using-a-ftdi-adapter-as-an-ir\" class=\"series-287\" title=\"Using a FTDI adapter as an IR\">Using a FTDI adapter as an IR<\/a><\/div><p>[Crazy intro, just talking to myself]<\/p>\n<p>20 years ago, when I first heard about linux, it didn&#8217;t attract me much&#8230; spending lots ot time compiling the kernel or the drivers on a 486 seemed so crazy, I wasn&#8217;t geeky enough for that. Catched the linux train many years later with Ubuntu 6.04 or 6.10 when almost everything &#8220;just worked&#8221; and a few google searchs were enough when something didn&#8217;t work as expected.<\/p>\n<p>And this week I found myself compiling libftdi and LIRC on a LEGO Mindstorms EV3! Gosh&#8230; what happened to me?<\/p>\n<p>[end of crazy intro, start of long and probably boring intro]<\/p>\n<p>Two years ago I found a way to<a href=\"http:\/\/ofalcao.pt\/blog\/tag\/remote-control\"> use the soundcard of my laptop as a remote controller<\/a> for my LEGO motors, thanks to LIRC. It worked OK and it even worked with my Android 4.x phone but never worked properly with ev3dev (and, some months later, I found that it also didn&#8217;t worked with my Android 5.x phone) and like most everything else I never really gave him a good use.<\/p>\n<p>Last week someone asked at eurobricks forum how to use a HiTech IR controller with EV3. I also found a way to use it with ev3dev (picking up other people work) and once again never really giving it a good use. Hey, but somebody in the forum said it had tried my code! Thats amazing!<\/p>\n<p>So I returned to LIRC and soundcard, decided to update my own howto. LIRC had evolved a bit in last two years and while reading some docs I found that <a href=\"http:\/\/www.lirc.org\/html\/ftdix.html\">LIRC also supports FTDI adapters<\/a>. In fact, its extremely easy to make a <a href=\"http:\/\/www.huitsing.nl\/irftdi\/\">USB IR emmiter<\/a> with just a FTDI adapter and an IR LED, not even a resistor is needed!<\/p>\n<p>I already had a few FTDI cables and adapters but they all use the FT232R model and for reliable timings a FT230X is needed. But my &#8220;local&#8221; supplier had a <a href=\"https:\/\/www.sparkfun.com\/products\/13746\">Sparkfun Beefy 3<\/a> that used FT231X, it&#8217;s not the FT230X but it seemed similar enough so I gave it a try&#8230; and it works!<\/p>\n<p>[end of long boring intro]<\/p>\n<p>So we need a recent version of <a href=\"http:\/\/www.lirc.org\/\">LIRC<\/a> that implements ftdix driver. Ubuntu and Debian jessie (so ev3dev also) only have 0.9.0&#8230; We need to <a href=\"https:\/\/sourceforge.net\/projects\/lirc\/files\/LIRC\/\">download the source code directly from LIRC<\/a> and compile ourselves. I downloaded the last version available, 0.9.4d.<\/p>\n<p>For LIRC to compile the ftdix driver we also need <a href=\"https:\/\/www.intra2net.com\/en\/developer\/libftdi\/\">libftdi<\/a>. Ubuntu and Debian have it but I also compiled it from source &#8211; not sure why but at least for ev3dev just installing Debian libftdi packages is not enough and it seems that just compilling libftdi is also not enough, I had to do both.<\/p>\n<p>My EV3 is running ev3dev snaphot 2017-02-06. Two days later there&#8217;s already a new snaphot but I only updated with apt:<\/p>\n<pre>sudo apt update\r\nsudo apt upgrade\r\nsudo apt dist-upgrade<\/pre>\n<p>After reboot my Ev3 is running kernel 4.4.47:<\/p>\n<pre>Linux ev3dev 4.4.47-19-ev3dev-ev3 #1 PREEMPT Wed Feb 8 14:15:28 CST 2017 armv5tejl GNU\/Linux<\/pre>\n<p>First we install all dependencies needed for both LIRC and libftdi:<\/p>\n<pre>sudo apt install libftdi-dev build-essential pkg-config xsltproc libusb-1.0 cmake libboost-all-dev<\/pre>\n<p>This takes about an hour, lots of packages (mostly related to libboost)<\/p>\n<p>Before spending lots of time compiling it is better to test if out FTDI adapter works as expected so we can download this &#8220;hello ftdi&#8221; <a href=\"http:\/\/hackaday.com\/2009\/09\/22\/introduction-to-ftdi-bitbang-mode\/\">example<\/a>:<\/p>\n<p>I saved it as &#8220;hello-ftdi.c&#8221;.<\/p>\n<p>Now we insert our FTDI adapter and look for it at the end of dmesg:<\/p>\n<pre>[47800.964059] usb 1-1.2: new full-speed USB device number 8 using ohci\r\n[47801.111337] usb 1-1.2: New USB device found, idVendor=0403, idProduct=6015\r\n[47801.111424] usb 1-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=3\r\n[47801.111472] usb 1-1.2: Product: FT231X USB UART\r\n[47801.111515] usb 1-1.2: Manufacturer: FTDI\r\n[47801.111553] usb 1-1.2: SerialNumber: DN01DR29\r\n[47801.306754] usbcore: registered new interface driver usbserial\r\n[47801.414060] usbcore: registered new interface driver ftdi_sio\r\n[47801.423075] usbserial: USB Serial support registered for FTDI USB Serial Device\r\n[47801.437994] ftdi_sio 1-1.2:1.0: FTDI USB Serial Device converter detected\r\n[47801.449290] usb 1-1.2: Detected FT-X\r\n[47801.454135] usb 1-1.2: FTDI USB Serial Device converter now attached to ttyUSB0<\/pre>\n<p>We see that it is detected and we take note of the idVendor and idProduct values:<\/p>\n<pre>idVendor=0403, idProduct=6015<\/pre>\n<p>For later use we also take note of its Serial Number:<\/p>\n<pre>DN01DR29<\/pre>\n<p>Now we edit the &#8220;hello-ftdi.c&#8221; program and update the idVendor and idProduct.<\/p>\n<pre>\/* hello-ftdi.c: flash LED connected between CTS and GND.\r\n\u00a0\u00a0 This example uses the libftdi API.\r\n\u00a0\u00a0 Minimal error checking; written for brevity, not durability. *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n#include &lt;ftdi.h&gt;\r\n\r\n#define LED 0x08\u00a0 \/* CTS \u00a0(brown wire on FTDI cable) *\/\r\n\r\nint main()\r\n{\r\n\u00a0\u00a0\u00a0 unsigned char c = 0;\r\n\u00a0\u00a0\u00a0 struct ftdi_context ftdic;\r\n\r\n\u00a0\u00a0\u00a0 \/* Initialize context for subsequent function calls *\/\r\n\u00a0\u00a0\u00a0 ftdi_init(&amp;ftdic);\r\n\r\n\u00a0\u00a0\u00a0 \/* Open FTDI device based on FT232R vendor &amp; product IDs *\/\r\n\u00a0\u00a0\u00a0 if(ftdi_usb_open(&amp;ftdic, 0x0403, 0x6015) &lt; 0) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 puts(\"Can't open device\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return 1;\r\n\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0 \/* Enable bitbang mode with a single output line *\/\r\n\u00a0\u00a0\u00a0 ftdi_enable_bitbang(&amp;ftdic, LED);\r\n\r\n\u00a0\u00a0\u00a0 \/* Endless loop: invert LED state, write output, pause 1 second *\/\r\n\u00a0\u00a0\u00a0 for(;;) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 c ^= LED;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ftdi_write_data(&amp;ftdic, &amp;c, 1);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 sleep(1);\r\n\u00a0\u00a0\u00a0 }\r\n}<\/pre>\n<p>Then we compile our &#8220;hello-ftdi&#8221; test program:<\/p>\n<pre>gcc hello-ftdi.c -lftdi -o hello-ftdi\r\nhello-ftdi.c: In function 'main':\r\nhello-ftdi.c:25:5: warning: 'ftdi_enable_bitbang' is deprecated (declared at \/usr\/include\/ftdi.h:413) [-Wdeprecated-declarations]\r\n\u00a0\u00a0\u00a0\u00a0 ftdi_enable_bitbang(&amp;ftdic, LED);<\/pre>\n<p>We can ignore that warning, as long as we get a &#8220;hello-ftdi&#8221; binary file.<\/p>\n<p>To run it we need root permissions:<\/p>\n<pre>sudo .\/hello-ftdi<\/pre>\n<p>If we connect a common led between CTS (A) and GND (K) we will see it blink each second so we can replace it with a infrared LED (940 nm is better but 950 nm will also work). And yes, we don&#8217;t need to use a resistor.<\/p>\n<p>If we check dmesg again, we notice that our test program disconnected the ttyUSB device, as required:<\/p>\n<pre>[15564.602213] ftdi_sio ttyUSB0: FTDI USB Serial Device converter now disconnected from ttyUSB0\r\n[15564.602615] ftdi_sio 1-1.2:1.0: device disconnected<\/pre>\n<p>This post was to long for my web server so I broke in several.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"seriesmeta\">This post is part 1  of 5 of \u00a0<a href=\"https:\/\/ofalcao.pt\/blog\/series\/using-a-ftdi-adapter-as-an-ir\" class=\"series-287\" title=\"Using a FTDI adapter as an IR\">Using a FTDI adapter as an IR<\/a><\/div><p>[Crazy intro, just talking to myself] 20 years ago, when I first heard about linux, it didn&#8217;t attract me much&#8230; spending lots ot time compiling the kernel or the drivers on a 486 seemed so crazy, I wasn&#8217;t geeky enough for that. Catched the linux train many years later with Ubuntu 6.04 or 6.10 when &hellip; <a href=\"https:\/\/ofalcao.pt\/blog\/2017\/using-ftdi-as-ir\" class=\"more-link\">Continuar a ler<span class=\"screen-reader-text\"> &#8220;Using a FTDI adapter as an IR emitter&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[286],"tags":[177,136],"series":[287],"class_list":["post-1001","post","type-post","status-publish","format-standard","hentry","category-ir","tag-lirc","tag-remote-control","series-using-a-ftdi-adapter-as-an-ir"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2Mhyv-g9","_links":{"self":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/1001","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/comments?post=1001"}],"version-history":[{"count":0,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/1001\/revisions"}],"wp:attachment":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/media?parent=1001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/categories?post=1001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/tags?post=1001"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/series?post=1001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}