Quantcast
Channel: Manfaat Sehat
Viewing all articles
Browse latest Browse all 61

must be const: VUSB libraries for the Arduino with Debian Linux

$
0
0
Ever wanted a to wire the doorbell up to your computer?

The VUSB libraries are designed to allow your Arduino to emulate a generic USB device such as a keyboard.  When your Arduino is connected to external gadgetry like a pushbutton (doorbell) or a methane sensor, you can have your computer react as if keys were being typed or hotkey combos were being pressed to run an automated task.

I thought this sounded pretty fun to explore as I am learning about my Arduino computer (get one here), and I found some great websites which tell me how to emulate a keyboard (resources below).  I'm not really a programmer, so a lot of the errors I've come across have required some digging, and I hope providing this all in one place is helpful.  I'm running Debian GNU/Linux Wheezy/Sid (2013.02.07).

First, this writeup assumes you already have an Arduino board and working Arduino environment.  In your home directory (~) you should have the folder ~/sketchbook/ that your Arduino is using to store your sketches, as well as ~/sketchbook/libraries/.

You next have to download the VUSB libraries, available on googlecode.  Unpack the zipfile, dig into the folders, copy the 3 folders from the recently unpacked libraries/ folder and put them into your ~/sketchbook/libraries/ so you have this:

/home/user/sketchbook/libraries/UsbDevice/
/home/user/sketchbook/libraries/UsbKeyboard/
/home/user/sketchbook/libraries/UsbStream/


We're off to a good start.  However, these libraries were written before a somewhat recent update to GCC, the GNU C Compiler that Arduino requires.  The new GCC needs better defined variables, I believe is the reason for that change.  If you try to write a sketch with #include "UsbKeyboard.h" you'll get errors like:

usbdrv/usbdrv.h:455:6: error: variable 'usbDescriptorDevice' must be const in order to be put into read-only section by means of '__attribute__((progmem))'
usbdrv/usbdrv.h:461:6: error: variable 'usbDescriptorConfiguration' must be const in order to be put into read-only section by means of '__attribute__((progmem))'
usbdrv/usbdrv.h:467:6: error: variable 'usbDescriptorHidReport' must be const in order to be put into read-only section by means of '__attribute__((progmem))'
usbdrv/usbdrv.h:473:6: error: variable 'usbDescriptorString0' must be const in order to be put into read-only section by means of '__attribute__((progmem))'
usbdrv/usbdrv.h:479:5: error: variable 'usbDescriptorStringVendor' must be const in order to be put into read-only section by means of '__attribute__((progmem))'
usbdrv/usbdrv.h:485:5: error: variable 'usbDescriptorStringDevice' must be const in order to be put into read-only section by means of '__attribute__((progmem))'
usbdrv/usbdrv.h:491:5: error: variable 'usbDescriptorStringSerialNumber' must be const in order to be put into read-only section by means of '__attribute__((progmem))'
usbdrv/usbdrv.c:70:14: error: variable 'usbDescriptorString0' must be const in order to be put into read-only section by means of '__attribute__((progmem))'
usbdrv/usbdrv.c:80:14: error: variable 'usbDescriptorStringVendor' must be const in order to be put into read-only section by means of '__attribute__((progmem))'
usbdrv/usbdrv.c:89:14: error: variable 'usbDescriptorStringDevice' must be const in order to be put into read-only section by means of '__attribute__((progmem))'
usbdrv/usbdrv.c:111:14: error: variable 'usbDescriptorDevice' must be const in order to be put into read-only section by means of '__attribute__((progmem))'
usbdrv/usbdrv.c:142:14: error: variable 'usbDescriptorConfiguration' must be const in order to be put into read-only section by means of '__attribute__((progmem))'


No good, although I don't really know what it means.  It's worth noting that the parts in bold (my emphasis) refer to the libraries that we just downloaded and used with #include "UsbKeyboard.h", and the errors represent the file name, a line number, and a column number, to help us track down the issues.  You may also receive an error like this while trying to use the VUSB libraries, which is due to this same issue:
conflicting types for ‘usbDescriptorString0’

What you have to do is file and replace 'PROGMEM' in a few different files with  'const PROGMEM'.  You can run this simple find/replace on your own files:

shell$  cd ~/sketchbook/libraries/UsbDevice/
shell$  perl -p -i -e 's/PROGMEM/const PROGMEM/g' * 

shell$  cd ~/sketchbook/libraries/UsbKeyboard/
shell$  perl -p -i -e 's/PROGMEM/const PROGMEM/g' * 

shell$  cd ~/sketchbook/libraries/UsbStream/
shell$  perl -p -i -e 's/PROGMEM/const PROGMEM/g' *


Great, but now we're likely to run into the problem:
avr-gcc: error: unrecognized command line option ‘-assembler-with-cpp’


Yes, GCC throws an error with this as well.  The fix here is to downgrade the packages 'avr-libc' and 'gcc-avr' to either Debian Stable (Squeeze) or something from the Debian Snapshots, which is likely a newer version of the libraries (instructions here).

I went with Debian Stable for this; I made sure Debian Stable was in my /etc/apt/sources.list file and updated my catalog, found the older versions of the packages, and installed them:

shell$  sudo aptitude update
shell$  sudo apt-cache policy avr-libc
shell$  sudo apt-cache policy gcc-avr
shell$  sudo aptitude install  gcc-avr=1:4.3.5-1+b1 avr-libc=1:1.6.8-2


Now, my Arduino sketch compiles cleanly!  Most excellent.  Does it do what I want?  I have no idea yet :) but I hope this helps you get past some of the errors that you've maybe been having!

Leave a message if this helped you and please come back again.



Thank you so much to the various websites, bug reports, and forums which have helped me get through these errors, including:

Obdev.at, http://forums.obdev.at/viewtopic.php?f=8&t=6424
CodeAndLife.com, http://codeandlife.com/2012/01/29/avr-attiny-usb-tutorial-part-3/
Libelium.com, http://www.libelium.com/forum/viewtopic.php?f=14&t=9833

and to the creators of the VUSB libraries,
Obdev.at, http://www.obdev.at/products/vusb/index.html
and the RancidBacon port of VUSB to Arduino, available at
http://code.google.com/p/vusb-for-arduino/



Using the Arduino as a Keyboard, some resources as much for me, later, as for you now:

Arduino USB HID Keyboard contains a few examples:
http://mitchtech.net/arduino-usb-hid-keyboard/

USB-Keyboard with Arduino and V-USB library, an example
http://blog.petrockblock.com/2012/05/19/usb-keyboard-with-arduino-and-v-usb-library-an-example/

The Teensy board ($19), which is like a specialized-for-USB version of Arduino and offers keyboard codes,
http://www.pjrc.com/teensy/usb_keyboard.html

These CodeAndLife over-the-top amazing tutorials which I'm still wrapping my head around, AVR ATtiny USB Tutorial
http://codeandlife.com/2012/01/22/avr-attiny-usb-tutorial-part-1/
http://codeandlife.com/2012/01/25/avr-attiny-usb-tutorial-part-2/
http://codeandlife.com/2012/01/29/avr-attiny-usb-tutorial-part-3/
http://codeandlife.com/2012/02/04/avr-attiny-usb-tutorial-part-4/
http://codeandlife.com/2012/03/03/diy-usb-password-generator/


And, far more that I can understand about USB but apparently, made simple:
http://www.usbmadesimple.co.uk/ums_4.htm



Viewing all articles
Browse latest Browse all 61

Trending Articles