The all API is available through one simple object, Tux.
First you need to create and connect your Tux object
API allows you to control your TuxDroid from it's different body parts :Tux tux = new Tux('localhost'); // if your daemon is on an other machine, change localhost to its name
tux.connect();
Every object can have commands and events. Commands allways return a Query object that you can use to block the call or not.
// Blocking call. Eyes will not be opened if wings are not
Query query = tux.wings().open();
query.waitForResponse();
tux.eyes.open();
// Non-Blocking call. Eyes and Wings will be open at (nearly) the same time.
tux.wings.open();
tux.eyes.open();
When the API trigger an event, the associated call to the listener methods (we'll see that latter), are in their own thread. So if you push the head button and quickly after the right wing. Both call will occur at the same time, in tow separate threads.
Basically, all parts that have motors, have methods open,close, isOpen, toggle. Parameters depends of hardware abilities. For example wings got speed and counter, eyes got counter but no speed.
// I gonna fly !!
tux.head().eyes().open();
tux.wings().toggle(3,Speed.veryQuick);
When you want to move the all tux, you can use Tux object itself
// let's dance !
tux.turnLeft(3,Speed.slow);
tux.turnRight(2,Speed.quick);
Lights are only available on Eye object. Methods are simply lightOn and lightOff.
// Lets be a cyclope
tux.eyes().left().lightOn();
tux.eyes().left().lightOff();
Listening is basically the same as Swing model with some little calling differences. You always have :
For now there is 3 listeners : ButtonEventListeners that you can add to head, left wing et right wing. ShutterEventListener for eye and mouth. And RemoteControlEventListener for eyes.
// I'll dance if you tap on my head
tux.head().buttonListeners().add(new BasicButtonEventListener()
{
public void buttonPressed(ButtonEvent event) throws TuxException
{
tux.wings().toggle(6, Speed.verySlow);
tux.wings().toggle(6, Speed.veryQuick);
}
});
Remote Control use the same concept :
// I control my Tux
tux.head().eyes().remoteControlListeners().add(new BasicRemoteControlEventListener()
{
public void remoteButtonPressed(RemoteControlEvent event) throws TuxException
{
switch (event.key())
{
case left:
tux.turnLeft(1, Speed.quick);
break;
case right:
tux.turnRight(1, Speed.quick);
break;
}
}
}
For now it's a basic but working voice implementation. There is no control on voice Yet but you can make Tux speak (with a tiny cute voice
in a blocking or non-blocking way.
tux.head().mouth().open(); // We don't want to wait here
tux.speak ("Hello world").WaitForResponse(); // We wait tux finish speaking
tux.head().mouth().close();
That's it for now, more will come soon
Poster un nouveau commentaire