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.dali.internal.protocol;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.dali.internal.handler.DaliException;
19 * The {@link DaliAddress} represents an address on the DALI bus.
21 * @author Robert Schmid - Initial contribution
24 public abstract class DaliAddress {
25 private DaliAddress() {
28 protected abstract <T extends DaliFrame> T addToFrame(T frame) throws DaliException;
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");
34 return new DaliAddress() {
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);
44 throw new DaliException("Unsupported frame size");
51 static DaliAddress createRawAddress(int address) {
52 return new DaliAddress() {
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);
61 throw new DaliException("Unsupported frame size");
68 public static DaliAddress createBroadcastAddress() {
69 return new DaliAddress() {
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;
77 throw new DaliException("Unsupported frame size");
84 public static DaliAddress createBroadcastUnaddressedAddress() {
85 return new DaliAddress() {
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;
93 throw new DaliException("Unsupported frame size");
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");
104 return new DaliAddress() {
106 protected <T extends DaliFrame> T addToFrame(T frame) throws DaliException {
107 if (frame.length() == 16) {
109 throw new DaliException("Groups 16..31 are not supported in 16-bit forward frames");
111 frame.data |= (0x80 | ((address & 0b1111) << 1)) << 8;
112 } else if (frame.length() == 24) {
113 frame.data |= (0x80 | ((address & 0b11111) << 1)) << 16;
115 throw new DaliException("Unsupported frame size");