]> git.basschouten.com Git - openhab-addons.git/blob
047923be38703929753eacfb7b5cfbf1e2accb41
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.bluetooth.enoceanble.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16
17 /**
18  * The {@link EnoceanBlePtm215Event} class is parsing the BLE manufacturer data into an event object.
19  *
20  * @author Patrick Fink - Initial contribution
21  */
22 @NonNullByDefault
23 public class EnoceanBlePtm215Event {
24
25     private static final byte PRESSED = 0x1;
26
27     private static final byte BUTTON1_DIR1 = 0x10;
28     private static final byte BUTTON1_DIR2 = 0x8;
29     private static final byte BUTTON2_DIR1 = 0x4;
30     private static final byte BUTTON2_DIR2 = 0x2;
31
32     private final byte byteState;
33
34     public EnoceanBlePtm215Event(byte[] manufacturerData) {
35         byteState = manufacturerData[6];
36     }
37
38     public boolean isPressed() {
39         return checkFlag(PRESSED);
40     }
41
42     public boolean isButton1() {
43         return checkFlag(BUTTON1_DIR1) || checkFlag(BUTTON1_DIR2);
44     }
45
46     public boolean isButton2() {
47         return checkFlag(BUTTON2_DIR1) || checkFlag(BUTTON2_DIR2);
48     }
49
50     public boolean isDir1() {
51         return checkFlag(BUTTON1_DIR1) || checkFlag(BUTTON2_DIR1);
52     }
53
54     public boolean isDir2() {
55         return checkFlag(BUTTON1_DIR2) || checkFlag(BUTTON2_DIR2);
56     }
57
58     private boolean checkFlag(int flag) {
59         return (byteState & flag) == flag;
60     }
61
62     @Override
63     public String toString() {
64         return "Button " + (isButton1() ? 1 : 2) + " Dir " + (isDir1() ? 1 : 2) + " "
65                 + (isPressed() ? "PRESSED" : "RELEASED");
66     }
67 }