Spin is an 'object-oriented lite' programming language. I like it because it eschews some of the more esoteric concepts of OOP but maintains most of the productivity gains. In the blinky light tutorial we went through the syntax of Spin, and in this walkthrough you'll see how objects work and how you can use them.
There's also a video tutorial on using objects with servo control as an example;
What is an object?
An object is: "a collection of methods and/or supporting data in a .spin file." Let me break that statement down for you;
You have a 'Top Object File', and program execution begins with the first PUB method in your top object file. The Propeller Tool has tabs so you can have multiple objects (.spin files) open at the same time. Your top object file is simply the active tab when you compiled your code.
the Keyboard_Demo.spin file is our top object file because it was the active tab when I compiled my code. It refers to 2 other objects, TV_Terminal.spin and Keyboard.spin. And TV_Terminal.spin refers to 2 other objects, TV.spin and Graphics.spin.
Why use other objects?
Your program doesn't have to be split up into multiple objects. Many simple programs shouldn't be split into multiple objects, too. So when should you use other objects? When it simplifies code re-use and debugging.
How to use objects
1 - Tell the Propeller tool you're going to refer to a method in another object. In your top object file, add an OBJ Block. You tell the Propeller tool to include the object with the syntax: name : file. In the instance below, the name for the first object is term. And the file tv_terminal.spin. The Propeller Tool will search through the active directory and 'c:/program files/Parallax/Propeller Tool' for a file with that name. If it doesn't find the file, it will throw up an error.
2 - Run a method in the other object with the syntax: name.method. Look at the object you've included and you should see several public methods. To execute the start method in the term object, your code would be: term.start. If the Propeller Tool doesn't find a method with the name start in that object, it will throw an error on compilation.
How objects aren't just a collection of methods
Variables declared in an object's VAR block(s) are only available to methods in the same object. There are some ways to get around this, though.
PRI methods can only be called from within their object. They cannot be called directly from another object.
In the OBJ block, if you do 'PWM : 'PWM', you've declared 2 instances of the PWM object (PWM and PWM). Each object will have the same program code (PUB, PRI, DAT), but different VAR code.
Values declared in an object's CON block(s) can be read with the hash; objectname#Convalue. If your display object has a contstant called 'pin', you can read that from another object with display#pin. Remember that CONstants cannot be changed in runtime.