Skip to content

Commit

Permalink
introducing GPIOListener class
Browse files Browse the repository at this point in the history
  • Loading branch information
sovcik committed May 7, 2016
1 parent 1dc0442 commit 301a4f1
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 47 deletions.
20 changes: 20 additions & 0 deletions JPigpio/src/jpigpio/GPIOListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package jpigpio;

/**
* Simple interface for implementing GPIO callbacks
*/
public abstract class GPIOListener {

public int bit; // bit-map representing respective GPIOs
public int edge;
public int gpio;

public GPIOListener(int gpio, int edge){
this.gpio = gpio;
this.bit = 1 << this.gpio;
this.edge = edge;
}

abstract public void processNotification(int gpio, int level, long tick);

}
16 changes: 8 additions & 8 deletions JPigpio/src/jpigpio/JPigpio.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public interface JPigpio {

public long getCurrentTick() throws PigpioException;

// ################ WAVES
// ################ WAVEFORMS
/**
* This function clears all waveforms and any data added by calls to the wave_add_* functions.
*
Expand Down Expand Up @@ -374,7 +374,7 @@ public interface JPigpio {
* Returns the new total number of pulses in the current waveform.
* @throws PigpioException
*/
public int waveAddSerial(int userGpio, int baud, byte[] data, int offset, int bbBits, int bbStop) throws PigpioException;
public int waveAddSerial(int gpio, int baud, byte[] data, int offset, int bbBits, int bbStop) throws PigpioException;

/**
* Starts a new empty waveform.
Expand Down Expand Up @@ -625,19 +625,19 @@ public interface JPigpio {
/**
* Add callback listener object which would receive notifications related to
* GPIO levels which listener has specified.
* @param cb
* NotificationListener object
* @param listener
* GPIOListener object
* @throws PigpioException
*/
public void addCallback(NotificationListener cb) throws PigpioException;
public void addCallback(GPIOListener listener) throws PigpioException;

/**
* Remove callback listener object from notification thread.
* @param cb
* NotificationListener object
* @param listener
* GPIOListener object
* @throws PigpioException
*/
public void removeCallback(NotificationListener cb) throws PigpioException;
public void removeCallback(GPIOListener listener) throws PigpioException;

public static final int PI_GPIO2 = 2;
public static final int PI_GPIO3 = 3;
Expand Down
12 changes: 2 additions & 10 deletions JPigpio/src/jpigpio/NotificationListener.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package jpigpio;

import static jpigpio.JPigpio.PI_RISING_EDGE;

/**
* NotificationListener is object, which after being registered with NotificationRouter (see class PigpioSocket)
* receives notifications coming from Pigpiod. Every relevant notification (NotificationRouter filters out those
* notifications irrelevant for specific NotificationListener object based on EDGE and GPIO pin which listener
* is registered for) triggers calling method processNotification.
*/
public class NotificationListener {

protected int bit = 0; // bit-map representing respective GPIOs
protected int edge = PI_RISING_EDGE;
protected int gpio = -1;
public class NotificationListener extends GPIOListener{

protected int count = 0;
protected boolean reset = false;
Expand All @@ -22,9 +16,7 @@ public class NotificationListener {


public NotificationListener(int userGpio, int edge){
this.gpio = userGpio;
this.edge = edge;
this.bit = 1 << userGpio;
super(userGpio, edge);
}

public void processNotification(int gpio, int level, long tick){
Expand Down
4 changes: 2 additions & 2 deletions JPigpio/src/jpigpio/Pigpio.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,12 @@ public long getCurrentTick() throws PigpioException{
public native long gpioxPulseAndWait(int outGpio, int inGpio, long waitDuration, long pulseHoldDuration, boolean pulseLow) throws PigpioException;

@Override
public void addCallback(NotificationListener cb) throws PigpioException {
public void addCallback(GPIOListener gpioListener) throws PigpioException {
throw new NotImplementedException();
}

@Override
public void removeCallback(NotificationListener cb) throws PigpioException{
public void removeCallback(GPIOListener cgpioListener) throws PigpioException{
throw new NotImplementedException();
}

Expand Down
46 changes: 23 additions & 23 deletions JPigpio/src/jpigpio/PigpioSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class PigpioSocket extends CommonPigpio {

SocketLock slCmd; // socket for sending commands to PIGPIO

NotificationRouter listener = null;
NotificationRouter router = null;

public final int PIGPIOD_MESSAGE_SIZE = 12;

Expand Down Expand Up @@ -146,7 +146,7 @@ class NotificationRouter implements Runnable{
boolean go = true;
Thread thread;

ArrayList<NotificationListener> notificationListeners = new ArrayList<>();
ArrayList<GPIOListener> gpioListeners = new ArrayList<>();
int monitor = 0;

/**
Expand Down Expand Up @@ -195,14 +195,14 @@ public void terminate() throws PigpioException {
}

/**
* Add notificationListener object to the list of objects to call when notification is received
* @param notificationListener NotificationListener object to be added to the list
* Add GPIOListener object to the list of objects to call when notification is received
* @param gpioListener GPIOListener object to be added to the list
* @throws PigpioException
*/
public void addListener(NotificationListener notificationListener) throws PigpioException{
public void addListener(GPIOListener gpioListener) throws PigpioException{
try {
notificationListeners.add(notificationListener);
monitor = monitor | notificationListener.bit;
gpioListeners.add(gpioListener);
monitor = monitor | gpioListener.bit;
// send command to start sending notifications for bit-map specified GPIOs
slPiCmd.sendCmd(CMD_NB, handle, monitor);
} catch (IOException e) {
Expand All @@ -211,17 +211,17 @@ public void addListener(NotificationListener notificationListener) throws Pigpio
}

/**
* Remove object from the list of notificationListener objects to be notified
* @param notificationListener NotificationListener object to be removed from the list.
* Remove object from the list of GPIOListener objects to be notified
* @param gpioListener GPIOListener object to be removed from the list.
* @throws PigpioException
*/
public void removeListener(NotificationListener notificationListener) throws PigpioException{
public void removeListener(GPIOListener gpioListener) throws PigpioException{
int newMonitor = 0;

if (notificationListeners.remove(notificationListener)){
if (gpioListeners.remove(gpioListener)){

// calculate new bit-map in case no other notificationListener monitors PIGPIO of notificationListener being removed
for (NotificationListener c: notificationListeners)
for (GPIOListener c: gpioListeners)
newMonitor |= c.bit;

// if new bit-map differs, let PIGPIO know
Expand Down Expand Up @@ -267,7 +267,7 @@ public void run(){
if (flags == 0) {
changed = level ^ lastLevel;
lastLevel = level;
for (NotificationListener cb : notificationListeners) {
for (GPIOListener cb : gpioListeners) {
// check if changed GPIO is the one listener is waiting for
if ((cb.bit & changed) != 0) {
// let's assume new gpio level is "low"
Expand All @@ -285,7 +285,7 @@ public void run(){
// is it a watchdog message?
if ((flags & PI_NTFY_FLAGS_WDOG) != 0) {
gpio = flags & PI_NTFY_FLAGS_WDOG;
for (NotificationListener cb : notificationListeners)
for (GPIOListener cb : gpioListeners)
if (cb.gpio == gpio)
cb.processNotification(cb.gpio, PI_TIMEOUT, tick);
}
Expand Down Expand Up @@ -336,9 +336,9 @@ public void gpioInitialize() throws PigpioException {
try {
if (slCmd == null)
slCmd = new SocketLock(host, port);
if (listener == null) {
listener = new NotificationRouter(slCmd, host, port);
listener.start();
if (router == null) {
router = new NotificationRouter(slCmd, host, port);
router.start();
}
} catch (Exception e) {
throw new PigpioException("gpioInitialize", e);
Expand All @@ -352,7 +352,7 @@ public void gpioInitialize() throws PigpioException {
public void gpioTerminate() throws PigpioException {
try {
// stop listener thread
listener.terminate();
router.terminate();
// stop command socket to pigpio
slCmd.terminate();
} catch (Exception e) {
Expand Down Expand Up @@ -921,17 +921,17 @@ public long gpioxPulseAndWait(int outGpio, int inGpio, long waitDuration, long p
*
* cb1.cancel() # To cancel callback cb1.
* ...
* @param cb
* @param gpioListener
* user supplied callback object.
*/
@Override
public void addCallback(NotificationListener cb) throws PigpioException{
listener.addListener(cb);
public void addCallback(GPIOListener gpioListener) throws PigpioException{
this.router.addListener(gpioListener);
}

@Override
public void removeCallback(NotificationListener cb) throws PigpioException{
listener.removeListener(cb);
public void removeCallback(GPIOListener gpioListener) throws PigpioException{
this.router.removeListener(gpioListener);
}


Expand Down
6 changes: 3 additions & 3 deletions JPigpio/src/tests/Test_Rf433Rx.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Test_Rf433Rx {

public static void main(String[] args) throws PigpioException, InterruptedException {

int GPIO_RX = 24; // GPIO for receiving
int GPIO_RX = 17; // GPIO for receiving

// pigpiod host & port
String host = "pigpiod-host";
Expand All @@ -26,8 +26,8 @@ public static void main(String[] args) throws PigpioException, InterruptedExcept
// protocol defines message length, repetition of messages, signal levels etc.
Protocol protocol = new Protocol();

protocol.setRepeatCount(5); // setting this to lowe value than transmitter means duplicates will be reported
protocol.setDataSize(15);
protocol.setRepeatCount(2); // setting this to lowe value than transmitter means duplicates will be reported
protocol.setDataSize(16);

// connect to gpigpiod instance
JPigpio pigpio = new PigpioSocket(host,port);
Expand Down
2 changes: 1 addition & 1 deletion JPigpio/src/tests/Test_Rf433Tx.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class Test_Rf433Tx {

// GPIO pin for transmitting
static int GPIO_TX = 23;
static int GPIO_TX = 27;

// pigpiod host & port
static String host = "pigpiod-host";
Expand Down

0 comments on commit 301a4f1

Please sign in to comment.