The classes listed below run in the Javelin Stamp. They provide
a temperature reading when sent a request over the serial line.
The request will come from the SerialToJavelin
program discussed in Chapter 24, which runs on a desktop, or other
J2SE compatible platform on which a javax.comm
package is available. This program sends the request (password number
first) to the Javelin over the serial for a data value.
See further code description on previous
page.
SendTempDataFromJavelin |
import
stamp.core.*;
import stamp.peripheral.sensor.temperature.DS1620;
/**
* This program transmits temperature readings
from the DS1620
* chip when it receives a request over the serial
line. The
* request must include a password number at
the start. If the
* password is OK, then a temperature value is
obtained and
* transmitted.
*
* The "raw" temperature readings are obtained
via the
* TempDataGenJavelin class, which in turn uses
the DS1620 class
* provided in the stamp.peripheral.sensor.temperature
package.
*
* The serial communications code uses the stamp.core.Uart
* virtual peripheral class.
**/
public class SendTempDataFromJavelin {
// Since only one SimData program per card,
make
// the password static.
private static int fPassword__ = 0x2201;
// Label pins on evaluation card for serial
I/O
final static int SERIAL_TX_PIN =
CPU.pin0;
final static int SERIAL_RTS_PIN = CPU.pin1;
final static int SERIAL_CTS_PIN = CPU.pin2;
final static int SERIAL_RX_PIN =
CPU.pin3;
// Create the UART vp for transmission via COM
serial port
static Uart fTxUart__ = new Uart (
Uart.dirTransmit, SERIAL_TX_PIN,
Uart.dontInvert, SERIAL_RTS_PIN,
Uart.dontInvert, Uart.speed9600,
Uart.stop1 );
// Create the UART vp for reception via the
COM serial port
static Uart fRxUart__ = new Uart (
Uart.dirReceive, SERIAL_RX_PIN,
Uart.dontInvert, SERIAL_CTS_PIN,
Uart.dontInvert, Uart.speed9600,
Uart.stop1 );
/** Resetting Javelin will start the program
here. **/
public static void main () {
// Local variable
int data=0;
// Create a temperature data sensor.
DataGenerator temp_sensor = new
TempDataGenJavelin ();
// Loop continuously, each time
waiting for a request
// to arrive with the password (PW).
Then get the
// temperature reading and send
it.
do {
// Go into receive mode
and wait for PW
checkPW ();
// Get the data
data = temp_sensor.getData
();
// and then send it
to the requestor.
sendInt (data);
} while (true);
} // main
/**
* Utility method to send int value
as
* two bytes. Use "big-endian" mode
with most
* significant byte sent first.
**/
static void sendInt (int data){
fTxUart__.sendByte (data >>>
8);
fTxUart__.sendByte (data
& 0x00FF);
}
/**
* Utility method to receive 2 bytes and
make an
* int value (16 bits in Javelin)
from them.
**/
static int receiveInt () {
int byte1 = fRxUart__.receiveByte
();
int byte2 = fRxUart__.receiveByte
();
return byte2 | (byte1
<< 8);
}
/**
* Utility method to recieve an int
value
* and compare it to the password.
**/
static void checkPW () {
do {
int data = receiveInt
();
if (data == fPassword__)
{
fTxUart__.sendString
("PW OK!\n\r");
break;
}
fTxUart__.sendString
("Wrong PW!\n\r");
} while (true);
} // checkPW
} // class SendTempDataJavelin
|
The TempDataGenJavelin
class, shown below, communicates with DS1620 chip on the breadboard
of the evaluation card (see the Javelin
Stamp Users Manual Version 1.0a for the circuit setup). The
class DS1620
does most of the work of communicating with the chip. All we have
to do is invoke the getTempRaw()
method and we obtain the current temperature in units of 0.5 degrees
Celsius.
Perhaps there came a situation where we have several sensors of
different kinds that we want to read (e.g. temperature, pressure,
wind direction, etc.) So that we could give them all the same base
class type, we created the the abstract DataGenerator
class, which TempDataGenJavelin
class extends. This would normally be done with an interface but
our Javelin API does not provide for interfaces so we make it an
abstract class instead. We could then reference all of our sensors
with a DataGenerator
array and loop over the array and invoke the getData()
for each element.
|
import
stamp.core.*;
import stamp.peripheral.sensor.temperature.DS1620;
/**
* Create a class that obtains the current temperature
* with the DS1620 chip. It extends the DataGenerator
* class and overrides the getData () method to
return
* the temperature value in an int value.
**/
public class TempDataGenJavelin extends DataGenerator
{
DS1620 thermometer = null;
/** Constructor creates an instance of DS1620.**/
TempDataGenJavelin () {
thermometer = new DS1620 (CPU.pin4,CPU.pin5,CPU.pin6);
} // ctor
/** Return temperature in units of 0.5 degrees
Celsius **/
int getData () {
return thermometer.getTempRaw ();
}
} // class TempDataGenJavelin |
|
/**
* Javelin does not allow for interfaces so use
an abstract
* class to represent types of data generators.
**/
public abstract class DataGenerator {
abstract int getData ();
} // class DataGenerator |
References and Web Resources
Latest update: Dec. 14, 2004
|