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

org.sunspotworld.demo.util
Class PeriodicTask

java.lang.Object
  extended by org.sunspotworld.demo.util.PeriodicTask
All Implemented Interfaces:
com.sun.spot.service.IService
Direct Known Subclasses:
AccelMonitor

public abstract class PeriodicTask
extends java.lang.Object
implements com.sun.spot.service.IService

Framework to provide for running a task, such as taking samples, at a regular interval. Extend the doTask method to specify what actions need to be performed.

Uses the AT91_TC Timer/counter for accurate timings provided task period is less than 2 seconds. For task periods greater than 2 seconds the regular Thread.sleep() is used.

Note: for more details on the AT91_TC Timer/counter please refer to the Timer/Counter application note in the AppNotes folder.

Implements the com.sun.spot.service.IService interface.

Author:
Ron Goldman
Date: January 10, 2007
See Also:
IService, IAT91_TC

Field Summary
protected  int clk_index
           
protected  int priority
           
protected  int shiftTask
           
protected  int status
           
protected  int taskPeriod
           
protected  int taskPeriodCount
           
protected  java.lang.Thread thread
           
protected  com.sun.spot.peripheral.IAT91_TC timer
           
 
Fields inherited from interface com.sun.spot.service.IService
PAUSED, PAUSING, RESUMING, RUNNING, STARTING, STOPPED, STOPPING
 
Constructor Summary
PeriodicTask(int index)
          Creates a new instance of PeriodicTask.
PeriodicTask(int index, int period)
          Creates a new instance of PeriodicTask.
PeriodicTask(int index, int period, int priority)
          Creates a new instance of PeriodicTask.
 
Method Summary
abstract  void doTask()
          Called once per task period to perform measurements.
 int getCounter()
          Return the current counter value.
 boolean getEnabled()
          Return whether service is started automatically on reboot.
 java.lang.String getServiceName()
          Return service name
 int getStatus()
          Return current task execution status.
 int getTaskPeriod()
          Return the current doTask period in milliseconds.
 int getTaskPeriodCount()
          Return the current doTask period in clock cycles.
 boolean isRunning()
          Return if currently running.
 boolean pause()
          Pause the service, and return whether successful.
 boolean resume()
          Resume the service, and return whether successful.
 void setEnabled(boolean enable)
          Enable/disable whether service is started automatically.
 void setServiceName(java.lang.String who)
          Assign a name to this service.
 void setTaskPeriod(int period)
          Set the current doTask period.
 void setTaskPriority(int priority)
          Set the priority for the task loop thread.
 void shiftStart(int shift)
          Shift when the task gets run by N milliseconds.
 boolean start()
          Start periodic task execution.
 void starting()
          Routine called when task execution is about to start up.
 boolean stop()
          Stop future task execution.
 void stopping()
          Routine called when task execution is finished.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

taskPeriod

protected int taskPeriod

taskPeriodCount

protected int taskPeriodCount

shiftTask

protected int shiftTask

clk_index

protected int clk_index

status

protected int status

thread

protected java.lang.Thread thread

priority

protected int priority

timer

protected com.sun.spot.peripheral.IAT91_TC timer
Constructor Detail

PeriodicTask

public PeriodicTask(int index)
Creates a new instance of PeriodicTask.

Parameters:
index - specifies which timer to use (0-3)

PeriodicTask

public PeriodicTask(int index,
                    int period)
Creates a new instance of PeriodicTask.

Parameters:
index - specifies which timer to use (0-3) or -1 if just using Thread.sleep()
period - in milliseconds between tasks

PeriodicTask

public PeriodicTask(int index,
                    int period,
                    int priority)
Creates a new instance of PeriodicTask.

Parameters:
index - specifies which timer to use (0-3) or -1 if just using Thread.sleep()
period - in milliseconds between tasks
priority - the priority for the task loop thread
Method Detail

doTask

public abstract void doTask()
Called once per task period to perform measurements. Must be defined in classes that extend PeriodicTask.


setTaskPriority

public void setTaskPriority(int priority)
Set the priority for the task loop thread.

Parameters:
priority - the priority for the task loop thread

getTaskPeriod

public int getTaskPeriod()
Return the current doTask period in milliseconds.

Returns:
the doTask period in milliseconds

getTaskPeriodCount

public int getTaskPeriodCount()
Return the current doTask period in clock cycles.

Returns:
the doTask period in clock cycles

setTaskPeriod

public void setTaskPeriod(int period)
Set the current doTask period. Note: the task must not be currently running!

Parameters:
period - the doTask period in milliseconds

starting

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


stopping

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


shiftStart

public void shiftStart(int shift)
Shift when the task gets run by N milliseconds. Sleep one time so task loop start is shifted. Does nothing if the task loop is not running.

Parameters:
shift - number of milliseconds to shift by

getCounter

public int getCounter()
Return the current counter value.

Returns:
the current counter value

stop

public boolean stop()
Stop future task execution.

Specified by:
stop in interface com.sun.spot.service.IService
Returns:
true if will stop running

start

public boolean start()
Start periodic task execution.

Specified by:
start in interface com.sun.spot.service.IService
Returns:
true if will start running

pause

public boolean pause()
Pause the service, and return whether successful. Since there is no particular state associated with this service then pause() can be implemented by calling stop().

Specified by:
pause in interface com.sun.spot.service.IService
Returns:
true if the service was successfully paused

resume

public boolean resume()
Resume the service, and return whether successful. Since there was no particular state associated with this service then resume() can be implemented by calling start().

Specified by:
resume in interface com.sun.spot.service.IService
Returns:
true if the service was successfully resumed

getServiceName

public java.lang.String getServiceName()
Return service name

Specified by:
getServiceName in interface com.sun.spot.service.IService
Returns:
the name of this service

setServiceName

public void setServiceName(java.lang.String who)
Assign a name to this service.

Specified by:
setServiceName in interface com.sun.spot.service.IService
Parameters:
who - the new name for this service

isRunning

public boolean isRunning()
Return if currently running.

Specified by:
isRunning in interface com.sun.spot.service.IService
Returns:
true if currently running

getStatus

public int getStatus()
Return current task execution status.

Specified by:
getStatus in interface com.sun.spot.service.IService
Returns:
current task execution status: STOPPED, STARTING, RUNNING, or STOPPING.

getEnabled

public boolean getEnabled()
Return whether service is started automatically on reboot.

Specified by:
getEnabled in interface com.sun.spot.service.IService
Returns:
false as this service is never started automatically on reboot

setEnabled

public void setEnabled(boolean enable)
Enable/disable whether service is started automatically. Noop for task execution.

Specified by:
setEnabled in interface com.sun.spot.service.IService
Parameters:
enable - ignored

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

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