OOPic Programming Fundamentals Compiled vs. Interpreted programs –Compiled program converts high level code into native machine language e.g., y=x+5; Machine language runs on the target Done in levels Cross-compiler code program on one machine (PC) generate machine code for another machine (microcontroller) Features –Fast –Interpreted program executes high level code on the machine that the interpreter is running on Each line of code is interpreted and run each time each time the program is run Fetch token from EEPROM wait for fetch complete decode token (objects that represent an action) locate native code associated with token execute native code advance EEPROM address Features –Easier to write an interpreter than a compiler –Slow
Object Oriented Pic Object conceptual approach allowing the programmer to interact with what appears to be a physical object –Grouping of code and data treated as a single unit Like pre-written code libraries –Examples: oFreq oButton oLCD Why objects? –More intutive, faster to learn to use –Functionality built into the objects, so coding becomes mostly just manipulating objects (rather than reading and writing to memory locations) –Like having prewritten code libraries Ex: PWM –“multitasking code” objects operate “continuously” rather than line-by-line –Can link objects together
Types (classes) of Objects Hardware –Represents or encapsulates a physically implemented piece of hardware: oDio1 oA2D10 Processing –Retrieves values from other objects, performs calculations, stores resulting value in another object oMath Variable –Stores a value and provides evaluation properties about that value oByte System –controls one of several system functions oOOPic
Creating Objects Need to create an instance of the particular class of object –oA2D10 light_level = new oA2D10; Identifier Names must begin with a letter Identifier Names cannot contain a period. Identifier Names must not exceed 32 characters. Identifier Names must be unique within the application. (Identifier names are case insensitive) The name of the Identifier 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 Identifier's instance –Not sensitive to case: Light_level = = liGHt_LeVEl –Creates an instance of an oA2D10 object (10 bit A/D) –Must be done at the beginning of the program –Allocates memory out of the free RAM to hold the instance of the Object, and is added to the Object list in the order defined If object operation is time critical, place next to each other –86 bytes of object memory available (96 total, but OOPic object takes 10 bytes) Don’t add object that you don’t need! They will take up memory and operating time Operating system steps through the object list and executes the defined properties of the object
Creating Objects, cont. Can create arrays of objects by specifying a index value when the object is declared –Example of array of 3 A2D10 objects oA2D10 light_level(3)= new oA2D10; –Access using index value: Z=light_level(1).value; Some class of some objects have variable size –Example oGate (provides logic gate functions) with two inputs oGate 2-input_OR = new oGate(2);//declare 2-input OR gate oGate objects can have up to 8 inputs
Object Properties Properties are things that allow you to customize the characteristics of an object and determine the value an object holds. Some of the common ones: –Result A value (number) the result of processing done by the object –Value A value (number) specifying the state, content or magnitude of an Object –Flag A value (number) that signals the beginning or end of an object process –String Allow the value to be represented as a string –Operate Turns the object on (enables it to respond to input changes) –It will hold the state of its last change until operate property set to 1 Syntax to set: –object_name.Property=expression; –Ex. oA2D10 light_level = new oA2D10; light_level.IOLine = 1; // Map A2D10 object to A/D line 1 light_level.Operate = cvTrue;// Enable A2D10 object Syntax to determine the state of an object: –Variable=object_name.Property;
Object Methods Methods are actions that objects can perform
Object Method Example oDio4 Nib1 = new oDio4; oDio4 Nib2 = new oDio4; Const num1 = 1;// Defines the constant “num1” equal to 1 Sub void Main (void) Nib1.IOGroup = 1;//IO lines 8-15 Nib1.Nibble = 0;//IO lines 8-11 (the lower 4 bits) Nib1.Direction = cvInput;// Digital inputs (cvInput = = 1) Nib2.IOGroup = 1;//IO lines 8-15 Nib2.Nibble = 1;//IO lines (the upper 4 bits) Nib2.Direction = 0;// Digital outputs (cvOutput = = 0) Nib2.Value = Nib1.Invert;//Nib1 bits are inverted and sent out Nib2 Nib2 = Nib1.LShift;//Nib1 bits are shifted left and sent out Nib2
Linking Objects Together Virtual Circuit is a way to link objects together so that the state of one object can signal or modify the state of another object, like two elements connected in an electronic circuit –Can have any number of objects in the circuit –Need at least one Processing object to accomplish the link Processing object retrieve their input values and store their output values in the properties of other Objects –Fast! –“Multitasking” functionality (output continues to update the objects connected as long as processing object’s operating bit is set
Linking Objects Together, cont. Link Method –Uses pointers to “point” to the appropriate property is located Flag pointer –Flags signal an object to begin or end processing »Ex. oDIO1’s Value property is a 1-bit flag –Any flag output from any object can be linked to any flag input of another object using a processing object such as: »oGate (2 + no. of inputs) bytes »oWire (3 bytes) »oFanout (2 + no. of outputs) bytes »oOneShot (3 bytes) »oFlipFlop (5 bytes) »oEvent (3 bytes) Object pointer –These link the default values between objects via a processing object –e.g., usually between hardware objects and processing objects »oMath (4 bytes) »oBus (3 bytes)
Linking Objects Together, cont. Link Method syntax: Processing_object_name.Input.Link(other_object_name) Processing_object_name.Output.Link(other_object_name)
Object Linking Example Problem: Acquire the light level from two photoresistors, PR1 and PR2. If the light level on PR1 is higher than that for PR2, output a ‘high’ on pin B7. If the light level for PR2 is higher than that for PR1, output a logic ‘low’ on pin B7. // Procedural Approach oA2D10 PR1 = new oA2D10;// 3 bytes oA2D10 PR2 = new oA2D10; // 3 bytes oDIO1 OUT1 = new oDIO1; // 1 byte total = 7 bytes (79 left) Sub void Main (void) { PR1.IOLine = 1;// Map A2D10 object to A/D line 1 PR1.Operate = cvTrue;// Enable A2D10 object PR2.IOLine = 2;// Map A2D10 object to A/D line 2 PR2.Operate = cvTrue;// Enable A2D10 object While (1) { If (PR1.Value > PR2.Value) { OUT1 = 1; } else { OUT1 = 0; { } // Virtual Circuit Approach oA2D10 PR1 = new oA2D10; // 3 bytes oA2D10 PR2 = new oA2D10; // 3 bytes oDIO1 OUT1 = new oDIO1; // 1 byte oWire W = new oWire; // 3 bytes oCompare COMP = new oCompare; // 4 bytes total = 14 bytes (72 left) Sub void Main (void) { PR1.IOLine = 1;// Map A2D10 object to A/D line 1 PR1.Operate = cvTrue;// Enable A2D10 object PR2.IOLine = 2;// Map A2D10 object to A/D line 2 PR2.Operate = cvTrue;// Enable A2D10 object COMP.Input.Link(PR1);// Link PR1 to Input of oCompare object COMP.Fuzziness = 10;// Set ‘deadband’ to 10 COMP.ReferenceIn1.Link(PR2); // Link PR2 value to ReferenceIn1 input of oCompare OUT1.Ioline=15;// Set OUT1 to line 15 – pin B7 OUT1.Direction=cvOutput;// Make OUT1 a digital output W.Input.Link(COMP.Above);// Link Above flag to input of oWire object W.Output.Link (OUT1);// Link output of oWire to oDIO1 object }
VC Graphical Layout PR1 oA2D10 I/O line 1, pin A0 PR2 oA2D10 I/O line 2, pin A1 Input ReferenceIn1 oCompare: COMP Fuzziness=10 Above Between Below oWire: W InputOutput OUT1 oDIO1 I/O line 1, pin A0
Procedures // Example of a procedure // from OOPic manual oDio1 A = New oDio1; Sub void main(void) { A.IOLine = 31; A.Direction = cvOutput; Blink(); } Sub void Blink(void) { A.Value = cvON; OOPic.Wait = 100; A.Value = cvOff; OOPic.Wait = 100; } // Example of a procedure passing an argument // from OOPic manual Dio1 A = New oDio1; Sub void main(void) { A.IOLine = 31; A.Direction = cvOutput; Blink(5); } Sub void Blink(Byte xTimes) { While (xTimes.Value > 0) { A.Value = cvON; OOPic.Wait = 100; A.Value = cvOff; OOPic.Wait = 100; xTimes = xTimes - 1; }
Functions Functions can return a value Function Byte GetMyVal(void) { If (SW.Value == cvPressed) { GetMyVal = A1.Value; } Else { GetMyVal = A2.Value; } Function ( ) { return [ ]; }
Events An event is defined as any change in state that is recognized by an Object. An Event Driven Program is a program where any change in state can cause a subprocedure to be executed even when the program flow was not expecting to do so. Therefore, the order in which your code executes depends on which events occur. In normal operation, when an oEvent Object's Operate property transitions from 0 to 1 the program flow is interrupted with a call to a Sub procedure specified by the oEvent Object's name. While the Sub procedure is executing the oEvent Object ignores the Operate property and waits for the program flow to return from the Sub procedure. When the program flow returns from the Sub procedure the oEvent Object resumes watching for the Operate property to transition from 0 to 1