]> git.basschouten.com Git - openhab-addons.git/blob
845a1da383ce3ce7214656c35f98a85bc1599db0
[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.regoheatpump.internal.rego6xx;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16
17 /**
18  * The {@link ValueConverter} is responsible for converting various rego 6xx specific data types.
19  *
20  * @author Boris Krivonog - Initial contribution
21  */
22 @NonNullByDefault
23 class ValueConverter {
24     public static byte[] shortToSevenBitFormat(short value) {
25         byte b1 = (byte) ((value & 0xC000) >> 14);
26         byte b2 = (byte) ((value & 0x3F80) >> 7);
27         byte b3 = (byte) (value & 0x007F);
28
29         return new byte[] { b1, b2, b3 };
30     }
31
32     public static short sevenBitFormatToShort(byte[] buffer, int offset) {
33         return (short) (buffer[offset] << 14 | buffer[offset + 1] << 7 | buffer[offset + 2]);
34     }
35
36     public static byte arrayToByte(byte[] buffer, int offset) {
37         return (byte) (buffer[offset] << 4 | buffer[offset + 1]);
38     }
39
40     public static String stringFromBytes(byte[] buffer, int offset, int charCount) {
41         StringBuilder builder = new StringBuilder(charCount);
42
43         int length = offset + charCount * 2;
44         for (int i = offset; i < length; i += 2) {
45             builder.append((char) arrayToByte(buffer, i));
46         }
47
48         return builder.toString();
49     }
50 }