|
1 | 1 | #include "LedDeviceHD108.h" |
2 | 2 |
|
3 | | -// Constructor |
| 3 | +/** |
| 4 | + * @brief Constructor for the HD108 LED device. |
| 5 | + * |
| 6 | + * @param deviceConfig JSON configuration object for this device. |
| 7 | + */ |
4 | 8 | LedDeviceHD108::LedDeviceHD108(const QJsonObject &deviceConfig) |
5 | 9 | : ProviderSpi(deviceConfig) |
6 | 10 | { |
7 | | - // Overwrite non supported/required features |
8 | | - _latchTime_ms = 0; |
9 | | - // Initialize _global_brightness |
10 | | - _global_brightness = 0xFFFF; |
| 11 | + // By default, set the global brightness register to full (16-bit max) |
| 12 | + _global_brightness = 0xFFFF; |
11 | 13 | } |
12 | 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 | + */ |
13 | 21 | LedDevice* LedDeviceHD108::construct(const QJsonObject &deviceConfig) |
14 | 22 | { |
15 | | - return new LedDeviceHD108(deviceConfig); |
| 23 | + return new LedDeviceHD108(deviceConfig); |
16 | 24 | } |
17 | 25 |
|
18 | | -// Initialization method |
| 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 | + */ |
19 | 35 | bool LedDeviceHD108::init(const QJsonObject &deviceConfig) |
20 | 36 | { |
21 | | - bool isInitOK = false; |
| 37 | + bool isInitOK = false; |
22 | 38 |
|
23 | | - if ( ProviderSpi::init(deviceConfig) ) |
24 | | - { |
25 | | - _brightnessControlMaxLevel = deviceConfig["brightnessControlMaxLevel"].toInt(HD108_BRIGHTNESS_MAX_LEVEL); |
26 | | - Info(_log, "[%s] Setting maximum brightness to [%d] = %d%%", QSTRING_CSTR(_activeDeviceType), _brightnessControlMaxLevel, _brightnessControlMaxLevel * 100 / HD108_BRIGHTNESS_MAX_LEVEL); |
| 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); |
27 | 51 |
|
28 | | - // Set the global brightness or control byte based on the provided formula |
29 | | - _global_brightness = (1 << 15) | (_brightnessControlMaxLevel << 10) | (_brightnessControlMaxLevel << 5) | _brightnessControlMaxLevel; |
| 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; |
30 | 59 |
|
31 | | - isInitOK = true; |
32 | | - } |
| 60 | + isInitOK = true; |
| 61 | + } |
33 | 62 |
|
34 | | - return isInitOK; |
| 63 | + return isInitOK; |
35 | 64 | } |
36 | 65 |
|
37 | | -// Write method to update the LED colors |
| 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 | + */ |
38 | 85 | int LedDeviceHD108::write(const std::vector<ColorRgb> & ledValues) |
39 | 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 |
40 | 97 | std::vector<uint8_t> hd108Data; |
| 98 | + hd108Data.reserve(totalSize); |
41 | 99 |
|
42 | | - // Start frame - 64 bits of 0 (8 bytes of 0) |
| 100 | + // 1) Start frame: 64 bits of 0x00 |
43 | 101 | hd108Data.insert(hd108Data.end(), 8, 0x00); |
44 | 102 |
|
45 | | - // Adapted logic from your HD108 library's "show" and "setPixelColor8Bit" methods |
| 103 | + // 2) For each LED, insert 8 bytes: 16 bits brightness, 16 bits R, 16 bits G, 16 bits B |
46 | 104 | for (const ColorRgb &color : ledValues) |
47 | 105 | { |
48 | | - // Convert 8-bit to 16-bit colors |
49 | | - uint16_t red16 = (color.red << 8) | color.red; |
50 | | - uint16_t green16 = (color.green << 8) | color.green; |
51 | | - uint16_t blue16 = (color.blue << 8) | color.blue; |
| 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; |
52 | 110 |
|
53 | | - // Push global and color components into hd108Data |
54 | | - // Brightness |
| 111 | + // Global brightness (16 bits) |
55 | 112 | hd108Data.push_back(_global_brightness >> 8); |
56 | | - hd108Data.push_back(_global_brightness & 0xFF); |
57 | | - // Color - Red |
| 113 | + hd108Data.push_back(_global_brightness & 0xFF); |
| 114 | + |
| 115 | + // Red (16 bits) |
58 | 116 | hd108Data.push_back(red16 >> 8); |
59 | | - hd108Data.push_back(red16 & 0xFF); |
60 | | - // Color - Green |
| 117 | + hd108Data.push_back(red16 & 0xFF); |
| 118 | + |
| 119 | + // Green (16 bits) |
61 | 120 | hd108Data.push_back(green16 >> 8); |
62 | | - hd108Data.push_back(green16 & 0xFF); |
63 | | - // Color - Blue |
| 121 | + hd108Data.push_back(green16 & 0xFF); |
| 122 | + |
| 123 | + // Blue (16 bits) |
64 | 124 | hd108Data.push_back(blue16 >> 8); |
65 | | - hd108Data.push_back(blue16 & 0xFF); |
| 125 | + hd108Data.push_back(blue16 & 0xFF); |
66 | 126 | } |
67 | 127 |
|
68 | | - // End frame - write "1"s equal to at least how many pixels are in the string |
69 | | - hd108Data.insert(hd108Data.end(), ledValues.size() / 16 + 1, 0xFF); |
| 128 | + // 3) End frame: at least (ledCount / 16 + 1) bytes of 0xFF |
| 129 | + hd108Data.insert(hd108Data.end(), (ledCount / 16) + 1, 0xFF); |
70 | 130 |
|
71 | | - // Use ProviderSpi's writeBytes method to send the data |
| 131 | + // Finally, transmit the assembled data via SPI |
72 | 132 | return writeBytes(hd108Data.size(), hd108Data.data()); |
73 | 133 | } |
0 commit comments