]> git.basschouten.com Git - openhab-addons.git/blob
df5582caaef75a9e4190ac7a21d97ab11f0f4bf9
[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.dali.internal.protocol;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.dali.internal.handler.DaliException;
17
18 /**
19  * The {@link DaliAddress} represents an address on the DALI bus.
20  *
21  * @author Robert Schmid - Initial contribution
22  */
23 @NonNullByDefault
24 public abstract class DaliAddress {
25     private DaliAddress() {
26     }
27
28     protected abstract <T extends DaliFrame> T addToFrame(T frame) throws DaliException;
29
30     public static DaliAddress createShortAddress(int address) throws DaliException {
31         if (address < 0 || address > 63) {
32             throw new DaliException("address must be in the range 0..63");
33         }
34         return new DaliAddress() {
35             @Override
36             protected <T extends DaliFrame> T addToFrame(T frame) throws DaliException {
37                 if (frame.length() == 16) {
38                     frame.data &= ~(1 << 15); // unset bit 15
39                     frame.data |= ((address & 0b111111) << 9);
40                 } else if (frame.length() == 24) {
41                     frame.data &= ~(1 << 23); // unset bit 23
42                     frame.data |= ((address & 0b111111) << 17);
43                 } else {
44                     throw new DaliException("Unsupported frame size");
45                 }
46                 return frame;
47             }
48         };
49     }
50
51     static DaliAddress createRawAddress(int address) {
52         return new DaliAddress() {
53             @Override
54             protected <T extends DaliFrame> T addToFrame(T frame) throws DaliException {
55                 // Keep all bits of the raw address
56                 if (frame.length() == 16) {
57                     frame.data |= ((address & 0xff) << 8);
58                 } else if (frame.length() == 24) {
59                     frame.data |= ((address & 0xff) << 16);
60                 } else {
61                     throw new DaliException("Unsupported frame size");
62                 }
63                 return frame;
64             }
65         };
66     }
67
68     public static DaliAddress createBroadcastAddress() {
69         return new DaliAddress() {
70             @Override
71             protected <T extends DaliFrame> T addToFrame(T frame) throws DaliException {
72                 if (frame.length() == 16) {
73                     frame.data |= 0x7f << 9;
74                 } else if (frame.length() == 24) {
75                     frame.data |= 0x7f << 17;
76                 } else {
77                     throw new DaliException("Unsupported frame size");
78                 }
79                 return frame;
80             }
81         };
82     }
83
84     public static DaliAddress createBroadcastUnaddressedAddress() {
85         return new DaliAddress() {
86             @Override
87             protected <T extends DaliFrame> T addToFrame(T frame) throws DaliException {
88                 if (frame.length() == 16) {
89                     frame.data |= 0x7e << 9;
90                 } else if (frame.length() == 24) {
91                     frame.data |= 0x7e << 17;
92                 } else {
93                     throw new DaliException("Unsupported frame size");
94                 }
95                 return frame;
96             }
97         };
98     }
99
100     public static DaliAddress createGroupAddress(int address) throws DaliException {
101         if (address < 0 || address > 31) {
102             throw new DaliException("address must be in the range 0..31");
103         }
104         return new DaliAddress() {
105             @Override
106             protected <T extends DaliFrame> T addToFrame(T frame) throws DaliException {
107                 if (frame.length() == 16) {
108                     if (address > 15) {
109                         throw new DaliException("Groups 16..31 are not supported in 16-bit forward frames");
110                     }
111                     frame.data |= (0x80 | ((address & 0b1111) << 1)) << 8;
112                 } else if (frame.length() == 24) {
113                     frame.data |= (0x80 | ((address & 0b11111) << 1)) << 16;
114                 } else {
115                     throw new DaliException("Unsupported frame size");
116                 }
117                 return frame;
118             }
119         };
120     }
121 }