1 /*
2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "battery_led.h"
17 #include "charger_log.h"
18
19 using namespace OHOS::HDI::Battery::V2_0;
20 using namespace OHOS::HDI::Light::V1_0;
21 using namespace std;
22
23 namespace OHOS {
24 namespace PowerMgr {
25 namespace {
26 constexpr uint32_t MOVE_RIGHT_16 = 16;
27 constexpr uint32_t MOVE_RIGHT_8 = 8;
28 } // namespace
InitLight()29 void BatteryLed::InitLight()
30 {
31 batteryLight_ = ILightInterface::Get();
32 if (batteryLight_ == nullptr) {
33 BATTERY_HILOGW(FEATURE_CHARGING, "Light interface is null");
34 return;
35 }
36
37 vector<HdfLightInfo> lightInfo;
38 if (batteryLight_->GetLightInfo(lightInfo) < HDF_SUCCESS) {
39 BATTERY_HILOGW(FEATURE_CHARGING, "Get battert light failed");
40 return;
41 }
42
43 available_ = std::any_of(lightInfo.begin(), lightInfo.end(), [](const auto &item) {
44 return item.lightId == HdfLightId::HDF_LIGHT_ID_BATTERY;
45 });
46 BATTERY_HILOGI(FEATURE_CHARGING, "Battery light is available: %{public}d", available_);
47 }
48
TurnOff()49 void BatteryLed::TurnOff()
50 {
51 if (!available_) {
52 return;
53 }
54 int32_t ret = batteryLight_->TurnOffLight(HdfLightId::HDF_LIGHT_ID_BATTERY);
55 if (ret < HDF_SUCCESS) {
56 BATTERY_HILOGW(FEATURE_CHARGING, "Failed to turn off the battery light");
57 }
58 lightColor_ = (ret < HDF_SUCCESS) ? lightColor_ : 0;
59 }
60
TurnOn(uint32_t color)61 void BatteryLed::TurnOn(uint32_t color)
62 {
63 if (!available_) {
64 return;
65 }
66 struct HdfLightEffect effect;
67 effect.lightColor.colorValue.rgbColor.r = static_cast<uint8_t>((color >> MOVE_RIGHT_16) & 0xFF);
68 effect.lightColor.colorValue.rgbColor.g = static_cast<uint8_t>((color >> MOVE_RIGHT_8) & 0xFF);
69 effect.lightColor.colorValue.rgbColor.b = static_cast<uint8_t>(color & 0xFF);
70
71 BATTERY_HILOGD(FEATURE_CHARGING, "battery light color is %{public}d", color);
72 int32_t ret = batteryLight_->TurnOnLight(HdfLightId::HDF_LIGHT_ID_BATTERY, effect);
73 if (ret < HDF_SUCCESS) {
74 BATTERY_HILOGW(FEATURE_CHARGING, "Failed to turn on the battery light");
75 }
76 lightColor_ = (ret < HDF_SUCCESS) ? lightColor_ : color;
77 }
78
UpdateColor(int32_t chargeState,int32_t capacity)79 bool BatteryLed::UpdateColor(int32_t chargeState, int32_t capacity)
80 {
81 if ((chargeState == static_cast<int32_t>(BatteryChargeState::CHARGE_STATE_NONE)) ||
82 (chargeState == static_cast<int32_t>(BatteryChargeState::CHARGE_STATE_RESERVED)) || !available_) {
83 BATTERY_HILOGD(FEATURE_CHARGING, "not in charging state, turn off battery light");
84 TurnOff();
85 return false;
86 }
87
88 const auto& lightConf = BatteryConfig::GetInstance().GetLightConf();
89 for (const auto& it : lightConf) {
90 if ((capacity >= it.beginSoc) && (capacity <= it.endSoc)) {
91 if (lightColor_ == it.rgb) {
92 return true;
93 }
94 TurnOff();
95 TurnOn(it.rgb);
96 return true;
97 }
98 }
99 return false;
100 }
101
IsAvailable() const102 bool BatteryLed::IsAvailable() const
103 {
104 return available_;
105 }
106
GetLightColor() const107 uint32_t BatteryLed::GetLightColor() const
108 {
109 return lightColor_;
110 }
111 } // namespace PowerMgr
112 } // namespace OHOS
113