]> git.basschouten.com Git - openhab-addons.git/blob
082f9f6eb7f237dce6744a29a38750c31b8d9637
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 & 0b11111) << 9);
40                 } else if (frame.length() == 24) {
41                     frame.data &= ~(1 << 23); // unset bit 23
42                     frame.data |= ((address & 0b11111) << 17);
43                 } else {
44                     throw new DaliException("Unsupported frame size");
45                 }
46                 return frame;
47             }
48         };
49     }
50
51     public static DaliAddress createBroadcastAddress() {
52         return new DaliAddress() {
53             @Override
54             protected <T extends DaliFrame> T addToFrame(T frame) throws DaliException {
55                 if (frame.length() == 16) {
56                     frame.data |= 0x7f << 9;
57                 } else if (frame.length() == 24) {
58                     frame.data |= 0x7f << 17;
59                 } else {
60                     throw new DaliException("Unsupported frame size");
61                 }
62                 return frame;
63             }
64         };
65     }
66
67     public static DaliAddress createBroadcastUnaddressedAddress() {
68         return new DaliAddress() {
69             @Override
70             protected <T extends DaliFrame> T addToFrame(T frame) throws DaliException {
71                 if (frame.length() == 16) {
72                     frame.data |= 0x7e << 9;
73                 } else if (frame.length() == 24) {
74                     frame.data |= 0x7e << 17;
75                 } else {
76                     throw new DaliException("Unsupported frame size");
77                 }
78                 return frame;
79             }
80         };
81     }
82
83     public static DaliAddress createGroupAddress(int address) throws DaliException {
84         if (address < 0 || address > 31) {
85             throw new DaliException("address must be in the range 0..31");
86         }
87         return new DaliAddress() {
88             @Override
89             protected <T extends DaliFrame> T addToFrame(T frame) throws DaliException {
90                 if (frame.length() == 16) {
91                     if (address > 15) {
92                         throw new DaliException("Groups 16..31 are not supported in 16-bit forward frames");
93                     }
94                     frame.data |= ((0x4 << 3) & (address & 0b111)) << 9;
95                 } else if (frame.length() == 24) {
96                     frame.data |= ((0x2 << 4) & (address & 0b1111)) << 17;
97                 } else {
98                     throw new DaliException("Unsupported frame size");
99                 }
100                 return frame;
101             }
102         };
103     }
104 }