|
| 1 | +/* LedDeviceHD108.cpp |
| 2 | +* |
| 3 | +* MIT License |
| 4 | +* |
| 5 | +* Copyright (c) 2023 awawa-dev |
| 6 | +* |
| 7 | +* Project homesite: https://github.com/awawa-dev/HyperHDR |
| 8 | +* |
| 9 | +* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +* of this software and associated documentation files (the "Software"), to deal |
| 11 | +* in the Software without restriction, including without limitation the rights |
| 12 | +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +* copies of the Software, and to permit persons to whom the Software is |
| 14 | +* furnished to do so, subject to the following conditions: |
| 15 | +* |
| 16 | +* The above copyright notice and this permission notice shall be included in all |
| 17 | +* copies or substantial portions of the Software. |
| 18 | +
|
| 19 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | +* SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +#include "LedDeviceHD108.h" |
| 29 | + |
| 30 | +// Local HyperHDR includes |
| 31 | +#include <utils/Logger.h> |
| 32 | +#include <cmath> |
| 33 | +#include <algorithm> |
| 34 | + |
| 35 | +const int HD108_MAX_LEVEL = 0x1F; |
| 36 | +const uint32_t HD108_MAX_THRESHOLD = 100u; |
| 37 | +const uint32_t HD108_MAX_THRESHOLD_2 = HD108_MAX_THRESHOLD * HD108_MAX_THRESHOLD; |
| 38 | +const uint16_t HD108_MAX_LEVEL_BIT = 0b1000000000000000; |
| 39 | + |
| 40 | +LedDeviceHD108::LedDeviceHD108(const QJsonObject& deviceConfig) |
| 41 | + : ProviderSpi(deviceConfig) |
| 42 | + , _globalBrightnessControlThreshold(HD108_MAX_THRESHOLD) |
| 43 | + , _globalBrightnessControlMaxLevel(HD108_MAX_LEVEL) |
| 44 | +{ |
| 45 | +} |
| 46 | + |
| 47 | +LedDevice* LedDeviceHD108::construct(const QJsonObject& deviceConfig) |
| 48 | +{ |
| 49 | + return new LedDeviceHD108(deviceConfig); |
| 50 | +} |
| 51 | + |
| 52 | +bool LedDeviceHD108::init(const QJsonObject& deviceConfig) |
| 53 | +{ |
| 54 | + bool isInitOK = false; |
| 55 | + |
| 56 | + // Initialise sub-class |
| 57 | + if (ProviderSpi::init(deviceConfig)) |
| 58 | + { |
| 59 | + _globalBrightnessControlThreshold = static_cast<uint32_t>(std::min( |
| 60 | + std::lround(deviceConfig["globalBrightnessControlThreshold"].toDouble(HD108_MAX_THRESHOLD) * HD108_MAX_THRESHOLD), |
| 61 | + (long)HD108_MAX_THRESHOLD_2)); |
| 62 | + _globalBrightnessControlMaxLevel = deviceConfig["globalBrightnessControlMaxLevel"].toInt(HD108_MAX_LEVEL); |
| 63 | + Info(_log, "[HD108] Using global brightness control with threshold of %d and max level of %d", _globalBrightnessControlThreshold, _globalBrightnessControlMaxLevel); |
| 64 | + |
| 65 | + _ledBuffer.resize(0, 0x00); |
| 66 | + _ledBuffer.resize((_ledCount * 8) + 16, 0x00); |
| 67 | + |
| 68 | + isInitOK = true; |
| 69 | + } |
| 70 | + return isInitOK; |
| 71 | +} |
| 72 | + |
| 73 | +static inline uint16_t MSB_FIRST(uint16_t x, bool littleEndian) |
| 74 | +{ |
| 75 | + if (littleEndian) |
| 76 | + return (x >> 8) | (x << 8); |
| 77 | + else |
| 78 | + return x; |
| 79 | +} |
| 80 | + |
| 81 | +int LedDeviceHD108::write(const std::vector<ColorRgb>& ledValues) |
| 82 | +{ |
| 83 | + int littleEndian = 1; |
| 84 | + |
| 85 | + if (*(char*)&littleEndian != 1) |
| 86 | + littleEndian = 0; |
| 87 | + |
| 88 | + |
| 89 | + if (_ledCount != ledValues.size()) |
| 90 | + { |
| 91 | + Warning(_log, "HD108 led's number has changed (old: %d, new: %d). Rebuilding buffer.", _ledCount, ledValues.size()); |
| 92 | + _ledCount = static_cast<uint>(ledValues.size()); |
| 93 | + _ledBuffer.resize(0, 0x00); |
| 94 | + _ledBuffer.resize((_ledCount * 8) + 16, 0x00); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + int index = 8; |
| 99 | + uint16_t* data = reinterpret_cast<uint16_t*>(_ledBuffer.data()); |
| 100 | + for (auto const& rgb : ledValues) |
| 101 | + { |
| 102 | + const int isLit = (rgb.red || rgb.green || rgb.blue); |
| 103 | + uint16_t red = rgb.red * 256u; |
| 104 | + uint16_t green = rgb.green * 256u; |
| 105 | + uint16_t blue = rgb.blue * 256u; |
| 106 | + uint16_t level = HD108_MAX_LEVEL_BIT; |
| 107 | + |
| 108 | + for (int i = 0; i < 3 && isLit; i++) |
| 109 | + { |
| 110 | + level |= (_globalBrightnessControlMaxLevel & HD108_MAX_LEVEL) << (5 * i); |
| 111 | + } |
| 112 | + |
| 113 | + if (_globalBrightnessControlThreshold < HD108_MAX_THRESHOLD_2) |
| 114 | + { |
| 115 | + red = (red * _globalBrightnessControlThreshold) / HD108_MAX_THRESHOLD_2; |
| 116 | + green = (green * _globalBrightnessControlThreshold) / HD108_MAX_THRESHOLD_2; |
| 117 | + blue = (blue * _globalBrightnessControlThreshold) / HD108_MAX_THRESHOLD_2; |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + data[index + 0] = MSB_FIRST(level, littleEndian); |
| 122 | + data[index + 1] = MSB_FIRST(red, littleEndian); |
| 123 | + data[index + 2] = MSB_FIRST(green, littleEndian); |
| 124 | + data[index + 3] = MSB_FIRST(blue, littleEndian); |
| 125 | + index += 4; |
| 126 | + } |
| 127 | + |
| 128 | + return writeBytes(_ledBuffer.size(), _ledBuffer.data()); |
| 129 | +} |
0 commit comments