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.bluetooth.enoceanble.internal;
15 import java.nio.ByteBuffer;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
20 * The {@link EnoceanBlePtm215Event} class is parsing the BLE manufacturer data into an event object.
22 * @author Patrick Fink - Initial contribution
25 public class EnoceanBlePtm215Event {
27 private static final byte PRESSED = 0x1;
29 private static final byte BUTTON1_DIR1 = 0x10;
30 private static final byte BUTTON1_DIR2 = 0x8;
31 private static final byte BUTTON2_DIR1 = 0x4;
32 private static final byte BUTTON2_DIR2 = 0x2;
34 private final byte byteState;
35 private final int sequence;
37 public EnoceanBlePtm215Event(byte[] manufacturerData) {
38 byteState = manufacturerData[6];
40 byte[] sequenceBytes = new byte[] { manufacturerData[5], manufacturerData[4], manufacturerData[3],
41 manufacturerData[2] };
42 ByteBuffer sequenceBytesBuffered = ByteBuffer.wrap(sequenceBytes); // big-endian by default
43 sequence = sequenceBytesBuffered.getInt();
46 public boolean isPressed() {
47 return checkFlag(PRESSED);
50 public boolean isButton1() {
51 return checkFlag(BUTTON1_DIR1) || checkFlag(BUTTON1_DIR2);
54 public boolean isButton2() {
55 return checkFlag(BUTTON2_DIR1) || checkFlag(BUTTON2_DIR2);
58 public boolean isDir1() {
59 return checkFlag(BUTTON1_DIR1) || checkFlag(BUTTON2_DIR1);
62 public boolean isDir2() {
63 return checkFlag(BUTTON1_DIR2) || checkFlag(BUTTON2_DIR2);
66 private boolean checkFlag(int flag) {
67 return (byteState & flag) == flag;
70 public int getSequence() {
75 public String toString() {
76 return "Button " + (isButton1() ? 1 : 2) + " Dir " + (isDir1() ? 1 : 2) + " "
77 + (isPressed() ? "PRESSED" : "RELEASED") + " (seq. " + this.sequence + ")";