|Propeller Programming: Objects in Spin|
|All News -> Designer News|
|Written by Nicholas McClanahan|
|Tuesday, 06 April 2010 10:30|
A tutorial on using objects in Spin
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 OTHER 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
That's my intro on using objects in Spin. Here are some resources to keep reading more;
My 'Intro to Spin' Tutorial
Propeller Manual (pdf)