Deprecated: Assigning the return value of new by reference is deprecated in /home/nicholas/public_html/ggnew/components/libraries/cmslib/spframework.php on line 101

Deprecated: Assigning the return value of new by reference is deprecated in /home/nicholas/public_html/ggnew/components/libraries/cmslib/spframework.php on line 644
Tutorial: Objects
Tutorial: Objects

<< Go Back to Other Tutorials

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;

  1. "A collection of methods and/or supporting data"
    Most objects will have a few methods, and some supporting data / variables.
  2. "In a .spin file"
    An object is simply a .spin file.  Creating a new object = creating a new .spin file.

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. 

EXAMPLE
Say you've got a robot with an LCD Matrix display.  You'll probably want to put the LCD Matrix display functions in a separate object (or re-use someone elses object).  It might have some simple methods like display.dec(val) to print a number on the screen, and display.clr to clear the screen. In your main program, you'll have code that's easier to read and understand;

IF (robotstatus := "Stuck")
display.dec(1)
waitcnt(clkfreq +cnt) ' wait for 1 second
display.clr

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 are only scoped to their object.
    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 cannot be called by other objects.
    PRI methods can only be called from within their object.  They cannot be called directly from another object.
  • You can create multiple instances of a single object
    In the OBJ block, if you do 'PWM[2] : 'PWM', you've declared 2 instances of the PWM object (PWM[0] and PWM[1]).  Each object will have the same program code (PUB, PRI, DAT), but different VAR code.
  • Other objects can read the CONstants from another object.
    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.

  • << Go Back to Other Tutorials

     

    Other Stuff