diff --git a/README.md b/README.md index 834a00f..4963947 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ PN532-1.6 SPI Channel 0, CS Pin 8: UID '' received. * Major refactoring to clean up the code and make it much easier to understand and use * All connection methods are working, there was a bug in the Serial receive method in the raspi-pn532 library * Added Pn532SamThread to make usage simple -* Added Main/MainListener to provide example implementation +* Added Example to provide example implementation * Added Pn532Utility for logging and exception handling * Added Pn532ContextHelper @@ -35,7 +35,7 @@ PN532-1.6 SPI Channel 0, CS Pin 8: UID '' received. ## Example Implementation Releases include a runnable jar which will run a Pn532SamThread for each connection type with default parameters. ``` -sudo java -jar raspi-pn532-v2-1.0.1.jar +sudo java -jar raspi-pn532-v2-1.0.2.jar ``` ## Library Usage diff --git a/pom.xml b/pom.xml index e013b58..d7385ce 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 mk.hsilomedus raspi-pn532-v2 - 1.0.1 + 1.0.2 src/main/java diff --git a/src/main/java/mk/hsilomedus/pn532/Example.java b/src/main/java/mk/hsilomedus/pn532/Example.java new file mode 100644 index 0000000..933646a --- /dev/null +++ b/src/main/java/mk/hsilomedus/pn532/Example.java @@ -0,0 +1,64 @@ +package mk.hsilomedus.pn532; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import mk.hsilomedus.pn532.Pn532SamThread.Pn532SamThreadListener; + +public final class Example { + public static final void main(String[] args) throws IOException { + new ExampleListener().run(); + } + + private static class ExampleListener implements Pn532SamThreadListener { + + @SuppressWarnings("rawtypes") + private List samThreads = new ArrayList<>(); + + public void run() throws IOException { + Pn532ContextHelper.initialize(); + + samThreads.add(new Pn532SamThread<>(this, new Pn532I2c())); + samThreads.add(new Pn532SamThread<>(this, new Pn532Serial())); + samThreads.add(new Pn532SamThread<>(this, new Pn532Spi())); + + for (var samThread : samThreads) { + samThread.start(); + } + + System.out.println("Press Enter to exit..."); + System.in.read(); + + for (var samThread : samThreads) { + closeThread(samThread); + } + + Pn532ContextHelper.shutdown(); + } + + @Override + public void receiveMessage(String message) { + System.out.println(message); + } + + @Override + public void uidReceived(String displayName, byte[] uid) { + System.out.println(displayName + ": UID '" + Pn532SamThreadListener.getUidString(uid) + "' received."); + } + + @SuppressWarnings("rawtypes") + private void closeThread(Pn532SamThread thread) { + if (thread != null && thread.isAlive()) { + thread.close(); + + try { + thread.join(); + } catch (InterruptedException e) { + System.out.println("Error closing thread: " + e.getMessage()); + Thread.currentThread().interrupt(); + } + } + } + } +} \ No newline at end of file diff --git a/src/main/java/mk/hsilomedus/pn532/Main.java b/src/main/java/mk/hsilomedus/pn532/Main.java deleted file mode 100644 index b026e3d..0000000 --- a/src/main/java/mk/hsilomedus/pn532/Main.java +++ /dev/null @@ -1,9 +0,0 @@ -package mk.hsilomedus.pn532; - -import java.io.IOException; - -public final class Main { - public static final void main(String[] args) throws IOException { - new MainListener().run(); - } -} \ No newline at end of file diff --git a/src/main/java/mk/hsilomedus/pn532/MainListener.java b/src/main/java/mk/hsilomedus/pn532/MainListener.java deleted file mode 100644 index 5326ff7..0000000 --- a/src/main/java/mk/hsilomedus/pn532/MainListener.java +++ /dev/null @@ -1,58 +0,0 @@ -package mk.hsilomedus.pn532; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import mk.hsilomedus.pn532.Pn532SamThread.Pn532SamThreadListener; - -class MainListener implements Pn532SamThreadListener { - - @SuppressWarnings("rawtypes") - private List samThreads = new ArrayList<>(); - - public void run() throws IOException { - Pn532ContextHelper.initialize(); - - samThreads.add(new Pn532SamThread<>(this, new Pn532I2c())); - samThreads.add(new Pn532SamThread<>(this, new Pn532Serial())); - samThreads.add(new Pn532SamThread<>(this, new Pn532Spi())); - - for (var samThread : samThreads) { - samThread.start(); - } - - System.out.println("Press Enter to exit..."); - System.in.read(); - - for (var samThread : samThreads) { - closeThread(samThread); - } - - Pn532ContextHelper.shutdown(); - } - - @Override - public void receiveMessage(String message) { - System.out.println(message); - } - - @Override - public void uidReceived(String displayName, byte[] uid) { - System.out.println(displayName + ": UID '" + Pn532SamThreadListener.getUidString(uid) + "' received."); - } - - @SuppressWarnings("rawtypes") - private void closeThread(Pn532SamThread thread) { - if (thread != null && thread.isAlive()) { - thread.close(); - - try { - thread.join(); - } catch (InterruptedException e) { - System.out.println("Error closing thread: " + e.getMessage()); - Thread.currentThread().interrupt(); - } - } - } -} \ No newline at end of file