Skip to content

Commit

Permalink
Clean up example implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottyDoesKnow committed Oct 30, 2023
1 parent e451df0 commit b531e12
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 70 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ PN532-1.6 SPI Channel 0, CS Pin 8: UID '<redacted>' 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

Expand All @@ -35,7 +35,7 @@ PN532-1.6 SPI Channel 0, CS Pin 8: UID '<redacted>' 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
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>mk.hsilomedus</groupId>
<artifactId>raspi-pn532-v2</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>

<build>
<sourceDirectory>src/main/java</sourceDirectory>
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/mk/hsilomedus/pn532/Example.java
Original file line number Diff line number Diff line change
@@ -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<Pn532SamThread> 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();
}
}
}
}
}
9 changes: 0 additions & 9 deletions src/main/java/mk/hsilomedus/pn532/Main.java

This file was deleted.

58 changes: 0 additions & 58 deletions src/main/java/mk/hsilomedus/pn532/MainListener.java

This file was deleted.

0 comments on commit b531e12

Please sign in to comment.