2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.gpio.internal.handler;
15 import java.util.Date;
16 import java.util.concurrent.ScheduledExecutorService;
17 import java.util.concurrent.TimeUnit;
18 import java.util.function.Consumer;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.gpio.internal.GPIOBindingConstants;
22 import org.openhab.binding.gpio.internal.InvalidPullUpDownException;
23 import org.openhab.binding.gpio.internal.NoGpioIdException;
24 import org.openhab.binding.gpio.internal.configuration.GPIOInputConfiguration;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.types.Command;
27 import org.openhab.core.types.RefreshType;
28 import org.openhab.core.types.State;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import eu.xeli.jpigpio.GPIO;
33 import eu.xeli.jpigpio.JPigpio;
34 import eu.xeli.jpigpio.PigpioException;
37 * Thing Handler for digital GPIO inputs.
39 * @author Nils Bauer - Initial contribution
40 * @author Jan N. Klug - Channel redesign
41 * @author Martin Dagarin - Pull Up/Down GPIO pin
44 public class PigpioDigitalInputHandler implements ChannelHandler {
46 private final Logger logger = LoggerFactory.getLogger(PigpioDigitalInputHandler.class);
47 private Date lastChanged = new Date();
49 private final GPIOInputConfiguration configuration;
50 private final GPIO gpio;
51 private final Consumer<State> updateStatus;
53 public PigpioDigitalInputHandler(GPIOInputConfiguration configuration, JPigpio jPigpio,
54 ScheduledExecutorService scheduler, Consumer<State> updateStatus)
55 throws PigpioException, InvalidPullUpDownException, NoGpioIdException {
56 this.configuration = configuration;
57 this.updateStatus = updateStatus;
58 Integer gpioId = configuration.gpioId;
60 throw new NoGpioIdException();
62 Integer pullupdown = JPigpio.PI_PUD_OFF;
63 String pullupdownStr = configuration.pullupdown.toUpperCase();
64 if (pullupdownStr.equals(GPIOBindingConstants.PUD_DOWN)) {
65 pullupdown = JPigpio.PI_PUD_DOWN;
66 } else if (pullupdownStr.equals(GPIOBindingConstants.PUD_UP)) {
67 pullupdown = JPigpio.PI_PUD_UP;
69 if (!pullupdownStr.equals(GPIOBindingConstants.PUD_OFF)) {
70 throw new InvalidPullUpDownException();
73 gpio = new GPIO(jPigpio, gpioId, JPigpio.PI_INPUT);
74 jPigpio.gpioSetAlertFunc(gpio.getPin(), (gpio, level, tick) -> {
75 lastChanged = new Date();
76 Date thisChange = new Date();
77 scheduler.schedule(() -> afterDebounce(thisChange), configuration.debouncingTime, TimeUnit.MILLISECONDS);
79 jPigpio.gpioSetPullUpDown(gpio.getPin(), pullupdown);
82 private void afterDebounce(Date thisChange) {
84 // Check if value changed over time
85 if (!thisChange.before(lastChanged)) {
86 updateStatus.accept(OnOffType.from(configuration.invert != gpio.getValue()));
88 } catch (PigpioException e) {
89 logger.warn("Unknown pigpio exception", e);
94 public void handleCommand(Command command) {
95 if (command instanceof RefreshType) {
97 updateStatus.accept(OnOffType.from(configuration.invert != gpio.getValue()));
98 } catch (PigpioException e) {
99 logger.warn("Unknown pigpio exception while handling Refresh", e);