]> git.basschouten.com Git - openhab-addons.git/blob
88c3ba9db46d31f799c18ba97183ed8dfaf3b436
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.alarmdecoder.internal.protocol;
14
15 import java.util.ArrayList;
16 import java.util.Collection;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20
21 /**
22  * Defines keypad device addresses used in an AD keypad address mask.
23  *
24  * @author Bob Adair - Initial contribution
25  */
26 @NonNullByDefault
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),
36
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),
45
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),
54
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);
63
64     private final long mask;
65     private final int device;
66
67     ADAddress(long mask, int device) {
68         this.mask = mask;
69         this.device = device;
70     }
71
72     /** Returns the device bit mask **/
73     public long mask() {
74         return mask;
75     }
76
77     /** Returns the device number (0-31) **/
78     public int deviceNum() {
79         return device;
80     }
81
82     /**
83      * Returns the first device address found in addressMask or null if none are found
84      *
85      * @param addressMask
86      */
87     public static @Nullable ADAddress getDevice(long addressMask) {
88         for (ADAddress address : ADAddress.values()) {
89             if ((address.mask() & addressMask) != 0) {
90                 return address;
91             }
92         }
93         return null;
94     }
95
96     /**
97      * Returns a Collection of the device addresses found in addressMask.
98      * Returns an empty collection if none are found.
99      *
100      * @param addressMask
101      */
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);
107             }
108         }
109         return addressCollection;
110     }
111
112     /**
113      * Return true if 1 and only 1 address bit is set in addressMask
114      */
115     public static boolean singleAddress(long addressMask) {
116         return (Long.bitCount(addressMask) == 1);
117     }
118 }