 |
ooPIC Programmer's Guide
Chapter 3 - Introduction to OOP
|
|
|
|
What is OOP?
|
| | OOP is an acronym for Object-Oriented Programming. It is a term used to identify languages that use Objects. As we discovered in chapter 2, Object-Oriented Programming makes it very easy to write programs. This is because Object-Oriented programs are all about telling the objects what to do and letting them do all of the actual work for us. Technically speaking, the objects in an OOP language are also programs. The oLED Object used in chapter 2 for example is a very small program that knows how to turn an LED on, off, and control how bright it is. Other than the fact that OOP uses object, the rest of the program works exactly the same as non-Object-Oriented programs. |
Why Objects?
|
| | An Object-Oriented Programming language is advantageous because humans instinctively know how to interact with physical objects. By arranging a programming language so that a programmer can interact with virtual objects in the same way as real objects, the learning curve is reduced significantly. The statement "LED.TurnOn" that was used in chapter 2 for example needs no explanation. This is because the idea of turning on a light is a familiar one. Compare this to a statement like "Out 7, 1" used by other non-Object-Oriented programming languages to turn on a light. In non-Object-Oriented programming it is unfortunate but necessary that the method used to control physical objects is several dozen or so unrelated commands and keywords that need to be memorized. In fact, several of these commands and keywords are proprietary and are not found in any other vendor's languages. This creates a situation where it is difficult to keep up with all the different "flavors" of a language. In OOP languages, most of the processing and functionality has been moved to the Objects. The language itself is reduced to a small set of commands and statements needed to handle the program flow and Object manipulation. This reduces the amount one needs to memorize. In addition, the functionality that has been moved to the objects is accessed in familiar wording. E.I. LED.Turnon Another consequence of non-Object-Oriented programming languages is the processing is sequential. This results in a program where only one thing can happen at a time. Of course, a program could be written to juggle all the things that it needs to do at the same time, but that winds up being a headache. In OOP languages, each object runs at the same time which makes the application multi-tasking by default. If a program needs to have two things happen at the same time, then it just creates two objects. |
What Can You Do With Objects?
|
| | In chapter 2, we discovered how the oLED Object can control a real LED that is connected to the ooPIC. Objects can control a wide variety of electronic devices from lights to motors to relays, from simple to complex. Another thing that an Objects can do with hardware is report what the hardware is doing. For example, one could use a Knob Object to report the position of a knob or use a Sonar Object to report how far away the wall is. Objects are not limited to hardware functions. Since objects are programs themselves, they can do just about anything. Some objects move data around in the background. Other objects manage numbers. Even the ooPIC Operating System is an object. The following table describes the types of objects you can use in ooPIC languages.
| Type | Description |
| Hardware | An object that controls and/or reports on a physical piece of hardware. |
| Processing | An object that retrieves values from one object, performs a specified calculation and then stores the resulting value in another object. These objects are the building blocks of Virtual Circuits. |
| Variable | An object that manages a value and provides evaluations about that value. |
| System | An object that controls one of the system functions. |
|
Where Do Objects Come From?
|
| | There are actually two answers to this question. The first answers the question "Where did the oLED Object learn how to control an LED?" At some point in time, someone writes a small program that does everything that is required to control an LED. This mini-program is wrapped up into what is known as an Object Class. The Object List is a list of all the object classes that have already been written. These object are ready for you to use. The second answers the question "How does my program specify what objects it needs to use." A single statement in the application's code creates what is call an Object Instance. In Basic syntax for example, the programmer uses a statement that specifies a unique name for the new Object and the Object class. Then, when the application is run and it encounters this statement in code, an instance of that type of Object will be created. The following examples shows how an instance of the oLED Object named "L" is created.
To understand the relationship between an Object Instance and its Class, think of an automobile and it’s blueprints. The blueprints would be the class, because it defines the characteristics of each car - for instance, size, shape and how the car works. This blueprint is then used to create one or more automobiles and the automobiles that are created, would be the Objects. Each Object that is created from a class is considered to be an instance of that class. |
Creating Objects
|
| | It is important to note here that the ooPIC compiler can understand more than one language, so there are several ways to create objects. In Visual Basic syntax for example, the programmer would use the Dim statement. The Dim statement expects the programmer to specify a unique name for the new Object and the Object class that tells this new object what kind of object it is. Then, when the application is run and it encounters this statement in code, an instance of that type of Object will be created. The following examples demonstrate various statements used to create an instance of an Object where the Object’s name would be "L" and the Object’s type would be oLED.
Basic syntax | Visual-Basic Syntax | C and Java Syntax |
L As oLED | Dim L As oLED | oLED L = New oLED; |
To understand the relationship between an Object Instance and its Class, think of L as the LED object that was created and oLED as the blue-prints that the program used to know how to create it. In the ooPIC Basic syntax programming language, an application must declare its intent to create an instance of an Object by using the Dim command. This statement allocates memory out of the free RAM area to hold the instance of the Object. The programmer gives the new Object a name that the application program will use to identify it with. Any attempt made in the application program to refer to an Object that has not yet been created will result in a compile time error and the compilation will not complete. The following examples demonstrates the statement used to create an instance of an Object in Basic and C/Java syntax where the Object’s name would be "TheNumberThatWeWant" and the Object’s type would be oByte.
| Basic syntax | TheNumberThatWeWant As oByte |
| Visual-Basic Syntax | Dim TheNumberThatWeWant As New oByte |
C and Java Syntax | oByte TheNumberThatWeWant = New oByte; |
Object Names
Each Object within an application program must have a unique name so that the application program can refer to it. Object Names can be anything so long as they follow standard naming conventions. The following lists the rules of the standard naming convention:
Object Names must begin with a letter
Object Names cannot contain a period.
Object Names must not exceed 32 characters.
Object Names must be unique within the application. (Object names are case insensitive)
An object is not created at run-time, but rather is static and no matter when it is defined, the object has already been given space in RAM. Objects may not be destroyed.
The name of the Object is not stored in RAM and therefore can be any length (up to 32 characters) with out affecting the amount of RAM that gets allocated for the Object's instance. Object Names are case insensitive. I.E. the names: "ThingOne" and "thingone" are recognized as the same Object.
Creating Arrays of Objects
You can declare and use arrays of Objects by specifying a subscript value in the Dim statement. In the following example two arrays of Objects are being created in Basic and then C/Java syntax, the first is an array of 3 oByte Objects and the second is an array of 6 oDio1 Objects. An ooPIC array is always starts at 1, even in C/Java syntax.
Basic syntax | Visual-Basic Syntax | C and Java Syntax |
BA(3) As oByte
PA(6) As oDIO1 | Dim BA(3) As New oByte
Dim PA(6) As New oDIO1 | oByte BA(3) = New oByte;
oDIO1 PA(6) = New oDIO1; |
These Objects are now accessed by including the subscript number. In the following example, All the values in BA(1) through BA(3) are added together in Z.
Basic & Visual-Basic Syntax | C and Java Syntax |
For X = 1 To 3
Z = Z + BA(X).Value
Next X | For (X = 1; X <= 3; X++){
Z = Z + BA[X].Value;
} |
|
Working With Objects
|
| | ooPIC Objects support properties, methods, and events. You can change an Object’s characteristics by changing its properties. In addition to Properties, Objects have methods. Methods are a part of Objects just as properties are. Generally, methods are actions that are perform on the Object, while properties are the attributes you set or retrieve. Objects also have events. Events are triggered when some aspect of the Object is changed and program code is executed in response to this change. Once an instance of an Object has been created, all references to that instance are done by specifying its name followed by a period and then the property or method that you want to reference. When an Object instance is created, it is an identical copy of its class. Once it exists as an individual Object, it can be customized. For example, if you create 3 Objects using the same class, each one of the 3 new Objects is an identical instance of that class. Each of the 3 new Objects shares a common set of characteristics and capabilities (properties, methods, and events), defined by the class. However, each one must be created with a different name and once created, each Object's properties and methods can be separately controlled.
Basic & Visual-Basic Syntax | C and Java Syntax |
Led1.TurnOn
Led2.TurnOff
Led3.BlinkFast | Led1.TurnOn;
Led2.TurnOff;
Led3.BlinkFast; |
|
Controlling Objects with Their Properties
|
| | An Object’s properties are the values that it holds. These properties can be numeric values for storage or specifying what to do or even for indicating that something was done.
Setting Property Values
You would set the value of a property when you want to change the behavior of an Object. For example, you change the Direction property of a oDio1 Object to specify whether you want the I/O Line to be an input or an output. To set the value of an Object's property, specify the Object's name followed by a period and then the property to set. The Following shows the syntax of setting an Object's property to a value:
Basic & Visual-Basic Syntax | C and Java Syntax |
Object.property = expression | Object.property = expression; |
The following example demonstrates a property being set in both Basic and C/Java syntax:
Visual-Basic Syntax | C and Java Syntax |
Dim ThingA As New oDIO1
Sub Main()
ThingA.IOLine = 1
ThingA.Direction = cvInput
End Sub | oDIO1 ThingA = New oDIO1;
Sub Void Main(Void){
ThingA.IOLine = 1;
ThingA.Direction = cvInput;
} |
In this example, an Object named ThingA is told that its IOLine property is to be set to 1 and its Direction property is to be set to 1, (cvInput). cvInput is a constant that equals 1. Therefore, after this example is executed, the Direction property of the Object with the name "ThingA" is equal to 1. When the Direction property of an oDio1 Object is 1, the State property of that Object is updated with the electrical state of the I/O Line 1. Some Objects have properties that are read only. These Objects generally have done some calculations to derive at the value set in the property and writing to them would only erase these values.
Getting Property Values
Reading the value of a property is done when the application program needs to find the state of an Object. For example, a program can determine whether or not a voltage is present at an I/O Line by reading the State property of a oDio1 Object which points to that I/O Line. To get the value of an Object's property, specify the Object's name followed by a period and then the property to get. The Following shows the syntax of getting an Object's property value:
Basic & Visual-Basic Syntax | C and Java Syntax |
Variable = Object.property | Variable = Object.property; |
In the following example the state of one oDio1 is read, and a second oDio1 is set to its value.
Basic & Visual-Basic Syntax | C and Java Syntax |
Dim ThingA As New oDIO1
Dim ThingB As New oDIO1
Sub Main()
ThingA.IOLine = 1
ThingA.Direction = cvInput
ThingB.IOLine = 2
ThingB.Direction = cvOutput
Do
ThingB.State = ThingA.State
Loop
End Sub | oDIO1 ThingA = New oDIO1;
oDIO1 ThingB = New oDIO1;
Void Main(Void){
ThingA.IOLine = 1;
ThingA.Direction = cvInput;
ThingB.IOLine = 2;
ThingB.Direction = cvOutput;
Do{
ThingB.State = ThingA.State;
} While (1)
} |
A property value can also be used in a complex expressions. In the following code example, MyNumber is set to either 0 or 5 depending on whether or not there was a voltage present on the I/O Line at the time the statement is executed.
Basic & Visual-Basic Syntax | C and Java Syntax |
Dim ThingA As New oDIO1
Dim MyNumber As New oByte
Sub Main()
ThingA.IOLine = 1
ThingA.Direction = cvInput
MyNumber.Value = ThingA.State * 5
End Sub | oDIO1 ThingA = New oDIO1
oByte MyNumber = New oByte
Void Main(Void){
ThingA.IOLine = 1;
ThingA.Direction = cvInput;
MyNumber.Value = ThingA.State * 5;
} |
Using Default Properties
Objects have what is called a default property. Default properties can be used to simplify the application’s code, because the code does not need to refer explicitly to the default property when setting or retrieving the value. The default property can be set when the Object is dimensioned. In every case, the default property is the first property of the object. For an Object where Value is the default property, the following 2 code fragments are equivalent:
Basic & Visual-Basic Syntax | C and Java Syntax |
Object = 20 | Object = 20; |
And
Basic & Visual-Basic Syntax | C and Java Syntax |
Object.Value = 20 | Object.Value = 20; |
Variable Objects will always use "Value" as the default property. Not all Objects have a default property, in these cases, specifying the properties will always be required. Another example, In the following 2 code fragments, the value of the variable Q is incremented by five.
Basic & Visual-Basic Syntax | C and Java Syntax |
Q = Q + 5 | Q = Q + 5; |
And
Basic & Visual-Basic Syntax | C and Java Syntax |
Q.Value = Q.Value + 5 | Q.Value = Q.Value + 5; |
Refer to the ooPIC technical guide for a list of Object properties. |
Performing Actions with Methods
|
| | A method is an action that an Object can perform. When invoked, they instruct the Object to do a specified function.
Using Methods in Code
You would use a method when you want the Object to perform a specified function. For example, an Object will increase its Value by 1 when the Inc method is invoked. To invoke an Object's method, specify the Object's name followed by a period and then the method to invoke. The Following shows the syntax of invoking an Object's method.
Basic & Visual-Basic Syntax | C and Java Syntax |
Object.method | Object.method; |
The following example demonstrates a method being invoked.:
Basic & Visual-Basic Syntax | C and Java Syntax |
Dim ThingA As New oLED
Sub Main()
ThingA.TurnOn
End Sub | oLED ThingA = New oLED
Void Main(Void){
ThingA.TurnOn
} |
In this example, an Object named ThingA is told to Turn On. |
Responding to Events
|
| | An Event is any action that has happened within an Object. Often, an application program will need to perform a procedure when a particular Event has occurred.
The oEvent Object
The oEvent Object is a special ooPIC Object that will execute a specified sub procedure when it's Operate property is set to 1. The oEvent Object's Operate property can be linked to a Virtual Circuit allowing any event in the ooPIC to trigger events. |
Linking Objects Together
|
| | Several of the Objects defined in the ooPIC languages are capable of manipulating the properties of other Objects. This type of configuration can exchange data and do calculations in the background while the application program is focusing on what to do with, and how to react to the state of the objects. When objects are used to manipulate the properties of other objects, the set of objects is referred to as a Virtual Circuit. The building blocks of Virtual Circuits are the Processing Objects. These Objects retrieve their input values and store their output values in the properties of the other Objects. For further information on Virtual Circuits can be found in Chapter 8 - Virtual Circuits |
Dynamic Data Exchange
|
| | Utilizing Virtual Circuits, The ooPIC programming language has the ability to communicate and exchange data with the Objects within an entirely different ooPIC. This is helpful when you dedicate one ooPIC to do a particular function and want to control its activities from another ooPIC or other computer. The ooPIC uses the Philips I2C two wire network to transfer information about the values within the Objects to other computers hooked into the I2C network. Up to 127 different components can be collectively connected to this network by attaching 2 wires and a ground reference. No specialized cables or components are required to use the I2C network. For further information on the Philips I2C network, see Chapter 12 - Dynamic Data Exchange (DDE) |
Further Reading
|
| | The software industry defines several fundamental principles that qualify a language as Object-Oriented. (OOP) These principles include encapsulation, inheritance, and polymorphism. Although the ooPIC manuals do not refer to these principles often, it is useful to understand them. OOP is a method of programming that seeks to mimic the way we perceive things in the real world. OOP exploits our natural tendencies to generalize and classify and allows us to group logically related portions of an application. In most OOP languages, the OOP language extensions allow you to create extremely complex Object models that can closely mimic real life Objects. With ooPIC however, the greatest advantage of using Object-oriented techniques is the ease at which you can interface with the physical hardware components present within the ooPIC itself. Encapsulation simply means to group related items together. Rather than treating a number of logically related variables and functions dedicated to manipulating those variables as unrelated pieces of code, encapsulation collects them together in groups called classes. Other parts of the program can then use the variables and functions as a single Object. Once encapsulated, variables are referred to as properties of the Object and the functions are referred to as methods of the Object. Through encapsulation, much of an Object's complexity can be kept internal to the Object. And only the few select methods and properties that need to be available to other parts of a program are exposed. These exposed methods and properties, which can be accessed, are called the Object's interface. The term Data Abstraction refers to the way an Object's interface doesn’t reveal the way the Object stores its data or performs its methods. The result of data abstraction is a simple Object interface that interacts with functionality that is considerably more complex. Polymorphism is a term borrowed from the Greek language. It means literally "Having many shapes". In OOP this is seen in having multiple Objects use the same names for properties and methods. For example, there are several Hardware Objects that have the IOLine property which is used to select an I/O line for the hardware to use. Even thou the function of each Object is different, the name "IOLine" is the same. This makes the list of things to remember shorter. Another example is the Clear method. This method can be invoked without the programmer knowing the Object's exact data type and it will always have the same effect. Inheritance allows you to base one Object type on another and build new derived Objects. Since ooPIC's Objects are hardware based and the routines are written in optimized assembly language and stored in PROM, there is no way too physical create a new Object type to inherit the class of another. However, by creating Virtual Circuits, and User Classes, inheritance is accomplished. |
|