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.pioneeravr.internal.protocol.utils;
15 import java.text.DecimalFormat;
19 * @author Antoine Besnard - Initial contribution
21 public final class VolumeConverter {
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" };
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 };
35 * Return the double value of the volume from the value received in the IpControl response.
37 * @param ipControlVolume
38 * @return the volume in Db
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];
47 * Return the string parameter to send to the AVR based on the given volume.
50 * @return the volume for IpControlRequest
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);
59 * Return the String parameter to send to the AVR based on the given persentage of the max volume level.
61 * @param volumePercent
62 * @return the volume for IpControlRequest
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);
71 * Return the percentage of the max volume levelfrom the value received in the IpControl response.
73 * @param ipControlVolume
74 * @return the volume percentage
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];
83 * Format the given double value to an IpControl volume.
85 * @param ipControlVolume
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));
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");