Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 17 | #ifndef _UI_INPUTREADER_LIGHT_CONTROLLER_H |
| 18 | #define _UI_INPUTREADER_LIGHT_CONTROLLER_H |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 19 | |
Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 20 | #include "PeripheralControllerInterface.h" |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 21 | |
| 22 | namespace android { |
| 23 | |
Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 24 | class PeripheralController : public PeripheralControllerInterface { |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 25 | // Refer to https://developer.android.com/reference/kotlin/android/graphics/Color |
| 26 | /* Number of colors : {red, green, blue} */ |
| 27 | static constexpr size_t COLOR_NUM = 3; |
| 28 | static constexpr int32_t MAX_BRIGHTNESS = 0xff; |
| 29 | |
| 30 | public: |
Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 31 | explicit PeripheralController(InputDeviceContext& deviceContext); |
| 32 | ~PeripheralController() override; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 33 | |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 34 | void populateDeviceInfo(InputDeviceInfo* deviceInfo) override; |
| 35 | void dump(std::string& dump) override; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 36 | bool setLightColor(int32_t lightId, int32_t color) override; |
| 37 | bool setLightPlayerId(int32_t lightId, int32_t playerId) override; |
| 38 | std::optional<int32_t> getLightColor(int32_t lightId) override; |
| 39 | std::optional<int32_t> getLightPlayerId(int32_t lightId) override; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 40 | std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override; |
| 41 | std::optional<int32_t> getBatteryStatus(int32_t batteryId) override; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 42 | |
| 43 | private: |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 44 | inline int32_t getDeviceId() { return mDeviceContext.getId(); } |
| 45 | inline InputDeviceContext& getDeviceContext() { return mDeviceContext; } |
| 46 | |
| 47 | InputDeviceContext& mDeviceContext; |
| 48 | void configureLights(); |
| 49 | void configureBattries(); |
| 50 | |
| 51 | struct Battery { |
| 52 | explicit Battery(InputDeviceContext& context, const std::string& name, int32_t id) |
| 53 | : context(context), name(name), id(id) {} |
| 54 | virtual ~Battery() {} |
| 55 | InputDeviceContext& context; |
| 56 | std::string name; |
| 57 | int32_t id; |
| 58 | }; |
| 59 | |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 60 | struct Light { |
Greg Kaiser | 7d3bb35 | 2021-02-23 08:07:55 -0800 | [diff] [blame] | 61 | explicit Light(InputDeviceContext& context, const std::string& name, int32_t id, |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 62 | InputDeviceLightType type) |
| 63 | : context(context), name(name), id(id), type(type) {} |
| 64 | virtual ~Light() {} |
| 65 | InputDeviceContext& context; |
| 66 | std::string name; |
| 67 | int32_t id; |
| 68 | InputDeviceLightType type; |
| 69 | |
| 70 | virtual bool setLightColor(int32_t color) { return false; } |
| 71 | virtual std::optional<int32_t> getLightColor() { return std::nullopt; } |
| 72 | virtual bool setLightPlayerId(int32_t playerId) { return false; } |
| 73 | virtual std::optional<int32_t> getLightPlayerId() { return std::nullopt; } |
| 74 | |
| 75 | virtual void dump(std::string& dump) {} |
| 76 | |
| 77 | std::optional<std::int32_t> getRawLightBrightness(int32_t rawLightId); |
| 78 | void setRawLightBrightness(int32_t rawLightId, int32_t brightness); |
| 79 | }; |
| 80 | |
Chris Ye | 8575833 | 2021-05-16 23:05:17 -0700 | [diff] [blame] | 81 | struct MonoLight : public Light { |
| 82 | explicit MonoLight(InputDeviceContext& context, const std::string& name, int32_t id, |
| 83 | int32_t rawId) |
| 84 | : Light(context, name, id, InputDeviceLightType::MONO), rawId(rawId) {} |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 85 | int32_t rawId; |
| 86 | |
| 87 | bool setLightColor(int32_t color) override; |
| 88 | std::optional<int32_t> getLightColor() override; |
| 89 | void dump(std::string& dump) override; |
| 90 | }; |
| 91 | |
| 92 | struct RgbLight : public Light { |
| 93 | explicit RgbLight(InputDeviceContext& context, int32_t id, |
Greg Kaiser | 7d3bb35 | 2021-02-23 08:07:55 -0800 | [diff] [blame] | 94 | const std::unordered_map<LightColor, int32_t>& rawRgbIds, |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 95 | std::optional<int32_t> rawGlobalId) |
| 96 | : Light(context, "RGB", id, InputDeviceLightType::RGB), |
| 97 | rawRgbIds(rawRgbIds), |
| 98 | rawGlobalId(rawGlobalId) { |
| 99 | brightness = rawGlobalId.has_value() |
| 100 | ? getRawLightBrightness(rawGlobalId.value()).value_or(MAX_BRIGHTNESS) |
| 101 | : MAX_BRIGHTNESS; |
| 102 | } |
| 103 | // Map from color to raw light id. |
| 104 | std::unordered_map<LightColor, int32_t /* rawLightId */> rawRgbIds; |
| 105 | // Optional global control raw light id. |
| 106 | std::optional<int32_t> rawGlobalId; |
| 107 | int32_t brightness; |
| 108 | |
| 109 | bool setLightColor(int32_t color) override; |
| 110 | std::optional<int32_t> getLightColor() override; |
| 111 | void dump(std::string& dump) override; |
| 112 | }; |
| 113 | |
| 114 | struct MultiColorLight : public Light { |
Greg Kaiser | 7d3bb35 | 2021-02-23 08:07:55 -0800 | [diff] [blame] | 115 | explicit MultiColorLight(InputDeviceContext& context, const std::string& name, int32_t id, |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 116 | int32_t rawId) |
| 117 | : Light(context, name, id, InputDeviceLightType::MULTI_COLOR), rawId(rawId) {} |
| 118 | int32_t rawId; |
| 119 | |
| 120 | bool setLightColor(int32_t color) override; |
| 121 | std::optional<int32_t> getLightColor() override; |
| 122 | void dump(std::string& dump) override; |
| 123 | }; |
| 124 | |
| 125 | struct PlayerIdLight : public Light { |
Greg Kaiser | 7d3bb35 | 2021-02-23 08:07:55 -0800 | [diff] [blame] | 126 | explicit PlayerIdLight(InputDeviceContext& context, const std::string& name, int32_t id, |
| 127 | const std::unordered_map<int32_t, int32_t>& rawLightIds) |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 128 | : Light(context, name, id, InputDeviceLightType::PLAYER_ID), |
| 129 | rawLightIds(rawLightIds) {} |
| 130 | // Map from player Id to raw light Id |
| 131 | std::unordered_map<int32_t, int32_t> rawLightIds; |
| 132 | |
| 133 | bool setLightPlayerId(int32_t palyerId) override; |
| 134 | std::optional<int32_t> getLightPlayerId() override; |
| 135 | void dump(std::string& dump) override; |
| 136 | }; |
| 137 | |
| 138 | int32_t mNextId = 0; |
| 139 | |
| 140 | // Light color map from light color to the color index. |
| 141 | static const std::unordered_map<std::string, size_t> LIGHT_COLORS; |
| 142 | |
| 143 | // Light map from light ID to Light |
| 144 | std::unordered_map<int32_t, std::unique_ptr<Light>> mLights; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 145 | |
| 146 | // Battery map from battery ID to battery |
| 147 | std::unordered_map<int32_t, std::unique_ptr<Battery>> mBatteries; |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | } // namespace android |
| 151 | |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 152 | #endif // _UI_INPUTREADER_LIGHT_CONTROLLER_H |