]> git.basschouten.com Git - openhab-addons.git/blob
dc452bfafb970b16188cdcfcb141ba2fec3c05dd
[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.pioneeravr.internal.protocol.utils;
14
15 import java.text.DecimalFormat;
16
17 /**
18  *
19  * @author Antoine Besnard - Initial contribution
20  */
21 public final class VolumeConverter {
22
23     // Volume Format / MAX_IP / Min_DB for Zones
24     // Zone1 Command ***VL 000 to 185 (-80.0dB - +12.0dB - 1step = 0.5dB)
25     // Zone2 Command **ZV 00 to 81 (-80.0dB - + 0.0dB - 1step = 1.0dB)
26     // Zone3 Command **YV 00 to 81 (-80.0dB - + 0.0dB - 1step = 1.0dB)
27     // HDZone Command **YV 00 to 81 (-80.0dB - + 0.0dB - 1step = 1.0dB)
28     private static final String[] IP_CONTROL_VOLUME_FORMAT = { "000", "00", "00", "00" };
29     private static final String[] IP_CONTROL_VOLUME_DEFAULT_VALUE = { "000", "00", "00", "00" };
30
31     private static final double[] MAX_IP_CONTROL_VOLUME = { 184, 80, 80, 80 };
32     private static final double[] MIN_DB_VOLUME = { 80, 80, 80, 80 };
33
34     /**
35      * Return the double value of the volume from the value received in the IpControl response.
36      *
37      * @param ipControlVolume
38      * @return the volume in Db
39      */
40     public static double convertFromIpControlVolumeToDb(String ipControlVolume, int zone) {
41         validateZone(zone - 1);
42         double ipControlVolumeInt = Double.parseDouble(ipControlVolume);
43         return ((ipControlVolumeInt - 1d) / 2d) - MIN_DB_VOLUME[zone - 1];
44     }
45
46     /**
47      * Return the string parameter to send to the AVR based on the given volume.
48      *
49      * @param volumeDb
50      * @return the volume for IpControlRequest
51      */
52     public static String convertFromDbToIpControlVolume(double volumeDb, int zone) {
53         validateZone(zone - 1);
54         double ipControlVolume = ((MIN_DB_VOLUME[zone - 1] + volumeDb) * 2d) + 1d;
55         return formatIpControlVolume(ipControlVolume, zone);
56     }
57
58     /**
59      * Return the String parameter to send to the AVR based on the given persentage of the max volume level.
60      *
61      * @param volumePercent
62      * @return the volume for IpControlRequest
63      */
64     public static String convertFromPercentToIpControlVolume(double volumePercent, int zone) {
65         validateZone(zone - 1);
66         double ipControlVolume = 1 + (volumePercent * MAX_IP_CONTROL_VOLUME[zone - 1] / 100);
67         return formatIpControlVolume(ipControlVolume, zone);
68     }
69
70     /**
71      * Return the percentage of the max volume levelfrom the value received in the IpControl response.
72      *
73      * @param ipControlVolume
74      * @return the volume percentage
75      */
76     public static double convertFromIpControlVolumeToPercent(String ipControlVolume, int zone) {
77         validateZone(zone - 1);
78         double ipControlVolumeInt = Double.parseDouble(ipControlVolume);
79         return ((ipControlVolumeInt - 1d) * 100d) / MAX_IP_CONTROL_VOLUME[zone - 1];
80     }
81
82     /**
83      * Format the given double value to an IpControl volume.
84      *
85      * @param ipControlVolume
86      * @return
87      */
88     private static String formatIpControlVolume(double ipControlVolume, int zone) {
89         validateZone(zone - 1);
90         DecimalFormat formatter = new DecimalFormat(IP_CONTROL_VOLUME_FORMAT[zone - 1]);
91         return formatter.format(Math.round(ipControlVolume));
92     }
93
94     private static void validateZone(int zone) {
95         if (zone < 0 || zone > 3) {
96             throw new IllegalArgumentException("An unexpected zone was received, the value should be in the range 0-3");
97         }
98     }
99 }