[Sun SPOT API Tips(1)] Sun SPOTのバッテリー残量少/リセット/パワーオフのタイミングで処理を実行したい時は?
Sun SPOT アプリケーションで
- Sun SPOTのバッテリ残量が少なくなった
- Sun SPOTをリセットした
- Sun SPOTをパワーオフした
タイミングで処理を実行したい場合、
FiqInterruptDaemon クラスが使えます。
o Sun SPOT API V3.0
FiqInterruptDaemon のインスタンスを Spot から取得して、イベントを処理するハンドラを登録します。例えばこんな感じです↓
package org.sunspotworld;
import com.sun.spot.peripheral.FiqInterruptDaemon; import com.sun.spot.peripheral.IEventHandler; import com.sun.spot.peripheral.Spot; import com.sun.spot.util.*; import com.sun.squawk.VM;
import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException;
public class StartApplication extends MIDlet { protected void startApp() throws MIDletStateChangeException { new BootloaderListener().start(); // monitor the USB (if connected) and recognize commands from host
// FiqInterruptDaemon は、eSPOT(メインボード)の電源コントローラからの // イベントを処理するハンドラを扱うためのクラス FiqInterruptDaemon fiqDaemon = Spot.getInstance().getFiqInterruptDaemon();
// リセットボタンを押した時の処理を実行するハンドラを登録する fiqDaemon.setButtonHandler(new IEventHandler() { public void signalEvent() { System.out.println("Resetting..." ); // TODO: ここに何か処理を実装 VM.stopVM(0); } }); // 電源をオフにした時の処理を実行するハンドラを登録する fiqDaemon.setPowerOffHandler(new IEventHandler() { public void signalEvent() { System.out.println("Powering off..." ); // TODO: ここに何か処理を実装 // APIドキュメントによると、パワーオフまでにこのハンドラで // 処理できる時間は 400ms 程度。 } }); // バッテリー残量が少なくなった時の処理を実行するハンドラを登録する fiqDaemon.setLowBatteryHandler(new IEventHandler() { public void signalEvent() { System.out.println("Battery is Low!!" ); // TODO: ここに何か処理を実装 } }); while (true) { // TODO: ここに何か処理を実装 Utils.sleep(2000L); } // notifyDestroyed(); // cause the MIDlet to exit }
protected void pauseApp() { }
protected void destroyApp(boolean unconditional) throws MIDletStateChangeException { } }
|
Posted at
06:10午後 5 01, 2008
by Shuichi Machida in SunSPOT |