]> git.basschouten.com Git - openhab-addons.git/blob
65086458a0071080b3467fd28e4ea1bbd12e6042
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.gpio.internal.handler;
14
15 import java.util.Date;
16 import java.util.concurrent.ScheduledExecutorService;
17 import java.util.concurrent.TimeUnit;
18 import java.util.function.Consumer;
19
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;
31
32 import eu.xeli.jpigpio.GPIO;
33 import eu.xeli.jpigpio.JPigpio;
34 import eu.xeli.jpigpio.PigpioException;
35
36 /**
37  * Thing Handler for digital GPIO inputs.
38  *
39  * @author Nils Bauer - Initial contribution
40  * @author Jan N. Klug - Channel redesign
41  * @author Martin Dagarin - Pull Up/Down GPIO pin
42  */
43 @NonNullByDefault
44 public class PigpioDigitalInputHandler implements ChannelHandler {
45
46     private final Logger logger = LoggerFactory.getLogger(PigpioDigitalInputHandler.class);
47     private Date lastChanged = new Date();
48
49     private final GPIOInputConfiguration configuration;
50     private final GPIO gpio;
51     private final Consumer<State> updateStatus;
52
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;
59         if (gpioId == null) {
60             throw new NoGpioIdException();
61         }
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;
68         } else {
69             if (!pullupdownStr.equals(GPIOBindingConstants.PUD_OFF)) {
70                 throw new InvalidPullUpDownException();
71             }
72         }
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);
78         });
79         jPigpio.gpioSetPullUpDown(gpio.getPin(), pullupdown);
80     }
81
82     private void afterDebounce(Date thisChange) {
83         try {
84             // Check if value changed over time
85             if (!thisChange.before(lastChanged)) {
86                 updateStatus.accept(OnOffType.from(configuration.invert != gpio.getValue()));
87             }
88         } catch (PigpioException e) {
89             logger.warn("Unknown pigpio exception", e);
90         }
91     }
92
93     @Override
94     public void handleCommand(Command command) {
95         if (command instanceof RefreshType) {
96             try {
97                 updateStatus.accept(OnOffType.from(configuration.invert != gpio.getValue()));
98             } catch (PigpioException e) {
99                 logger.warn("Unknown pigpio exception while handling Refresh", e);
100             }
101         }
102     }
103 }