Gforth interface to the wiringPi GPIO interface library for Raspberry Pi
Forth
Permalink
Failed to load latest commit information.
examples
.gitattributes
.gitignore
README.md
UNLICENSE.txt
wiringPi.fs

README.md

wiringPi.fs defines an interface to the functions in the wiringPi GPIO interface library, allowing those functions to be used in Gforth programs on a Raspberry Pi.

Installation

You can install Gforth on a Raspbian system with apt-get:

sudo apt-get install gforth

Gforth's C interface requires a C compiler toolchain, which is usually already installed on a Raspbian system. It also requires libtool, which is usually not installed by default, so you can install it like this:

sudo apt-get install libtool-bin

To install the most recent available version of the wiringPi library, follow the instructions here: http://wiringpi.com/download-and-install/

Note: The Raspbian distribution includes a wiringpi package that can be managed with apt-get. However, that package may lag behind the most recent official version of wiringPi available from http://wiringpi.com.

Usage

Access to the GPIO hardware requires root access, so you must invoke Gforth with sudo gforth when using wiringPi.

To call wiringPi functions from Gforth, push the arguments onto the stack and then invoke the corresponding Forth word. For example, if you would write this in C:

pinMode(18, OUTPUT);
digitalWrite(18, HIGH);

Then you would write this in Gforth:

18 OUTPUT pinMode
18 HIGH digitalWrite

When translating wiringPi code from C to Gforth, remember that result codes cannot be ignored. For example, the wiringPiSetupXXX() functions each return a status code (always 0), which you need to DROP to keep your Forth stack balanced.

To load the interface into Gforth and use it interactively, launch Gforth like this:

sudo gforth wiringPi.fs

To use wiringPi within a Gforth program, use require wiringPi.fs or one of the other ways to read Forth source files.

Here is a complete example of a Gforth program that uses wiringPi to make an LED blink:

\ Blink an LED connected to GPIO pin 18
\ 
\ Must be run with root permissions. For example: "sudo gforth blink.fs"

\ Load the wiringPi Gforth interface.
require wiringPi.fs

\ Initialize the wiringPi library.
wiringPiSetupGpio drop

\ Declare what GPIO pin we are using, and set it to output mode.
18 constant ledPin
ledPin OUTPUT pinMode

\ Define words to turn the LED on/off, and to wait a bit.
: ledOn   ledPin HIGH digitalWrite ;
: ledOff  ledPin LOW digitalWrite ;
: pause   500 delay ;

\ Our program is a simple loop that never ends.
: blink   begin ledOn pause ledOff pause again ;

\ Start our program.
blink

The first time you use wiringPi.fs, Gforth will build a library in your /root/.gforth/libcc-named/.libs/ directory. Subsequently, Gforth will reuse that cached library. Note that Gforth will continue to use that cached library even if you make your own changes to wiringPi.fs, so if you do make changes, you need to delete the libraries in that directory or change the name for the c-library declaration in wiringPi.fs.