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.alarmdecoder.internal.protocol;
15 import java.util.ArrayList;
16 import java.util.Collection;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
22 * Defines keypad device addresses used in an AD keypad address mask.
24 * @author Bob Adair - Initial contribution
27 public enum ADAddress {
28 KEYPAD0(0x01000000, 0),
29 KEYPAD1(0x02000000, 1),
30 KEYPAD2(0x04000000, 2),
31 KEYPAD3(0x08000000, 3),
32 KEYPAD4(0x10000000, 4),
33 KEYPAD5(0x20000000, 5),
34 KEYPAD6(0x40000000, 6),
35 KEYPAD7(0x80000000, 7),
37 KEYPAD8(0x00010000, 8),
38 KEYPAD9(0x00020000, 9),
39 KEYPAD10(0x00040000, 10),
40 KEYPAD11(0x00080000, 11),
41 KEYPAD12(0x00100000, 12),
42 KEYPAD13(0x00200000, 13),
43 KEYPAD14(0x00400000, 14),
44 KEYPAD15(0x00800000, 15),
46 KEYPAD16(0x00000100, 16),
47 KEYPAD17(0x00000200, 17),
48 KEYPAD18(0x00000400, 18),
49 KEYPAD19(0x00000800, 19),
50 KEYPAD20(0x00001000, 20),
51 KEYPAD21(0x00002000, 21),
52 KEYPAD22(0x00004000, 22),
53 KEYPAD23(0x00008000, 23),
55 KEYPAD24(0x00000001, 24),
56 KEYPAD25(0x00000002, 25),
57 KEYPAD26(0x00000004, 26),
58 KEYPAD27(0x00000008, 27),
59 KEYPAD28(0x00000010, 28),
60 KEYPAD29(0x00000020, 29),
61 KEYPAD30(0x00000040, 30),
62 KEYPAD31(0x00000080, 31);
64 private final long mask;
65 private final int device;
67 ADAddress(long mask, int device) {
72 /** Returns the device bit mask **/
77 /** Returns the device number (0-31) **/
78 public int deviceNum() {
83 * Returns the first device address found in addressMask or null if none are found
87 public static @Nullable ADAddress getDevice(long addressMask) {
88 for (ADAddress address : ADAddress.values()) {
89 if ((address.mask() & addressMask) != 0) {
97 * Returns a Collection of the device addresses found in addressMask.
98 * Returns an empty collection if none are found.
102 public static Collection<ADAddress> getDevices(long addressMask) {
103 Collection<ADAddress> addressCollection = new ArrayList<>();
104 for (ADAddress address : ADAddress.values()) {
105 if ((address.mask() & addressMask) != 0) {
106 addressCollection.add(address);
109 return addressCollection;
113 * Return true if 1 and only 1 address bit is set in addressMask
115 public static boolean singleAddress(long addressMask) {
116 return (Long.bitCount(addressMask) == 1);