This is part 1 of a multi-part series. The other parts are here.
The Parallax Propeller can be programmed C, Spin, or Assembly; I think most people use Spin. I like Spin, and one of it's best features is it's 'object oriented lite' design. I thought it might be useful for beginners if I walked through the basic blocks of Spin programs.
SPIN BASICS
I talked a bit about Spin with my Miss Princess walkthrough, but here's the big picture - Spin is a high-level language that looks like a mix of BASIC and Python. It's organized into blocks:
- VAR Holds global variables
- CON Holds program constants
- PUB & PRI Hold code
- OBJ Links to other objects
There's one more block, DAT, but I'll skip over that for now, it's not always used. Here's a screenshot to show you what a typical program look like. Don't worry about the code it contains for now, but it should give you an idea of how the blocks come together.
Spin is also object-oriented. I call it OOP-lite because it doesn't implement some of the more esoteric OOP concepts. Let's look at the different blocks in Spin:
CON
The CON block holds constants. If you want an easy way to change a fixed value in your program, CON is the ticket. For instance:
Run this code and see the results. You can change every instance of ledpin by just changing the value in the constant. The CON block can also hold formulas:
but note that they will calculated when compiled and can't be changed in the program. Use formulas to make your code easier to read.
Two special constants you'll often see are '_clkmode' and '_xinfreq'. These constants set the initial clock speed of the propeller. If you don't specify anything here, the Prop will use the internal oscillator to run at 12 MHz.
VAR
The VAR block declares global variables. These variables will be accessible to every method in your program (object).
First, decide how big your variable needs to be. Byte: 0 - 255, Word: 0 - 65,535, Long: -2,147,438,648 - 2,147,438,648. Then, decide if you'd like an array, or just a single variable. Here's how the syntax looks;
The last VAR creates 10 longs in a row called positionhistory. I can refer to each by using the index number - positionhistory[1] refers to the first long, positionhistory[5] refers to the 5th.
It's a bit weird, but you can't set an initial value in the VAR block. You can do that in the first lines of your program, though.
PUB & PRI
These blocks hold code. Program execution begins at the first PUB block in your program. It doesn't matter what it's called, but I usually call it 'main'
The most basic example
PUB and PRI work a lot like functions in other languages. That means you can pass it value:
and you can also have it return a value to whatever called it:
When this method runs, the value of the variable result will be returned.
One more trick up it's sleeve. Plenty of programs use throwaway variables like i when incrementing. You can declare these variables with the method for cleaner code;
I've declared 'i' in the first line with a pipe. Multiple variables can declared using a comma;
The difference between PUB and PRI is minor. PUB methods can be called from other objects. PRI methods cannot - this is a good segue to the next block!
OBJ
The OBJ section usually looks something like this:
This tells the compiler: If I refer to a method in the pwm object, check the file 'pwm.spin' for it. You refer to methods in other objects with the syntax objectname.methodname
When your program compiles, the compiler will check a few directories for the pwm.spin:
1 - The directory your main program is saved in
2 - Any currently open files
3 - c:/program files/Parallax/Propeller Tool
If it doesn't find the file, it will spit out an error. Also, you don't need to include the filename extension but I've put it here for clarity.
I'll do a more detailed discussion of Object Oriented Programming and Spin in a separate walkthrough, but I wanted to show you the basics of using objects.
That's it for now! As usual, let me know if you have any questions.