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.function.Consumer;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.gpio.internal.NoGpioIdException;
19 import org.openhab.binding.gpio.internal.configuration.GPIOOutputConfiguration;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.types.Command;
22 import org.openhab.core.types.RefreshType;
23 import org.openhab.core.types.State;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import eu.xeli.jpigpio.GPIO;
28 import eu.xeli.jpigpio.JPigpio;
29 import eu.xeli.jpigpio.PigpioException;
32 * Thing Handler for digital GPIO outputs.
34 * @author Nils Bauer - Initial contribution
35 * @author Jan N. Klug - Channel redesign
38 public class PigpioDigitalOutputHandler implements ChannelHandler {
41 private final Logger logger = LoggerFactory.getLogger(PigpioDigitalOutputHandler.class);
43 private final GPIOOutputConfiguration configuration;
44 private final GPIO gpio;
45 private final Consumer<State> updateStatus;
48 * Constructor for PigpioDigitalOutputHandler
50 * @param configuration The channel configuration
51 * @param jPigpio The jPigpio instance
52 * @param updateStatus Is called when the state should be changed
54 * @throws PigpioException Can be thrown by Pigpio
55 * @throws NoGpioIdException Is thrown when no gpioId is defined
57 public PigpioDigitalOutputHandler(GPIOOutputConfiguration configuration, JPigpio jPigpio,
58 Consumer<State> updateStatus) throws PigpioException, NoGpioIdException {
59 this.configuration = configuration;
60 this.updateStatus = updateStatus;
61 Integer gpioId = configuration.gpioId;
63 throw new NoGpioIdException();
65 this.gpio = new GPIO(jPigpio, gpioId, JPigpio.PI_OUTPUT);
69 public void handleCommand(Command command) {
70 if (command instanceof RefreshType) {
72 updateStatus.accept(OnOffType.from(configuration.invert != gpio.getValue()));
73 } catch (PigpioException e) {
74 logger.warn("Unknown pigpio exception while handling Refresh", e);
77 if (command instanceof OnOffType) {
79 gpio.setValue(configuration.invert != (OnOffType.ON.equals(command)));
80 } catch (PigpioException e) {
81 logger.warn("An error occured while changing the gpio value: {}", e.getMessage());