]> git.basschouten.com Git - openhab-addons.git/blob
538dee85c10741373eb0bf90cefaaaf8e0200d7c
[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         gpio = new GPIO(jPigpio, gpioId, JPigpio.PI_INPUT);
73         jPigpio.gpioSetAlertFunc(gpio.getPin(), (gpio, level, tick) -> {
74             lastChanged = new Date();
75             Date thisChange = new Date();
76             scheduler.schedule(() -> afterDebounce(thisChange), configuration.debouncingTime, TimeUnit.MILLISECONDS);
77         });
78         jPigpio.gpioSetPullUpDown(gpio.getPin(), pullupdown);
79     }
80
81     private void afterDebounce(Date thisChange) {
82         try {
83             // Check if value changed over time
84             if (!thisChange.before(lastChanged)) {
85                 updateStatus.accept(OnOffType.from(configuration.invert != gpio.getValue()));
86             }
87         } catch (PigpioException e) {
88             logger.warn("Unknown pigpio exception", e);
89         }
90     }
91
92     @Override
93     public void handleCommand(Command command) {
94         if (command instanceof RefreshType) {
95             try {
96                 updateStatus.accept(OnOffType.from(configuration.invert != gpio.getValue()));
97             } catch (PigpioException e) {
98                 logger.warn("Unknown pigpio exception while handling Refresh", e);
99             }
100         }
101     }
102 }