[tuxdroid-svn] r1409 - in software_suite_v2/tuxware: . java-api java-api/trunk java-api/trunk/src j
Status: Beta
Brought to you by:
ks156
From: remi <c2m...@c2...> - 2008-07-29 07:55:11
|
Author: remi Date: 2008-07-29 09:55:18 +0200 (Tue, 29 Jul 2008) New Revision: 1409 Added: software_suite_v2/tuxware/java-api/ software_suite_v2/tuxware/java-api/branches/ software_suite_v2/tuxware/java-api/tags/ software_suite_v2/tuxware/java-api/trunk/ software_suite_v2/tuxware/java-api/trunk/.classpath software_suite_v2/tuxware/java-api/trunk/.project software_suite_v2/tuxware/java-api/trunk/.settings/ software_suite_v2/tuxware/java-api/trunk/src/ software_suite_v2/tuxware/java-api/trunk/src/TestAPI.java software_suite_v2/tuxware/java-api/trunk/src/com/ software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/ software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/ software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/SLock.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/SThread.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPI.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIAccess.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIAttitune.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIButton.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIConst.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIDongleRadio.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIEvent.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIFlippers.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPILed.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPILedBase.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIMisc.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIMouthEyes.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIRemote.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIServer.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPISoundFlash.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPISpinning.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIStatus.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPISwitch.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPITTS.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxEventHandler.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxEventHandlers.java software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxHTTPRequest.java Log: * added "java-api" project at version "0.0.1-ALPHA" Added: software_suite_v2/tuxware/java-api/trunk/.classpath =================================================================== --- software_suite_v2/tuxware/java-api/trunk/.classpath (rev 0) +++ software_suite_v2/tuxware/java-api/trunk/.classpath 2008-07-29 07:55:18 UTC (rev 1409) @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="src"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="output" path="bin"/> +</classpath> Added: software_suite_v2/tuxware/java-api/trunk/.project =================================================================== --- software_suite_v2/tuxware/java-api/trunk/.project (rev 0) +++ software_suite_v2/tuxware/java-api/trunk/.project 2008-07-29 07:55:18 UTC (rev 1409) @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>TuxAPI</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> Added: software_suite_v2/tuxware/java-api/trunk/src/TestAPI.java =================================================================== --- software_suite_v2/tuxware/java-api/trunk/src/TestAPI.java (rev 0) +++ software_suite_v2/tuxware/java-api/trunk/src/TestAPI.java 2008-07-29 07:55:18 UTC (rev 1409) @@ -0,0 +1,96 @@ +import com.tuxisalive.api.*; + +public class TestAPI +{ + public static void onAllEvent(String name, String value, Double delay) + { + //String str = String.format("onAllEvent : %s %s %f", name, value, delay); + //System.out.println(str); + } + + public static void remote_button(String value, Double delay){ + String str = String.format("onRemote : %s %f", value, delay); + System.out.println(str); + } + + /** + * @param args + */ + public static void main(String[] args) { + System.out.println("Create a TuxAPI object"); + TuxAPI tux = new TuxAPI("localhost", 270); + System.out.println("Register the 'all' events callback"); + tux.event.handler.register("all", new TestAPI(), "onAllEvent"); + tux.button.remote.registerEventOnPressed(new TestAPI(), "remote_button", null); + System.out.println("Connect to a Tuxdroid server"); + tux.server.autoConnect(TuxAPIConst.CLIENT_LEVEL_RESTRICTED, "Test", "myPasswd"); + System.out.println("Wait API connected (10 seconds )..."); + tux.server.waitConnected(10.0); + if (tux.server.getConnected()) + { + System.out.println("Wait Dongle connected (10 seconds )..."); + tux.dongle.waitConnected(10.0); + if (tux.dongle.getConnected()) + { + System.out.println("Wait Radio connected (10 seconds )..."); + tux.radio.waitConnected(10.0); + if (tux.radio.getConnected()) + { + System.out.println("Acquire the resource access :"); + System.out.println(tux.access.acquire(TuxAPIConst.ACCESS_PRIORITY_HIGH)); + System.out.println("Leds blink during 5 seconds."); + tux.led.both.blinkDuring(1, 5.0, TuxAPIConst.LFX_STEP); + TuxAPIMisc.sleep(5.); + System.out.println("Load an attitune."); + tux.attitune.load("http://www.tuxisalive.com/Members/remi/hammer.att"); + System.out.println("Play the attitune"); + tux.attitune.play(); + System.out.println("Wait 10 seconds"); + TuxAPIMisc.sleep(10.); + System.out.println("Stop the attitune."); + tux.attitune.stop(); + System.out.println("Open the mouth."); + tux.mouth.open(); + TuxAPIMisc.sleep(1.); + System.out.println("Close the mouth."); + tux.mouth.close(); + TuxAPIMisc.sleep(1.); + System.out.println("7 movements of mouth."); + tux.mouth.on(7, TuxAPIConst.SSV_CLOSE); + TuxAPIMisc.sleep(1.); + System.out.println("4 seconds of movement of mouth."); + tux.mouth.onDuring(4.0, TuxAPIConst.SSV_CLOSE); + System.out.println("Close the eyes."); + tux.eyes.close(); + TuxAPIMisc.sleep(1.); + System.out.println("Open the eyes."); + tux.eyes.open(); + TuxAPIMisc.sleep(1.); + System.out.println("7 movements of eyes."); + tux.eyes.on(7, TuxAPIConst.SSV_OPEN); + TuxAPIMisc.sleep(1.); + System.out.println("4 seconds of movement of eyes."); + tux.eyes.onDuring(4.0, TuxAPIConst.SSV_OPEN); + System.out.println("Set the flippers to up."); + tux.flippers.up(); + TuxAPIMisc.sleep(1.); + System.out.println("Set the flippers to down."); + tux.flippers.down(); + TuxAPIMisc.sleep(1.); + System.out.println("7 movements of flippers."); + tux.flippers.on(7, TuxAPIConst.SSV_DOWN, TuxAPIConst.SPV_VERYSLOW); + TuxAPIMisc.sleep(1.); + System.out.println("4 seconds of movement of flippers."); + tux.flippers.onDuring(4.0, TuxAPIConst.SSV_DOWN); + tux.spinning.rightOnDuring(1.); + System.out.println("Release the resource access :"); + System.out.println(tux.access.release()); + } + } + } + System.out.println("Destroy the TuxAPI ..."); + tux.destroy(); + System.out.println("... Finish"); + } + +} Added: software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/SLock.java =================================================================== --- software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/SLock.java (rev 0) +++ software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/SLock.java 2008-07-29 07:55:18 UTC (rev 1409) @@ -0,0 +1,65 @@ +package com.tuxisalive.api; + +public class SLock +{ + private Object mutex; + private Boolean locked; + + public SLock() + { + locked = false; + mutex = new Object(); + } + + public void acquire() + { + synchronized (mutex) + { + while (locked) + { + try + { + mutex.wait(); + } + catch (Exception e) {} + } + locked = true; + } + } + + public void acquireTimeout(Double timeout) + { + Double startTime = System.currentTimeMillis() / 1000.0; + Double currentTime; + + synchronized (mutex) + { + locked = true; + while (locked) + { + currentTime = System.currentTimeMillis() / 1000.0; + if (timeout <= (currentTime - startTime)) + { + break; + } + else + { + try + { + mutex.wait(100); + } + catch (Exception e) {} + } + } + } + } + + public void release() + { + synchronized (mutex) + { + mutex.notifyAll(); + locked = false; + } + } +} Added: software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/SThread.java =================================================================== --- software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/SThread.java (rev 0) +++ software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/SThread.java 2008-07-29 07:55:18 UTC (rev 1409) @@ -0,0 +1,48 @@ +package com.tuxisalive.api; + +import java.lang.reflect.Method; + +public class SThread extends Thread +{ + private Object pSender; + private String pMethod; + private Object[] pArgs; + + public SThread (Object sender, String method, Object... args) + { + pSender = sender; + pMethod = method; + pArgs = args; + } + + public void run() + { + Class<?> targetClass = pSender.getClass(); + Class<?> args[] = new Class[pArgs.length]; + + for (int i = 0; i < pArgs.length; i++) + { + args[i] = pArgs[i].getClass(); + + if (pArgs[i] != null) + { + args[i] = pArgs[i].getClass(); + } + else + { + args[i] = Object.class; + } + } + + try + { + Method targetMethod = targetClass.getDeclaredMethod(pMethod, args); + targetMethod.invoke(pSender, pArgs); + } + catch(Exception e) + { + System.out.println(String.format("Error when starting the thread : (%s)", + pMethod)); + } + } +} Added: software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPI.java =================================================================== --- software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPI.java (rev 0) +++ software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPI.java 2008-07-29 07:55:18 UTC (rev 1409) @@ -0,0 +1,152 @@ +package com.tuxisalive.api; + +/** + * Main module class to control Tuxdroid. + * + * @author R Jocaille + * @version 0.1 + * @since 0.1 + */ +public class TuxAPI +{ + /* + * Event handlers field + */ + private TuxEventHandlers eventHandlers; + + /* + * Public field + */ + public TuxAPIServer server; + public TuxAPIEvent event; + public TuxAPIAccess access; + public TuxAPIStatus status; + public TuxAPIMouthEyes mouth; + public TuxAPIMouthEyes eyes; + public TuxAPIFlippers flippers; + public TuxAPISpinning spinning; + public TuxAPIDongleRadio dongle; + public TuxAPIDongleRadio radio; + public TuxAPIAttitune attitune; + public TuxAPILed led; + public TuxAPITTS tts; + public TuxAPISoundFlash soundFlash; + public TuxAPIButton button; + + /** + * Constructor of the class. + * + * @param host host of the server. + * @param port port of the server. + */ + public TuxAPI(String host, Integer port) + { + // Event handlers + eventHandlers = new TuxEventHandlers(); + fillEventHandlers(); + // Client of server + server = new TuxAPIServer(this, host, port); + server.registerEventOnConnected(this, "onServerConnected"); + server.registerEventOnDisconnected(this, "onServerDisconnected"); + // Event + event = new TuxAPIEvent(this); + // Access + access = new TuxAPIAccess(this); + // Status + status = new TuxAPIStatus(this); + // Mouth + mouth = new TuxAPIMouthEyes(this, TuxAPIConst.ST_NAME_MOUTH_POSITION, + TuxAPIConst.ST_NAME_MOUTH_RM, "mouth"); + // Eyes + eyes = new TuxAPIMouthEyes(this, TuxAPIConst.ST_NAME_EYES_POSITION, + TuxAPIConst.ST_NAME_EYES_RM, "eyes"); + // Flippers + flippers = new TuxAPIFlippers(this); + // Spinning + spinning = new TuxAPISpinning(this); + // Dongle + dongle = new TuxAPIDongleRadio(this, TuxAPIConst.ST_NAME_DONGLE_PLUG); + // Radio + radio = new TuxAPIDongleRadio(this, TuxAPIConst.ST_NAME_RADIO_STATE); + // Attitune + attitune = new TuxAPIAttitune(this); + // Led + led = new TuxAPILed(this); + // TTS + tts = new TuxAPITTS(this); + // Sound flash + soundFlash = new TuxAPISoundFlash(this); + // Button + button = new TuxAPIButton(this); + } + + /** + * Destructor of the class. + */ + public void destroy() + { + button.destroy(); + soundFlash.destroy(); + tts.destroy(); + led.destroy(); + attitune.destroy(); + radio.destroy(); + dongle.destroy(); + spinning.destroy(); + flippers.destroy(); + eyes.destroy(); + mouth.destroy(); + status.destroy(); + access.destroy(); + event.destroy(); + server.destroy(); + eventHandlers.destroy(); + } + + /* + * + */ + private void fillEventHandlers() + { + int i; + + for (i = 0; i < TuxAPIConst.SW_NAME_DRIVER.length; i++) + { + eventHandlers.insert(TuxAPIConst.SW_NAME_DRIVER[i]); + } + for (i = 0; i < TuxAPIConst.SW_NAME_OSL.length; i++) + { + eventHandlers.insert(TuxAPIConst.SW_NAME_OSL[i]); + } + for (i = 0; i < TuxAPIConst.SW_NAME_API.length; i++) + { + eventHandlers.insert(TuxAPIConst.SW_NAME_API[i]); + } + } + + /** + * Get the event handlers of the API. + * + * @return the event handlers object. + */ + public TuxEventHandlers getEventHandlers() + { + return eventHandlers; + } + + /* + * + */ + protected void onServerConnected(Boolean value, Double delay) + { + event.start(); + } + + /* + * + */ + protected void onServerDisconnected(Boolean value, Double delay) + { + event.stop(); + } +} Added: software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIAccess.java =================================================================== --- software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIAccess.java (rev 0) +++ software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIAccess.java 2008-07-29 07:55:18 UTC (rev 1409) @@ -0,0 +1,180 @@ +package com.tuxisalive.api; + +import java.util.*; + +/** + * Class to control the resource access. When your have your level to + * CLIENT_LEVEL_RESTRICTED, you need to acquiring and releasing the resource. + * It mechanism is needed for the synchronization of the resource access by the + * programs which want using Tuxdroid. + * CLIENT_LEVEL_FREE, CLIENT_LEVEL_ROOT and CLIENT_LEVEL_ANONYME don't have + * this restriction. + * When you make a tux gadget, you must to use the CLIENT_LEVEL_RESTRICTED level. + * (Only by convention ;) ) + * + * @author R Jocaille + * @version 0.1 + * @since 0.1 + */ +public class TuxAPIAccess +{ + /* + * Parent field + */ + private TuxAPI pParent; + + /** + * Constructor of the class. + */ + public TuxAPIAccess(TuxAPI parent) + { + pParent = parent; + } + + /** + * Destructor of the class. + */ + public void destroy() {} + + /* + * + */ + private Boolean cmdSimpleResult(String cmd) + { + Hashtable<Object,Object> varStruct = new Hashtable<Object,Object>(); + Hashtable<Object,Object> varResult = new Hashtable<Object,Object>(); + + return pParent.server.request(cmd, varStruct, varResult, false); + } + + /** + * To acquiring the resource access. + * Need for CLIENT_LEVEL_RESTRICTED level. + * Don't forget to release the access after !!! + * + * @param priorityLevel (ACCESS_PRIORITY_LOW|ACCESS_PRIORITY_NORMAL| + * ACCESS_PRIORITY_HIGH|ACCESS_PRIORITY_CRITICAL) + * @return the success of the acquiring. + */ + public Boolean acquire(Integer priorityLevel) + { + String cmd; + cmd = String.format("access/acquire?priority_level=%d", priorityLevel); + return cmdSimpleResult(cmd); + } + + /** + * To acquiring the resource access. + * Need for CLIENT_LEVEL_RESTRICTED level. + * Don't forget to release the access after !!! + * + * @return the success of the acquiring. + */ + public Boolean acquire() + { + return acquire(TuxAPIConst.ACCESS_PRIORITY_NORMAL); + } + + /** + * Wait that the resource can be acquired. + * Need for CLIENT_LEVEL_RESTRICTED level. + * Don't forget to release the access after !!! + * + * @param timeout maximal delay to wait. + * @param priorityLevel (ACCESS_PRIORITY_LOW|ACCESS_PRIORITY_NORMAL| + * ACCESS_PRIORITY_HIGH|ACCESS_PRIORITY_CRITICAL) + * @return the success of the acquiring. + */ + public Boolean waitAcquire(Double timeout, Integer priorityLevel) + { + Double tBegin = System.currentTimeMillis() / 1000.0; + Double currentTime; + + while (!acquire(priorityLevel)) + { + currentTime = System.currentTimeMillis() / 1000.0; + if ((currentTime - tBegin) >= timeout) + { + return false; + } + TuxAPIMisc.sleep(0.25); + } + return true; + } + + /** + * Wait that the resource can be acquired. + * Need for CLIENT_LEVEL_RESTRICTED level. + * Don't forget to release the access after !!! + * + * @param timeout maximal delay to wait. + * @return the success of the acquiring. + */ + public Boolean waitAcquire(Double timeout) + { + return waitAcquire(timeout, TuxAPIConst.ACCESS_PRIORITY_NORMAL); + } + + /** + * To releasing the resource access. + * Need for CLIENT_LEVEL_RESTRICTED level. + * + * @return the success of the command. + */ + public Boolean release() + { + String cmd = "access/release?"; + return cmdSimpleResult(cmd); + } + + /** + * To force the acquisition of the resource by a specified client. + * Only available for CLIENT_LEVEL_ROOT level. + * + * @param idClient idx of the client. + * @return the success of the command. + */ + public Boolean forcingAcquire(Integer idClient) + { + String cmd; + cmd = String.format("access/forcing_acquire?id_client=%d", idClient); + return cmdSimpleResult(cmd); + } + + /** + * To force the releasing of the resource. + * Only available for CLIENT_LEVEL_ROOT level. + * + * @return the success of the command. + */ + public Boolean forcingRelease() + { + String cmd = "access/forcing_release?"; + return cmdSimpleResult(cmd); + } + + /** + * To lock the resource access. After it, nobody will can acquiring + * the resource. + * Only available for CLIENT_LEVEL_ROOT level. + * + * @return the success of the command. + */ + public Boolean lock() + { + String cmd = "access/lock?"; + return cmdSimpleResult(cmd); + } + + /** + * To unlock the resource access. + * Only available for CLIENT_LEVEL_ROOT level. + * + * @return the success of the command. + */ + public Boolean unlock() + { + String cmd = "access/unlock?"; + return cmdSimpleResult(cmd); + } +} Added: software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIAttitune.java =================================================================== --- software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIAttitune.java (rev 0) +++ software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIAttitune.java 2008-07-29 07:55:18 UTC (rev 1409) @@ -0,0 +1,88 @@ +package com.tuxisalive.api; + +import java.util.Hashtable; + +/** + * Class to control the attitune files. + * + * @author R Jocaille + * @version 0.1 + * @since 0.1 + */ +public class TuxAPIAttitune +{ + /* + * Parent field + */ + private TuxAPI pParent; + + /** + * Constructor of the class. + * @param parent parent object as TuxAPI + */ + public TuxAPIAttitune(TuxAPI parent) + { + pParent = parent; + } + + /** + * Destructor of the class. + */ + public void destroy() {} + + /* + * + */ + private Boolean cmdSimpleResult(String cmd) + { + Hashtable<Object,Object> varStruct = new Hashtable<Object,Object>(); + Hashtable<Object,Object> varResult = new Hashtable<Object,Object>(); + + return pParent.server.request(cmd, varStruct, varResult, false); + } + + /** + * Load an attitune file. + * + * @param path path of the attitune file. + * @return the success of the command. + */ + public Boolean load(String path) + { + String cmd = String.format("attitune/load?path=%s", path); + return cmdSimpleResult(cmd); + } + + /** + * Play the loaded attitune. + * + * @param begin starting second. + * @return the success of the command. + */ + public Boolean play(Double begin) + { + String cmd = String.format("attitune/play?begin=%g", begin); + return cmdSimpleResult(cmd); + } + + /** + * Play the loaded attitune. + * + * @return the success of the command. + */ + public Boolean play() + { + return play(0.0); + } + + /** + * Stop the current attitune. + * + * @return the success of the command. + */ + public Boolean stop() + { + String cmd = String.format("attitune/stop?"); + return cmdSimpleResult(cmd); + } +} Added: software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIButton.java =================================================================== --- software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIButton.java (rev 0) +++ software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIButton.java 2008-07-29 07:55:18 UTC (rev 1409) @@ -0,0 +1,36 @@ +package com.tuxisalive.api; + +/** + * Class to control the buttons. + * + * @author R Jocaille + * @version 0.1 + * @since 0.1 + */ +public class TuxAPIButton +{ + /* + * Public field + */ + public TuxAPISwitch left; + public TuxAPISwitch right; + public TuxAPISwitch head; + public TuxAPIRemote remote; + + /** + * Constructor of the class. + * @param parent parent object as TuxAPI + */ + public TuxAPIButton(TuxAPI parent) + { + left = new TuxAPISwitch(parent, TuxAPIConst.ST_NAME_LEFT_BUTTON); + right = new TuxAPISwitch(parent, TuxAPIConst.ST_NAME_RIGHT_BUTTON); + head = new TuxAPISwitch(parent, TuxAPIConst.ST_NAME_HEAD_BUTTON); + remote = new TuxAPIRemote(parent); + } + + /** + * Destructor of the class. + */ + public void destroy() {} +} Added: software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIConst.java =================================================================== --- software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIConst.java (rev 0) +++ software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIConst.java 2008-07-29 07:55:18 UTC (rev 1409) @@ -0,0 +1,358 @@ +package com.tuxisalive.api; + +/** + * Interface which contains all constants of the API. + * + * @author R Jocaille + * @version 0.1 + * @since 0.1 + */ +public interface TuxAPIConst { + /* + * Client levels + */ + public static final Integer CLIENT_LEVEL_ANONYME = -1; + public static final Integer CLIENT_LEVEL_FREE = 0; + public static final Integer CLIENT_LEVEL_RESTRICTED = 1; + public static final Integer CLIENT_LEVEL_ROOT = 2; + public static final Integer[] CLIENT_LEVELS = { + CLIENT_LEVEL_ANONYME, + CLIENT_LEVEL_FREE, + CLIENT_LEVEL_RESTRICTED, + CLIENT_LEVEL_ROOT + }; + /* + * Access priority levels + */ + public static final Integer ACCESS_PRIORITY_LOW = 0; + public static final Integer ACCESS_PRIORITY_NORMAL = 1; + public static final Integer ACCESS_PRIORITY_HIGH = 2; + public static final Integer ACCESS_PRIORITY_CRITICAL = 3; + public static final Integer[] ACCESS_PRIORITIES = { + ACCESS_PRIORITY_LOW, + ACCESS_PRIORITY_NORMAL, + ACCESS_PRIORITY_HIGH, + ACCESS_PRIORITY_CRITICAL + }; + /* + * Statuses declaration + */ + // From libtuxdriver + public static final String ST_NAME_HEAD_BUTTON = "head_button"; + public static final String ST_NAME_LEFT_BUTTON = "left_wing_button"; + public static final String ST_NAME_RIGHT_BUTTON = "right_wing_button"; + public static final String ST_NAME_REMOTE_BUTTON = "remote_button"; + public static final String ST_NAME_MOUTH_POSITION = "mouth_position"; + public static final String ST_NAME_MOUTH_RM = "mouth_remaining_movements"; + public static final String ST_NAME_EYES_POSITION = "eyes_position"; + public static final String ST_NAME_EYES_RM = "eyes_remaining_movements"; + public static final String ST_NAME_FLIPPERS_POSITION = "flippers_position"; + public static final String ST_NAME_FLIPPERS_RM = "flippers_remaining_movements"; + public static final String ST_NAME_SPINNING_DIRECTION = "spinning_direction"; + public static final String ST_NAME_SPINNING_RM = "spinning_remaining_movements"; + public static final String ST_NAME_DONGLE_PLUG = "dongle_plug"; + public static final String ST_NAME_RADIO_STATE = "radio_state"; + public static final String ST_NAME_LEFT_LED = "left_led_state"; + public static final String ST_NAME_RIGHT_LED = "right_led_state"; + public static final String ST_NAME_AUDIO_FLASH_PLAY = "audio_flash_play"; + public static final String ST_NAME_SOUND_REFLASH_END = "sound_reflash_end"; + public static final String ST_NAME_EYES_MOTOR_ON = "eyes_motor_on"; + public static final String ST_NAME_MOUTH_MOTOR_ON = "mouth_motor_on"; + public static final String ST_NAME_FLIPPERS_MOTOR_ON = "flippers_motor_on"; + public static final String ST_NAME_SPIN_LEFT_MOTOR_ON = "spin_left_motor_on"; + public static final String ST_NAME_SPIN_RIGHT_MOTOR_ON = "spin_right_motor_on"; + public static final String ST_NAME_FLASH_SOUND_COUNT = "sound_flash_count"; + public static final String[] SW_NAME_DRIVER = { + ST_NAME_FLIPPERS_POSITION, + ST_NAME_FLIPPERS_RM, + ST_NAME_SPINNING_DIRECTION, + ST_NAME_SPINNING_RM, + ST_NAME_LEFT_BUTTON, + ST_NAME_RIGHT_BUTTON, + ST_NAME_HEAD_BUTTON, + ST_NAME_REMOTE_BUTTON, + ST_NAME_MOUTH_POSITION, + ST_NAME_MOUTH_RM, + ST_NAME_EYES_POSITION , + ST_NAME_EYES_RM, + "descriptor_complete", + ST_NAME_RADIO_STATE, + ST_NAME_DONGLE_PLUG, + "charger_state", + "battery_level", + "battery_state", + "light_level", + ST_NAME_LEFT_LED , + ST_NAME_RIGHT_LED , + "connection_quality", + ST_NAME_AUDIO_FLASH_PLAY, + "audio_general_play", + "flash_programming_current_track", + "flash_programming_last_track_size", + "tuxcore_symbolic_version", + "tuxaudio_symbolic_version", + "fuxusb_symbolic_version", + "fuxrf_symbolic_version", + "tuxrf_symbolic_version", + "driver_symbolic_version", + "sound_reflash_begin", + ST_NAME_SOUND_REFLASH_END, + ST_NAME_EYES_MOTOR_ON, + ST_NAME_MOUTH_MOTOR_ON, + ST_NAME_FLIPPERS_MOTOR_ON, + ST_NAME_SPIN_LEFT_MOTOR_ON, + ST_NAME_SPIN_RIGHT_MOTOR_ON, + ST_NAME_FLASH_SOUND_COUNT + }; + // From libtuxosl + public static final String ST_NAME_SPEAK_STATUS = "tts_0_speak_status"; + public static final String ST_NAME_TTS_SOUND_STATE = "tts_0_sound_state"; + public static final String ST_NAME_VOICE_LIST = "tts_0_voice_list"; + public static final String ST_NAME_WAV_CHANNEL_START = "tts_wav_channel_start"; + public static final String ST_NAME_WAV_0_SOUND_STATE = "wav_0_sound_state"; + public static final String ST_NAME_WAV_1_SOUND_STATE = "wav_1_sound_state"; + public static final String ST_NAME_WAV_2_SOUND_STATE = "wav_2_sound_state"; + public static final String ST_NAME_WAV_3_SOUND_STATE = "wav_3_sound_state"; + public static final String[] SW_NAME_OSL = { + "osl_symbolic_version", + "general_sound_state", + "wav_volume", + "tts_volume", + "tts_pitch", + "tts_locutor", + ST_NAME_WAV_0_SOUND_STATE, + "wav_0_pause_state", + "wav_0_stop", + ST_NAME_WAV_1_SOUND_STATE, + "wav_1_pause_state", + "wav_1_stop", + ST_NAME_WAV_2_SOUND_STATE, + "wav_2_pause_state", + "wav_2_stop", + ST_NAME_WAV_3_SOUND_STATE, + "wav_3_pause_state", + "wav_3_stop", + ST_NAME_TTS_SOUND_STATE, + "tts_0_pause_state", + "tts_0_stop", + "tts_0_voice_loaded", + ST_NAME_SPEAK_STATUS, + ST_NAME_VOICE_LIST, + ST_NAME_WAV_CHANNEL_START, + }; + // From TuxAPI + public static final String ST_NAME_API_CONNECT = "api_connect"; + public static final String[] SW_NAME_API = { + ST_NAME_API_CONNECT, + }; + /* + * Possible string values of statuses. + */ + public static final String SSV_NDEF = "NDEF"; + public static final String SSV_OPEN = "OPEN"; + public static final String SSV_CLOSE = "CLOSE"; + public static final String SSV_UP = "UP"; + public static final String SSV_DOWN = "DOWN"; + public static final String SSV_LEFT = "LEFT"; + public static final String SSV_RIGHT = "RIGHT"; + public static final String SSV_ON = "ON"; + public static final String SSV_OFF = "OFF"; + public static final String SSV_CHANGING = "CHANGING"; + // Mouth and eyes positions + public static final String[] SSV_MOUTHEYES_POSITIONS = { + SSV_NDEF, + SSV_OPEN, + SSV_CLOSE, + }; + // Flippers positions + public static final String[] SSV_FLIPPERS_POSITIONS = { + SSV_NDEF, + SSV_UP, + SSV_DOWN, + }; + // Spinning directions + public static final String[] SSV_SPINNING_DIRECTIONS = { + SSV_NDEF, + SSV_LEFT, + SSV_RIGHT, + }; + // Led states + public static final String[] SSV_LED_STATES = { + SSV_ON, + SSV_OFF, + SSV_CHANGING, + }; + + /* + * Speed values + */ + public static final Integer SPV_VERYSLOW = 1; + public static final Integer SPV_SLOW = 2; + public static final Integer SPV_NORMAL = 3; + public static final Integer SPV_FAST = 4; + public static final Integer SPV_VERYFAST = 5; + public static final Integer[] SPV_SPEED_VALUES = { + SPV_VERYSLOW, + SPV_SLOW, + SPV_NORMAL, + SPV_FAST, + SPV_VERYFAST, + }; + + /* + * Led effects + */ + // Simples + public static final Integer LFX_NONE = 0; + public static final Integer LFX_FADE = 1; + public static final Integer LFX_STEP = 2; + public static final Integer[] LED_EFFECT_TYPE = { + LFX_NONE, + LFX_FADE, + LFX_STEP, + }; + // Extended + public static final String LFXEX_UNAFFECTED = "UNAFFECTED"; + public static final String LFXEX_LAST = "LAST"; + public static final String LFXEX_NONE = "NONE"; + public static final String LFXEX_DEFAULT = "DEFAULT"; + public static final String LFXEX_FADE_DURATION = "FADE_DURATION"; + public static final String LFXEX_FADE_RATE = "FADE_RATE"; + public static final String LFXEX_GRADIENT_NBR = "GRADIENT_NBR"; + public static final String LFXEX_GRADIENT_DELTA = "GRADIENT_DELTA"; + public static final String[] LED_EFFECT_TYPE_EX_NAMES = { + LFXEX_UNAFFECTED, + LFXEX_LAST, + LFXEX_NONE, + LFXEX_DEFAULT, + LFXEX_FADE_DURATION, + LFXEX_FADE_RATE, + LFXEX_GRADIENT_NBR, + LFXEX_GRADIENT_DELTA, + }; + /* + * Led names + */ + public static final String LED_NAME_BOTH = "LED_BOTH"; + public static final String LED_NAME_RIGHT = "LED_RIGHT"; + public static final String LED_NAME_LEFT = "LED_LEFT"; + + /* + * Sound reflash errors + */ + public static final String SOUND_REFLASH_NO_ERROR = "NO_ERROR"; + public static final String SOUND_REFLASH_ERROR_RF_OFFLINE = "ERROR_RF_OFFLINE"; + public static final String SOUND_REFLASH_ERROR_WAV = "ERROR_WAV"; + public static final String SOUND_REFLASH_ERROR_USB = "ERROR_USB"; + public static final String SOUND_REFLASH_ERROR_PARAMETERS = "ERROR_PARAMETERS"; + public static final String SOUND_REFLASH_ERROR_BUSY = "ERROR_BUSY"; + public static final String SOUND_REFLASH_ERROR_BADWAVFILE = "ERROR_BADWAVFILE"; + public static final String SOUND_REFLASH_ERROR_WAVSIZEEXCEDED = "ERROR_WAVSIZEEXCEDED"; + + /* + * Wav channels + */ + public static final String[] WAV_CHANNELS_NAME_LIST = { + ST_NAME_WAV_0_SOUND_STATE, + ST_NAME_WAV_1_SOUND_STATE, + ST_NAME_WAV_2_SOUND_STATE, + ST_NAME_WAV_3_SOUND_STATE, + }; + + /* + * Remote keys + */ + public static final String K_0 = "K_0"; + public static final String K_1 = "K_1"; + public static final String K_2 = "K_2"; + public static final String K_3 = "K_3"; + public static final String K_4 = "K_4"; + public static final String K_5 = "K_5"; + public static final String K_6 = "K_6"; + public static final String K_7 = "K_7"; + public static final String K_8 = "K_8"; + public static final String K_9 = "K_9"; + public static final String K_STANDBY = "K_STANDBY"; + public static final String K_MUTE = "K_MUTE"; + public static final String K_VOLUMEPLUS = "K_VOLUMEPLUS"; + public static final String K_VOLUMEMINUS = "K_VOLUMEMINUS"; + public static final String K_ESCAPE = "K_ESCAPE"; + public static final String K_YES = "K_YES"; + public static final String K_NO = "K_NO"; + public static final String K_BACKSPACE = "K_BACKSPACE"; + public static final String K_STARTVOIP = "K_STARTVOIP"; + public static final String K_RECEIVECALL = "K_RECEIVECALL"; + public static final String K_HANGUP = "K_HANGUP"; + public static final String K_STAR = "K_STAR"; + public static final String K_SHARP = "K_SHARP"; + public static final String K_RED = "K_RED"; + public static final String K_GREEN = "K_GREEN"; + public static final String K_BLUE = "K_BLUE"; + public static final String K_YELLOW = "K_YELLOW"; + public static final String K_CHANNELPLUS = "K_CHANNELPLUS"; + public static final String K_CHANNELMINUS = "K_CHANNELMINUS"; + public static final String K_UP = "K_UP"; + public static final String K_DOWN = "K_DOWN"; + public static final String K_LEFT = "K_LEFT"; + public static final String K_RIGHT = "K_RIGHT"; + public static final String K_OK = "K_OK"; + public static final String K_FASTREWIND = "K_FASTREWIND"; + public static final String K_FASTFORWARD = "K_FASTFORWARD"; + public static final String K_PLAYPAUSE = "K_PLAYPAUSE"; + public static final String K_STOP = "K_STOP"; + public static final String K_RECORDING = "K_RECORDING"; + public static final String K_PREVIOUS = "K_PREVIOUS"; + public static final String K_NEXT = "K_NEXT"; + public static final String K_MENU = "K_MENU"; + public static final String K_MOUSE = "K_MOUSE"; + public static final String K_ALT = "K_ALT"; + public static final String K_RELEASED = "RELEASED"; + + public static final String[] REMOTE_KEY_LIST = { + K_0, + K_1, + K_2, + K_3, + K_4, + K_5, + K_6, + K_7, + K_8, + K_9, + K_STANDBY, + K_MUTE, + K_VOLUMEPLUS, + K_VOLUMEMINUS, + K_ESCAPE, + K_YES, + K_NO, + K_BACKSPACE, + K_STARTVOIP, + K_RECEIVECALL, + K_HANGUP, + K_STAR, + K_SHARP, + K_RED, + K_GREEN, + K_BLUE, + K_YELLOW, + K_CHANNELPLUS, + K_CHANNELMINUS, + K_UP, + K_DOWN, + K_LEFT, + K_RIGHT, + K_OK, + K_FASTREWIND, + K_FASTFORWARD, + K_PLAYPAUSE, + K_STOP, + K_RECORDING, + K_PREVIOUS, + K_NEXT, + K_MENU, + K_MOUSE, + K_ALT, + K_RELEASED, + }; +} Added: software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIDongleRadio.java =================================================================== --- software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIDongleRadio.java (rev 0) +++ software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIDongleRadio.java 2008-07-29 07:55:18 UTC (rev 1409) @@ -0,0 +1,163 @@ +package com.tuxisalive.api; + +/** + * Class to interact with the connection/disconnection of the radio/dongle. + * + * @author R Jocaille + * @version 0.1 + * @since 0.1 + */ +public class TuxAPIDongleRadio +{ + /* + * Parent field + */ + private TuxAPI pParent; + private TuxEventHandlers eventHandlers; + private TuxEventHandler eventHandler; + /* + * Status field + */ + private String pStStName; + + /** + * Constructor of the class. + * + * @param parent parent object as TuxAPI. + * @param stStName status name. + */ + public TuxAPIDongleRadio(TuxAPI parent, String stStName) + { + pStStName = stStName; + pParent = parent; + eventHandlers = pParent.getEventHandlers(); + eventHandler = (TuxEventHandler)eventHandlers.getEventHandler(pStStName); + } + + /** + * Destructor of the class. + */ + public void destroy() {} + + /** + * Get the state of the radio/dongle connection. + * + * @return the state of the radio/dongle connection. + */ + public Boolean getConnected() + { + Object[] result = pParent.status.requestOne(pStStName); + if (result[0] == null) + { + return false; + } + if (result[0].equals("True")) + { + return true; + } + else + { + return false; + } + } + + /** + * Wait until the radio/dongle was connected. + * + * @param timeout maximal delay to wait. + * @return the state of the wait result. + */ + public Boolean waitConnected(Double timeout) + { + if (getConnected()) + { + return true; + } + return eventHandler.waitCondition(timeout, "True", null); + } + + /** + * Wait until the radio/dongle was disconnected. + * + * @param timeout maximal delay to wait. + * @return the state of the wait result. + */ + public Boolean waitDisconnected(Double timeout) + { + if (!getConnected()) + { + return true; + } + return eventHandler.waitCondition(timeout, "False", null); + } + + /** + * Register a callback on the connected event. + * + * @param sender parent object of the method. + * @param method method name as string. + * @return the new index of the callback in the handler. + */ + public Integer registerEventOnConnected(Object sender, String method) + { + return registerEventOnConnected(sender, method, -1); + } + + /** + * Register a callback on the connected event. + * + * @param sender parent object of the method. + * @param method method name as string. + * @param idx index from a previous register. + * @return the new index of the callback in the handler. + */ + public Integer registerEventOnConnected(Object sender, String method, Integer idx) + { + return eventHandler.register(idx, sender, method, "True", null); + } + + /** + * Unregister a callback from the connected event. + * + * @param idx index from a previous register. + */ + public void unregisterEventOnConnected(Integer idx) + { + eventHandler.unregister(idx); + } + + /** + * Register a callback on the disconnected event. + * + * @param sender parent object of the method. + * @param method method name as string. + * @return the new index of the callback in the handler. + */ + public Integer registerEventOnDisconnected(Object sender, String method) + { + return registerEventOnDisconnected(sender, method, -1); + } + + /** + * Register a callback on the disconnected event. + * + * @param sender parent object of the method. + * @param method method name as string. + * @param idx index from a previous register. + * @return the new index of the callback in the handler. + */ + public Integer registerEventOnDisconnected(Object sender, String method, Integer idx) + { + return eventHandler.register(idx, sender, method, "False", null); + } + + /** + * Unregister a callback from the disconnected event. + * + * @param idx index from a previous register. + */ + public void unregisterEventOnDisconnected(Integer idx) + { + eventHandler.unregister(idx); + } +} Added: software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIEvent.java =================================================================== --- software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIEvent.java (rev 0) +++ software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIEvent.java 2008-07-29 07:55:18 UTC (rev 1409) @@ -0,0 +1,153 @@ +package com.tuxisalive.api; + +import java.util.Hashtable; + +/** + * TuxAPIEvent is a module part of TuxAPI. It run an asynchronous loop to retrieve the + * events from Tuxdroid, every 100 msec. + * + * @author R Jocaille + * @version 0.1 + * @since 0.1 + */ +public class TuxAPIEvent +{ + /* + * Parent field + */ + private TuxAPI pParent; + private TuxEventHandlers eventHandlers; + public TuxEventHandlers handler; + /* + * Event loop field + */ + private SThread eventLoopThread; + private SLock eventLoopMutex; + private Boolean eventLoopRun; + private Double eventLoopDelay; + + /** + * Constructor of the class. + * + * @param parent parent object as TuxAPI. + */ + public TuxAPIEvent(TuxAPI parent) + { + pParent = parent; + eventHandlers = pParent.getEventHandlers(); + handler = eventHandlers; + eventLoopThread = null; + eventLoopMutex = new SLock(); + eventLoopRun = false; + eventLoopDelay = 0.1; + } + + /** + * Destructor of the class + */ + public void destroy() + { + stop(); + } + + /* + * + */ + private Boolean getEventLoopRun() + { + Boolean value; + + eventLoopMutex.acquire(); + value = eventLoopRun; + eventLoopMutex.release(); + return value; + } + + /* + * + */ + private void setEventLoopRun(Boolean value) + { + eventLoopMutex.acquire(); + eventLoopRun = value; + eventLoopMutex.release(); + } + + /** + * Start the loop of event retrieving. + */ + public void start() + { + if (getEventLoopRun()) + { + return; + } + eventLoopThread = new SThread((Object)this, "eventLoop"); + eventLoopThread.start(); + } + + /** + * Stop the loop of event retrieving. + */ + @SuppressWarnings("deprecation") + public void stop() + { + if (!getEventLoopRun()) + { + return; + } + setEventLoopRun(false); + TuxAPIMisc.sleep(eventLoopDelay); + if (eventLoopThread != null) + { + if (eventLoopThread.isAlive()) + { + eventLoopThread.stop(); + } + } + } + + /* + * + */ + @SuppressWarnings("unchecked") + protected void eventLoop() + { + String stName; + String stValue; + Double stDelay; + + setEventLoopRun(true); + while (getEventLoopRun()) + { + // Make command + String cmd = "status/events?"; + Hashtable<Object,Object> varStruct = new Hashtable<Object,Object>(); + Hashtable<Object,Object> varResult = new Hashtable<Object,Object>(); + // Request + if (pParent.server.request(cmd, varStruct, varResult, false)) + { + Integer dataCount = (Integer)varResult.get("data_count"); + for (int i = 0; i < dataCount; i++) + { + String dataName = String.format("data%d", i); + try + { + Hashtable<Object,Object> eventStruct = (Hashtable)varResult.get(dataName); + stName = (String)eventStruct.get("name"); + stValue = (String)eventStruct.get("value"); + String tmp = (String)eventStruct.get("delay"); + stDelay = Double.parseDouble(tmp); + } + catch (Exception e) + { + continue; + } + eventHandlers.emit(stName, stValue, stDelay); + } + } + // Wait before the next cycle + TuxAPIMisc.sleep(eventLoopDelay); + } + } +} Added: software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIFlippers.java =================================================================== --- software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIFlippers.java (rev 0) +++ software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPIFlippers.java 2008-07-29 07:55:18 UTC (rev 1409) @@ -0,0 +1,350 @@ +package com.tuxisalive.api; + +import java.util.Hashtable; + +/** + * Class to control the flippers movements. + * + * @author R Jocaille + * @version 0.1 + * @since 0.1 + */ +public class TuxAPIFlippers +{ + /* + * Parent field + */ + private TuxAPI pParent; + private TuxEventHandlers eventHandlers; + + /** + * Constructor of the class. + * @param parent parent object as TuxAPI + */ + public TuxAPIFlippers(TuxAPI parent) + { + pParent = parent; + eventHandlers = pParent.getEventHandlers(); + } + + /** + * Destructor of the class. + */ + public void destroy() {} + + /* + * + */ + private Boolean cmdSimpleResult(String cmd) + { + Hashtable<Object,Object> varStruct = new Hashtable<Object,Object>(); + Hashtable<Object,Object> varResult = new Hashtable<Object,Object>(); + + return pParent.server.request(cmd, varStruct, varResult, false); + } + + /* + * + */ + private Integer checkSpeed(Integer speed) + { + if (speed < TuxAPIConst.SPV_VERYSLOW) + { + speed = TuxAPIConst.SPV_VERYSLOW; + } + if (speed > TuxAPIConst.SPV_VERYFAST) + { + speed = TuxAPIConst.SPV_VERYFAST; + } + return speed; + } + + /** + * Set the flippers to up. + * + * @return the success of the command. + */ + public Boolean up() + { + String cmd; + + cmd = "flippers/up?"; + return cmdSimpleResult(cmd); + } + + /** + * Set the flippers to down. + * + * @return the success of the command. + */ + public Boolean down() + { + String cmd; + + cmd = "flippers/down?"; + return cmdSimpleResult(cmd); + } + + /** + * Stop the movement of flippers. + * + * @return the success of the command. + */ + public Boolean off() + { + String cmd; + + cmd = "flippers/off?"; + return cmdSimpleResult(cmd); + } + + /** + * Set the speed of the flippers movement. + * + * @param speed speed of the movement. + * @return the success of the command. + */ + public Boolean setSpeed(Integer speed) + { + speed = checkSpeed(speed); + String cmd = String.format("flippers/speed?value=%d", speed); + return cmdSimpleResult(cmd); + } + + /** + * Move the flippers. + * (asynchronous) + * + * @param count number of movements. + * @param finalState requested state after the movement. + * (SSV_NDEF|SSV_UP|SSV_DOWN) + * @param speed speed of the movement. + * @return the success of the command. + */ + public Boolean onAsync(Integer count, String finalState, Integer speed) + { + String cmd; + + if (!TuxAPIMisc.stringInStringArray(TuxAPIConst.SSV_FLIPPERS_POSITIONS, + finalState)) + { + return false; + } + + cmd = String.format("flippers/on?count=%d&final_state=%s", count, finalState); + Boolean ret = cmdSimpleResult(cmd); + if (ret) + { + ret = setSpeed(speed); + } + return ret; + } + + /** + * Move the flippers. + * (asynchronous) + * + * @param count number of movements. + * @param finalState requested state after the movement. + * (SSV_NDEF|SSV_UP|SSV_DOWN) + * @return the success of the command. + */ + public Boolean onAsync(Integer count, String finalState) + { + return onAsync(count, finalState, TuxAPIConst.SPV_VERYFAST); + } + + /** + * Move the flippers. + * + * @param count number of movements. + * @param finalState requested state after the movement. + * (SSV_NDEF|SSV_UP|SSV_DOWN) + * @param speed speed of the movement. + * @return the success of the command. + */ + public Boolean on(Integer count, String finalState, Integer speed) + { + if (!TuxAPIMisc.stringInStringArray(TuxAPIConst.SSV_FLIPPERS_POSITIONS, + finalState)) + { + return false; + } + Double timeout = count * 1.0; + Boolean ret = onAsync(count, finalState, speed); + if (ret) + { + ret = waitMovingOff(timeout); + } + return ret; + } + + /** + * Move the flippers. + * + * @param count number of movements. + * @param finalState requested state after the movement. + * (SSV_NDEF|SSV_UP|SSV_DOWN) + * @return the success of the command. + */ + public Boolean on(Integer count, String finalState) + { + return on(count, finalState, TuxAPIConst.SPV_VERYFAST); + } + + /** + * Move the flippers during a number of seconds. + * (asynchronous) + * + * @param duration duration time in seconds. + * @param finalState requested state after the movement. + * (SSV_NDEF|SSV_UP|SSV_DOWN) + * @param speed speed of the movement. + * @return the success of the command. + */ + public Boolean onDuringAsync(Double duration, String finalState, Integer speed) + { + String cmd; + + if (!TuxAPIMisc.stringInStringArray(TuxAPIConst.SSV_FLIPPERS_POSITIONS, + finalState)) + { + return false; + } + + cmd = String.format("flippers/on_during?duration=%g&final_state=%s", + duration, finalState); + Boolean ret = cmdSimpleResult(cmd); + if (ret) + { + ret = setSpeed(speed); + } + return ret; + } + + /** + * Move the flippers during a number of seconds. + * (asynchronous) + * + * @param duration duration time in seconds. + * @param finalState requested state after the movement. + * (SSV_NDEF|SSV_UP|SSV_DOWN) + * @return the success of the command. + */ + public Boolean onDuringAsync(Double duration, String finalState) + { + return onDuringAsync(duration, finalState, TuxAPIConst.SPV_VERYFAST); + } + + /** + * Move the flippers during a number of seconds. + * + * @param duration duration time in seconds. + * @param finalState requested state after the movement. + * (SSV_NDEF|SSV_UP|SSV_DOWN) + * @param speed speed of the movement. + * @return the success of the command. + */ + public Boolean onDuring(Double duration, String finalState, Integer speed) + { + Double timeout = duration * 2.0; + Boolean ret = onDuringAsync(duration, finalState, speed); + if (ret) + { + ret = waitMovingOff(timeout); + } + return ret; + } + + /** + * Move the flippers during a number of seconds. + * + * @param duration duration time in seconds. + * @param finalState requested state after the movement. + * (SSV_NDEF|SSV_UP|SSV_DOWN) + * @return the success of the command. + */ + public Boolean onDuring(Double duration, String finalState) + { + return onDuring(duration, finalState, TuxAPIConst.SPV_VERYFAST); + } + + /** + * Get the position of the flippers. + * + * @return (SSV_NDEF|SSV_UP|SSV_DOWN) + */ + public String getPosition() + { + Object[] result = pParent.status.requestOne(TuxAPIConst.ST_NAME_FLIPPERS_POSITION); + if (result[0] == null) + { + return TuxAPIConst.SSV_NDEF; + } + if (!TuxAPIMisc.stringInStringArray(TuxAPIConst.SSV_FLIPPERS_POSITIONS, + (String)result[0])) + { + return TuxAPIConst.SSV_NDEF; + } + else + { + return (String)result[0]; + } + } + + /** + * Get the moving state of the flippers. + * + * @return a boolean. + */ + public Boolean getMovingState() + { + Object[] result = pParent.status.requestOne(TuxAPIConst.ST_NAME_FLIPPERS_MOTOR_ON); + if (result[0] == null) + { + return false; + } + if (result[0].equals("False")) + { + return false; + } + else + { + return true; + } + } + + /** + * Wait that this body part don't move. + * + * @param timeout maximal delay to wait. + * @return the state of the wait result. + */ + public Boolean waitMovingOff(Double timeout) + { + return eventHandlers.waitCondition(TuxAPIConst.ST_NAME_FLIPPERS_MOTOR_ON, timeout, + "False", null); + } + + /** + * Wait a specific position of the flippers. + * + * @param position position to wait. + * @param timeout maximal delay to wait. + * @return the state of the wait result. + */ + public Boolean waitPosition(String position, Double timeout) + { + if (!TuxAPIMisc.stringInStringArray(TuxAPIConst.SSV_FLIPPERS_POSITIONS, + position)) + { + return false; + } + if (getPosition().equals(position)) + { + return true; + } + return eventHandlers.waitCondition(TuxAPIConst.ST_NAME_FLIPPERS_POSITION, timeout, + position, null); + } +} Added: software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPILed.java =================================================================== --- software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPILed.java (rev 0) +++ software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPILed.java 2008-07-29 07:55:18 UTC (rev 1409) @@ -0,0 +1,34 @@ +package com.tuxisalive.api; + +/** + * Class to control the blue leds. + * + * @author R Jocaille + * @version 0.1 + * @since 0.1 + */ +public class TuxAPILed +{ + /* + * Public field + */ + public TuxAPILedBase left; + public TuxAPILedBase right; + public TuxAPILedBase both; + + /** + * Constructor of the class. + * @param parent parent object as TuxAPI + */ + public TuxAPILed(TuxAPI parent) + { + left = new TuxAPILedBase(parent, "left"); + right = new TuxAPILedBase(parent, "right"); + both = new TuxAPILedBase(parent, "both"); + } + + /** + * Destructor of the class. + */ + public void destroy() {} +} Added: software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPILedBase.java =================================================================== --- software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPILedBase.java (rev 0) +++ software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxAPILedBase.java 2008-07-29 07:55:18 UTC (rev 1409) @@ -0,0 +1,362 @@ +package com.tuxisalive.api; + +import java.util.Hashtable; + +/** + * Base class to control a led. + * + * @author R Jocaille + * @version 0.1 + * @since 0.1 + */ +public class TuxAPILedBase +{ + /* + * Parent field + */ + private TuxAPI pParent; + + /* + * Private field + */ + private String ledNamex; + + /** + * Constructor of the class. + * @param parent parent object as TuxAPI + */ + public TuxAPILedBase(TuxAPI parent, String ledName) + { + pParent = parent; + if (ledName == "both") + { + ledNamex = TuxAPIConst.LED_NAME_BOTH; + } + else if (ledName == "left") + { + ledNamex = TuxAPIConst.LED_NAME_LEFT; + } + else + { + ledNamex = TuxAPIConst.LED_NAME_RIGHT; + } + } + + /** + * Destructor of the class. + */ + public void destroy() {} + + /* + * + */ + private Boolean cmdSimpleResult(String cmd) + { + Hashtable<Object,Object> varStruct = new Hashtable<Object,Object>(); + Hashtable<Object,Object> varResult = new Hashtable<Object,Object>(); + + return pParent.server.request(cmd, varStruct, varResult, false); + } + + /* + * + */ + private Boolean changeIntensity(Integer fxType, Double intensity) + { + String cmd; + Integer fxStep; + String fxCType; + + if (fxType == TuxAPIConst.LFX_NONE) + { + cmd = String.format("leds/on?intensity=%g&leds=%s", intensity, ledNamex); + } + else + { + if (fxType == TuxAPIConst.LFX_FADE) + { + fxStep = 10; + } + else // TuxAPIConst.LFX_STEP + { + fxStep = 3; + } + fxCType = TuxAPIConst.LFXEX_GRADIENT_NBR; + cmd = String.format("leds/set?fx_speed=0.5&fx_step=%d&fx_type=%s&intensity=%g&leds=%s", + fxStep, fxCType, intensity, ledNamex); + } + return cmdSimpleResult(cmd); + } + + /** + * Set the intensity of the led. + * + * @param intensity intensity of the led (0.0 .. 1.0) + * @return the success of the command. + */ + public Boolean setIntensity(Double intensity) + { + return changeIntensity(TuxAPIConst.LFX_NONE, intensity); + } + + /** + * Set the led state to ON. + * + * @param fxType type of the transition effect. + * (LFX_NONE|LFX_FADE|LFX_STEP) + * @return the success of the command. + */ + public Boolean on(Integer fxType) + { + return changeIntensity(fxType, 1.0); + } + + /** + * Set the led state to ON. + * + * @return the success of the command. + */ + public Boolean on() + { + return on(TuxAPIConst.LFX_NONE); + } + + /** + * Set the led state to OFF. + * + * @param fxType type of the transition effect. + * (LFX_NONE|LFX_FADE|LFX_STEP) + * @return the success of the command. + */ + public Boolean off(Integer fxType) + { + return changeIntensity(fxType, 0.0); + } + + /** + * Set the led state to OFF. + * + * @return the success of the command. + */ + public Boolean off() + { + return off(TuxAPIConst.LFX_NONE); + } + + /** + * Make a pulse effect with the led. + * (Asynchronous) + * + * @param speed speed of the state changing. + * (SPV_VERYSLOW|SPV_SLOW|SPV_NORMAL|SPV_FAST|SPV_VERYFAST) + * @param duration duration of the effect. + * @param fxType type of the transition effect. + * (LFX_NONE|LFX_FADE|LFX_STEP) + * @return the success of the command. + */ + public Boolean blinkDuringAsync(Integer speed, Double duration, Integer fxType) + { + Double perSec = speed * 1.0; + duration = duration * perSec * 2.; + Integer count = duration.intValue(); + Double delay = 1.0 / perSec; + String cmd; + Integer fxStep; + String fxCType; + Double fxSpeed; + + if (fxType == TuxAPIConst.LFX_NONE) + { + cmd = String.format("leds/blink?leds=%s&count=%d&delay=%g", ledNamex, + count, delay); + } + else + { + if (fxType == TuxAPIConst.LFX_FADE) + { + fxStep = 10; + } + else // TuxAPIConst.LFX_STEP + { + fxStep = 2; + } + fxCType = TuxAPIConst.LFXEX_GRADIENT_NBR; + fxSpeed = delay / 3.0; + cmd = String.format("leds/pulse?count=%d&fx_speed=%g&fx_step=%d&fx_type=%s&leds=%s&max_intensity=1.0&min_intensity=0.0&period=%g", + count, fxSpeed, fxStep, fxCType, ledNamex, delay); + } + return cmdSimpleResult(cmd); + } + + /** + * Make a pulse effect with the led. + * (Asynchronous) + * + * @param speed speed of the state changing. + * (SPV_VERYSLOW|SPV_SLOW|SPV_NORMAL|SPV_FAST|SPV_VERYFAST) + * @param duration duration of the effect. + * @return the success of the command. + */ + public Boolean blinkDuringAsync(Integer speed, Double duration) + { + return blinkDuringAsync(speed, duration, TuxAPIConst.LFX_NONE); + } + + /** + * Make a pulse effect with the led. + * + * @param speed speed of the state changing. + * (SPV_VERYSLOW|SPV_SLOW|SPV_NORMAL|SPV_FAST|SPV_VERYFAST) + * @param duration duration of the effect. + * @param fxType type of the transition effect. + * (LFX_NONE|LFX_FADE|LFX_STEP) + * @return the success of the command. + */ + public Boolean blinkDuring(Integer speed, Double duration, Integer fxType) + { + Boolean ret = blinkDuringAsync(speed, duration, fxType); + if (ret) + { + TuxAPIMisc.sleep(duration); + } + return ret; + } + + /** + * Make a pulse effect with the led. + * + * @param speed speed of the state changing. + * (SPV_VERYSLOW|SPV_SLOW|SPV_NORMAL|SPV_FAST|SPV_VERYFAST) + * @param duration duration of the effect. + * @return the success of the command. + */ + public Boolean blinkDuring(Integer speed, Double duration) + { + return blinkDuring(speed, duration, TuxAPIConst.LFX_NONE); + } + + /** + * Make a pulse effect with the led. + * (Asynchronous) + * + * @param speed speed of the state changing. + * (SPV_VERYSLOW|SPV_SLOW|SPV_NORMAL|SPV_FAST|SPV_VERYFAST) + * @param count number of blinks. + * @param fxType type of the transition effect. + * (LFX_NONE|LFX_FADE|LFX_STEP) + * @return the success of the command. + */ + public Boolean blinkAsync(Integer speed, Integer count, Integer fxType) + { + count *= 2; + Double delay = 1.0 / speed; + String cmd; + Integer fxStep; + String fxCType; + Double fxSpeed; + + if (fxType == TuxAPIConst.LFX_NONE) + { + cmd = String.format("leds/blink?leds=%s&count=%d&delay=%g", ledNamex, + count, delay); + } + else + { + if (fxType == TuxAPIConst.LFX_FADE) + { + fxStep = 10; + } + else // TuxAPIConst.LFX_STEP + { + fxStep = 2; + } + fxCType = TuxAPIConst.LFXEX_GRADIENT_NBR; + fxSpeed = delay / 3.0; + cmd = String.format("leds/pulse?count=%d&fx_speed=%g&fx_step=%d&fx_type=%s&leds=%s&max_intensity=1.0&... [truncated message content] |