2014-10-27 22:27:25 +00:00
parent 2a5584624c
commit fd95b81c10

View File

@@ -125,11 +125,8 @@ public class IBuddy {
private Device device; private Device device;
/* /*
Current i-Buddy state. * i-Buddy's current state.
*/ */
private boolean open;
private boolean closed;
private boolean heart; private boolean heart;
private boolean headBlue; private boolean headBlue;
private boolean headGreen; private boolean headGreen;
@@ -208,17 +205,21 @@ public class IBuddy {
* @param command the command byte. * @param command the command byte.
*/ */
private synchronized void sendMessage(byte command) throws IBuddyException { private synchronized void sendMessage(byte command) throws IBuddyException {
open();
try { try {
device.controlMsg(USB.REQ_TYPE_TYPE_CLASS | USB.REQ_TYPE_RECIP_INTERFACE, USB.REQ_SET_CONFIGURATION, 0x02, 0x01, INIT, INIT.length, 100, true); device.controlMsg(USB.REQ_TYPE_TYPE_CLASS | USB.REQ_TYPE_RECIP_INTERFACE, USB.REQ_SET_CONFIGURATION, 0x02, 0x01, INIT, INIT.length, 100, true);
} catch (USBException e) { } catch (USBException e) {
close();
throw new IBuddyException("Could not send message to i-Buddy", e); throw new IBuddyException("Could not send message to i-Buddy", e);
} }
byte[] commandMessage = getCommandMessage(command); byte[] commandMessage = getCommandMessage(command);
try { try {
device.controlMsg(USB.REQ_TYPE_TYPE_CLASS | USB.REQ_TYPE_RECIP_INTERFACE, USB.REQ_SET_CONFIGURATION, 0x02, 0x01, commandMessage, commandMessage.length, 100, true); device.controlMsg(USB.REQ_TYPE_TYPE_CLASS | USB.REQ_TYPE_RECIP_INTERFACE, USB.REQ_SET_CONFIGURATION, 0x02, 0x01, commandMessage, commandMessage.length, 100, true);
} catch (USBException e) { } catch (USBException e) {
close();
throw new IBuddyException("Could not send message to i-Buddy", e); throw new IBuddyException("Could not send message to i-Buddy", e);
} }
close();
} }
/** /**
@@ -493,53 +494,25 @@ public class IBuddy {
* *
* @throws IBuddyException in case of problem while opening the usb device or if the i-Buddy was already open. * @throws IBuddyException in case of problem while opening the usb device or if the i-Buddy was already open.
*/ */
public synchronized void open() throws IBuddyException { private synchronized void open() throws IBuddyException {
if (open) {
throw new IBuddyException("i-Buddy is already open");
}
device = USB.getDevice(DEVICE_VENDOR, DEVICE_PRODUCT); device = USB.getDevice(DEVICE_VENDOR, DEVICE_PRODUCT);
try { try {
device.open(1, 0, 0); device.open(1, 0, 0);
} catch (USBException e) { } catch (USBException e) {
throw new IBuddyException("Could not open i-Buddy", e); throw new IBuddyException("Could not open i-Buddy", e);
} }
sendAllOff();
open = true;
} }
/** /**
* Closes the usb connection to the i-Buddy. If {@code reset} is {@code true}, the "turn eveything off" command is * Closes the usb connection to the i-Buddy.
* first sent.
* *
* @throws IBuddyException in case of problem while closing the usb device or if the i-Buddy was already closed or * @throws IBuddyException in case of problem while closing the usb device.
* if it was not open..
*/ */
public synchronized void close(boolean reset) throws IBuddyException { private synchronized void close() throws IBuddyException {
if (closed) {
throw new IBuddyException("i-Buddy is already closed");
}
if (!open) {
throw new IBuddyException("i-Buddy is not open");
}
if (reset) {
sendAllOff();
}
try { try {
device.close(); device.close();
} catch (USBException e) { } catch (USBException e) {
throw new IBuddyException("Could not close i-Buddy", e); throw new IBuddyException("Could not close i-Buddy", e);
} }
open = false;
closed = true;
}
/**
* Closes the usb connection to the i-Buddy. The "turn eveything off" command is first sent.
*
* @throws IBuddyException in case of problem while closing the usb device or if the i-Buddy was already closed or
* if it was not open..
*/
public synchronized void close() throws IBuddyException {
close(true);
} }
} }