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.leapmotion.internal;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.core.library.types.DecimalType;
17 import org.openhab.core.library.types.HSBType;
18 import org.openhab.core.library.types.OnOffType;
19 import org.openhab.core.library.types.PercentType;
20 import org.openhab.core.thing.profiles.ProfileCallback;
21 import org.openhab.core.thing.profiles.ProfileTypeUID;
22 import org.openhab.core.thing.profiles.TriggerProfile;
23 import org.openhab.core.types.State;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * The {@link LeapMotionColorProfile} class implements the behavior when being linked to a Color item.
30 * @author Kai Kreuzer - Initial contribution
33 public class LeapMotionColorProfile implements TriggerProfile {
35 private final Logger logger = LoggerFactory.getLogger(LeapMotionColorProfile.class);
37 private ProfileCallback callback;
38 private HSBType lastState = HSBType.BLACK;
40 public LeapMotionColorProfile(ProfileCallback callback) {
41 this.callback = callback;
45 public ProfileTypeUID getProfileTypeUID() {
46 return LeapMotionProfileFactory.UID_COLOR;
50 public void onStateUpdateFromItem(State state) {
51 if (state instanceof HSBType hsbState) {
54 PercentType currentBrightness = state.as(PercentType.class);
55 if (currentBrightness != null) {
56 lastState = new HSBType(lastState.getHue(), lastState.getSaturation(), currentBrightness);
62 public void onTriggerFromHandler(String event) {
63 if (event.equals(LeapMotionBindingConstants.GESTURE_TAP)) {
64 callback.sendCommand(lastState.getBrightness().equals(PercentType.ZERO) ? OnOffType.ON : OnOffType.OFF);
65 } else if (event.equals(LeapMotionBindingConstants.GESTURE_CLOCKWISE)) {
66 HSBType color = changeColor(lastState, true);
67 callback.sendCommand(color);
69 } else if (event.equals(LeapMotionBindingConstants.GESTURE_ANTICLOCKWISE)) {
70 HSBType color = changeColor(lastState, false);
71 callback.sendCommand(color);
73 } else if (event.startsWith(LeapMotionBindingConstants.GESTURE_FINGERS)) {
74 // the brightness is determined by the height of the palm over the sensor, where higher means brighter
76 .valueOf(Character.toString(event.charAt(LeapMotionBindingConstants.GESTURE_FINGERS.length())));
80 .valueOf(event.substring(LeapMotionBindingConstants.GESTURE_FINGERS.length() + 2));
81 height = Math.min(100 * height / LeapMotionDimmerProfile.MAX_HEIGHT, 100); // don't use values over
83 PercentType brightness = new PercentType(height);
84 callback.sendCommand(brightness);
85 lastState = new HSBType(lastState.getHue(), lastState.getSaturation(), brightness);
86 } catch (NumberFormatException e) {
87 logger.error("Found illegal format of finger event: {}", event, e);
93 private HSBType changeColor(HSBType color, boolean clockwise) {
94 int hue = clockwise ? (color.getHue().toBigDecimal().intValue() - 20 + 360) % 360
95 : (color.getHue().toBigDecimal().intValue() + 20 + 360) % 360;
96 logger.debug("New hue value: {}", hue);
97 return new HSBType(new DecimalType(hue), color.getSaturation(), color.getBrightness());