Monday, December 22, 2008

Three Easy Pieces: 3 different ways to program the TouchShield

This past weekend, I met up with a friend of mine who is one of those I'm-an-engineer-at-heart-but-like-memorizing-integrals-too-much-so-I-studied-physics-instead kind of guys :) He let me borrow a copy of his CD collection of Richard Feyman's lectures on audio. To be honest, if everything I learned in undergrad had been taught like that, I think we'd probably have a lot more people studying engineering. Anyway, I've gotten a couple of emails since Chris and I wrote the new core, so I thought I'd try to explain a little more how the new TouchShield core is different that the old one...

The new core changes a lot... but it can also be confusing, so I hope this blog post helps a little. There are three ways to write programs on the TouchShield:



1) Beginner - this is pretty much the easiest way to program the TouchShield. It puts the TouchShield into Canvas mode, so that the Arduino just sends out commands to it over the serial connection. If you write a new program, you just change whatever program was running on the Arduino, and you never really change the TouchShield code. In the Linux world, the equivalent is the X server. X is the graphics server that accepts drawing commands from applications, and displays buttons, lines, windows, and pictures on the screen. The final touch is that programs written on the Arduino can use the same functions that are used in the Processing graphics language (or a subset at least).

This works like by putting this on the TouchShield:

void setup(void) {
}

void loop(void) {
beginCanvas();
}

And then by using functions on the Arduino like:

drawcircle(), drawrect()




2) Intermediate - this was the old way to progam the TouchShield, and the way most of the examples are written over at liquidware.com to date. In this setup, you write one program for the Arduino, and one program for the TouchShield. This setup meant you had to use tricks like this http://antipastohw.blogspot.com/2008/12/passing-large-values-between-arduino-to.html to send data between the Arduino and TouchShield. These programs are still possible to write, but the new core simplifies a lot of the cross-talk.

This works like by putting this on the TouchShield:

void setup(void){
Serial.begin(9600);
}
void loop(void){
buttonValue = Serial.read();
if (buttonValue) rect(10,10,25,25);
}


And then by using functions on the Arduino like:

AFSoftSerial mySerial = AFSoftSerial(RXPIN, TXPIN);
void setup(void){
mySerial.begin(9600);
}
void loop(void){
serial_sendDigital(getInputShieldButton(1));
}



3) Advanced - actually, I just created this category for Cathedrow and Josh, since they're insane. In this setup, you can actually access the low level functions of the TouchShield's OLED to do things like bitmap testing, bulk blitting, scanlining, and a whole slew of things that are usually only tricks known to game programmers. In fact, the only reason the Arduino is there is to provide power to the TouchShield, especially if the goal is to try to optimize the speed of the code (and you don't want to spend time communicating with the Arduino).

If you're coding this way, you probably are using lots of lines like:

#ifndef _CLTS_DEFINES_
#define _CLTS_DEFINES_
LCD_CTRL_PORT &= ~(1 << LCD_DC);
LCD_CTRL_PORT |= ((1 << LCD_CS) | (1 << LCD_RD));



No comments: