|
| 1 | +#include "LedDeviceHD108.h" |
| 2 | + |
| 3 | +/** |
| 4 | + * @brief Constructor for the HD108 LED device. |
| 5 | + * |
| 6 | + * @param deviceConfig JSON configuration object for this device. |
| 7 | + */ |
| 8 | +LedDeviceHD108::LedDeviceHD108(const QJsonObject &deviceConfig) |
| 9 | + : ProviderSpi(deviceConfig) |
| 10 | +{ |
| 11 | + // By default, set the global brightness register to full (16-bit max) |
| 12 | + _global_brightness = 0xFFFF; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * @brief Factory method: creates an instance of LedDeviceHD108. |
| 17 | + * |
| 18 | + * @param deviceConfig The JSON configuration for the device. |
| 19 | + * @return A pointer to the newly constructed LedDeviceHD108 instance. |
| 20 | + */ |
| 21 | +LedDevice* LedDeviceHD108::construct(const QJsonObject &deviceConfig) |
| 22 | +{ |
| 23 | + return new LedDeviceHD108(deviceConfig); |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * @brief Initializes the HD108 device using the given JSON configuration. |
| 28 | + * |
| 29 | + * This reads certain device-specific parameters, such as the maximum brightness |
| 30 | + * level, and configures the global brightness register accordingly. |
| 31 | + * |
| 32 | + * @param deviceConfig The JSON object containing device parameters. |
| 33 | + * @return True if initialization succeeded, false otherwise. |
| 34 | + */ |
| 35 | +bool LedDeviceHD108::init(const QJsonObject &deviceConfig) |
| 36 | +{ |
| 37 | + bool isInitOK = false; |
| 38 | + |
| 39 | + // First, let the base SPI provider perform its initialization |
| 40 | + if (ProviderSpi::init(deviceConfig)) |
| 41 | + { |
| 42 | + // Read brightnessControlMaxLevel from the config, falling back to a default if absent |
| 43 | + _brightnessControlMaxLevel = deviceConfig["brightnessControlMaxLevel"].toInt(HD108_BRIGHTNESS_MAX_LEVEL); |
| 44 | + |
| 45 | + // Log the brightness info |
| 46 | + Info(_log, |
| 47 | + "[%s] Setting maximum brightness to [%d] = %d%%", |
| 48 | + QSTRING_CSTR(_activeDeviceType), |
| 49 | + _brightnessControlMaxLevel, |
| 50 | + _brightnessControlMaxLevel * 100 / HD108_BRIGHTNESS_MAX_LEVEL); |
| 51 | + |
| 52 | + // Combine the brightness levels into the HD108's 16-bit brightness field. |
| 53 | + // According to the HD108 spec, this is composed of a control bit plus |
| 54 | + // the brightness level split into three segments for R, G, B. |
| 55 | + _global_brightness = (1 << 15) |
| 56 | + | (_brightnessControlMaxLevel << 10) |
| 57 | + | (_brightnessControlMaxLevel << 5) |
| 58 | + | _brightnessControlMaxLevel; |
| 59 | + |
| 60 | + isInitOK = true; |
| 61 | + } |
| 62 | + |
| 63 | + return isInitOK; |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * @brief Writes a vector of RGB colors to the HD108 LEDs. |
| 68 | + * |
| 69 | + * The HD108 protocol requires: |
| 70 | + * - A start frame of 64 bits (8 bytes) all set to 0x00. |
| 71 | + * - For each LED, 64 bits: |
| 72 | + * - 16 bits of global brightness |
| 73 | + * - 16 bits for red |
| 74 | + * - 16 bits for green |
| 75 | + * - 16 bits for blue |
| 76 | + * - An end frame of at least (ledCount / 16 + 1) bytes of 0xFF. |
| 77 | + * |
| 78 | + * Each 8-bit color value is expanded to 16 bits by copying it into both the high |
| 79 | + * and low byte (e.g. 0x7F -> 0x7F7F). This ensures a correct mapping to the HD108's |
| 80 | + * internal 16-bit color resolution and allows for a true "off" state at 0x0000. |
| 81 | + * |
| 82 | + * @param ledValues A vector of ColorRgb (red, green, blue) structures. |
| 83 | + * @return The result of the SPI write operation (0 for success, or an error code). |
| 84 | + */ |
| 85 | +int LedDeviceHD108::write(const std::vector<ColorRgb> & ledValues) |
| 86 | +{ |
| 87 | + // Calculate how much space we need in total: |
| 88 | + // - 8 bytes for the start frame |
| 89 | + // - 8 bytes per LED (16 bits global brightness + 16 bits R + G + B) |
| 90 | + // - end frame: ledCount / 16 + 1 bytes of 0xFF |
| 91 | + const size_t ledCount = ledValues.size(); |
| 92 | + const size_t totalSize = 8 // start frame |
| 93 | + + (ledCount * 8) // LED data (8 bytes each) |
| 94 | + + (ledCount / 16 + 1); // end frame bytes |
| 95 | + |
| 96 | + // Reserve enough space to avoid multiple allocations |
| 97 | + std::vector<uint8_t> hd108Data; |
| 98 | + hd108Data.reserve(totalSize); |
| 99 | + |
| 100 | + // 1) Start frame: 64 bits of 0x00 |
| 101 | + hd108Data.insert(hd108Data.end(), 8, 0x00); |
| 102 | + |
| 103 | + // 2) For each LED, insert 8 bytes: 16 bits brightness, 16 bits R, 16 bits G, 16 bits B |
| 104 | + for (const ColorRgb &color : ledValues) |
| 105 | + { |
| 106 | + // Expand 8-bit color components to 16 bits each |
| 107 | + uint16_t red16 = (static_cast<uint16_t>(color.red) << 8) | color.red; |
| 108 | + uint16_t green16 = (static_cast<uint16_t>(color.green) << 8) | color.green; |
| 109 | + uint16_t blue16 = (static_cast<uint16_t>(color.blue) << 8) | color.blue; |
| 110 | + |
| 111 | + // Global brightness (16 bits) |
| 112 | + hd108Data.push_back(_global_brightness >> 8); |
| 113 | + hd108Data.push_back(_global_brightness & 0xFF); |
| 114 | + |
| 115 | + // Red (16 bits) |
| 116 | + hd108Data.push_back(red16 >> 8); |
| 117 | + hd108Data.push_back(red16 & 0xFF); |
| 118 | + |
| 119 | + // Green (16 bits) |
| 120 | + hd108Data.push_back(green16 >> 8); |
| 121 | + hd108Data.push_back(green16 & 0xFF); |
| 122 | + |
| 123 | + // Blue (16 bits) |
| 124 | + hd108Data.push_back(blue16 >> 8); |
| 125 | + hd108Data.push_back(blue16 & 0xFF); |
| 126 | + } |
| 127 | + |
| 128 | + // 3) End frame: at least (ledCount / 16 + 1) bytes of 0xFF |
| 129 | + hd108Data.insert(hd108Data.end(), (ledCount / 16) + 1, 0xFF); |
| 130 | + |
| 131 | + // Finally, transmit the assembled data via SPI |
| 132 | + return writeBytes(hd108Data.size(), hd108Data.data()); |
| 133 | +} |
0 commit comments