|Chapter 3 - Applications|
|The ElectroMac class|
On the Macintosh I wrote a C++ class to handle communication with ElectroMac.
When writing other programs you can then just create an ElectroMac object and
make calls to its member functions. This simplifies the coding of the other
programs because you do not have to worry about the sending of serial commands
or the conversion of data received from ElectroMac, the class handles all this
for you. An example project window in the Code Warrior IDE is shown below:|
When an ElectroMac class is created, you must specify the port that ElectroMac is connected to (Modem or Printer). The class then opens an input and output serial driver for that port and keeps the connection open until the object is deleted and the deconstructor subroutine is called. The class provides calls for all of ElectroMac's standard commands as well as others such as checking, setting and clearing bits of input and output bytes. Below is an extract from the classes header file showing the syntax of all the public member functions and comments describing each. A full listing can be found in Appendix D.
Boolean areYouThere(); // Check to see is interface is connected void resetAll(); // Tell the interface to reset void enableInts(); // Enable interrupts on interface void disableInts(); // Disable interrupts on interface char* getVersion(); // Get the EPROM version char getInput(); // Get the current value on the input port char getOutput(); // Get the current value on the output port void setOutput(short inVal); // Set the output port value char getInVal(); // Get the input value stored on computer char getOutVal(); // Get the output value stored on computer void setOutBit(short bitNum); // Set a single bit of the output port void clearOutBit(short bitNum); // Clear a single bit of the output port void toggleOutBit(short bitNum); // Toggle a single bit of the output port Boolean getInBit(short bitNum); // Get a single bit of the input port long getAnalogIn(short inChannel); // Get the value of a A/D channel long getAnalogOut(short inChannel); // Get the value of a D/A channel void setAnalogOut(short inChannel, long inValue);// Set the value of a D/A channel long getAnInVal(short inChannel); // Get the input value of a channel on computer long getAnOutVal(short inChannel); // Get the input value of a channel on computerNote: The projects are setup to treat all variable of type char as of type unsigned char so that the ASCII values convert correctly.
The first program I wrote on the Macintosh was a basic console which I used to test the ElectroMac class. This tested each of the functions in the ElectroMac class to ensure that it all worked. I used the SIOUX console library to handle all communication with the user so that I did not have to waste time programming the user interface. The SIOUX console console allows you to use the standard C++ iostream in exactly the same way that a DOS or UNIX program would.
The next stage was to write a simple program with a graphical interface to turn the digital outputs on and off and find out the status of the inputs. I wrote the program using PowerPlant which handles the graphical interface.
To demonstrate what ElectroMac is capable of I borrowed a model House from the technology department. This house has light bulbs in the four windows, a buzzer, doorbell switch and magnetic switch on the front door. I wrote a program which could turn on and off these lights from the computer via ElectroMac. I made a 3D rendered model of the house on the computer which shows the state of the real model house. The user could then click on the windows of the house and the lights on the model house turned On and Off.
The ElectroMac class was derived to include PowerPlant library class features. When an interrupt is received then the PowerPlant broadcaster class sends a signal to its listeners (in this case the application). The application listener then updates the on screen graphic accordingly.
The main pane of the graphics window is a custom class derived from the library class LControl. When the pane is clicked on then the class checks to see if the user clicked within one of the 'Hot Spots' and if they did broadcasts a message to the application listener. The controls value defines what the graphic state is (which parts of the images are turned on and off).
ElectroMac should be capable of timing events accurately by continuously monitoring the input port and sending a serial signal when it changes. I wrote a program to time the receiving of the serial signal on the Macintosh and display the time in seconds.
The timer application was also written using PowerPlant, but this time used its own class derived from the ElectroMac class. This class has a member function that handled all of the timing and communication with ElectroMac. It returns the length of the event in microseconds. It first waits for an interrupt signal from the interface and if valid stores the current time in the startTime variable. It again waits for the interface to send an interrupt signal, verifies it and stores it as endTime. startTime is subtracted from endTime and returned to the calling function.
©1998 Nicholas Humfrey <e-mail> <www>