Class CarAPI

java.lang.Object
  extended byCarAPI

public class CarAPI
extends java.lang.Object

Copyright (c) 2003 Sony Ericsson Mobile Communications AB
All Rights Reserved.

CarAPI is the interface between the GUI and the other classes. It provides methods for GUI to control the Bluetooth car and check its status. CarConnection in the other hand retrieves the control commands from CarAPI and send them to the car.

Version:
2.0
Author:
Christopher Wong, Åke Rydgren

Field Summary
static int CAR_CHARGING
          Constant for car charging.
static int CAR_DIRECTION_LEFT
          Constant for car turning left.
static int CAR_DIRECTION_LIGHT_LEFT
          Constant for car turning left lightly.
static int CAR_DIRECTION_LIGHT_RIGHT
          Constant for car turning right lightly.
static int CAR_DIRECTION_RIGHT
          Constant for car turning right.
static int CAR_DIRECTION_STRAIGHT
          Constant for car going straight.
static int CAR_NO_LINK
          Constant for no connection to the car.
static int CAR_READY
          Constant for car ready to drive.
static int CAR_SPEED_1
          Constant for car driving forward with speed 1.
static int CAR_SPEED_2
          Constant for car driving forward with speed 2.
static int CAR_SPEED_3
          Constant for car driving forward with speed 3.
static int CAR_SPEED_N
          Constant for car stopped.
static int CAR_SPEED_R
          Constant for car driving backward.
 boolean timeToCloseConnection
          Flag indicating that the car connection should close as soon as possible.
 
Constructor Summary
CarAPI(BluetoothCar bluetoothCar)
          Constructs a new CarAPI object.
 
Method Summary
 void decSpeed()
          Decrease the speed by one.
 int getAndClearDirection()
          Clears the flag and retrieves the direction set for the Bluetooth car.
 int getAndClearSpeed()
          Clears the flag and retrieves the speed set for the Bluetooth car.
 int getDirection()
          Retrieves the direction set for the Bluetooth car.
 int getSpeed()
          Retrieves the speed set for the Bluetooth car.
 int getStatus()
          Returns the current status of the Bluetooth car.
 void incSpeed()
          Increase the speed by one.
 boolean isAppActive()
          Check if the MIDlet is in Active state and shown.
 boolean isDirectionChanged()
          Check if a new direction is set for the Bluetooth car.
 boolean isSpeedChanged()
          Check if a new speed is set for the Bluetooth car.
 void setAppActive(boolean active)
          Called when the MIDlet switches between the Paused and the Active state and when the Canvas in GUI switches between being hidden and shown.
 void setDirection(int d)
          Controls the speed of the car.
 void setSpeed(int s)
          Controls the speed of the car.
 void setStatus(int s)
          Sets the current status of the Bluetooth car.
 void startDisconnectTimer()
          Starts a timer that will set the flag timeToCloseConnection, which is checked by the processEvents method in CarConnection (looped from SPPServer).
 void stop()
          This will stop the car.
 void stopTimer()
          Stops the disconnectTimer.
 void stopWaiting()
          Releases any thread that is trapped in wait mode in the waitUntilActive() method above.
 void turnLeft()
          Sets the car direction to turn one step to the left.
 void turnRight()
          Sets the car direction to turn one step to the right.
 void waitUntilActive()
          Puts the calling thread into wait mode if the MIDlet is inactive or not visible and if there is no connection to the car and the GUI has processed that state (i.e. the CAR_NO_LINK state).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CAR_READY

public static final int CAR_READY
Constant for car ready to drive.

See Also:
Constant Field Values

CAR_CHARGING

public static final int CAR_CHARGING
Constant for car charging.

See Also:
Constant Field Values

CAR_NO_LINK

public static final int CAR_NO_LINK
Constant for no connection to the car.

See Also:
Constant Field Values

CAR_SPEED_3

public static final int CAR_SPEED_3
Constant for car driving forward with speed 3.

See Also:
Constant Field Values

CAR_SPEED_2

public static final int CAR_SPEED_2
Constant for car driving forward with speed 2.

See Also:
Constant Field Values

CAR_SPEED_1

public static final int CAR_SPEED_1
Constant for car driving forward with speed 1.

See Also:
Constant Field Values

CAR_SPEED_N

public static final int CAR_SPEED_N
Constant for car stopped.

See Also:
Constant Field Values

CAR_SPEED_R

public static final int CAR_SPEED_R
Constant for car driving backward.

See Also:
Constant Field Values

CAR_DIRECTION_LEFT

public static final int CAR_DIRECTION_LEFT
Constant for car turning left.

See Also:
Constant Field Values

CAR_DIRECTION_LIGHT_LEFT

public static final int CAR_DIRECTION_LIGHT_LEFT
Constant for car turning left lightly.

See Also:
Constant Field Values

CAR_DIRECTION_STRAIGHT

public static final int CAR_DIRECTION_STRAIGHT
Constant for car going straight.

See Also:
Constant Field Values

