1 /*
2 This source is part of the
3 _____ ___ ____
4 __ / / _ \/ _ | / __/___ _______ _
5 / // / , _/ __ |/ _/_/ _ \/ __/ _ `/
6 \___/_/|_/_/ |_/_/ (_)___/_/ \_, /
7 /___/
8 repository. It is in the public domain.
9 Contact BoD@JRAF.org for more information.
10
11 $Id: IBuddyUtils.java 265 2008-09-07 14:37:02Z bod $
12 */
13 package org.jraf.jlibibuddy;
14
15 /**
16 * Utility class providing high-level commands for the i-Buddy.
17 */
18 public class IBuddyUtils {
19 private IBuddyUtils() {
20 // suppress default constructor for noninstanciability
21 throw new UnsupportedOperationException("Utility class should not be instantieded");
22 }
23
24 /**
25 * Makes the current thread sleep a number of milliseconds.
26 * This simply calls {@link Thread#sleep(long)} and ignores the {@link InterruptedException} if it is thrown.
27 * @param ms the number of milliseconds to sleep.
28 */
29 public static void sleep(long ms) {
30 try {
31 Thread.sleep(ms);
32 } catch (InterruptedException e) {
33 // ignore if happens
34 }
35 }
36
37 /**
38 * Makes the i-Buddy's head blink.
39 *
40 * @param iBuddy the {@link IBuddy} object.
41 * @param color the color to use.
42 * @param onTime the duration in milliseconds to wait after turning the head on.
43 * @param offTime the duration in milliseconds to wait after turning the head off.
44 * @param times number of times to blink.
45 *
46 * @throws IBuddyException in case of problem while sending the usb command.
47 */
48 public static void blink(IBuddy iBuddy, IBuddy.Color color, long onTime, long offTime, int times) throws IBuddyException {
49 for (int i = 0; i < times; i++) {
50 iBuddy.sendHeadColor(color);
51 sleep(onTime);
52 iBuddy.sendHeadColor(IBuddy.Color.OFF);
53 if (i != times - 1) {
54 sleep(offTime);
55 }
56 }
57 }
58
59 /**
60 * Makes the i-Buddy nudge (rotate left, then right).
61 *
62 * @param iBuddy the {@link IBuddy} object.
63 * @param delay the delay in milliseconds to use between rotating left and right.
64 * @param times the number of times to nudge.
65 *
66 * @throws IBuddyException in case of problem while sending the usb command.
67 */
68 public static void nudge(IBuddy iBuddy, long delay, int times) throws IBuddyException {
69 for (int i = 0; i < times; i++) {
70 iBuddy.sendRotate(IBuddy.Rotate.LEFT);
71 sleep(delay);
72 iBuddy.sendRotate(IBuddy.Rotate.RIGHT);
73 sleep(delay);
74 }
75 iBuddy.sendRotate(IBuddy.Rotate.AT_EASE);
76 }
77
78 /**
79 * Makes the i-Buddy flap its wings (move up then down).
80 *
81 * @param iBuddy the {@link IBuddy} object.
82 * @param delay the delay in milliseconds to use between moving the wings up and down.
83 * @param times the number of times to flap.
84 *
85 * @throws IBuddyException in case of problem while sending the usb command.
86 */
87 public static void flap(IBuddy iBuddy, long delay, int times) throws IBuddyException {
88 for (int i = 0; i < times; i++) {
89 iBuddy.sendWings(IBuddy.Wings.DOWN);
90 sleep(delay);
91 iBuddy.sendWings(IBuddy.Wings.UP);
92 sleep(delay);
93 }
94 iBuddy.sendWings(IBuddy.Wings.AT_EASE);
95 }
96 }