Add lambda overloads for interrupts (#1636)

This commit is contained in:
Thad House
2019-05-30 17:59:35 -07:00
committed by Peter Johnson
parent 90957aeea4
commit 7de9477347
3 changed files with 141 additions and 17 deletions

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
@@ -7,6 +7,8 @@
package edu.wpi.first.wpilibj;
import java.util.function.Consumer;
import edu.wpi.first.hal.InterruptJNI;
import edu.wpi.first.hal.util.AllocationException;
@@ -26,6 +28,18 @@ public abstract class InterruptableSensorBase extends SendableBase {
WaitResult(int value) {
this.value = value;
}
public static WaitResult getValue(boolean rising, boolean falling) {
if (rising && falling) {
return kBoth;
} else if (rising) {
return kRisingEdge;
} else if (falling) {
return kFallingEdge;
} else {
return kTimeout;
}
}
}
/**
@@ -67,6 +81,36 @@ public abstract class InterruptableSensorBase extends SendableBase {
*/
public abstract int getPortHandleForRouting();
/**
* Request one of the 8 interrupts asynchronously on this digital input.
*
* @param handler The {@link InterruptHandler} that contains the method {@link
* InterruptHandlerFunction#onInterrupt(boolean, boolean)} that will be called
* whenever there is an interrupt on this device. Request interrupts in synchronous
* mode where the user program interrupt handler will be called when an interrupt
* occurs. The default is interrupt on rising edges only.
*/
public void requestInterrupts(Consumer<WaitResult> handler) {
if (m_interrupt != 0) {
throw new AllocationException("The interrupt has already been allocated");
}
allocateInterrupts(false);
assert m_interrupt != 0;
InterruptJNI.requestInterrupts(m_interrupt, getPortHandleForRouting(),
getAnalogTriggerTypeForRouting());
setUpSourceEdge(true, false);
InterruptJNI.attachInterruptHandler(m_interrupt, (mask, obj) -> {
// Rising edge result is the interrupt bit set in the byte 0xFF
// Falling edge result is the interrupt bit set in the byte 0xFF00
boolean rising = (mask & 0xFF) != 0;
boolean falling = (mask & 0xFF00) != 0;
handler.accept(WaitResult.getValue(rising, falling));
}, null);
}
/**
* Request one of the 8 interrupts asynchronously on this digital input.
*
@@ -154,16 +198,9 @@ public abstract class InterruptableSensorBase extends SendableBase {
// Falling edge result is the interrupt bit set in the byte 0xFF00
// Set any bit set to be true for that edge, and AND the 2 results
// together to match the existing enum for all interrupts
int rising = ((result & 0xFF) != 0) ? 0x1 : 0x0;
int falling = ((result & 0xFF00) != 0) ? 0x0100 : 0x0;
result = rising | falling;
for (WaitResult mode : WaitResult.values()) {
if (mode.value == result) {
return mode;
}
}
return null;
boolean rising = (result & 0xFF) != 0;
boolean falling = (result & 0xFF00) != 0;
return WaitResult.getValue(rising, falling);
}
/**