|
| 1 | +package io.flic.flic2libandroid; |
| 2 | + |
| 3 | +import java.util.Objects; |
| 4 | + |
| 5 | +/** |
| 6 | + * Flic 2 button event. |
| 7 | + * |
| 8 | + * <p>Emitted from {@link Flic2ButtonListener#onButtonEvent(Flic2Button, Flic2ButtonEvent)}.</p> |
| 9 | + */ |
| 10 | +public class Flic2ButtonEvent { |
| 11 | + /** |
| 12 | + * Up or down. |
| 13 | + * |
| 14 | + * <p>Triggered on every button down or release.</p> |
| 15 | + */ |
| 16 | + public static final int EVENT_CLASS_UP_OR_DOWN = 0; |
| 17 | + |
| 18 | + /** |
| 19 | + * Click or hold. |
| 20 | + * |
| 21 | + * <p>Used if you want to distinguish between click and hold.</p> |
| 22 | + */ |
| 23 | + public static final int EVENT_CLASS_CLICK_OR_HOLD = 1; |
| 24 | + |
| 25 | + /** |
| 26 | + * Single or double click. |
| 27 | + * |
| 28 | + * <p>Used if you want to distinguish between a single click and a double click.</p> |
| 29 | + */ |
| 30 | + public static final int EVENT_CLASS_SINGLE_OR_DOUBLE_CLICK = 2; |
| 31 | + |
| 32 | + /** |
| 33 | + * Single or double click or hold. |
| 34 | + * |
| 35 | + * <p>Used if you want to distinguish between a single click, a double click and a hold.</p> |
| 36 | + */ |
| 37 | + public static final int EVENT_CLASS_SINGLE_OR_DOUBLE_CLICK_OR_HOLD = 3; |
| 38 | + |
| 39 | + /** |
| 40 | + * The button was pressed. |
| 41 | + */ |
| 42 | + public static final int EVENT_TYPE_UP = 0; |
| 43 | + |
| 44 | + /** |
| 45 | + * The button was released. |
| 46 | + */ |
| 47 | + public static final int EVENT_TYPE_DOWN = 1; |
| 48 | + |
| 49 | + /** |
| 50 | + * The button was clicked, and was held for at most 1 seconds between press and release. |
| 51 | + */ |
| 52 | + public static final int EVENT_TYPE_CLICK = 2; |
| 53 | + |
| 54 | + /** |
| 55 | + * The button was clicked once. |
| 56 | + */ |
| 57 | + public static final int EVENT_TYPE_SINGLE_CLICK = 3; |
| 58 | + |
| 59 | + /** |
| 60 | + * The button was clicked twice. The time between the first and second press must be at most 0.5 seconds. |
| 61 | + */ |
| 62 | + public static final int EVENT_TYPE_DOUBLE_CLICK = 4; |
| 63 | + |
| 64 | + /** |
| 65 | + * The button was held for at least 1 second. |
| 66 | + */ |
| 67 | + public static final int EVENT_TYPE_HOLD = 5; |
| 68 | + |
| 69 | + /** |
| 70 | + * Button number for the only button on Flic 2 or the big button on Flic Duo. |
| 71 | + */ |
| 72 | + public static final int BUTTON_NUMBER_BIG = 0; |
| 73 | + |
| 74 | + /** |
| 75 | + * Button number for the small button on Flic Duo. |
| 76 | + */ |
| 77 | + public static final int BUTTON_NUMBER_SMALL = 1; |
| 78 | + |
| 79 | + /** |
| 80 | + * The Flic did not detect any gesture being performed. |
| 81 | + */ |
| 82 | + public static final int GESTURE_NO_GESTURE = -1; |
| 83 | + |
| 84 | + /** |
| 85 | + * The Flic did detect that a gesture was being performed, but it was unrecognized. |
| 86 | + */ |
| 87 | + public static final int GESTURE_UNRECOGNIZED_GESTURE = 0; |
| 88 | + |
| 89 | + /** |
| 90 | + * The Flic did detect a swipe left gesture. |
| 91 | + */ |
| 92 | + public static final int GESTURE_LEFT = 1; |
| 93 | + |
| 94 | + /** |
| 95 | + * The Flic did detect a swipe right gesture. |
| 96 | + */ |
| 97 | + public static final int GESTURE_RIGHT = 2; |
| 98 | + |
| 99 | + /** |
| 100 | + * The Flic did detect a swipe up gesture. |
| 101 | + */ |
| 102 | + public static final int GESTURE_UP = 3; |
| 103 | + |
| 104 | + /** |
| 105 | + * The Flic detect a swipe down gesture. |
| 106 | + */ |
| 107 | + public static final int GESTURE_DOWN = 4; |
| 108 | + |
| 109 | + private final byte eventClass; |
| 110 | + private final byte eventType; |
| 111 | + private final int eventCount; |
| 112 | + private final byte buttonNumber; |
| 113 | + private final boolean wasQueued; |
| 114 | + private final boolean lastQueued; |
| 115 | + private final long timestamp; |
| 116 | + private final byte x, y, z; |
| 117 | + private final byte gesture; |
| 118 | + private final boolean downAtLeastHalfASecond; |
| 119 | + |
| 120 | + Flic2ButtonEvent(int eventClass, int eventType, int eventCount, byte buttonNumber, boolean wasQueued, boolean lastQueued, long timestamp, byte x, byte y, byte z, byte gesture, boolean downAtLeastHalfASecond) { |
| 121 | + this.eventClass = (byte) eventClass; |
| 122 | + this.eventType = (byte) eventType; |
| 123 | + this.eventCount = eventCount; |
| 124 | + this.buttonNumber = buttonNumber; |
| 125 | + this.wasQueued = wasQueued; |
| 126 | + this.lastQueued = lastQueued; |
| 127 | + this.timestamp = timestamp; |
| 128 | + this.x = x; |
| 129 | + this.y = y; |
| 130 | + this.z = z; |
| 131 | + this.gesture = gesture; |
| 132 | + this.downAtLeastHalfASecond = downAtLeastHalfASecond; |
| 133 | + } |
| 134 | + |
| 135 | + Flic2ButtonEvent(int eventClass, int eventType, int eventCount, boolean wasQueued, boolean lastQueued, long timestamp) { |
| 136 | + this.eventClass = (byte) eventClass; |
| 137 | + this.eventType = (byte) eventType; |
| 138 | + this.eventCount = eventCount; |
| 139 | + this.buttonNumber = 0; |
| 140 | + this.wasQueued = wasQueued; |
| 141 | + this.lastQueued = lastQueued; |
| 142 | + this.timestamp = timestamp; |
| 143 | + this.x = 0; |
| 144 | + this.y = 0; |
| 145 | + this.z = 0; |
| 146 | + this.gesture = -1; |
| 147 | + this.downAtLeastHalfASecond = false; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Button event class. |
| 152 | + * |
| 153 | + * <p>Each time the button is interacted with, one or more events will be sent.</p> |
| 154 | + * |
| 155 | + * <p>Usually an application only needs to listen to one event class.</p> |
| 156 | + * |
| 157 | + * <p>Since distinguishing between single and double click needs some waiting time after the first |
| 158 | + * click to detect if a second press will occur or not, single click events will be delayed for the |
| 159 | + * last two event classes but not for the first two, it is important to pick the right event class |
| 160 | + * for the use case.</p> |
| 161 | + * |
| 162 | + * <p>For a particular event class, only the specified button event types may be emitted.</p> |
| 163 | + * |
| 164 | + * @return The possible options are: {@link #EVENT_CLASS_UP_OR_DOWN}, {@link #EVENT_CLASS_CLICK_OR_HOLD}, |
| 165 | + * {@link #EVENT_CLASS_SINGLE_OR_DOUBLE_CLICK} and {@link #EVENT_CLASS_SINGLE_OR_DOUBLE_CLICK_OR_HOLD}. |
| 166 | + */ |
| 167 | + public int getEventClass() { |
| 168 | + return eventClass; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Button event type within the specified event class. |
| 173 | + * |
| 174 | + * <p>For {@link #EVENT_CLASS_UP_OR_DOWN}, event type will be either {@link #EVENT_TYPE_UP} or {@link #EVENT_TYPE_DOWN}.</p> |
| 175 | + * |
| 176 | + * <p>For {@link #EVENT_CLASS_CLICK_OR_HOLD}, event type will be either {@link #EVENT_TYPE_CLICK} or {@link #EVENT_TYPE_HOLD}.</p> |
| 177 | + * |
| 178 | + * <p>For {@link #EVENT_CLASS_SINGLE_OR_DOUBLE_CLICK}, event type will be either {@link #EVENT_TYPE_SINGLE_CLICK} or {@link #EVENT_TYPE_DOUBLE_CLICK}.</p> |
| 179 | + * |
| 180 | + * <p>For {@link #EVENT_CLASS_SINGLE_OR_DOUBLE_CLICK_OR_HOLD}, event type will be either {@link #EVENT_TYPE_SINGLE_CLICK}, {@link #EVENT_TYPE_DOUBLE_CLICK} or {@link #EVENT_TYPE_HOLD}.</p> |
| 181 | + * |
| 182 | + * @return The event type. |
| 183 | + */ |
| 184 | + public int getEventType() { |
| 185 | + return eventType; |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * An event counter that starts at zero when the Flic boots and always increases. |
| 190 | + * |
| 191 | + * <p>This value divided by four indicates roughly how many times the button has been pressed and released.</p> |
| 192 | + * |
| 193 | + * <p>More specific, event_count mod 4 should be 1: down, 2: hold, 3: up, 0: single click timeout.</p> |
| 194 | + * |
| 195 | + * <p>Note: Flic Duo has one individual event counter per button.</p> |
| 196 | + */ |
| 197 | + public int getEventCount() { |
| 198 | + return eventCount; |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Physical button number that was pressed. |
| 203 | + * |
| 204 | + * @return For Flic 2, always 0. For Flic Duo, the big button has number 0 and the small button has number 1. See {@link #BUTTON_NUMBER_BIG} and {@link #BUTTON_NUMBER_SMALL}. |
| 205 | + */ |
| 206 | + public int getButtonNumber() { |
| 207 | + return buttonNumber; |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + * Indicates if this button event was queued, i.e. it was pressed some time ago before connection setup completed. |
| 212 | + * |
| 213 | + * @return true or false. |
| 214 | + */ |
| 215 | + public boolean getWasQueued() { |
| 216 | + return wasQueued; |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Indicates that this button event was the last queued. |
| 221 | + * |
| 222 | + * @return true or false. |
| 223 | + */ |
| 224 | + public boolean isLastQueued() { |
| 225 | + return lastQueued; |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * Timestamp of event. |
| 230 | + * |
| 231 | + * <p>The difference between {@link Flic2Button#getReadyTimestamp()} and this value can be used to know the age of the event for a queued button event.</p> |
| 232 | + * |
| 233 | + * @return when the event occurred in milliseconds, relative to button boot. |
| 234 | + */ |
| 235 | + public long getTimestamp() { |
| 236 | + return timestamp; |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * The X axis accelerometer value. |
| 241 | + * |
| 242 | + * <p>The returned value is valid only for Flic Duo.</p> |
| 243 | + * |
| 244 | + * @return X. |
| 245 | + */ |
| 246 | + public float getX() { |
| 247 | + return (float)(x / 64.036875); |
| 248 | + } |
| 249 | + |
| 250 | + /** |
| 251 | + * The Y axis accelerometer value. |
| 252 | + * |
| 253 | + * <p>The returned value is valid only for Flic Duo.</p> |
| 254 | + * |
| 255 | + * @return Y. |
| 256 | + */ |
| 257 | + public float getY() { |
| 258 | + return (float)(y / 64.036875); |
| 259 | + } |
| 260 | + |
| 261 | + /** |
| 262 | + * The Z axis accelerometer value. |
| 263 | + * |
| 264 | + * <p>The returned value is valid only for Flic Duo.</p> |
| 265 | + * |
| 266 | + * @return Z. |
| 267 | + */ |
| 268 | + public float getZ() { |
| 269 | + return (float)(z / 64.036875); |
| 270 | + } |
| 271 | + |
| 272 | + /** |
| 273 | + * Gesture recognition. |
| 274 | + * |
| 275 | + * <p>If multiple events emitted for the same button release, it will contain the same gesture.</p> |
| 276 | + * |
| 277 | + * <p>The returned value is valid only for Flic Duo.</p> |
| 278 | + * |
| 279 | + * @return One of {@link #GESTURE_NO_GESTURE}, {@link #GESTURE_UNRECOGNIZED_GESTURE}, {@link #GESTURE_LEFT}, {@link #GESTURE_RIGHT}, {@link #GESTURE_UP} or {@link #GESTURE_DOWN}. |
| 280 | + */ |
| 281 | + public int getGesture() { |
| 282 | + return gesture; |
| 283 | + } |
| 284 | + |
| 285 | + /** |
| 286 | + * For events that are emitted as a response to a button release or hold, whether the button was pressed down at least half a second. |
| 287 | + * |
| 288 | + * <p>The returned value is valid only for Flic Duo.</p> |
| 289 | + * |
| 290 | + * @return true or false. |
| 291 | + */ |
| 292 | + public boolean getDownAtLeastHalfASecond() { |
| 293 | + return downAtLeastHalfASecond; |
| 294 | + } |
| 295 | + |
| 296 | + @Override |
| 297 | + public boolean equals(Object o) { |
| 298 | + if (o == null || getClass() != o.getClass()) return false; |
| 299 | + Flic2ButtonEvent that = (Flic2ButtonEvent) o; |
| 300 | + return eventClass == that.eventClass && eventType == that.eventType && eventCount == that.eventCount && buttonNumber == that.buttonNumber && wasQueued == that.wasQueued && timestamp == that.timestamp && x == that.x && y == that.y && z == that.z && gesture == that.gesture && downAtLeastHalfASecond == that.downAtLeastHalfASecond; |
| 301 | + } |
| 302 | + |
| 303 | + @Override |
| 304 | + public int hashCode() { |
| 305 | + return Objects.hash(eventClass, eventType, eventCount, buttonNumber, wasQueued, timestamp, x, y, z, gesture, downAtLeastHalfASecond); |
| 306 | + } |
| 307 | +} |
0 commit comments