CAR_DIRECTION_LIGHT_RIGHT

public static final int CAR_DIRECTION_LIGHT_RIGHT
Constant for car turning right lightly.

See Also:
Constant Field Values

CAR_DIRECTION_RIGHT

public static final int CAR_DIRECTION_RIGHT
Constant for car turning right.

See Also:
Constant Field Values

timeToCloseConnection

public boolean timeToCloseConnection
Flag indicating that the car connection should close as soon as possible.

Constructor Detail

CarAPI

public CarAPI(BluetoothCar bluetoothCar)
Constructs a new CarAPI object.

Method Detail

isAppActive

public boolean isAppActive()
Check if the MIDlet is in Active state and shown.

Returns:
true if the MIDlet is Active, false otherwise.

setAppActive

public void setAppActive(boolean active)
Called when the MIDlet switches between the Paused and the Active state and when the Canvas in GUI switches between being hidden and shown. The GUI Canvas becomes hidden when it's partly or completely obscured by another application window. If we are entering the Paused state or being hidden, the car is stopped and the disconnectTimer is started. If we are entering the Active state or being shown, any thread trapped in the waitUntilActive() method will be signalled to exit from its wait state and the disconnectTimer is stopped.

Parameters:
active - true if we are entering the Active state or being shown, false if we are entering the Paused state or being hidden.

startDisconnectTimer

public void startDisconnectTimer()
Starts a timer that will set the flag timeToCloseConnection, which is checked by the processEvents method in CarConnection (looped from SPPServer). The SPPServer will close the connection when this flag is set.


stopTimer

public void stopTimer()
Stops the disconnectTimer.


waitUntilActive

public void waitUntilActive()
Puts the calling thread into wait mode if the MIDlet is inactive or not visible and if there is no connection to the car and the GUI has processed that state (i.e. the CAR_NO_LINK state).


stopWaiting

public void stopWaiting()
Releases any thread that is trapped in wait mode in the waitUntilActive() method above.


setStatus

public void setStatus(int s)
Sets the current status of the Bluetooth car. If the GUI is trapped waiting for the application to become active, and the new status is different from the previous, the GUI is released from the wait() method and starts running again.

Parameters:
s - the status to be set. It has to be either CAR_READY, CAR_CHARGING, or CAR_NO_LINK.

getStatus

public int getStatus()
Returns the current status of the Bluetooth car.

Returns:
the current status fo the Bluetooth car

isSpeedChanged

public boolean isSpeedChanged()
Check if a new speed is set for the Bluetooth car.

Returns:
true if the car speed has changed
false otherwise

setSpeed

public void setSpeed(int s)
Controls the speed of the car.

Parameters:
s - this can be CAR_SPEED_R, CAR_SPEED_N, CAR_SPEED_1, CAR_SPEED_2, or CAR_SPEED_3.

incSpeed

public void incSpeed()
Increase the speed by one.


decSpeed

public void decSpeed()
Decrease the speed by one.


getSpeed

public int getSpeed()
Retrieves the speed set for the Bluetooth car.

Returns:
the speed set. It can be either CAR_SPEED_R, CAR_SPEED_N, CAR_SPEED_1, CAR_SPEED_2, or CAR_SPEED_3.

getAndClearSpeed

public int getAndClearSpeed()
Clears the flag and retrieves the speed set for the Bluetooth car.

Returns:
the speed set. It can be either CAR_SPEED_R, CAR_SPEED_N, CAR_SPEED_1, CAR_SPEED_2, or CAR_SPEED_3.

isDirectionChanged

public boolean isDirectionChanged()
Check if a new direction is set for the Bluetooth car.

Returns:
true if the car direction has changed
false otherwise

setDirection

public void setDirection(int d)
Controls the speed of the car.

Parameters:
d - this can be CAR_DIRECTION_LEFT, CAR_DIRECTION_LIGHT_LEFT, CAR_DIRECTION_STRAIGHT, CAR_DIRECTION_LIGHT_RIGHT, or CAR_DIRECTION_RIGHT.

turnLeft

public void turnLeft()
Sets the car direction to turn one step to the left.


turnRight

public void turnRight()
Sets the car direction to turn one step to the right.


getDirection

public int getDirection()
Retrieves the direction set for the Bluetooth car.

Returns:
the direction set. It can be either CAR_DIRECTION_LEFT, CAR_DIRECTION_LIGHT_LEFT, CAR_DIRECTION_STRAIGHT, CAR_DIRECTION_LIGHT_RIGHT, or CAR_DIRECTION_RIGHT.

getAndClearDirection

public int getAndClearDirection()
Clears the flag and retrieves the direction set for the Bluetooth car.

Returns:
the direction set. It can be either CAR_DIRECTION_LEFT, CAR_DIRECTION_LIGHT_LEFT, CAR_DIRECTION_STRAIGHT, CAR_DIRECTION_LIGHT_RIGHT, or CAR_DIRECTION_RIGHT.

stop

public void stop()
This will stop the car.