Forth

Forth on RPi

Start the Forth interpreter with ./atlast and exit with Ctrl-d

atlast.html and atlast.pdf is included and is an extensive atlast-forth manual. Read it online and read about Forth itself here  and here  (Beware that the Forth dialect in the book Starting Forth is a bit outdated compared to Atlast Forth). Leo Brodie wrote another book, Thinking Forth, read it here

Adding new words
Most of the power of Atlast Forth derives from the ease with which C coded primitives can be added to the language. In my case I will add some words for controlling my Gertboard. There is a detailed description on how to do this in the atlast-forth manual. And you can copy much of the word implementations from the gertboard_sw directory if you have downloaded the gertboard demo files. It is easy to add your own words, just add a "define GERTBOARD" to atlast.c around line 56.

Include the gb_common.h right after include 

Then add your own word definitions at the end of the section with word definitions, around line 2704, right after #endif /* COMPILERW */ in atlast.c

And finally, add the actual words to the Table of primitive words, right after #endif /* EVALUATE */ at line 2960 or so.

As we are using code from the Gertboard demos, copy the files gb_common.o and gb_common.h from the gertboard_sw directory to atlast-1.2 directory (it's there if you have run make in this directory as well).

Add gb_common.o to the file Makefile in atlast-1.2. Now, save and run "make" again to recompile atlast.c.

Test the new words
Wire up the Gertboard according to the information you get when you run the command sudo ./leds in the Gertboard demo directory.

Run sudo ./atlast in the atlast-1.2 directory.

Type 1 gertboard

Type 22 1 setio and press enter, the corresponding LED will go on.

Type 22 0 setio and the LED will go off.

Type 0 gertboard

Play with it
Define your own LED demo, start the interpreter with sudo ./atlast. Define these words:

Now type use gertboard leds on and press enter and all leds should go on. Type leds off and they should go off again. And finally type free gertboard

A real Use Case
I have a kWh meter that I would like to read with the RPi. On the meter there is a small light that blinks once per 3.6 sec at 1 kW. So I need a way to detect time between pulses. I mounted a simple LDR over the blinking light and connected it to Gertboard Buf1 and ground. Set B1 as an input with a jumper on the board and connect GP25 to B1.

Now that you know how to add a primitive word to atlast I just list the code for the word: Test the command Result: Quite good, I started with 0.685714 as the result of kw f. and after turning on a 40W lightbulb I got 0.724346.

However, having the kW load on the command line in a terminal is not good enough, I want it online on the web or as a mobile app. If you are interested, follow me over to the Erlang page where I vill use this Atlast Forth application as an Erlang Port to access Gertboard from an Erlang process.