From fd95b81c107b634ed9ee485f80d461060c106098 Mon Sep 17 00:00:00 2001 From: Rik Veenboer Date: Mon, 27 Oct 2014 22:27:25 +0000 Subject: [PATCH] Update source from http://www.jraf.org/static/maven/sites/jlibibuddy/org/jraf/jlibibuddy/1.1/jlibibuddy-1.1-dist.zip --- src/main/java/org/jraf/jlibibuddy/IBuddy.java | 45 ++++--------------- 1 file changed, 9 insertions(+), 36 deletions(-) diff --git a/src/main/java/org/jraf/jlibibuddy/IBuddy.java b/src/main/java/org/jraf/jlibibuddy/IBuddy.java index bf65e13..adf6427 100644 --- a/src/main/java/org/jraf/jlibibuddy/IBuddy.java +++ b/src/main/java/org/jraf/jlibibuddy/IBuddy.java @@ -125,11 +125,8 @@ public class IBuddy { private Device device; /* - Current i-Buddy state. + * i-Buddy's current state. */ - private boolean open; - private boolean closed; - private boolean heart; private boolean headBlue; private boolean headGreen; @@ -208,17 +205,21 @@ public class IBuddy { * @param command the command byte. */ private synchronized void sendMessage(byte command) throws IBuddyException { + open(); try { 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) { + close(); throw new IBuddyException("Could not send message to i-Buddy", e); } byte[] commandMessage = getCommandMessage(command); try { 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) { + close(); 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. */ - public synchronized void open() throws IBuddyException { - if (open) { - throw new IBuddyException("i-Buddy is already open"); - } + private synchronized void open() throws IBuddyException { device = USB.getDevice(DEVICE_VENDOR, DEVICE_PRODUCT); try { device.open(1, 0, 0); } catch (USBException 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 - * first sent. + * Closes the usb connection to the i-Buddy. * - * @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.. + * @throws IBuddyException in case of problem while closing the usb device. */ - public synchronized void close(boolean reset) 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(); - } + private synchronized void close() throws IBuddyException { try { device.close(); } catch (USBException 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); } } \ No newline at end of file