Skip to content

Commit ed6e381

Browse files
committed
Flic Duo support. Version 2.0.0.
1 parent 4036101 commit ed6e381

10 files changed

Lines changed: 1008 additions & 80 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
# Flic2 lib for Android
66

7-
The official library for Flic2 on Android.
7+
The official library for Flic 2 and Flic Duo buttons on Android.
88

99
The library is hosted at JitPack and can be included in your Android app by entering the following in your `build.gradle` file:
1010

1111
dependencies {
12-
implementation 'com.github.50ButtonsEach:flic2lib-android:1.+'
12+
implementation 'com.github.50ButtonsEach:flic2lib-android:2.+'
1313
}
1414

1515
If you have not already done so, include the JitPack repository in your root `build.gradle` file:
@@ -172,7 +172,7 @@ button.addListener(new Flic2ButtonListener() {
172172
});
173173
```
174174

175-
There are other kinds of listeners if you instead want to distinguish click, double click and hold. See the API documentation for more information.
175+
There are other kinds of listeners if you instead want to distinguish click, double click and hold. If you want to support Flic Duo, then implement the `onButtonEvent` callback ONLY instead; all button events will be delivered to this callback instead of the individual ones. See the API documentation for more information.
176176

177177
If the button was pressed while it was disconnected, the button events will be sent when it later connects (the latest that could fit on the internal memory). Sometimes it's not desired to get old events. In that case the formula `wasQueued && button.getReadyTimestamp() - timestamp > 15000` can be used in the event callback to detect if the event was older than 15 seconds.
178178

flic2lib-android/build.gradle

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ plugins {
44
}
55

66
group = 'io.flic'
7-
version = '1.3.2'
7+
version = '2.0.0'
88

99
android {
1010
compileSdkVersion 36
1111

1212
defaultConfig {
1313
minSdkVersion 19
1414
targetSdkVersion 36
15-
versionName '1.3.2'
16-
buildConfigField("String", "VERSION_NAME", "\"1.3.2\"")
15+
versionName '2.0.0'
16+
buildConfigField("String", "VERSION_NAME", "\"2.0.0\"")
1717

1818
consumerProguardFiles 'consumer-rules.pro'
1919
}
@@ -29,17 +29,32 @@ android {
2929
buildConfig = true
3030
}
3131

32+
publishing {
33+
singleVariant('release') {
34+
withSourcesJar()
35+
withJavadocJar()
36+
}
37+
}
38+
3239
namespace "io.flic.flic2libandroid"
3340
}
3441

3542
dependencies {
3643
implementation fileTree(dir: 'libs', include: ['*.jar'])
3744
}
3845

39-
project.ext {
40-
mavSiteUrl = "https://github.com/50ButtonsEach/flic2lib-android"
41-
mavGitUrl = mavSiteUrl + '.git'
42-
mavProjectName = 'flic2lib-android'
46+
afterEvaluate {
47+
publishing {
48+
publications {
49+
release(MavenPublication) {
50+
groupId = "io.flic"
51+
artifactId = "flic2lib-android"
52+
version = "2.0.0"
53+
54+
afterEvaluate {
55+
from components.release
56+
}
57+
}
58+
}
59+
}
4360
}
44-
45-
apply from: 'https://raw.githubusercontent.com/sky-uk/gradle-maven-plugin/1.0.4/gradle-mavenizer.gradle'

flic2lib-android/src/main/java/io/flic/flic2libandroid/Flic2Button.java

Lines changed: 389 additions & 53 deletions
Large diffs are not rendered by default.
Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
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

Comments
 (0)