| 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 |  | 
| Andy Chen | 22c330c | 2022-08-29 20:07:10 -0400 | [diff] [blame] | 34 | int32_t getEventHubId() const override; | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 35 | void populateDeviceInfo(InputDeviceInfo* deviceInfo) override; | 
|  | 36 | void dump(std::string& dump) override; | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 37 | bool setLightColor(int32_t lightId, int32_t color) override; | 
|  | 38 | bool setLightPlayerId(int32_t lightId, int32_t playerId) override; | 
|  | 39 | std::optional<int32_t> getLightColor(int32_t lightId) override; | 
|  | 40 | std::optional<int32_t> getLightPlayerId(int32_t lightId) override; | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 41 | std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override; | 
|  | 42 | std::optional<int32_t> getBatteryStatus(int32_t batteryId) override; | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 43 |  | 
|  | 44 | private: | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 45 | inline int32_t getDeviceId() { return mDeviceContext.getId(); } | 
|  | 46 | inline InputDeviceContext& getDeviceContext() { return mDeviceContext; } | 
| Andy Chen | 22c330c | 2022-08-29 20:07:10 -0400 | [diff] [blame] | 47 | inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; } | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 48 |  | 
|  | 49 | InputDeviceContext& mDeviceContext; | 
|  | 50 | void configureLights(); | 
|  | 51 | void configureBattries(); | 
|  | 52 |  | 
|  | 53 | struct Battery { | 
|  | 54 | explicit Battery(InputDeviceContext& context, const std::string& name, int32_t id) | 
|  | 55 | : context(context), name(name), id(id) {} | 
|  | 56 | virtual ~Battery() {} | 
|  | 57 | InputDeviceContext& context; | 
|  | 58 | std::string name; | 
|  | 59 | int32_t id; | 
|  | 60 | }; | 
|  | 61 |  | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 62 | struct Light { | 
| Greg Kaiser | 7d3bb35 | 2021-02-23 08:07:55 -0800 | [diff] [blame] | 63 | explicit Light(InputDeviceContext& context, const std::string& name, int32_t id, | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 64 | InputDeviceLightType type) | 
|  | 65 | : context(context), name(name), id(id), type(type) {} | 
|  | 66 | virtual ~Light() {} | 
|  | 67 | InputDeviceContext& context; | 
|  | 68 | std::string name; | 
|  | 69 | int32_t id; | 
|  | 70 | InputDeviceLightType type; | 
|  | 71 |  | 
|  | 72 | virtual bool setLightColor(int32_t color) { return false; } | 
|  | 73 | virtual std::optional<int32_t> getLightColor() { return std::nullopt; } | 
|  | 74 | virtual bool setLightPlayerId(int32_t playerId) { return false; } | 
|  | 75 | virtual std::optional<int32_t> getLightPlayerId() { return std::nullopt; } | 
|  | 76 |  | 
|  | 77 | virtual void dump(std::string& dump) {} | 
|  | 78 |  | 
|  | 79 | std::optional<std::int32_t> getRawLightBrightness(int32_t rawLightId); | 
|  | 80 | void setRawLightBrightness(int32_t rawLightId, int32_t brightness); | 
|  | 81 | }; | 
|  | 82 |  | 
| Chris Ye | 8575833 | 2021-05-16 23:05:17 -0700 | [diff] [blame] | 83 | struct MonoLight : public Light { | 
|  | 84 | explicit MonoLight(InputDeviceContext& context, const std::string& name, int32_t id, | 
|  | 85 | int32_t rawId) | 
|  | 86 | : Light(context, name, id, InputDeviceLightType::MONO), rawId(rawId) {} | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 87 | int32_t rawId; | 
|  | 88 |  | 
|  | 89 | bool setLightColor(int32_t color) override; | 
|  | 90 | std::optional<int32_t> getLightColor() override; | 
|  | 91 | void dump(std::string& dump) override; | 
|  | 92 | }; | 
|  | 93 |  | 
|  | 94 | struct RgbLight : public Light { | 
|  | 95 | explicit RgbLight(InputDeviceContext& context, int32_t id, | 
| Greg Kaiser | 7d3bb35 | 2021-02-23 08:07:55 -0800 | [diff] [blame] | 96 | const std::unordered_map<LightColor, int32_t>& rawRgbIds, | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 97 | std::optional<int32_t> rawGlobalId) | 
|  | 98 | : Light(context, "RGB", id, InputDeviceLightType::RGB), | 
|  | 99 | rawRgbIds(rawRgbIds), | 
|  | 100 | rawGlobalId(rawGlobalId) { | 
|  | 101 | brightness = rawGlobalId.has_value() | 
|  | 102 | ? getRawLightBrightness(rawGlobalId.value()).value_or(MAX_BRIGHTNESS) | 
|  | 103 | : MAX_BRIGHTNESS; | 
|  | 104 | } | 
|  | 105 | // Map from color to raw light id. | 
|  | 106 | std::unordered_map<LightColor, int32_t /* rawLightId */> rawRgbIds; | 
|  | 107 | // Optional global control raw light id. | 
|  | 108 | std::optional<int32_t> rawGlobalId; | 
|  | 109 | int32_t brightness; | 
|  | 110 |  | 
|  | 111 | bool setLightColor(int32_t color) override; | 
|  | 112 | std::optional<int32_t> getLightColor() override; | 
|  | 113 | void dump(std::string& dump) override; | 
|  | 114 | }; | 
|  | 115 |  | 
|  | 116 | struct MultiColorLight : public Light { | 
| Greg Kaiser | 7d3bb35 | 2021-02-23 08:07:55 -0800 | [diff] [blame] | 117 | explicit MultiColorLight(InputDeviceContext& context, const std::string& name, int32_t id, | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 118 | int32_t rawId) | 
|  | 119 | : Light(context, name, id, InputDeviceLightType::MULTI_COLOR), rawId(rawId) {} | 
|  | 120 | int32_t rawId; | 
|  | 121 |  | 
|  | 122 | bool setLightColor(int32_t color) override; | 
|  | 123 | std::optional<int32_t> getLightColor() override; | 
|  | 124 | void dump(std::string& dump) override; | 
|  | 125 | }; | 
|  | 126 |  | 
|  | 127 | struct PlayerIdLight : public Light { | 
| Greg Kaiser | 7d3bb35 | 2021-02-23 08:07:55 -0800 | [diff] [blame] | 128 | explicit PlayerIdLight(InputDeviceContext& context, const std::string& name, int32_t id, | 
|  | 129 | const std::unordered_map<int32_t, int32_t>& rawLightIds) | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 130 | : Light(context, name, id, InputDeviceLightType::PLAYER_ID), | 
|  | 131 | rawLightIds(rawLightIds) {} | 
|  | 132 | // Map from player Id to raw light Id | 
|  | 133 | std::unordered_map<int32_t, int32_t> rawLightIds; | 
|  | 134 |  | 
|  | 135 | bool setLightPlayerId(int32_t palyerId) override; | 
|  | 136 | std::optional<int32_t> getLightPlayerId() override; | 
|  | 137 | void dump(std::string& dump) override; | 
|  | 138 | }; | 
|  | 139 |  | 
|  | 140 | int32_t mNextId = 0; | 
|  | 141 |  | 
|  | 142 | // Light color map from light color to the color index. | 
|  | 143 | static const std::unordered_map<std::string, size_t> LIGHT_COLORS; | 
|  | 144 |  | 
|  | 145 | // Light map from light ID to Light | 
|  | 146 | std::unordered_map<int32_t, std::unique_ptr<Light>> mLights; | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 147 |  | 
|  | 148 | // Battery map from battery ID to battery | 
|  | 149 | std::unordered_map<int32_t, std::unique_ptr<Battery>> mBatteries; | 
| Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 150 | }; | 
|  | 151 |  | 
|  | 152 | } // namespace android | 
|  | 153 |  | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 154 | #endif // _UI_INPUTREADER_LIGHT_CONTROLLER_H |