Skip to content

Commit cfce175

Browse files
committed
Preparations for 2.3.5 release.
1 parent 0e68de5 commit cfce175

File tree

8 files changed

+81
-87
lines changed

8 files changed

+81
-87
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Add the following dependency to `pom.xml` of your project:
2121
<dependency>
2222
<groupId>com.github.kurbatov</groupId>
2323
<artifactId>firmata4j</artifactId>
24-
<version>2.3.4.1</version>
24+
<version>2.3.5</version>
2525
</dependency>
2626
```
2727

@@ -33,13 +33,13 @@ IODevice device = new FirmataDevice("/dev/ttyUSB0"); // construct the Firmata de
3333
// and/or device.getPin(n).addEventListener(...);
3434
device.start(); // initiate communication to the device
3535
device.ensureInitializationIsDone(); // wait for initialization is done
36-
// do actual work here
36+
// sending commands to the board
3737
device.stop(); // stop communication to the device
3838
```
3939

40-
The "actual work" consists in sending commands to the board. Registered
41-
listeners process events of the device asynchronously. You can add and
42-
remove listeners along the way.
40+
Sending commands to the board may cause the device to emit different events.
41+
Registered listeners process events of the device asynchronously. You can add
42+
and remove listeners along the way.
4343

4444
You can subscribe to events of the device or its pin.
4545

pom.xml

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.github.kurbatov</groupId>
55
<artifactId>firmata4j</artifactId>
6-
<version>2.5.0-SNAPSHOT</version>
6+
<version>2.3.5-SNAPSHOT</version>
77
<packaging>jar</packaging>
88
<name>firmata4j</name>
99
<description>firmata4j is a client library of Firmata.</description>
@@ -46,7 +46,7 @@
4646
<dependency>
4747
<groupId>org.slf4j</groupId>
4848
<artifactId>slf4j-api</artifactId>
49-
<version>1.7.7</version>
49+
<version>1.7.25</version>
5050
</dependency>
5151

5252
<dependency>
@@ -59,7 +59,7 @@
5959
<groupId>org.slf4j</groupId>
6060
<artifactId>slf4j-simple</artifactId>
6161
<scope>test</scope>
62-
<version>1.7.7</version>
62+
<version>1.7.25</version>
6363
</dependency>
6464

6565
</dependencies>
@@ -82,7 +82,7 @@
8282
<plugin>
8383
<groupId>org.apache.maven.plugins</groupId>
8484
<artifactId>maven-compiler-plugin</artifactId>
85-
<version>3.1</version>
85+
<version>3.6.1</version>
8686
<configuration>
8787
<source>1.7</source>
8888
<target>1.7</target>
@@ -102,7 +102,7 @@
102102
<plugin>
103103
<groupId>org.apache.maven.plugins</groupId>
104104
<artifactId>maven-release-plugin</artifactId>
105-
<version>2.5</version>
105+
<version>2.5.3</version>
106106
<configuration>
107107
<autoVersionSubmodules>true</autoVersionSubmodules>
108108
<useReleaseProfile>false</useReleaseProfile>
@@ -121,7 +121,7 @@
121121
<plugin>
122122
<groupId>org.apache.maven.plugins</groupId>
123123
<artifactId>maven-source-plugin</artifactId>
124-
<version>2.3</version>
124+
<version>3.0.1</version>
125125
<executions>
126126
<execution>
127127
<id>attach-sources</id>
@@ -134,7 +134,7 @@
134134
<plugin>
135135
<groupId>org.apache.maven.plugins</groupId>
136136
<artifactId>maven-javadoc-plugin</artifactId>
137-
<version>2.9.1</version>
137+
<version>2.10.4</version>
138138
<executions>
139139
<execution>
140140
<id>attach-javadocs</id>
@@ -150,7 +150,7 @@
150150
<plugin>
151151
<groupId>org.apache.maven.plugins</groupId>
152152
<artifactId>maven-gpg-plugin</artifactId>
153-
<version>1.5</version>
153+
<version>1.6</version>
154154
<executions>
155155
<execution>
156156
<id>sign-artifacts</id>

src/main/java/org/firmata4j/I2CEvent.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ public I2CDevice getDevice() {
6565
return device;
6666
}
6767

68-
@Override
69-
public String toString() {
70-
return "I2CEvent [device=" + device + ", register=0x" + Integer.toHexString(register) + ", data=" + Arrays.toString(data) + "]";
71-
}
68+
@Override
69+
public String toString() {
70+
return String.format("I2CEvent [device=%s, register=0x%02X, data=%s]", device, register, Arrays.toString(data));
71+
}
7272
}

