Sun SPOT Telemetry Demo -- SPOT-side
v1.1

org.sunspotworld.demo
Class AccelMonitor

java.lang.Object
  extended by org.sunspotworld.demo.util.PeriodicTask
      extended by org.sunspotworld.demo.AccelMonitor
All Implemented Interfaces:
com.sun.spot.service.IService, PacketTypes, PacketHandler

public class AccelMonitor
extends PeriodicTask
implements PacketHandler, PacketTypes

Routines to control and read data from the SPOT's accelerometer and send them via Radiogram packets to a display program running on a host computer.

The actual task scheduling is done by the PeriodicTask parent class.

Packets from the host application are received by the PacketReceiver class and then dispatched to this class to respond to. The replies are sent back via the PacketTransmitter class. The commands are defined in the PacketTypes class.

Author:
Ron Goldman
Date: May 8, 2006, revised: August 1, 2007
See Also:
PeriodicTask, PacketHandler, PacketReceiver, PacketTransmitter, PacketTypes

Field Summary
 
Fields inherited from class org.sunspotworld.demo.util.PeriodicTask
clk_index, priority, shiftTask, status, taskPeriod, taskPeriodCount, thread, timer
 
Fields inherited from interface org.sunspotworld.demo.PacketTypes
ACCEL_2G_DATA_REPLY, ACCEL_6G_DATA_REPLY, BLINK_LEDS_REQ, BROADCAST_PORT, CALIBRATE_ACCEL_REPLY, CALIBRATE_ACCEL_REQ, CONNECTED_PORT, DISPLAY_SERVER_AVAIL_REPLY, DISPLAY_SERVER_QUITTING, DISPLAY_SERVER_RESTART, GET_ACCEL_INFO_REPLY, GET_ACCEL_INFO_REQ, GET_ACCEL_INFO2_REPLY, LOCATE_DISPLAY_SERVER_REQ, MESSAGE_REPLY, PING_REPLY, PING_REQ, SEND_ACCEL_DATA_REQ, SET_ACCEL_SCALE_REPLY, SET_ACCEL_SCALE_REQ, STOP_ACCEL_DATA_REQ
 
Fields inherited from interface com.sun.spot.service.IService
PAUSED, PAUSING, RESUMING, RUNNING, STARTING, STOPPED, STOPPING
 
Constructor Summary
AccelMonitor(TelemetryMain m, int sampleInterval)
          Create a new accelerometer controller.
 
Method Summary
 void calibrate()
          Have the accelerometer calculate the current rest offsets.
 void doTask()
          Called once per task period to pack up accelerometer readings.
 void getAccInfo()
          Send a packet to inform host of current accelerometer settings.
 void handlePacket(byte type, com.sun.spot.io.j2me.radiogram.Radiogram pkt)
          Callback from PacketReceiver when a new command is received from the host.
 boolean is2GScale()
          Is the accelerometer using the 2G scale?
 void setName(java.lang.String who)
          temporary fix until IService interface fixed
 void setPacketConnection(PacketTransmitter xmit, PacketReceiver rcvr)
          Get the PacketTransmitter & PacketReceiver to use to talk with the host.
 void setScale(byte b)
          Set the accelerometer to use either the 2G or 6G scale.
 void starting()
          Routine called when task execution is about to start up.
 void stopping()
          Routine called when task execution is finished.
 
Methods inherited from class org.sunspotworld.demo.util.PeriodicTask
getCounter, getEnabled, getServiceName, getStatus, getTaskPeriod, getTaskPeriodCount, isRunning, pause, resume, setEnabled, setServiceName, setTaskPeriod, setTaskPriority, shiftStart, start, stop
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AccelMonitor

public AccelMonitor(TelemetryMain m,
                    int sampleInterval)
Create a new accelerometer controller.

Parameters:
m - reference to the main program getting commands from the host display
sampleInterval - how often to sample the accelerometer, in milliseconds
Method Detail

setPacketConnection

public void setPacketConnection(PacketTransmitter xmit,
                                PacketReceiver rcvr)
Get the PacketTransmitter & PacketReceiver to use to talk with the host. Register the commands this class handles.

Parameters:
xmit - the PacketTransmitter to send packets
rcvr - the PacketReceiver that will receive commands from the host and dispatch them to handlePacket()

handlePacket

public void handlePacket(byte type,
                         com.sun.spot.io.j2me.radiogram.Radiogram pkt)
Callback from PacketReceiver when a new command is received from the host. Note only the commands associated with the accelerometer are handled here.

Specified by:
handlePacket in interface PacketHandler
Parameters:
type - the command
pkt - the radiogram with any other required information

is2GScale

public boolean is2GScale()
Is the accelerometer using the 2G scale?

Returns:
true if the accelerometer using the 2G scale

getAccInfo

public void getAccInfo()
Send a packet to inform host of current accelerometer settings. Tell if scale is 2G or 6G. Send zero offsets, gains & rest offsets.


setScale

public void setScale(byte b)
Set the accelerometer to use either the 2G or 6G scale. Will send a packet to acknowledge the request. The reply will include the current scale (2 or 6) or be 0 if an invalid scale was requested.

Parameters:
b - the scale to use = 2 or 6

calibrate

public void calibrate()
Have the accelerometer calculate the current rest offsets. Send a packet back to the host with the 6 offset values.


starting

public void starting()
Routine called when task execution is about to start up.

Overrides:
starting in class PeriodicTask

stopping

public void stopping()
Routine called when task execution is finished.

Overrides:
stopping in class PeriodicTask

doTask

public void doTask()
Called once per task period to pack up accelerometer readings. When the packet is full it is queued for transmission.

Specified by:
doTask in class PeriodicTask

setName

public void setName(java.lang.String who)
temporary fix until IService interface fixed


Sun SPOT Telemetry Demo -- SPOT-side
v1.1

Copyright © 2006, 2007 Sun Microsystems, Inc. All Rights Reserved.