]> git.basschouten.com Git - openhab-addons.git/blob
a0110a589d18eca74075395c870fd9ba8359c423
[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.yamahareceiver.internal.config;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants.Zone;
18
19 /**
20  * Zone settings.
21  *
22  * @author Tomasz Maruszak - Initial contribution.
23  */
24 @NonNullByDefault
25 public class YamahaZoneConfig {
26     /**
27      * Zone name, will be one of {@link Zone}.
28      */
29     private String zone = "";
30     /**
31      * Volume relative change factor when sending {@link org.openhab.core.library.types.IncreaseDecreaseType}
32      * commands.
33      */
34     private float volumeRelativeChangeFactor = 0.5f; // Default: 0.5 percent
35     /**
36      * Minimum allowed volume in dB.
37      */
38     private float volumeDbMin = -80f; // -80.0 dB
39     /**
40      * Maximum allowed volume in dB.
41      */
42     private float volumeDbMax = 12f; // 12.0 dB
43
44     public @Nullable Zone getZone() {
45         return YamahaUtils.tryParseEnum(Zone.class, zone);
46     }
47
48     public String getZoneValue() {
49         return zone;
50     }
51
52     public float getVolumeRelativeChangeFactor() {
53         return volumeRelativeChangeFactor;
54     }
55
56     public float getVolumeDbMin() {
57         return volumeDbMin;
58     }
59
60     public float getVolumeDbMax() {
61         return volumeDbMax;
62     }
63
64     private float getVolumeDbRange() {
65         return getVolumeDbMax() - getVolumeDbMin();
66     }
67
68     /**
69      * Converts from volume percentage to volume dB.
70      *
71      * @param volume volume percentage
72      * @return volume dB
73      */
74     public float getVolumeDb(float volume) {
75         return volume * getVolumeDbRange() / 100.0f + getVolumeDbMin();
76     }
77
78     /**
79      * Converts from volume dB to volume percentage.
80      *
81      * @param volumeDb volume dB
82      * @return volume percentage
83      */
84     public float getVolumePercentage(float volumeDb) {
85         float volume = (volumeDb - getVolumeDbMin()) * 100.0f / getVolumeDbRange();
86         if (volume < 0 || volume > 100) {
87             volume = Math.max(0, Math.min(volume, 100));
88         }
89         return volume;
90     }
91 }