src/main/java/org/firmata4j/I2CExample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static void main(String[] args) throws IOException, InterruptedException
6565
try { // set look and feel
6666
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
6767
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
68-
Logger.getLogger(Example.class.getName()).log(Level.SEVERE, "Cannot load system look and feel.", ex);
68+
Logger.getLogger(I2CExample.class.getName()).log(Level.SEVERE, "Cannot load system look and feel.", ex);
6969
}
7070
// requesting a user to define the port name
7171
String port = requestPort();

src/main/java/org/firmata4j/IOEvent.java

+6-11
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,12 @@ public class IOEvent {
4242
* @param device the device that originated the event
4343
*/
4444
public IOEvent(IODevice device) {
45-
this.device = device;
46-
this.pin = null;
47-
this.value = 0;
48-
this.timestamp = System.currentTimeMillis();
45+
this(device, System.currentTimeMillis());
4946
}
5047

5148
/**
5249
* Constructs the event is relevant to {@link IODevice} as a whole.
53-
* <br/>
50+
*
5451
* This constructor allows setting the timestamp of event.
5552
*
5653
* @param device the device that originated the event
@@ -69,15 +66,12 @@ public IOEvent(IODevice device, long timestamp) {
6966
* @param pin the pin that originated the event
7067
*/
7168
public IOEvent(Pin pin) {
72-
this.device = pin.getDevice();
73-
this.pin = pin;
74-
this.value = pin.getValue();
75-
this.timestamp = System.currentTimeMillis();
69+
this(pin, System.currentTimeMillis());
7670
}
7771

7872
/**
7973
* Constructs the event is relevant to a particular {@link Pin}.
80-
* <br/>
74+
*
8175
* This constructor allows setting the timestamp of event.
8276
*
8377
* @param pin the pin that originated the event
@@ -112,7 +106,7 @@ public Pin getPin() {
112106

113107
/**
114108
* Returns the value the pin received.
115-
* <br/>
109+
*
116110
* If the event is not about pin, always returns 0.
117111
*
118112
* @return the value the pin received
@@ -123,6 +117,7 @@ public long getValue() {
123117

124118
/**
125119
* Returns the timestamp of the event.
120+
*
126121
* @return timestamp of the event
127122
*/
128123
public long getTimestamp() {

src/main/java/org/firmata4j/firmata/FirmataDevice.java

+20-16
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,36 @@
2323
*/
2424
package org.firmata4j.firmata;
2525

26-
import static org.firmata4j.firmata.parser.FirmataToken.*;
27-
2826
import java.io.IOException;
2927
import java.text.MessageFormat;
30-
import java.util.*;
28+
import java.util.ArrayList;
29+
import java.util.Collections;
30+
import java.util.HashMap;
31+
import java.util.HashSet;
32+
import java.util.LinkedHashSet;
33+
import java.util.List;
34+
import java.util.Map;
35+
import java.util.Set;
3136
import java.util.concurrent.ArrayBlockingQueue;
3237
import java.util.concurrent.BlockingQueue;
3338
import java.util.concurrent.atomic.AtomicBoolean;
3439
import java.util.concurrent.atomic.AtomicInteger;
35-
36-
import org.firmata4j.*;
40+
import jssc.SerialPort;
41+
import jssc.SerialPortEvent;
42+
import jssc.SerialPortEventListener;
43+
import jssc.SerialPortException;
44+
import org.firmata4j.I2CDevice;
45+
import org.firmata4j.IODevice;
46+
import org.firmata4j.IODeviceEventListener;
47+
import org.firmata4j.IOEvent;
48+
import org.firmata4j.Pin;
3749
import org.firmata4j.firmata.parser.FirmataToken;
3850
import org.firmata4j.firmata.parser.WaitingForMessageState;
3951
import org.firmata4j.fsm.Event;
4052
import org.firmata4j.fsm.FiniteStateMachine;
4153
import org.slf4j.Logger;
4254
import org.slf4j.LoggerFactory;
43-
44-
import jssc.*;
55+
import static org.firmata4j.firmata.parser.FirmataToken.*;
4556

4657
/**
4758
* Implements {@link IODevice} that is using Firmata protocol.
@@ -96,11 +107,6 @@ public void start() throws IOException {
96107
response. If this REPORT_FIRMWARE request isn't sent,
97108
these boards will not automatically send this info.
98109
99-
Arduino boards that reboot on DTR will act like a board
100-
that does not reboot, if DTR is not raised when the
101-
port opens. This program attempts to avoid raising
102-
DTR on windows. (is this possible on Linux and Mac OS-X?)
103-
104110
Either way, when we hear the REPORT_FIRMWARE reply, we
105111
know the board is alive and ready to communicate.
106112
*/
@@ -515,11 +521,9 @@ public void onEvent(Event event) {
515521
@Override
516522
public void run() {
517523
while (!Thread.interrupted()) {
518-
try
519-
{
524+
try {
520525
process(queue.take());
521-
if (!isReady())
522-
{
526+
if (!isReady()) {
523527
Thread.sleep(DELAY);
524528
}
525529
} catch (InterruptedException ex) {

src/main/java/org/firmata4j/firmata/FirmataI2CDevice.java

+29-28
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222
* THE SOFTWARE.
2323
*/
24-
2524
package org.firmata4j.firmata;
2625

2726
import java.io.IOException;
28-
import java.util.*;
27+
import java.util.Map;
28+
import java.util.Set;
29+
import java.util.concurrent.ConcurrentHashMap;
30+
import java.util.concurrent.ConcurrentSkipListSet;
2931
import java.util.concurrent.atomic.AtomicBoolean;
30-
3132
import org.firmata4j.I2CDevice;
3233
import org.firmata4j.I2CEvent;
3334
import org.firmata4j.I2CListener;
@@ -39,17 +40,18 @@
3940
* @author Oleg Kurbatov &lt;[email protected]&gt;
4041
*/
4142
public class FirmataI2CDevice implements I2CDevice {
42-
public static final int REGISTER_NOT_SET = -1;
43+
44+
public static final int REGISTER_NOT_SET = -1;
4345

4446
private final FirmataDevice masterDevice;
4547

4648
private final byte address;
4749

4850
private final AtomicBoolean receivingUpdates = new AtomicBoolean(false);
4951

50-
private final Map<Integer, I2CListener> callbacks = new HashMap<>();
52+
private final Map<Integer, I2CListener> callbacks = new ConcurrentHashMap<>();
5153

52-
private final Set<I2CListener> subscribers = Collections.synchronizedSet(new HashSet<I2CListener>());
54+
private final Set<I2CListener> subscribers = new ConcurrentSkipListSet<>();
5355

5456
FirmataI2CDevice(FirmataDevice masterDevice, byte address) {
5557
this.masterDevice = masterDevice;
@@ -73,12 +75,12 @@ public void tell(byte... data) throws IOException {
7375

7476
@Override
7577
public void ask(byte responseLength, I2CListener listener) throws IOException {
76-
ask(-1, responseLength, listener);
78+
ask(REGISTER_NOT_SET, responseLength, listener);
7779
}
78-
80+
7981
@Override
80-
public void ask(int register, byte responseLength, I2CListener listener) throws IOException {
81-
callbacks.put(Integer.valueOf(register), listener);
82+
public void ask(int register, byte responseLength, I2CListener listener) throws IOException {
83+
callbacks.put(register, listener);
8284
masterDevice.sendMessage(FirmataMessageFactory.i2cReadRequest(address, register, responseLength, false));
8385
}
8486

@@ -106,29 +108,28 @@ public void stopReceivingUpdates() throws IOException {
106108
}
107109
}
108110

109-
/**
110-
* {@link FirmataDevice} calls this method when receives a message from I2C
111-
* device.
112-
*
113-
* @param register The device register read from.
114-
* @param message actual data from I2C device
115-
*/
111+
/**
112+
* {@link FirmataDevice} calls this method when receives a message from I2C
113+
* device.
114+
*
115+
* @param register The device register read from.
116+
* @param message actual data from I2C device
117+
*/
116118
void onReceive(int register, byte[] message) {
117119
I2CEvent evt = new I2CEvent(this, register, message);
118-
I2CListener cb_listener = callbacks.get(Integer.valueOf(register));
119-
if (cb_listener != null) {
120-
cb_listener.onReceive(evt);
121-
} else {
122-
Set<I2CListener> notificationSet = new HashSet<>(subscribers);
123-
for (I2CListener listener : notificationSet) {
124-
listener.onReceive(evt);
120+
I2CListener listener = callbacks.remove(register);
121+
if (listener == null) {
122+
for (I2CListener subscriber : subscribers) {
123+
subscriber.onReceive(evt);
125124
}
125+
} else {
126+
listener.onReceive(evt);
126127
}
127128
}
128129

129-
@Override
130-
public String toString() {
131-
return "FirmataI2CDevice [address=0x" + Integer.toHexString(address) + "]";
132-
}
130+
@Override
131+
public String toString() {
132+
return String.format("FirmataI2CDevice [address=0x%02X]", address);
133+
}
133134

134135
}

0 commit comments

Comments
 (0)