Create a basic application with the Motion Sensor module
From BUG Wiki
Create a basic application for BUG that displays a message on the system output console.
- If necessary, switch Perspective to the Dragonfly perspective.
- Select the New BUG Project icon in the toolbar.
- In the New BUG Project window, in the Name field, enter: MotionSensorExample
- Click Next.
- Click Start Virtual BUG
- Once the Virtual Bug has started, right click on one of the slots and select Motion
- Under Target BUG select Virtual BUG
- Under Required Services begin typing IMotionSubject until it filters out everything but com.buglabs.bug.module.motion.pub.IMotionSubject, and check the box next to that that service
- Click Finish
- Right click on the MotionSensorExample project in the package explorer, and select New->Other->Class
- Click Next.
- Under package enter motionsensorexample.watcher
- Under name enter MotionSensorWatcher
- Click Add next to Interfaces
- Type IMotionObserver and then click Ok
- Click Finish
- In left-side Project Explorer view, expand the node for the MotionSensorExample application
- Expand the node for the motionsensorexample.servicetracker package
- Open the MotionSensorExampleServiceTracker.java file
- Add the following code to the import block at the top of the file
import com.buglabs.bug.module.motion.pub.IMotionSubject; import motionsensorexample.watcher.*;
- Near the top of the MotionSensorExampleServiceTracker class add the variable
private IMotionSubject motion;
- Under the doStart() method of MotionSensorExampleServiceTracker add the following code
motion = (IMotionSubject) getService(IMotionSubject.class); MotionSensorWatcher watcher = new MotionSensorWatcher(motion); motion.register(watcher);
- Open the MotionSensorWatcher.java file in the motionsensorexample.watcher package
- Add the following code to the import block
import com.buglabs.bug.module.motion.pub.IMotionSubject;
- Replace the class's code with the following
private IMotionSubject motion;
public MotionSensorWatcher (IMotionSubject motion) {
this.motion = motion;
}
public void motionDetected() {
System.out.println("Motion has been detected");
}
- Go to File > Save all
- Click Run As and choose Virtual BUG
- Right click on a slot on the virtual bug and choose the motion module
- Type motion in the console of the Dragonfly SDK after
- The module will run the motionDetected() method, printing "Motion has been detected" in the console of the Dragonfly SDK
- There is a working version of this program located here: http://www.buglabs.net/applications/kschultz/MotionSensorExample
