forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PigpioDigitalInputHandler.java
90 lines (81 loc) · 3.19 KB
/
PigpioDigitalInputHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.gpio.internal.handler;
import java.util.Date;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.gpio.internal.NoGpioIdException;
import org.openhab.binding.gpio.internal.configuration.GPIOInputConfiguration;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.types.Command;
import org.openhab.core.types.RefreshType;
import org.openhab.core.types.State;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import eu.xeli.jpigpio.GPIO;
import eu.xeli.jpigpio.JPigpio;
import eu.xeli.jpigpio.PigpioException;
/**
* Thing Handler for digital GPIO inputs.
*
* @author Nils Bauer - Initial contribution
* @author Jan N. Klug - Channel redesign
*/
@NonNullByDefault
public class PigpioDigitalInputHandler implements ChannelHandler {
private final Logger logger = LoggerFactory.getLogger(PigpioDigitalInputHandler.class);
private Date lastChanged = new Date();
private final GPIOInputConfiguration configuration;
private final GPIO gpio;
private final Consumer<State> updateStatus;
public PigpioDigitalInputHandler(GPIOInputConfiguration configuration, JPigpio jPigpio,
ScheduledExecutorService scheduler, Consumer<State> updateStatus)
throws PigpioException, NoGpioIdException {
this.configuration = configuration;
this.updateStatus = updateStatus;
Integer gpioId = configuration.gpioId;
if (gpioId == null) {
throw new NoGpioIdException();
}
gpio = new GPIO(jPigpio, gpioId, JPigpio.PI_INPUT);
jPigpio.gpioSetAlertFunc(gpio.getPin(), (gpio, level, tick) -> {
lastChanged = new Date();
Date thisChange = new Date();
scheduler.schedule(() -> afterDebounce(thisChange), configuration.debouncingTime, TimeUnit.MILLISECONDS);
});
Integer pullupdown = configuration.pullupdown;
jPigpio.gpioSetPullUpDown(gpio.getPin(), pullupdown);
}
private void afterDebounce(Date thisChange) {
try {
// Check if value changed over time
if (!thisChange.before(lastChanged)) {
updateStatus.accept(OnOffType.from(configuration.invert != gpio.getValue()));
}
} catch (PigpioException e) {
logger.warn("Unknown pigpio exception", e);
}
}
@Override
public void handleCommand(Command command) {
if (command instanceof RefreshType) {
try {
updateStatus.accept(OnOffType.from(configuration.invert != gpio.getValue()));
} catch (PigpioException e) {
logger.warn("Unknown pigpio exception while handling Refresh", e);
}
}
}
}