Thursday, April 1, 2010

Qtouch libraries

I'm so down with Atmel's Qtouch library. Basically, it lets you add capacitive touch detection to your projects- and it works through non-conductive surfaces. Think touch-sensitive sealed keypads, or the scroll wheel from the older generation of iPods.

Nobody- as near as I can tell- has this working with the Arduino environment yet. It should be doable, because you can do it in AVRStudio compiling code through WINAVR, which is just a distribution for AVR-GCC, which is what Arduino compiles its code with. I'm working on it, but here, in the meantime, is a walk-through of getting the Qtouch demos working with the '328P found on the Arduino board (under Windows- don't bug me about figuring out how to use it in Linux):

1. Download and install AVRStudio from Atmel- annoyingly, you have to fill in a long-ish form with the standard information about who you are and what you're doing. This is not, in and of itself, TOO annoying- but the fact that they make you do it for each download- even if you've registered before- is.
2. Download and install WINAVR from the SourceForge site- they do NOT make you register.
3. Download and install the Qtouch library from Atmel- you do NOT need to download and install Qtouch Studio- this is a frontend used for interfacing with some example projects that they have made and if you have the cha-ching to buy those you're probably not reading these instructions anyway.
4. Create a folder for your project- I don't care where you put it, but you'll want to have some files conveniently co-located to ease the paths in your include statements.
5. Move the following files from the Qtouch directory (default C:\Program Files\Atmel\Atmel_QTouch_Libraries_4.0) to the project directory (no need for subdirectories):
  • \include\touch_api.h
  • \AVR_Tiny_Mega_XMega\QTouch\common_files\touch_qt_config.h
  • \AVR_Tiny_Mega_XMega\QTouch\common_files\qt_asm_tiny_mega.S
  • \AVR_Tiny_Mega_XMega\QTouch\example_projects\avr5g1_qt_example\avr5g1_qt_example.aps
  • AVR_Tiny_Mega_XMega\QTouch\example_projects\avr5g1_qt_example\main_atmega324p.c
6. Fire up AVRStudio, and in the initial "Open Project" window, open the .aps file you just copied into your project directory. Expect some "project files not found" errors.
7. "Find" the project files that are "missing"- these will be listed in the project frame on the left side as files with a little red slash through them, and they will be the files you copied into your project directory.
8. Under the "Project" menu, choose "Configuration Options". You'll make the following changes:
  • In the "General" tab (selected from the icons on the left), you'll change the device listing to "atmega328p" from "atmega324p".
  • In the "Libraries" tab, you'll want to add the Qtouch library directory to the search path. By default, it's "C:\Program Files\Atmel\Atmel_QTouch_Libraries_4.0\AVR_Tiny_Mega_XMega\QTouch\library_files"
  • In "Custom Options", find the item in the list that says "-DSNSK1=A" and change the 'A' to 'C'. This is the one that took me some time to figure out, but it's related to which ports can be used for the SNS and SNSK functions. Ports B, C, and D can be used on the 328P, but A, B, C, and D can be used on the 324P. If you don't change that, you're trying to assign the role of SNSK to a non-existent port and you'll get an error in the assembler saying "Error: constant value required" in the qt_asm_tiny_mega.S file.
And that's it! Save your changes, hit "Build" and the example should compile up just fine. Some caveats at this point:
  • All you've done is prepare a template. You still need to assign pins to various functions, write code that checks the inputs and does something, and perhaps most importantly, make some kind of hardware capable of receiving the input. I haven't done ANY of these things yet.
  • You probably need a programmer to put this on your Arduino board, but I'm not sure. I think you can use AVR-DUDE to drop the generated file into memory using the bootloader but that's an exercise I've not tried yet.
  • There are a lot of things I don't know about this yet, but this is a jump-start for others because it took me a few hours to figure out all the necessary settings and changes to make to get the 328P compile working.
As I figure more stuff out, I'll continue to post- hopefully I'll figure out how to get all this working under Arduino, so more people can use it easily!