Using tcl/tk with the GP3

One of the nice things about the GP3 is that you use just about any programming language you like to program it. Today I decided to see how easy it would be to use the GP3 to do control functions using tcl/tk. Well actually I'm only going to use tcl -- but if you want a simple way to make windowed interfaces tk is VERY simple to use and I'll show you some snippets. But I don't want to get distracted on GUI programming in this article.

I did my tests on Linux, but tcl is very portable as is the GP3 library, so the same basic principles will apply even if you aren't using Linux.

The easiest way to get the GP3 working with tcl is to use >makeWIG on the GP3 library. The library can be built for Linux, Windows, or Cygwin. You can build it static or as a shared object (a DLL on Windows). I started with the shared object version which I have installed on my system as normal system libraries (that is, I have /usr/local/include/gp3lib.h and /usr/local/lib/libgp3lib.so.1). Keep in mind SWIG can build interfaces for lots of languages, not just tcl. SWIG supports 18 languages including Perl, PHP, R, C#, and lots of other languages too.

To use SWIG you have to create a gp3.i file. This is really easy. First open up the gp3lib.h file and copy just the function declarations (you can take the comments too, if you like, but you don't need them and you don't want any of the lines that start with #). Then put "extern" in front of each declaration. You should wind up with lines that look like this:

extern int gp3openport(char *port);  // returns -1 for error

At the start of the gp3.i file, you should put these two lines:

%module gp3
%{

Go down to the bottom of the function declarations and put a new line at the very end:

%}
Now copy all the declarations you modified (the ones between the %{ and the %} tokens and paste them at the very end of the file. It looks silly but you'll have one copy between the %{ and %} and one copy after. Save the file, you're done with it.

There are a few options you might want to pass to SWIG if you are an advanced tcl user, but you will probably do fine with this:

swig -tcl gp3.i

That's going to create some C files in the same directory. You need to compile these into a shared library (if you aren't using Linux, here's where things will get different; read the SWIG documentation):

gcc -fpic -c gp3_wrap.c
gcc -shared gp3_wrap.o -lgp3 -o gp3.so

That's it! I wired the LED output to the analog 0 input just to have something to control and measure. You can start tclsh (or wish, if you prefer; I personally like tkcon, if you have a copy of that, but any of these will work). At the prompt, enter (adjusting for your serial port, of course):

load gp3.so gp3
gp3openport /dev/ttyS0
gp3setLED 1
gp3a2d 0
gp3setLED 0
gp3a2d 0

The first a2d call should return a bit over 900 counts and the second one should return zero or very close to it (remember, there's a wire I added connecting the LED output to the analog 0 input).

You can cause delays with the after command in tcl, so here's a fun LED flasher:

proc ledon {} { gp3setLED 1; }
proc ledoff {} { gp3setLED 0; }
load gp3.so gp3
gp3openport /dev/ttyS0
set i 10
while { $i != 0 } {
  ledon
  after 500
  ledoff
  after 500
    set i [expr {$i - 1}]
}

Of course, you can use the GP3 calls in tk callbacks. So if you have a GUI where you want the button to turn on the LED you could have the command be "ledon" for the button and then use the same ledon proc shown above.


Site contents © 1997-2018 by AWC, Houston TX    (281) 334-4341