GCC Code Coverage Report


Directory: ./
File: non-gpl/BBShiftPanel/BBShiftPanel.cpp
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 794 1464 54.2%
Functions: 54 71 76.1%
Branches: 619 1858 33.3%

Line Branch Exec Source
1 /*
2 * This file is part of the Falcon Player (FPP) and is Copyright (C)
3 * 2013-2022 by the Falcon Player Developers.
4 *
5 * The Falcon Player (FPP) is free software, and is covered under
6 * multiple Open Source licenses. Please see the included 'LICENSES'
7 * file for descriptions of what files are covered by each license.
8 *
9 * This source file is covered under the CC-BY-ND as described in the
10 * included LICENSE.CC-BY-ND file. This file may be modified for
11 * personal use, but modified copies MAY NOT be redistributed in any form.
12 */
13
14 #include "fpp-pch.h"
15
16 #include "fpp-json.h"
17
18 #include <pthread.h>
19 #include <sched.h>
20 #include <sys/mman.h>
21 #include <sys/wait.h>
22 #include <time.h>
23 #include <tuple>
24 #include <unistd.h>
25
26 #include <algorithm>
27 #include <chrono>
28 #include <cmath>
29 #include <cstdlib>
30 #include <cstring>
31 #include <set>
32
33 // FPP includes
34 #include "../../Sequence.h"
35 #include "../../Warnings.h"
36 #include "channeloutput/ChannelOutputSetup.h"
37 #include "../../common.h"
38 #include "../../log.h"
39 #include "../../settings.h"
40
41 #include "BBShiftPanel.h"
42 #include "../../channeloutput/GammaLUT.h"
43 #include "../CapeUtils/CapeUtils.h"
44 #include "../../pru/SMEMRing.hp"
45 #include "util/BBBUtils.h"
46
47 #include "MapPixelsByDepth16.h"
48
49 #include "channeloutput/PanelInterleaveHandler.h"
50 #include "overlays/PixelOverlay.h"
51
52 #include "Plugin.h"
53 class BBShiftPanelPlugin : public FPPPlugins::Plugin, public FPPPlugins::ChannelOutputPlugin {
54 public:
55 52 BBShiftPanelPlugin() :
56
2/4
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 52 times.
✗ Branch 3 not taken.
156 FPPPlugins::Plugin("BBShiftPanel") {
57 52 }
58 52 virtual ChannelOutput* createChannelOutput(unsigned int startChannel, unsigned int channelCount) override {
59
2/6
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 52 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
104 return new BBShiftPanelOutput(startChannel, channelCount);
60 }
61 };
62
63 extern "C" {
64 52 FPPPlugins::Plugin* createPlugin() {
65
2/6
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 52 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
104 return new BBShiftPanelPlugin();
66 }
67 }
68
69 constexpr int ADDRESSING_MODE_STANDARD = 0;
70 constexpr int ADDRESSING_MODE_AB_SHIFT = 1; // row shift register, A=clock, B=data (active low)
71 constexpr int ADDRESSING_MODE_DIRECT = 2;
72 constexpr int ADDRESSING_MODE_ABC_SHIFT = 3; // row shift register, A=clock, C=data (active high)
73 constexpr int ADDRESSING_MODE_ABC_DE = 4; // SM5266P: A/B/C drive a shifter, D/E select the bank
74 constexpr int ADDRESSING_MODE_FM6353C = 50;
75 constexpr int ADDRESSING_MODE_FM6363C = 51;
76 constexpr int ADDRESSING_MODE_FM6373 = 52;
77 constexpr int ADDRESSING_MODE_DP3364 = 53;
78 constexpr int ADDRESSING_MODE_ICND1065L = 54;
79 constexpr int ADDRESSING_MODE_SM16380SH = 55;
80 constexpr int ADDRESSING_MODE_ICND2153 = 56;
81
82 constexpr int PANEL_TYPE_FM6126A = 1;
83 constexpr int PANEL_TYPE_FM6127 = 2;
84 // The PWM chips are presented in the UI as panel types (they are driver
85 // chips like the FM6126A), but internally they are PWM addressing modes
86 constexpr int PANEL_TYPE_FM6363C = 3;
87 constexpr int PANEL_TYPE_FM6353 = 4;
88 constexpr int PANEL_TYPE_FM6373 = 5; // the common DP32019B receiver boards
89 constexpr int PANEL_TYPE_DP3364 = 6;
90 constexpr int PANEL_TYPE_ICND1065L = 7;
91 constexpr int PANEL_TYPE_SM16380SH = 8;
92 // Split out of PANEL_TYPE_FM6353, which used to claim ICN2153 compatibility.
93 // The two take the same register grammar but not the same values, so an
94 // existing config on type 4 keeps the FM6353 payload it has always had.
95 constexpr int PANEL_TYPE_ICND2153 = 9;
96
97 // must match PWM_CHIP_CONFIG_OFFSET in BBShiftPanel_pwm.asm
98 #define PWM_CHIP_CONFIG_OFFSET 0x1DF8
99 // the register slots live at the front of the same PRU data RAM, so growing
100 // them must not reach the chip config word
101 static_assert(offsetof(BBShiftPanelData, registers) + sizeof(((BBShiftPanelData*)nullptr)->registers) <= PWM_CHIP_CONFIG_OFFSET);
102 // REG1_OFF in BBShiftPanel_pwm.asm is this offset, hardcoded as 16
103 static_assert(offsetof(BBShiftPanelData, registers) == 16);
104
105 // FM6373 config register sequence, one {address, value} word per register,
106 // different per color line. Captured from a working DP32019B 128x64 panel
107 // by the kingdo9/rpi-rgb-led-matrix_pwm_experiment project
108 // (lib/spwm-panel-registers.cc, register config 0); DMD_STM32 carries a
109 // shorter single-color variant of the same address space. The 0x02
110 // register holds the scan row count and is patched at runtime.
111 static const uint16_t FM6373_SEQ_R[] = {
112 0x0000, 0x0100, 0x021f, 0x033f, 0x0402, 0x0508, 0x0602, 0x0720,
113 0x0820, 0x0900, 0x0a00, 0x0b00, 0x0c01, 0x0d01, 0x0e04, 0x0f01,
114 0x10c2, 0x1121, 0x1201, 0x1300, 0x1400, 0x1500, 0x1600, 0x17f0,
115 0x181f, 0x1900, 0x1a1f, 0x1b10, 0x1c2a, 0x1d0a, 0x1e42, 0x1f04,
116 0x2008, 0x2101, 0x221c, 0x7000, 0x7100, 0x7200, 0x7300, 0x7400,
117 0xf000, 0xf100, 0xf200, 0xf300, 0xf400, 0xf500, 0x2300
118 };
119 static const uint16_t FM6373_SEQ_G[] = {
120 0x0000, 0x0100, 0x021f, 0x033f, 0x0402, 0x0508, 0x0602, 0x0720,
121 0x0820, 0x0900, 0x0a00, 0x0b00, 0x0c08, 0x0d01, 0x0e04, 0x0f01,
122 0x10c2, 0x1121, 0x1201, 0x1300, 0x1400, 0x1500, 0x1600, 0x17f0,
123 0x181f, 0x1950, 0x1a1f, 0x1b10, 0x1c2a, 0x1d0a, 0x1e46, 0x1f20,
124 0x2008, 0x2101, 0x221c, 0x7000, 0x7100, 0x7200, 0x7300, 0x7400,
125 0xf000, 0xf100, 0xf200, 0xf300, 0xf400, 0xf500, 0x2300
126 };
127 static const uint16_t FM6373_SEQ_B[] = {
128 0x0000, 0x0100, 0x021f, 0x033f, 0x0402, 0x0508, 0x0602, 0x0720,
129 0x0820, 0x0900, 0x0a00, 0x0b00, 0x0c08, 0x0d01, 0x0e04, 0x0f01,
130 0x10c2, 0x1121, 0x1201, 0x1300, 0x1400, 0x1500, 0x1600, 0x17f0,
131 0x182f, 0x1900, 0x1a1f, 0x1b10, 0x1c2a, 0x1d0a, 0x1e48, 0x1f20,
132 0x2010, 0x2101, 0x221c, 0x7000, 0x7100, 0x7200, 0x7300, 0x7400,
133 0xf000, 0xf100, 0xf200, 0xf300, 0xf400, 0xf500, 0x2300
134 };
135 constexpr int FM6373_SEQ_LEN = 47;
136 static_assert(sizeof(FM6373_SEQ_R) == FM6373_SEQ_LEN * sizeof(uint16_t));
137 static_assert(sizeof(FM6373_SEQ_G) == FM6373_SEQ_LEN * sizeof(uint16_t));
138 static_assert(sizeof(FM6373_SEQ_B) == FM6373_SEQ_LEN * sizeof(uint16_t));
139
140 // FM6373 registers for a full height 1/64 scan panel, from the same project
141 // (fm6373.profiles, fm6373_regtype2, a 128x64 capture). Nine registers differ
142 // from the 1/32 table above beyond the scan count, and the whole 0x70/0xf0
143 // tail block moves to a repeated 0x54 - which is why patching only the scan
144 // count does not turn a 1/32 table into a working 1/64 one.
145 static constexpr uint16_t FM6373_64S_SEQ_R[] = {
146 0x0000, 0x0100, 0x023f, 0x033f, 0x0402, 0x0508, 0x0602, 0x0710,
147 0x0810, 0x0900, 0x0a00, 0x0b00, 0x0c01, 0x0d03, 0x0e02, 0x0f11,
148 0x10c2, 0x1121, 0x1201, 0x1300, 0x1400, 0x1500, 0x1600, 0x17f0,
149 0x181f, 0x1900, 0x1a1f, 0x1b10, 0x1cbe, 0x1d0e, 0x1e42, 0x1f24,
150 0x2008, 0x2101, 0x221c, 0x5400, 0x5400, 0x5400, 0x5400, 0x5400,
151 0x5403, 0x5403, 0x5403, 0x5403, 0x5403, 0x5403, 0x2300
152 };
153 static constexpr uint16_t FM6373_64S_SEQ_G[] = {
154 0x0000, 0x0100, 0x023f, 0x033f, 0x0402, 0x0508, 0x0602, 0x0710,
155 0x0810, 0x0900, 0x0a00, 0x0b00, 0x0c08, 0x0d03, 0x0e04, 0x0f11,
156 0x10c2, 0x1121, 0x1201, 0x1300, 0x1400, 0x1500, 0x1600, 0x17f0,
157 0x181f, 0x1950, 0x1a1f, 0x1b10, 0x1cbe, 0x1d0e, 0x1e46, 0x1f20,
158 0x2008, 0x2101, 0x221c, 0x5400, 0x5400, 0x5400, 0x5400, 0x5400,
159 0x5403, 0x5403, 0x5403, 0x5403, 0x5403, 0x5403, 0x2300
160 };
161 static constexpr uint16_t FM6373_64S_SEQ_B[] = {
162 0x0000, 0x0100, 0x023f, 0x033f, 0x0402, 0x0508, 0x0602, 0x0710,
163 0x0810, 0x0900, 0x0a00, 0x0b00, 0x0c08, 0x0d01, 0x0e04, 0x0f11,
164 0x10c2, 0x1121, 0x1201, 0x1300, 0x1400, 0x1500, 0x1600, 0x17f0,
165 0x182f, 0x1900, 0x1a1f, 0x1b10, 0x1cbe, 0x1d0e, 0x1e48, 0x1f20,
166 0x2010, 0x2101, 0x221c, 0x5400, 0x5400, 0x5400, 0x5400, 0x5400,
167 0x5403, 0x5403, 0x5403, 0x5403, 0x5403, 0x5403, 0x2300
168 };
169
170 // ICND1065L config registers. From kingdo9/rpi-rgb-led-matrix_pwm_experiment
171 // (lib/spwm/registertest/data/icnd1065l.profiles, icnd1065l_regtype1 - the
172 // project's built-in default, captured from a 1/43 scan panel). The chip
173 // takes the same upload grammar as the FM6373, so only the payload differs.
174 static constexpr uint16_t ICND1065L_SEQ_R[] = {
175 0x0000, 0x026a, 0x0322, 0x0412, 0x0500, 0x0601, 0x0712, 0x0c10,
176 0x0d02, 0x0e84, 0x0f01, 0x1040, 0x1127, 0x1800, 0x1926, 0x1c60,
177 0x1d02, 0x1e71, 0x2040, 0x2101, 0x2380, 0x74a0
178 };
179 static constexpr uint16_t ICND1065L_SEQ_G[] = {
180 0x0000, 0x026a, 0x0322, 0x0412, 0x0500, 0x0601, 0x0712, 0x0c10,
181 0x0d04, 0x0e84, 0x0f01, 0x1040, 0x1127, 0x1800, 0x1908, 0x1c60,
182 0x1d02, 0x1e92, 0x2060, 0x2101, 0x2305, 0x74a0
183 };
184 static constexpr uint16_t ICND1065L_SEQ_B[] = {
185 0x0000, 0x026a, 0x0322, 0x0412, 0x0500, 0x0601, 0x0712, 0x0c10,
186 0x0d03, 0x0e84, 0x0f11, 0x1040, 0x1127, 0x1800, 0x190a, 0x1c60,
187 0x1d02, 0x1eb5, 0x2060, 0x2101, 0x2300, 0x74a0
188 };
189 constexpr int ICND1065L_SEQ_LEN = 22;
190
191 // SM16380SH config registers, same source (spwm-panel-registers.cc built-in
192 // main block, a 1/32 scan capture). This chip takes a sixth slot carrying
193 // 0xF003 ahead of the commit pair, and skips the FM6373's middle LAT burst.
194 static constexpr uint16_t SM16380SH_SEQ_R[] = {
195 0x021f, 0x0300, 0x0400, 0x0500, 0x0600, 0x078c, 0x0800, 0x0900,
196 0x0a02, 0x0b0c, 0x0c08, 0x0d00, 0x0e05, 0x0f00, 0x1000, 0x1100,
197 0x1200, 0x1308, 0x1414, 0x1500, 0x1630, 0x1700, 0x1801, 0x1904,
198 0x1a03, 0x1b14, 0x1c12, 0x1d00, 0x1e00, 0x1f0c, 0x2000, 0x2200
199 };
200 static constexpr uint16_t SM16380SH_SEQ_G[] = {
201 0x021f, 0x0300, 0x0400, 0x0500, 0x0600, 0x078c, 0x0800, 0x0900,
202 0x0a02, 0x0b0c, 0x0c18, 0x0d00, 0x0e05, 0x0f00, 0x1000, 0x1100,
203 0x1200, 0x1308, 0x1422, 0x1500, 0x1630, 0x1700, 0x1801, 0x1903,
204 0x1a01, 0x1b14, 0x1c8f, 0x1d00, 0x1e00, 0x1f0c, 0x2000, 0x2200
205 };
206 static constexpr uint16_t SM16380SH_SEQ_B[] = {
207 0x021f, 0x0300, 0x0400, 0x0500, 0x0600, 0x078c, 0x0800, 0x0900,
208 0x0a02, 0x0b0c, 0x0c30, 0x0d00, 0x0e05, 0x0f00, 0x1000, 0x1100,
209 0x1200, 0x1308, 0x1432, 0x1500, 0x1630, 0x1700, 0x1801, 0x1903,
210 0x1a01, 0x1b14, 0x1c8f, 0x1d00, 0x1e00, 0x1f0c, 0x2000, 0x2200
211 };
212 constexpr int SM16380SH_SEQ_LEN = 32;
213
214 // The FM6373, ICND1065L and SM16380SH uploads are one grammar: a few bare LAT
215 // bursts, then N 16-bit slots each latched over their last 5 clocks, with slot
216 // 3 rotating through the chip's config sequence one word per frame. They
217 // differ only in the payload, whether the middle (11 clock) LAT burst is sent,
218 // how many slots follow, and which register carries the scan count.
219 // A capture taken at one scan rate. Registers other than the scan count
220 // change with the scan rate on these chips, so a table is only really valid
221 // for the geometry it came from; where a second capture is known the closest
222 // one is used instead of stretching the default.
223 // PWMChipSeq / PWMChipSeqVariant are declared in BBShiftPanel.h so the
224 // manager can hold an imported profile by value.
225
226 // FM6353 / ICND2153 built-in register words per color, in the latch order
227 // the PRU sends them (4, 6, 8, 10, 2). The scan count bits of the first word
228 // are patched in at upload. nullptr for every other chip.
229 // FM6353: DMD_STM32 (board707/DMD_STM32, DMD_SPWM_Driver.h conf_6353 =
230 // {0x0008, 0x1f70, 0x6707, 0x40f7, 0x0040} in latch order 2,4,6,8,10).
231 // No per-color current data is known for this chip; brightness only
232 // comes from the GCLK blanking time.
233 // ICND2153: kingdo9/rpi-rgb-led-matrix_pwm_experiment (branch
234 // icnd2153_bsparacino, SPWM_ICND2153_REGISTER_ENTRIES), captured from a
235 // 64x32 1/8 scan panel. Unlike the FM6353 this chip carries per-color
236 // current words in the second register.
237 using FixedRegRow = uint16_t[5];
238 ✗ static const FixedRegRow* fixedRegDefaults(int addressingMode) {
239 static const uint16_t FM6353_REGS[3][5] = {
240 { 0x0070, 0x6707, 0x40f7, 0x0040, 0x0008 },
241 { 0x0070, 0x6707, 0x40f7, 0x0040, 0x0008 },
242 { 0x0070, 0x6707, 0x40f7, 0x0040, 0x0008 },
243 };
244 static const uint16_t ICND2153_REGS[3][5] = {
245 { 0x0070, 0x7dfe, 0x4207, 0x0040, 0x0008 },
246 { 0x0070, 0x71fe, 0x4207, 0x0040, 0x0008 },
247 { 0x0070, 0x5dfe, 0x4207, 0x0040, 0x0008 },
248 };
249 ✗ switch (addressingMode) {
250 ✗ case ADDRESSING_MODE_FM6353C:
251 ✗ return FM6353_REGS;
252 ✗ case ADDRESSING_MODE_ICND2153:
253 ✗ return ICND2153_REGS;
254 ✗ default:
255 ✗ return nullptr;
256 }
257 }
258
259 ✗ static const PWMChipSeq* pwmChipSeqFor(int addressingMode) {
260 // Scan count: every profile in the kingdo9 catalog puts (rows - 1) in the
261 // low 6 bits of this register and leaves the upper 2 bits as a chip
262 // constant, so the patch preserves whatever the table above carries there
263 // (ICND1065L sets bit 6; the other two leave it clear).
264 static const PWMChipSeqVariant FM6373_VARIANTS[] = {
265 { 64, FM6373_64S_SEQ_R, FM6373_64S_SEQ_G, FM6373_64S_SEQ_B },
266 };
267 static const PWMChipSeq FM6373_SEQ = {
268 FM6373_SEQ_R, FM6373_SEQ_G, FM6373_SEQ_B, FM6373_SEQ_LEN, 5, 0, true, 0x02,
269 32, FM6373_VARIANTS, 1
270 };
271 static const PWMChipSeq ICND1065L_SEQ = {
272 ICND1065L_SEQ_R, ICND1065L_SEQ_G, ICND1065L_SEQ_B, ICND1065L_SEQ_LEN, 5, 0, true, 0x02,
273 43, nullptr, 0
274 };
275 static const PWMChipSeq SM16380SH_SEQ = {
276 SM16380SH_SEQ_R, SM16380SH_SEQ_G, SM16380SH_SEQ_B, SM16380SH_SEQ_LEN, 6, 0xF003, false, 0x02,
277 32, nullptr, 0
278 };
279 ✗ switch (addressingMode) {
280 ✗ case ADDRESSING_MODE_FM6373:
281 ✗ return &FM6373_SEQ;
282 ✗ case ADDRESSING_MODE_ICND1065L:
283 ✗ return &ICND1065L_SEQ;
284 ✗ case ADDRESSING_MODE_SM16380SH:
285 ✗ return &SM16380SH_SEQ;
286 }
287 ✗ return nullptr;
288 }
289
290 // DP3364S config registers, one {address, value} word per register, different
291 // per color line. Captured from a working 128x64 1/64 scan panel and posted
292 // to hzeller/rpi-rgb-led-matrix issue #1821; the chip's own datasheet
293 // documents the access protocol but not the register map.
294 //
295 // The datasheet says 15 valid register addresses and that 15 frames complete
296 // a full refresh, which is exactly 0x02-0x0F plus 0x15 - the addresses the
297 // capture carries values for (0x10-0x14 all read back zero and are taken as
298 // reserved). Documented meanings, for the ones that are documented:
299 // 0x03[6:0] PWM display packet count - 1 (0x3f = 64, the async mode default)
300 // 0x04[6:0] row PWM display length - 1
301 // 0x06[2:0] internal GCLK multiplier, FGCLK = FDCLK * (n + 1)
302 // 0x08[7:0] linear output current multiplier - the brightness knob, patched
303 // at runtime; the captured 0x7f is what the panel was built for,
304 // so that is full brightness and FPP only scales down from it
305 // 0x0b[5] 1.5x current gain
306 // 0x0c[7:6] PWM display mode (01 = high gray data independent refresh, the
307 // free running row scan this driver generates); [1] = drop open
308 // circuit bad spots
309 // 0x0f[6:0] current reference
310 // 0x02 is undocumented but holds 0x3f on a 64 row panel and is patched at
311 // runtime with the scan count, the same as 0x02 on the FM6373.
312 static constexpr uint16_t DP3364_SEQ_R[] = {
313 0x023f, 0x033f, 0x041a, 0x0504, 0x0639, 0x0700, 0x087f, 0x0968,
314 0x0abe, 0x0b28, 0x0c58, 0x0d08, 0x0e08, 0x0f20, 0x1500
315 };
316 static constexpr uint16_t DP3364_SEQ_G[] = {
317 0x023f, 0x033f, 0x041a, 0x0504, 0x0639, 0x070c, 0x087f, 0x096b,
318 0x0abf, 0x0b2b, 0x0c58, 0x0d12, 0x0e0b, 0x0f20, 0x1504
319 };
320 static constexpr uint16_t DP3364_SEQ_B[] = {
321 0x023f, 0x033f, 0x041a, 0x0500, 0x0639, 0x070c, 0x087f, 0x0961,
322 0x0abe, 0x0b31, 0x0c58, 0x0d18, 0x0e01, 0x0f20, 0x1504
323 };
324 constexpr int DP3364_SEQ_LEN = 15;
325 // setupGCLKConfig reads the group count out of the 0x03 entry
326 static_assert((DP3364_SEQ_R[1] >> 8) == 0x03);
327 static_assert(sizeof(DP3364_SEQ_R) == DP3364_SEQ_LEN * sizeof(uint16_t));
328 static_assert(sizeof(DP3364_SEQ_G) == DP3364_SEQ_LEN * sizeof(uint16_t));
329 static_assert(sizeof(DP3364_SEQ_B) == DP3364_SEQ_LEN * sizeof(uint16_t));
330
331 // Approximate DCLK period in nanoseconds for the two PWM firmware variants,
332 // from the instruction counts in BBShiftPanel_pwm.asm at the AM62x PRU's 4ns
333 // cycle: OUTPUT_PIXEL is 24 cycles for 8 outputs and 42 for 16, plus a ~26
334 // cycle LOAD_DATA every 8 (resp. 4) pixels.
335 constexpr int DCLK_NS_8 = 109;
336 constexpr int DCLK_NS_16 = 194;
337
338 // Minimum DCLK periods the DP3364S needs per displayed row, from the "time of
339 // one line" formula the datasheet repeats for each PWM display mode:
340 // (2*(reg0x05[7:4]+1) + 2*(reg0x05[3:0]+1) + 4*(reg0x04[6:0]+1))
341 // / (reg0x06[2:0]+1)
342 // The three color lines are separate chips and need not agree, so the scan has
343 // to hold for the slowest of them.
344 ✗ static int dp3364MinLineDCLKs(const uint16_t* seq, int len) {
345 ✗ int r04 = 0, r05 = 0, r06 = 0;
346 ✗ for (int i = 0; i < len; i++) {
347 ✗ switch (seq[i] >> 8) {
348 ✗ case 0x04:
349 ✗ r04 = seq[i] & 0x7F;
350 ✗ break;
351 ✗ case 0x05:
352 ✗ r05 = seq[i] & 0xFF;
353 ✗ break;
354 ✗ case 0x06:
355 ✗ r06 = seq[i] & 0x07;
356 ✗ break;
357 }
358 }
359 ✗ return (2 * (((r05 >> 4) & 0x0F) + 1) + 2 * ((r05 & 0x0F) + 1) + 4 * (r04 + 1)) / (r06 + 1);
360 }
361
362 constexpr int PWM_COMMAND_SYNC = 0x0001;
363 constexpr int PWM_COMMAND_REGISTERS = 0x0002;
364 constexpr int PWM_COMMAND_STARTGCLK = 0x0004;
365 constexpr int PWM_COMMAND_DATA = 0x0008;
366 constexpr int PWM_COMMAND_HALT = 0xFFFF;
367
368 // Note: the ordering of the bit planes within a frame (formerly a set of
369 // hand-tuned BIT_ORDERS tables) is now computed by buildStrideSchedule(),
370 // which also splits the long MSB on-times into multiple pulses spread
371 // across the frame to raise the perceived refresh rate.
372
373 static const std::vector<std::string> PRU_PINS = { "P1-20", "P1-29", "P1-31", "P1-33", "P1-36",
374 "P2-02", "P2-04", "P2-06", "P2-08",
375 "P2-18", "P2-20", "P2-22", "P2-24",
376 "P2-28", "P2-30", "P2-32", "P2-33",
377 "P2-34", "P2-35" };
378
379 static const std::vector<std::string> PRU0_PWM_PINS = {
380 "P1-31", "P2-28", "P2-30", "P2-32", "P2-34", "P1-36"
381 };
382
383 constexpr int MAX_OUTPUTS = 16;
384
385 // how much the pump thread copies into the shared memory ring at a time; a
386 // multiple of 48 and 8 (the ring write splits blocks across the wrap point,
387 // so it does not need to divide the ring size evenly)
388 constexpr uint32_t PUMP_BLOCK_SIZE = 3072;
389
390 BBShiftPanelManager BBShiftPanelManager::INSTANCE;
391
392 52 BBShiftPanelOutput::BBShiftPanelOutput(unsigned int startChannel, unsigned int channelCount) :
393 52 ChannelOutput(startChannel, channelCount) {
394
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 LogDebug(VB_CHANNELOUT, "BBShiftPanelOutput::BBShiftPanelOutput(%u, %u)\n",
395 startChannel, channelCount);
396 52 }
397
398 1 void BBShiftPanelOutput::StartingOutput() {
399 // StoppingOutput is also called when the channel output thread simply
400 // goes idle, so the stopping flag has to clear when output resumes
401 1 m_stopping = false;
402 1 BBShiftPanelManager::INSTANCE.startingOutput();
403 1 }
404
405 1 void BBShiftPanelManager::startingOutput() {
406
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::lock_guard<std::mutex> memberLock(m_memberLock);
407
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (m_activeMembers++) {
408 // the cape is already refreshing for another matrix
409 ✗ return;
410 }
411 // Restart the refresh before the init registers go out below: the PRU
412 // has to be running to see them at all, and it only reads commands at a
413 // frame boundary, so if it is still working through a frame it needs the
414 // pump running to get there.
415
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 UnparkPRUs();
416
4/10
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 if (!isPWMPanel() && m_panelType && pruData) {
417 // FM6126A style panels need their configuration registers clocked
418 // out before they will display anything; the registers can be lost
419 // if the panels lose power, so resend them whenever output starts
420 ✗ sendPanelInitPackets();
421 }
422
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 }
423
424 // must match PANEL_INIT_OFFSET / ADDR_CONFIG_OFFSET in BBShiftPanel.asm
425 #define PANEL_INIT_OFFSET 0x1E00
426 #define ADDR_CONFIG_OFFSET 0x1DF8
427
428 ✗ void BBShiftPanelManager::sendPanelInitPackets() {
429 // panel configuration register writes, from the rpi-rgb-led-matrix
430 // project (lib/framebuffer.cc InitFM6126/InitFM6127): a register write
431 // is the pattern repeated across the whole chain width on every data
432 // line with LATCH held through the last (leCount-1) column clocks.
433 // Pattern bit i is the data value for column i mod 16.
434 auto encode = [](const char* p) {
435 uint16_t v = 0;
436 for (int i = 0; i < 16; i++) {
437 if (p[i] == '1') {
438 v |= 1 << i;
439 }
440 }
441 return v;
442 };
443 struct InitReg {
444 uint16_t leCount;
445 uint16_t pattern;
446 };
447 ✗ std::vector<InitReg> regs;
448 ✗ if (m_panelType == PANEL_TYPE_FM6126A) {
449 // FM6126A: config register 1 (full brightness), register 2 (panel on)
450 ✗ regs.push_back({ 12, encode("0111111111111111") });
451 ✗ regs.push_back({ 13, encode("0000000001000000") });
452 ✗ } else if (m_panelType == PANEL_TYPE_FM6127) {
453 // FM6127: registers 1-3
454 ✗ regs.push_back({ 12, encode("1111111111001110") });
455 ✗ regs.push_back({ 13, encode("1110000001100010") });
456 ✗ regs.push_back({ 11, encode("0101111100000000") });
457 } else {
458 ✗ return;
459 }
460 // the PRU data RAM cannot take a plain memcpy (uncacheable segment,
461 // SIGBUS on aarch64) - memcpyToPRU handles it
462 ✗ uint32_t buf[16] = { 0 };
463 ✗ buf[0] = (uint32_t)regs.size() | (((uint32_t)rowLen) << 16);
464 ✗ for (size_t i = 0; i < regs.size(); i++) {
465 ✗ buf[i + 1] = (uint32_t)regs[i].leCount | (((uint32_t)regs[i].pattern) << 16);
466 }
467 ✗ pru->memcpyToPRU(pru->data_ram + PANEL_INIT_OFFSET, (uint8_t*)buf, sizeof(buf));
468 ✗ __sync_synchronize();
469 ✗ pruData->command = 0xFFF0;
470 ✗ int cnt = 0;
471 ✗ while (pruData->command == 0xFFF0 && cnt < 250) {
472 ✗ std::this_thread::sleep_for(std::chrono::milliseconds(1));
473 ✗ cnt++;
474 }
475 ✗ if (pruData->command == 0xFFF0) {
476 ✗ LogWarn(VB_CHANNELOUT, "BBShiftPanel: panel init registers were not sent (PRU did not respond)\n");
477 } else {
478 ✗ LogDebug(VB_CHANNELOUT, "BBShiftPanel: sent %zu panel init registers for panel type %d\n", regs.size(), m_panelType);
479 }
480 ✗ }
481
482 1 void BBShiftPanelOutput::StoppingOutput() {
483 // Called when the channel output thread stops and before the output is
484 // deleted on a config reload; the thread may still be inside
485 // PrepData/SendData, so refuse new work and wait for any in-flight call
486 // to drain so a teardown cannot race them.
487 1 m_stopping = true;
488
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 while (m_inFlight > 0) {
489 ✗ std::this_thread::sleep_for(std::chrono::milliseconds(1));
490 }
491 1 BBShiftPanelManager::INSTANCE.stoppingOutput();
492 1 }
493
494 1 void BBShiftPanelManager::stoppingOutput() {
495
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::lock_guard<std::mutex> memberLock(m_memberLock);
496
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 if (m_activeMembers > 0 && --m_activeMembers) {
497 // another matrix on this cape is still outputting
498 ✗ return;
499 }
500 // a partially arrived or prepped frame must not carry across the park, or
501 // the first frame after the restart would publish stale content
502 1 m_preppedThisFrame = 0;
503 1 m_framePrepped = false;
504
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 ParkPRUs();
505
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 }
506
507 // The channel output thread only stops once nothing wants to output at all
508 // (forceOutput() in channeloutputthread.cpp covers sequences, overlays,
509 // effects, testing and bridging), and a shift panel only holds an image for
510 // as long as the pump keeps re-shifting it - an idle panel is simply dark.
511 // Streaming that blank frame costs most of a core for as long as fppd runs,
512 // so park instead:
513 // - the pump stops on a frame boundary and clears the command word
514 // - the PRU reads that at the end of the frame it is in and parks in its
515 // command wait loop, which holds the display off; the OE PRU parks the
516 // same way once the brightness handshake stops
517 // - both cores are then halted, which also freezes their cycle counters
518 // That last part is not just about saving (already idle) PRU cycles: the
519 // counter saturates after 2^32 cycles (~17s) and the hardware clears CTR_EN
520 // when it does, which RESET_PRU_CLOCK cannot undo, and the OE PRU's
521 // GET_PRU_CLOCK spin loops would then never exit. A halted core cannot
522 // reach that state.
523 //
524 // PWM panels refresh from their own registers and their pump already idles
525 // between commands, so none of this applies to them.
526 1 void BBShiftPanelManager::ParkPRUs() {
527
5/10
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 if (isPWMPanel() || !pru || !pruData || m_prusPaused) {
528 ✗ return;
529 }
530 1 m_pumpPaused = true;
531 // The pump only acts on this at a frame boundary, so it has written
532 // whole frames only.
533 1 int cnt = 0;
534
6/8
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1 times.
3 while (m_pumpRunning && !m_pumpParked && cnt < 500) {
535
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::this_thread::sleep_for(std::chrono::milliseconds(1));
536 2 cnt++;
537 }
538 // Then the frame still in flight has to drain, so the PRU ends up on the
539 // same frame boundary the pump stopped on and the two stay aligned in
540 // the byte stream. The ring is far smaller than a frame, so this is at
541 // most that frame's tail.
542
5/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
2 for (cnt = 0; !m_ring.drained() && cnt < 500; cnt++) {
543
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::this_thread::sleep_for(std::chrono::milliseconds(1));
544 }
545
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 if (!m_pumpParked || !m_ring.drained()) {
546 // never leave the cores halted somewhere unknown - a frozen mid-scan
547 // output would hold one row lit
548 ✗ LogWarn(VB_CHANNELOUT, "BBShiftPanel: could not park (pumpParked=%d drained=%d), leaving the refresh running\n",
549 (int)m_pumpParked, (int)m_ring.drained());
550 ✗ m_pumpPaused = false;
551 ✗ return;
552 }
553 // The OE PRU only leaves its wait loop when this PRU hands it a
554 // brightness, so with this PRU parked one more display period is enough
555 // for both to be sitting in their command wait loops with the display
556 // off. Halting anywhere else would freeze the pins mid-scan.
557
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::this_thread::sleep_for(std::chrono::milliseconds(10));
558 1 pru->pause();
559
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (pwmPru) {
560 ✗ pwmPru->pause();
561 }
562 1 m_prusPaused = true;
563 }
564
565 3 void BBShiftPanelManager::UnparkPRUs() {
566
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (m_prusPaused) {
567 // the OE PRU first, so it is already waiting by the time the panel
568 // PRU starts handing it brightnesses
569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (pwmPru) {
570 ✗ pwmPru->resume();
571 }
572
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (pru) {
573 1 pru->resume();
574 }
575 1 m_prusPaused = false;
576 }
577 3 m_pumpPaused = false;
578 3 }
579
580 208 BBShiftPanelOutput::~BBShiftPanelOutput() {
581 104 LogDebug(VB_CHANNELOUT, "BBShiftPanelOutput::~BBShiftPanelOutput()\n");
582
583 104 m_stopping = true;
584
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
104 while (m_inFlight > 0) {
585 ✗ std::this_thread::sleep_for(std::chrono::milliseconds(1));
586 }
587 // covers an Init that failed after the manager accepted this matrix, where
588 // the output is deleted without Close() ever being called
589 104 releaseToManager();
590 104 BBShiftPanelManager::INSTANCE.removeMember(this);
591
592
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 51 times.
104 if (m_matrix)
593
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
2 delete m_matrix;
594
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 51 times.
104 if (m_panelMatrix)
595
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
2 delete m_panelMatrix;
596 208 }
597
598 52 void BBShiftPanelManager::removeMember(BBShiftPanelOutput* m) {
599
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 std::lock_guard<std::mutex> memberLock(m_memberLock);
600
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 auto it = std::find(m_members.begin(), m_members.end(), m);
601
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 1 times.
52 if (it == m_members.end()) {
602 // never registered (its Init failed)
603 51 return;
604 }
605
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 m_members.erase(it);
606 1 m_preppedThisFrame = 0;
607 1 m_framePrepped = false;
608 // The remaining members keep the geometry they were mapped against. It
609 // stays valid - a rowLen longer than the remaining chains need just shifts
610 // a few extra pixels off the end of those chains - and rebuilding here
611 // would restart the PRU in the middle of a shutdown.
612
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (m_members.empty()) {
613
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 teardown();
614 }
615
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 51 times.
52 }
616
617 // Stops the hardware but keeps the parsed configuration, so a rebuild can
618 // bring it back up with new geometry.
619 2 void BBShiftPanelManager::teardownHardware() {
620 2 stopBackgroundThreads();
621 2 StopPRU();
622 2 m_pwmPruStalled = false;
623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!m_pwmStallWarning.empty()) {
624 ✗ WarningHolder::RemoveWarning(m_pwmStallWarning);
625 ✗ m_pwmStallWarning.clear();
626 }
627
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (m_heapBuffers) {
628
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (auto& b : outputBuffers) {
629
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (b) {
630 4 free(b);
631 4 b = nullptr;
632 }
633 }
634 1 m_heapBuffers = false;
635 }
636 2 m_frontBuffer = nullptr;
637 2 currOutputBuffer = 0;
638 2 }
639
640 1 void BBShiftPanelManager::teardown() {
641 1 teardownHardware();
642
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (currentChannelData) {
643
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 delete[] currentChannelData;
644 1 currentChannelData = nullptr;
645 }
646
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!m_refreshWarning.empty()) {
647 ✗ WarningHolder::RemoveWarning(m_refreshWarning);
648 ✗ m_refreshWarning.clear();
649 }
650 1 m_activeMembers = 0;
651 1 m_openMembers = 0;
652 1 }
653
654 1265 bool BBShiftPanelManager::isPWMPanel() const {
655 1265 return m_addressingMode >= ADDRESSING_MODE_FM6353C;
656 }
657
658 52 int BBShiftPanelOutput::Init(Json::Value config) {
659
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 LogDebug(VB_CHANNELOUT, "BBShiftPanelOutput::Init(JSON)\n");
660
661
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 Json::Value root;
662
2/4
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 52 times.
✗ Branch 3 not taken.
52 std::string subType = config["configName"].asString();
663
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 int outputs = CapeUtils::INSTANCE.getLicensedOutputs();
664
3/4
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
✓ Branch 3 taken 1 times.
52 if (!CapeUtils::INSTANCE.getPanelConfig(subType, root)) {
665
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
51 LogErr(VB_CHANNELOUT, "Could not read panel pin configuration for %s\n", subType.c_str());
666 51 return 0;
667 }
668
669 // Everything below here is this matrix's own: its pixel mapping, its
670 // channel range, its color handling. The hardware the frame goes out
671 // on is shared with any other matrix on the same cape and is set up by
672 // the manager in addMember() below.
673
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 m_invertedData = config["invertedData"].asInt();
674
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 m_colorOrder = ColorOrderFromString(config["colorOrder"].asString());
675
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (config.isMember("brightness")) {
676
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 m_brightness = config["brightness"].asInt();
677 }
678
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (m_brightness < 1 || m_brightness > 10) {
679 1 m_brightness = 10;
680 }
681 1 m_gamma = 2.2;
682
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (config.isMember("gamma")) {
683
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 m_gamma = atof(config["gamma"].asString().c_str());
684 }
685
686
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 int panelWidth = config["panelWidth"].asInt();
687
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 int panelHeight = config["panelHeight"].asInt();
688
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!panelWidth) {
689 ✗ panelWidth = 32;
690 }
691
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!panelHeight) {
692 ✗ panelHeight = 16;
693 }
694
695
3/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
1 m_panelMatrix = new PanelMatrix(panelWidth, panelHeight, m_invertedData);
696
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!m_panelMatrix) {
697 ✗ LogErr(VB_CHANNELOUT, "BBShiftPanelOutput: Unable to create PanelMatrix\n");
698 ✗ return 0;
699 }
700
4/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
2 for (int i = 0; i < config["panels"].size(); i++) {
701
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 Json::Value p = config["panels"][i];
702
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 if (p["outputNumber"].asInt() <= outputs) {
703 1 char orientation = 'N';
704
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 std::string o = p["orientation"].asString();
705
706
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!o.empty()) {
707 1 orientation = o[0];
708 }
709
710
4/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
1 if (p["colorOrder"].asString() == "") {
711
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 p["colorOrder"] = ColorOrderToString(m_colorOrder);
712 }
713
714 // a refused panel (out of range) must not count toward the outputs or chains below
715
4/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
2 if (!m_panelMatrix->AddPanel(p["outputNumber"].asInt(),
716
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 p["panelNumber"].asInt(),
717 orientation,
718
4/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
1 p["xOffset"].asInt(), p["yOffset"].asInt(),
719
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
2 ColorOrderFromString(p["colorOrder"].asString()))) {
720 ✗ continue;
721 }
722
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 if (p["panelNumber"].asInt() > m_longestChain) {
723 ✗ m_longestChain = p["panelNumber"].asInt();
724 }
725
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 }
726
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 }
727 1 m_longestChain++;
728
729 // get the dimensions of the matrix
730 1 m_panels = m_panelMatrix->PanelCount();
731 1 m_width = m_panelMatrix->Width();
732 1 m_height = m_panelMatrix->Height();
733
734 1 m_channelCount = m_width * m_height * 3;
735
736
3/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
1 m_matrix = new Matrix(m_startChannel, m_width, m_height);
737
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (config.isMember("subMatrices")) {
738 ✗ for (int i = 0; i < config["subMatrices"].size(); i++) {
739 ✗ Json::Value sm = config["subMatrices"][i];
740
741 ✗ m_matrix->AddSubMatrix(
742 ✗ sm["enabled"].asInt(),
743 ✗ sm["startChannel"].asInt() - 1,
744 ✗ sm["width"].asInt(),
745 ✗ sm["height"].asInt(),
746 ✗ sm["xOffset"].asInt(),
747 ✗ sm["yOffset"].asInt());
748 ✗ }
749 }
750
751 // Claims the outputs this matrix drives on the shared cape and brings the
752 // PRU up (or rebuilds it around the new geometry if another matrix is
753 // already running). This is what builds the gamma table and the scatter
754 // map, since both depend on the shared frame layout.
755
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!BBShiftPanelManager::INSTANCE.addMember(this, config, root)) {
756 ✗ return 0;
757 }
758 1 m_registered = true;
759
760
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (PixelOverlayManager::INSTANCE.isAutoCreatePixelOverlayModels()) {
761
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::string dd = "LED Panels";
762
4/16
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
1 if (config.isMember("LEDPanelMatrixName") && !config["LEDPanelMatrixName"].asString().empty()) {
763 ✗ dd = config["LEDPanelMatrixName"].asString();
764 }
765
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (config.isMember("description")) {
766 ✗ dd = config["description"].asString();
767 }
768
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::string desc = dd;
769 1 int count = 0;
770
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 while (PixelOverlayManager::INSTANCE.getModel(desc) != nullptr) {
771 ✗ count++;
772 ✗ desc = dd + "-" + std::to_string(count);
773 }
774
4/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
7 PixelOverlayManager::INSTANCE.addAutoOverlayModel(desc,
775 m_startChannel, m_channelCount, 3,
776
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 "H", m_invertedData ? "BL" : "TL",
777 1 m_height, 1);
778
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 m_autoCreatedModelName = desc;
779 1 }
780
781
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 return ChannelOutput::Init(config);
782 52 }
783
784 8 bool BBShiftPanelOutput::usesOutput(int o) const {
785
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
16 return m_panelMatrix && o >= 0 && o < MAX_MATRIX_OUTPUTS &&
786
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
16 !m_panelMatrix->m_outputPanels[o].empty();
787 }
788
789 // The settings below describe how the PRU shifts a frame out, so they belong
790 // to the cape rather than to any one matrix on it. The first matrix to
791 // register fixes them; checkCompatible() holds later ones to the same values.
792 1 BBShiftPanelManager::PanelParams BBShiftPanelManager::parsePanelParams(const Json::Value& config, const Json::Value& capeConfig) {
793 1 PanelParams p;
794
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 p.panelWidth = config["panelWidth"].asInt();
795
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 p.panelHeight = config["panelHeight"].asInt();
796
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!p.panelWidth) {
797 ✗ p.panelWidth = 32;
798 }
799
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!p.panelHeight) {
800 ✗ p.panelHeight = 16;
801 }
802
803
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 p.addressingMode = config["panelRowAddressType"].asInt();
804
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 p.panelType = config["panelType"].asInt();
805 // For PWM panel types the addressing dropdown selects how the GCLK
806 // program drives the row lines: Direct Row Select = binary row number
807 // on SEL0-4, anything else = the DP32020A style row shift register
808 1 p.pwmDirectRow = (p.addressingMode == ADDRESSING_MODE_DIRECT);
809 // ...and for the FM6373 family the reverse, because direct row select is
810 // what that family has always done: only an explicit ABC Shift selection
811 // switches it to the token shift register (GH #2955).
812 1 p.pwmShiftRow = (p.addressingMode == ADDRESSING_MODE_ABC_SHIFT);
813
814 // OE timing for the FM6373 family, in the reference implementation's units
815 // (DCLK periods) so its published values can be used directly. 0/absent
816 // keeps the default.
817
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (config.isMember("panelOEClkLength")) {
818 ✗ p.oeClkLength = config["panelOEClkLength"].asInt();
819 }
820
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (config.isMember("panelOEFirstClkLength")) {
821 ✗ p.oeFirstClkLength = config["panelOEFirstClkLength"].asInt();
822 }
823
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (config.isMember("panelOERowPeriodUs")) {
824 ✗ p.oeRowPeriodUs = config["panelOERowPeriodUs"].asInt();
825 }
826
827 // FM6373 family register upload grammar. A spacer of 0 is meaningful, so
828 // these carry their historical values as defaults rather than using 0 as
829 // an "unset" marker the way the OE knobs do.
830
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (config.isMember("panelRegVsyncClocks")) {
831 ✗ p.regVsyncClocks = config["panelRegVsyncClocks"].asInt();
832 }
833
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (config.isMember("panelRegMidClocks")) {
834 ✗ p.regMidClocks = config["panelRegMidClocks"].asInt();
835 }
836
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (config.isMember("panelRegPreClocks")) {
837 ✗ p.regPreClocks = config["panelRegPreClocks"].asInt();
838 }
839
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (config.isMember("panelRegSpacerClocks")) {
840 ✗ p.regSpacerClocks = config["panelRegSpacerClocks"].asInt();
841 }
842 // a burst of 0 clocks would hang the firmware's loop; only the middle
843 // burst may be skipped, and only the spacer may legitimately be 0
844
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (p.regVsyncClocks < 1 || p.regVsyncClocks > 255) {
845 ✗ p.regVsyncClocks = 3;
846 }
847
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (p.regMidClocks < 0 || p.regMidClocks > 255) {
848 ✗ p.regMidClocks = 11;
849 }
850
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (p.regPreClocks < 1 || p.regPreClocks > 255) {
851 ✗ p.regPreClocks = 14;
852 }
853
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (p.regSpacerClocks < 0 || p.regSpacerClocks > 255) {
854 ✗ p.regSpacerClocks = 8;
855 }
856 // 0 means "unset", and unset must reproduce exactly what this emitted
857 // before these knobs existed - a 600ns pulse and a 2us opener - rather
858 // than the reference's defaults, which convert to slightly different
859 // values and would quietly retime every existing panel of this family.
860
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (p.oeClkLength < 0 || p.oeClkLength > 255) {
861 ✗ p.oeClkLength = 0;
862 }
863
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (p.oeFirstClkLength < 0 || p.oeFirstClkLength > 255) {
864 ✗ p.oeFirstClkLength = 0;
865 }
866
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (p.oeRowPeriodUs < 1 || p.oeRowPeriodUs > 255) {
867 ✗ p.oeRowPeriodUs = 20;
868 }
869
870 // FM6353 / ICND2153 GCLK pulses per row packet. 0/absent keeps the
871 // built-in 138; the firmware's pulse loop never sees a 0 because of that.
872
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (config.isMember("panelGCLKFirstPulses")) {
873 ✗ p.gclkFirstPulses = config["panelGCLKFirstPulses"].asInt();
874 }
875
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (config.isMember("panelGCLKPulses")) {
876 ✗ p.gclkPulses = config["panelGCLKPulses"].asInt();
877 }
878
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (p.gclkFirstPulses < 0 || p.gclkFirstPulses > 255) {
879 ✗ p.gclkFirstPulses = 0;
880 }
881
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (p.gclkPulses < 0 || p.gclkPulses > 255) {
882 ✗ p.gclkPulses = 0;
883 }
884
885 // An imported PWM register profile (see www/co-ledPanels.php). The UI
886 // resolves a catalog entry down to its three word lists and stores them
887 // here, so the config is self contained and survives a backup/restore
888 // without the file it came from. Anything malformed is dropped with a
889 // warning rather than half applied - the built-in table is a working
890 // fallback, a truncated upload is not.
891
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (config.isMember("panelRegisters")) {
892 ✗ const Json::Value& pr = config["panelRegisters"];
893 static const char* KEYS[3] = { "r", "g", "b" };
894 ✗ std::vector<uint16_t> words[3];
895 ✗ bool ok = true;
896 ✗ for (int c = 0; c < 3 && ok; c++) {
897 ✗ if (!pr.isMember(KEYS[c]) || !pr[KEYS[c]].isArray() || pr[KEYS[c]].empty()) {
898 ✗ ok = false;
899 ✗ break;
900 }
901 ✗ for (const auto& w : pr[KEYS[c]]) {
902 // accept both the catalog's bare hex ("025f") and a number
903 ✗ unsigned long v = w.isString() ? strtoul(w.asString().c_str(), nullptr, 16)
904 ✗ : (unsigned long)w.asUInt();
905 ✗ if (v > 0xFFFF) {
906 ✗ ok = false;
907 ✗ break;
908 }
909 ✗ words[c].push_back((uint16_t)v);
910 }
911 }
912 ✗ if (ok && (words[0].size() != words[1].size() || words[0].size() != words[2].size())) {
913 ✗ ok = false;
914 }
915 ✗ if (ok) {
916 ✗ for (int c = 0; c < 3; c++) {
917 ✗ p.regProfile[c] = std::move(words[c]);
918 }
919 ✗ p.regProfileName = pr.isMember("profile") ? pr["profile"].asString() : "imported";
920 } else {
921 ✗ LogWarn(VB_CHANNELOUT, "BBShiftPanel: ignoring malformed panelRegisters profile; using the built-in table\n");
922 }
923 ✗ }
924
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (p.panelType == PANEL_TYPE_FM6363C) {
925 // the UI moved FM6363C from the addressing dropdown to the panel
926 // type dropdown; internally it stays the PWM addressing mode (old
927 // configs with panelRowAddressType == 51 keep working unchanged)
928 ✗ p.addressingMode = ADDRESSING_MODE_FM6363C;
929 ✗ p.panelType = 0;
930
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } else if (p.panelType == PANEL_TYPE_FM6353) {
931 ✗ p.addressingMode = ADDRESSING_MODE_FM6353C;
932 ✗ p.panelType = 0;
933
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } else if (p.panelType == PANEL_TYPE_FM6373) {
934 ✗ p.addressingMode = ADDRESSING_MODE_FM6373;
935 ✗ p.panelType = 0;
936
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } else if (p.panelType == PANEL_TYPE_DP3364) {
937 ✗ p.addressingMode = ADDRESSING_MODE_DP3364;
938 ✗ p.panelType = 0;
939
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } else if (p.panelType == PANEL_TYPE_ICND1065L) {
940 ✗ p.addressingMode = ADDRESSING_MODE_ICND1065L;
941 ✗ p.panelType = 0;
942
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } else if (p.panelType == PANEL_TYPE_SM16380SH) {
943 ✗ p.addressingMode = ADDRESSING_MODE_SM16380SH;
944 ✗ p.panelType = 0;
945
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } else if (p.panelType == PANEL_TYPE_ICND2153) {
946 ✗ p.addressingMode = ADDRESSING_MODE_ICND2153;
947 ✗ p.panelType = 0;
948 }
949 1 bool pwm = p.addressingMode >= ADDRESSING_MODE_FM6353C;
950
951 // A combo cape shares the PRUSS with a string driver on the other PRU:
952 // panels run single-PRU with the smaller split shared-memory ring and
953 // must not touch anything the other driver owns. (The flag is also
954 // honored from the output config for bench testing.)
955
6/12
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 times.
1 p.sharedPRUSS = capeConfig["sharedPRUSS"].asBool() || config["sharedPRUSS"].asBool();
956
957 1 p.colorDepth = 12;
958
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (config.isMember("panelColorDepth")) {
959
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 p.colorDepth = config["panelColorDepth"].asInt();
960 }
961 1 p.outputByRow = false;
962 1 p.outputBlankData = false;
963
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (config.isMember("panelOutputOrder")) {
964
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 p.outputByRow = config["panelOutputOrder"].asBool();
965 }
966
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (config.isMember("panelOutputBlankRow")) {
967
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 p.outputBlankData = config["panelOutputBlankRow"].asBool();
968 }
969
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (p.colorDepth < 0) {
970 ✗ p.colorDepth = -p.colorDepth;
971 ✗ p.outputBlankData = true;
972 }
973
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (p.colorDepth > 12 || p.colorDepth < 6) {
974 ✗ p.colorDepth = 8;
975 }
976
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (pwm) {
977 ✗ p.colorDepth = 16;
978 }
979
980
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 p.panelInterleave = "";
981
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (config.isMember("panelInterleave")) {
982
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 p.panelInterleave = config["panelInterleave"].asString();
983 }
984
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 p.panelScan = config["panelScan"].asInt();
985
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (p.panelScan == 0) {
986 // default scan is 1/2 the height of the panel
987 ✗ p.panelScan = p.panelHeight / 2;
988 }
989
990 // Full height data layouts. Only meaningful when every row has its own
991 // scan address, because that is what frees the second RGB lane to carry
992 // the other half of the same row instead of the other half of the panel.
993
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 p.dataLayout = config["panelDataLayout"].asInt();
994
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (p.dataLayout) {
995 ✗ if (p.panelScan != p.panelHeight) {
996 ✗ LogErr(VB_CHANNELOUT, "BBShiftPanel: full height data layout needs scan (%d) to equal panel height (%d); using the standard layout\n",
997 p.panelScan, p.panelHeight);
998 ✗ AddOutputConfigWarning(UNKNOWN_WARNING_ID, "LED panel data layout requires the scan rate to equal the panel height");
999 ✗ p.dataLayout = 0;
1000 ✗ } else if (p.panelWidth & 1) {
1001 ✗ LogErr(VB_CHANNELOUT, "BBShiftPanel: full height data layout needs an even panel width (%d); using the standard layout\n",
1002 p.panelWidth);
1003 ✗ p.dataLayout = 0;
1004 ✗ } else if (!p.panelInterleave.empty()) {
1005 // Interleave means nothing when every row has its own address.
1006 // The UI hides the dropdown for these panels but still saves
1007 // whatever it held - "0" (Off), or "8" left over from a smaller
1008 // scan - and falling back to the standard layout instead could
1009 // never work: it needs a handler, and none exists for scan ==
1010 // height.
1011 ✗ LogInfo(VB_CHANNELOUT, "BBShiftPanel: ignoring panel interleave '%s' for a full height data layout\n",
1012 p.panelInterleave.c_str());
1013 ✗ p.panelInterleave = "";
1014 }
1015 }
1016 1 return p;
1017 ✗ }
1018
1019 1 bool BBShiftPanelManager::parseCapeConfig(const Json::Value& root) {
1020 // Mux only the pins the cape actually uses: the named control pins plus
1021 // the data pins its outputs reference. On the AM62x every PRU1 pin can
1022 // alternatively be muxed to PRU0, so a cape that lists fewer outputs
1023 // leaves the unreferenced data pins available for another driver (e.g.
1024 // BBShiftString on the other PRU) - the PRU still writes all 8 data
1025 // bits of r30, writes to unmuxed bits just never reach a pin. Capes
1026 // without a controls section get the full historical pin set.
1027
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 if (root.isMember("controls") && root["controls"].size()) {
1028 1 std::set<std::string> pinNames;
1029
4/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 1 times.
12 for (auto const& name : root["controls"].getMemberNames()) {
1030
5/10
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 11 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 11 times.
✗ Branch 9 not taken.
11 pinNames.insert(root["controls"][name]["pin"].asString());
1031 1 }
1032
4/6
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 1 times.
9 for (int i = 0; i < (int)root["outputs"].size(); i++) {
1033
5/10
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 8 times.
✗ Branch 9 not taken.
8 pinNames.insert(root["outputs"][i]["pin"].asString());
1034 }
1035
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 m_configuredPins.assign(pinNames.begin(), pinNames.end());
1036 1 } else {
1037 ✗ m_configuredPins = PRU_PINS;
1038 }
1039
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1 times.
20 for (auto& pinName : m_configuredPins) {
1040
4/8
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 19 times.
✗ Branch 7 not taken.
95 PinCapabilities::getPinByName(pinName).configPin("pru1out", true);
1041 }
1042
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (root["controls"].isMember("oe")) {
1043
4/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
1 m_oePin = root["controls"]["oe"]["pin"].asString();
1044 }
1045 1 m_numOutputs = root["outputs"].size();
1046
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (m_numOutputs > MAX_OUTPUTS) {
1047 ✗ m_numOutputs = MAX_OUTPUTS;
1048 }
1049 1 m_numOutputSlots = 8;
1050
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 for (int i = 0; i < m_numOutputs; i++) {
1051
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 Json::Value s = root["outputs"][i];
1052
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 std::string pinName = s["pin"].asString();
1053
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 const BBBPinCapabilities* pin = (const BBBPinCapabilities*)(PinCapabilities::getPinByName(pinName).ptr());
1054
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (pin == nullptr) {
1055 // a pin name the cape's config gives that this board lacks
1056 ✗ LogErr(VB_CHANNELOUT, "BBShiftPanel: output %d uses unknown pin '%s'\n", i + 1, pinName.c_str());
1057 ✗ return false;
1058 }
1059 8 outputPin[i] = pin->pruPin(1);
1060 8 outputBank[i] = 0;
1061
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 if (s.isMember("index")) {
1062
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 outputBank[i] = s["index"].asInt();
1063
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (outputBank[i] > 0) {
1064 ✗ m_numOutputSlots = 16;
1065 }
1066 }
1067
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 }
1068
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (root.isMember("singlePRU")) {
1069 1 singlePRU = root["singlePRU"].asBool();
1070 }
1071 // singlePRU = true;
1072
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!singlePRU) {
1073 // if not using a single PRU, then we need to change the OE pin to the other PRU
1074 ✗ const PinCapabilities& pin = PinCapabilities::getPinByName(m_oePin);
1075 ✗ pin.configPin("pru0out", true);
1076 }
1077 1 return true;
1078 }
1079
1080 1 bool BBShiftPanelManager::adoptPanelParams(const PanelParams& p) {
1081 1 m_panelWidth = p.panelWidth;
1082 1 m_panelHeight = p.panelHeight;
1083 1 m_panelScan = p.panelScan;
1084 1 m_panelInterleave = p.panelInterleave;
1085 1 m_addressingMode = p.addressingMode;
1086 1 m_panelType = p.panelType;
1087 1 m_dataLayout = p.dataLayout;
1088 1 m_pwmDirectRow = p.pwmDirectRow;
1089 1 m_pwmShiftRow = p.pwmShiftRow;
1090 1 m_oeClkLength = p.oeClkLength;
1091 1 m_oeFirstClkLength = p.oeFirstClkLength;
1092 1 m_oeRowPeriodUs = p.oeRowPeriodUs;
1093 1 m_regVsyncClocks = p.regVsyncClocks;
1094 1 m_regMidClocks = p.regMidClocks;
1095 1 m_regPreClocks = p.regPreClocks;
1096 1 m_regSpacerClocks = p.regSpacerClocks;
1097 1 m_gclkFirstPulses = p.gclkFirstPulses;
1098 1 m_gclkPulses = p.gclkPulses;
1099 1 buildRegisterProfile(p);
1100 1 m_colorDepth = p.colorDepth;
1101 1 m_outputByRow = p.outputByRow;
1102 1 m_outputBlankData = p.outputBlankData;
1103 1 m_sharedPRUSS = p.sharedPRUSS;
1104
1105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (m_sharedPRUSS) {
1106 ✗ if (isPWMPanel()) {
1107 ✗ LogErr(VB_CHANNELOUT, "PWM panel types require both PRUs and cannot be used on a shared panels+strings cape\n");
1108 ✗ AddOutputConfigWarning(UNKNOWN_WARNING_ID, "PWM panel types cannot be used on a shared panels+strings cape");
1109 ✗ return false;
1110 }
1111 ✗ singlePRU = true;
1112 }
1113
1114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (isPWMPanel()) {
1115 ✗ for (auto& pinName : PRU0_PWM_PINS) {
1116 ✗ const PinCapabilities& pin = PinCapabilities::getPinByName(pinName);
1117 ✗ pin.configPin("pru0out", true);
1118 }
1119 }
1120 1 return true;
1121 }
1122
1123 // A second matrix on the cape shares one frame, one stride schedule and one
1124 // set of PRU firmware, so anything that shapes those has to be identical. The
1125 // per-matrix settings (gamma, color order, start corner, brightness, layout)
1126 // are deliberately not in here.
1127 ✗ bool BBShiftPanelManager::checkCompatible(const PanelParams& p) const {
1128 ✗ const char* bad = nullptr;
1129 ✗ if (p.panelWidth != m_panelWidth || p.panelHeight != m_panelHeight) {
1130 ✗ bad = "panel size";
1131 ✗ } else if (p.panelScan != m_panelScan) {
1132 ✗ bad = "panel scan";
1133 ✗ } else if (p.panelInterleave != m_panelInterleave) {
1134 ✗ bad = "panel interleave";
1135 ✗ } else if (p.addressingMode != m_addressingMode || p.panelType != m_panelType) {
1136 ✗ bad = "panel type / row addressing";
1137 ✗ } else if (p.dataLayout != m_dataLayout) {
1138 ✗ bad = "panel data layout";
1139 ✗ } else if (p.colorDepth != m_colorDepth) {
1140 ✗ bad = "color depth";
1141 ✗ } else if (p.outputByRow != m_outputByRow || p.outputBlankData != m_outputBlankData) {
1142 ✗ bad = "output order / blank row";
1143 }
1144 ✗ if (bad) {
1145 ✗ std::string w = std::string("All LED panel matrices on one cape must use the same ") + bad +
1146 ✗ "; this matrix was not started.";
1147 ✗ LogErr(VB_CHANNELOUT, "BBShiftPanel: %s\n", w.c_str());
1148 ✗ AddOutputConfigWarning(UNKNOWN_WARNING_ID, w);
1149 ✗ return false;
1150 ✗ }
1151 ✗ return true;
1152 }
1153
1154 1 bool BBShiftPanelManager::addMember(BBShiftPanelOutput* m, const Json::Value& config, const Json::Value& capeConfig) {
1155
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::lock_guard<std::mutex> memberLock(m_memberLock);
1156
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 PanelParams p = parsePanelParams(config, capeConfig);
1157
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (m_members.empty()) {
1158
5/10
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
1 if (!parseCapeConfig(capeConfig) || !adoptPanelParams(p)) {
1159 ✗ return false;
1160 }
1161 ✗ } else if (!checkCompatible(p)) {
1162 ✗ return false;
1163 }
1164
1165 // Each output is one set of byte lanes in the shared frame, so two
1166 // matrices cannot drive the same one - they would each be writing the
1167 // other's pixels.
1168
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 for (int o = 0; o < m_numOutputs; o++) {
1169
3/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 1 times.
8 if (!m->usesOutput(o)) {
1170 7 continue;
1171 }
1172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 for (auto* other : m_members) {
1173 ✗ if (other->usesOutput(o)) {
1174 ✗ std::string w = "Two LED panel matrices are both configured to use cape output " +
1175 ✗ std::to_string(o + 1) + "; the second matrix was not started.";
1176 ✗ LogErr(VB_CHANNELOUT, "BBShiftPanel: %s\n", w.c_str());
1177 ✗ AddOutputConfigWarning(UNKNOWN_WARNING_ID, w);
1178 ✗ return false;
1179 ✗ }
1180 }
1181 }
1182
1183
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 m_members.push_back(m);
1184 1 ++m_openMembers;
1185
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!rebuild()) {
1186 ✗ m_members.pop_back();
1187 ✗ --m_openMembers;
1188 ✗ if (m_members.empty()) {
1189 // nothing is left holding the hardware, so do not leave the
1190 // half-built frame buffers allocated
1191 ✗ teardown();
1192 }
1193 ✗ return false;
1194 }
1195 1 return true;
1196 1 }
1197
1198 // The frame is as wide as the longest chain on the cape and as tall as one
1199 // panel's scan requires; every member maps into that same shape.
1200 1 void BBShiftPanelManager::computeGeometry() {
1201 1 numRows = 0;
1202 1 maxRowLen = 0;
1203 // cleared up front so a member set with no panels leaves a zero sized
1204 // frame rather than the previous rebuild's geometry
1205 1 rowLen = 0;
1206
1207 1 bool anyPanels = false;
1208 1 m_longestChain = 0;
1209 1 m_brightness = 1;
1210
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (auto* m : m_members) {
1211
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (m->m_panels > 0) {
1212 1 anyPanels = true;
1213 }
1214
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (m->longestChain() > m_longestChain) {
1215 1 m_longestChain = m->longestChain();
1216 }
1217 // the OE on-time is shared, so the cape runs at the brightest setting
1218 // any matrix asked for and the others scale their gamma down to it
1219
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (m->configuredBrightness() > m_brightness) {
1220 1 m_brightness = m->configuredBrightness();
1221 }
1222 }
1223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!anyPanels) {
1224 ✗ return;
1225 }
1226
1227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (m_dataLayout) {
1228 // Full height: every row has its own scan address, and the two RGB
1229 // lanes split that row down the middle rather than splitting the
1230 // panel top from bottom. Interleave is rejected in parsePanelParams
1231 // for this layout, so there is no handler to consult.
1232 ✗ numRows = m_panelHeight;
1233 ✗ maxRowLen = m_panelWidth / 2;
1234 ✗ rowLen = maxRowLen * m_longestChain;
1235 ✗ return;
1236 }
1237
1238 1 PanelInterleaveHandler* handler = PanelInterleaveHandler::createHandler(m_panelInterleave, m_panelWidth, m_panelHeight, m_panelScan);
1239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!handler) {
1240 ✗ LogErr(VB_CHANNELOUT, "Failed to create panel interleave handler\n");
1241 ✗ return;
1242 }
1243 // the mapping only depends on the position within a panel, so one pass
1244 // over a single panel gives the row count and row length for all of them
1245
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1 times.
17 for (int y = 0; y < (m_panelHeight / 2); y++) {
1246
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 16 times.
1040 for (int x = 0; x < m_panelWidth; ++x) {
1247 1024 int yOut = y;
1248 1024 int xOut = x;
1249 1024 handler->map(xOut, yOut);
1250
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1008 times.
1024 if (yOut >= (int)numRows) {
1251 16 numRows = yOut + 1;
1252 }
1253
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 960 times.
1024 if (xOut >= (int)maxRowLen) {
1254 64 maxRowLen = xOut + 1;
1255 }
1256 }
1257 }
1258
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 delete handler;
1259 1 rowLen = maxRowLen * m_longestChain;
1260 }
1261
1262 // Brings the shared hardware up around the current member set. Called for
1263 // every registration, so adding a matrix with a longer chain re-maps the ones
1264 // already registered onto the new frame width.
1265 1 bool BBShiftPanelManager::rebuild() {
1266 1 teardownHardware();
1267 // a frame part way through arriving was counted against the old member
1268 // set; the next frame starts over with the new one
1269 1 m_preppedThisFrame = 0;
1270 1 m_framePrepped = false;
1271 1 computeGeometry();
1272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (numRows > MAX_SCAN_ROWS) {
1273 // full height panels scan every row (64 for a 64 high panel); the
1274 // per-row tables and the firmware's brightness table stop there
1275 ✗ LogErr(VB_CHANNELOUT, "BBShiftPanel: %u scan rows, at most %u are supported\n", numRows, MAX_SCAN_ROWS);
1276 ✗ return false;
1277 }
1278
1279 1 uint32_t dataSize = intermediateSize();
1280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!dataSize) {
1281 ✗ LogErr(VB_CHANNELOUT, "BBShiftPanel: no panels configured, nothing to output\n");
1282 ✗ return false;
1283 }
1284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (currentChannelData) {
1285 ✗ delete[] currentChannelData;
1286 }
1287
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 currentChannelData = new uint16_t[dataSize];
1288 1 memset(currentChannelData, 0, dataSize * sizeof(uint16_t));
1289
1290
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (auto* m : m_members) {
1291
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 m->setupGamma();
1292
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!m->buildScatterMap()) {
1293 ✗ return false;
1294 }
1295 }
1296
1297
2/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1 pru = new BBBPru(1, true, true);
1298 1 pruData = (BBShiftPanelData*)pru->data_ram;
1299
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!isPWMPanel()) {
1300 1 buildStrideSchedule();
1301 }
1302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (StartPRU() == 0) {
1303 ✗ return false;
1304 }
1305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (isPWMPanel()) {
1306 // A PRU that doesn't take the registers won't take frames either, and
1307 // started anyway the output thread would wait on it every frame.
1308 ✗ if (!setupPWMRegisters()) {
1309 ✗ LogErr(VB_CHANNELOUT, "BBShiftPanel: the PRU did not take the PWM panel registers; output not started\n");
1310 ✗ AddOutputConfigWarning(UNKNOWN_WARNING_ID, "BBShiftPanel: the PRU did not respond to the PWM panel setup. May require a reboot.");
1311 ✗ return false;
1312 }
1313 } else {
1314 1 setupBrightnessValues();
1315 }
1316 1 startBackgroundThreads();
1317 1 return true;
1318 }
1319
1320 1 void BBShiftPanelManager::startBackgroundThreads() {
1321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (bgThreadsRunning) {
1322 ✗ return;
1323 }
1324 1 bgThreadsRunning = true;
1325
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (int i = 0; i < std::thread::hardware_concurrency(); i++) {
1326
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 std::thread th(&BBShiftPanelManager::runBackgroundTasks, this);
1327
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 th.detach();
1328 4 }
1329 }
1330
1331 2 void BBShiftPanelManager::stopBackgroundThreads() {
1332
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (!bgThreadsRunning) {
1333 1 return;
1334 }
1335 1 bgThreadsRunning = false;
1336 1 bgTaskCondVar.notify_all();
1337
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 while (bgThreadCount > 0) {
1338
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 std::this_thread::sleep_for(std::chrono::milliseconds(5));
1339 }
1340 }
1341
1342 1 int BBShiftPanelManager::StartPRU() {
1343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!pru) {
1344 ✗ pru = new BBBPru(1, true, true);
1345 ✗ pruData = (BBShiftPanelData*)pru->data_ram;
1346 }
1347 1 bool started = true;
1348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (isPWMPanel()) {
1349 ✗ pwmPru = new BBBPru(0);
1350 ✗ started &= pwmPru->run("/opt/fpp/src/non-gpl/BBShiftPanel/BBShiftPanel_gclk.out");
1351 ✗ if (m_numOutputSlots == 16) {
1352 ✗ started &= pru->run("/opt/fpp/src/non-gpl/BBShiftPanel/BBShiftPanel_pwm_16.out");
1353 } else {
1354 ✗ started &= pru->run("/opt/fpp/src/non-gpl/BBShiftPanel/BBShiftPanel_pwm.out");
1355 }
1356 } else {
1357
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (singlePRU) {
1358 // on a shared cape the string driver owns the shared RAM half
1359 // and the other PRU - never clear them from here
1360
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (m_numOutputSlots == 16) {
1361 ✗ started &= pru->run("/opt/fpp/src/non-gpl/BBShiftPanel/BBShiftPanel_single_16.out", !m_sharedPRUSS);
1362 } else {
1363
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
3 started &= pru->run("/opt/fpp/src/non-gpl/BBShiftPanel/BBShiftPanel_single.out", !m_sharedPRUSS);
1364 }
1365 } else {
1366 // in the two-PRU shift configuration the OE PRU also prefetches
1367 // the ring data and hands it over through the scratchpad, which
1368 // shortens the main PRU's per-block load considerably
1369 ✗ if (m_numOutputSlots == 16) {
1370 ✗ started &= pru->run("/opt/fpp/src/non-gpl/BBShiftPanel/BBShiftPanel_prefetch_16.out");
1371 } else {
1372 ✗ started &= pru->run("/opt/fpp/src/non-gpl/BBShiftPanel/BBShiftPanel_prefetch.out");
1373 }
1374 ✗ pwmPru = new BBBPru(0);
1375 ✗ started &= pwmPru->run("/opt/fpp/src/non-gpl/BBShiftPanel/BBShiftPanel_oe_prefetch.out");
1376 }
1377 }
1378
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!started) {
1379 ✗ LogErr(VB_CHANNELOUT, "BBShiftPanel: Unable to start PRU(s). May require a reboot.\n");
1380 ✗ AddOutputConfigWarning(UNKNOWN_WARNING_ID, "BBShiftPanel: Unable to start PRU(s). May require a reboot.");
1381 ✗ return 0;
1382 }
1383 // The same data RAM word configures either firmware: the shift firmware
1384 // reads its row addressing config right after the ring config handshake,
1385 // the PWM firmware reads the chip family flag at each register upload.
1386 // It must land after run() (the firmware load clears the PRU memories)
1387 // and, for the shift firmware, before the ring attach below.
1388 uint32_t addrCfg;
1389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (isPWMPanel()) {
1390 // b0 = chip family, b1 = register slot count, b2 = middle LAT burst
1391 // length (0 = skip it). Only the FM6373 family reads b1/b2.
1392 ✗ if (const PWMChipSeq* seq = activePWMSeq()) {
1393 ✗ addrCfg = 1 | ((uint32_t)seq->slots << 8) | ((seq->midLatch ? 11u : 0u) << 16);
1394 ✗ } else if (m_addressingMode == ADDRESSING_MODE_DP3364) {
1395 // The DP3364S upload is the same shape as the FM6373 one with a
1396 // single register slot and no middle LAT burst: VSYNC (LE 3),
1397 // PRE_ACT (LE 14), then one word latched over its last 5 clocks.
1398 // Sharing that code rather than duplicating it matters - the
1399 // 16 output firmware only just fits in the PRU's 12KB IMEM.
1400 ✗ addrCfg = 1 | (1u << 8) | (0u << 16);
1401 } else {
1402 ✗ addrCfg = 0;
1403 }
1404 } else {
1405 // b2 = row shift chain length: the ABC shift firmware clocks one
1406 // bit per panel row, since panels can daisy chain a row driver per
1407 // band of scan rows
1408 2 addrCfg = (uint32_t)(m_addressingMode & 0xFF) | (((uint32_t)numRows) << 8) |
1409 1 (((uint32_t)std::min(m_panelHeight, 255)) << 16);
1410 }
1411 1 *(volatile uint32_t*)(pru->data_ram + ADDR_CONFIG_OFFSET) = addrCfg;
1412 // The FM6373 family upload grammar, in the word after the chip config:
1413 // the three LAT burst lengths and the LAT-low spacer between them. These
1414 // were fixed at 3/11/14 with a uniform 8 clock spacer, which is not what
1415 // the captures for every chip in the family show - the spacer in
1416 // particular varies per chip and per burst - so a panel that will not take
1417 // the registers has something to sweep. 0 in the middle burst skips it,
1418 // and 0 in the spacer emits no gap at all, which is what the reference
1419 // implementation does.
1420
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (isPWMPanel()) {
1421 ✗ uint32_t g = ((uint32_t)(m_regVsyncClocks & 0xFF)) |
1422 ✗ ((uint32_t)(m_regMidClocks & 0xFF) << 8) |
1423 ✗ ((uint32_t)(m_regPreClocks & 0xFF) << 16) |
1424 ✗ ((uint32_t)(m_regSpacerClocks & 0xFF) << 24);
1425 ✗ *(volatile uint32_t*)(pru->data_ram + ADDR_CONFIG_OFFSET + 4) = g;
1426 ✗ LogDebug(VB_CHANNELOUT, "BBShiftPanel: register upload grammar vsync=%d mid=%d pre=%d spacer=%d\n",
1427 m_regVsyncClocks, m_regMidClocks, m_regPreClocks, m_regSpacerClocks);
1428 }
1429 1 __sync_synchronize();
1430 // Both panel types are fed through a ring in the PRU shared memory (see
1431 // SMEMRing.hp); attach() must happen after run() since the firmware load
1432 // clears the PRU memories (and writes while the PRUSS is powered down
1433 // are silently dropped). A sole owner gets the entire shared RAM; on a
1434 // shared cape the panels are the PRU1 consumer and take the SPLIT1 half
1435 // (BBShiftString's PRU0 program uses SPLIT0, matching its existing
1436 // per-PRU split assignment).
1437
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (m_sharedPRUSS) {
1438 ✗ m_ring.attach(pru, SMEM_RING_SPLIT1_BASE, SMEM_RING_SPLIT_SIZE, false);
1439 } else {
1440 1 m_ring.attach(pru, SMEM_RING_DEFAULT_BASE, SMEM_RING_DEFAULT_SIZE, false);
1441 }
1442 1 uint32_t bytesPerPixel = (m_numOutputSlots * 6 * 2) / 16;
1443 1 uint32_t strideLen = rowLen * bytesPerPixel;
1444
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 uint32_t numStride = m_strideSchedule.empty() ? (numRows * m_colorDepth) : (m_strideSchedule.size() * numRows);
1445 1 uint32_t oframeSize = numStride * strideLen;
1446 // PWM panels consume numRows passes of 16 PWM bit planes of numBlocks
1447 // (rowLen / 16) blocks of 16 pixels, which works out to the same full
1448 // frame the pixel mapping produces
1449 1 m_frameBytes = oframeSize;
1450
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!isPWMPanel()) {
1451 1 pruData->numStrides = numStride;
1452 }
1453 // Frames live in normal cached memory now that the pump thread streams
1454 // them to the PRU; the prep threads write cached memory much faster than
1455 // the uncached PRU DDR region this replaced
1456 1 uint32_t allocSize = (oframeSize + 4095) & ~4095;
1457
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (int x = 0; x < NUM_OUTPUT_BUFFERS; x++) {
1458 4 outputBuffers[x] = (uint8_t*)aligned_alloc(4096, allocSize);
1459 4 memset(outputBuffers[x], 0, allocSize);
1460 }
1461 1 m_heapBuffers = true;
1462 1 m_frontBuffer = nullptr;
1463 // Reset the handshake counters with it. These outlive the pump thread, so
1464 // on a restart (teardownHardware() then back through here) stale values
1465 // would leave the new thread thinking a frame from the previous
1466 // configuration is still owed, and SendData waiting on a priming that
1467 // already happened.
1468 1 m_pumpSeq.store(0, std::memory_order_relaxed);
1469 1 m_pumpedSeq = 0;
1470 1 m_pumpPrimed.store(0, std::memory_order_relaxed);
1471 1 m_pumpRunning = true;
1472
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 m_pumpThread = std::thread(&BBShiftPanelManager::runPumpThread, this);
1473 1 return 1;
1474 }
1475
1476 1 void BBShiftPanelManager::runPumpThread() {
1477 struct sched_param sp;
1478 1 sp.sched_priority = 50;
1479 1 int err = pthread_setschedparam(pthread_self(), SCHED_FIFO, &sp);
1480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err) {
1481 ✗ LogWarn(VB_CHANNELOUT, "BBShiftPanel: could not set pump thread to SCHED_FIFO: %s\n", strerror(err));
1482 }
1483
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (isPWMPanel()) {
1484 // PWM panels send one frame per DATA command (the panels refresh
1485 // themselves from their internal PWM); stream once per queued
1486 // command so the byte flow stays in step with the commands
1487 ✗ while (m_pumpRunning) {
1488 ✗ if (m_pumpedSeq == m_pumpSeq.load(std::memory_order_acquire)) {
1489 // PrepDataPWM wakes this the moment a frame is published; the
1490 // timeout is only a backstop so shutdown cannot wedge here.
1491 // Polling on a 500us sleep used to leave the PRU shifting
1492 // against an empty ring for most of that wakeup latency.
1493 ✗ std::unique_lock<std::mutex> lk(m_pumpMutex);
1494 ✗ m_pumpCV.wait_for(lk, std::chrono::microseconds(200), [this] {
1495 ✗ return !m_pumpRunning || m_pumpSeq.load(std::memory_order_acquire) != m_pumpedSeq;
1496 });
1497 ✗ continue;
1498 ✗ }
1499 ✗ ++m_pumpedSeq;
1500 ✗ uint8_t* src = m_frontBuffer.load(std::memory_order_acquire);
1501 ✗ if (!src) {
1502 // The sequence can be ahead of the buffer if a reconfigure
1503 // republished it while a frame was in flight; there is nothing
1504 // to stream, and the shift path below makes the same check.
1505 ✗ m_pumpPrimed.store(m_pumpedSeq, std::memory_order_release);
1506 ✗ continue;
1507 }
1508 ✗ uint32_t srcOff = 0;
1509 ✗ bool primed = false;
1510 ✗ while (srcOff < m_frameBytes && m_pumpRunning) {
1511 ✗ uint32_t n = m_ring.write(src + srcOff, std::min(PUMP_BLOCK_SIZE, m_frameBytes - srcOff));
1512 ✗ if (n == 0) {
1513 // the ring holds as much as it can - the PRU may start.
1514 // Back off in small steps: at the drain rate a 150us sleep
1515 // was long enough to open a gap the panel could see.
1516 ✗ if (!primed) {
1517 ✗ primed = true;
1518 ✗ m_pumpPrimed.store(m_pumpedSeq, std::memory_order_release);
1519 }
1520 ✗ struct timespec ts = { 0, 20000 };
1521 ✗ nanosleep(&ts, nullptr);
1522 ✗ continue;
1523 ✗ }
1524 ✗ srcOff += n;
1525 }
1526 // a frame smaller than the ring never fills it
1527 ✗ if (!primed) {
1528 ✗ m_pumpPrimed.store(m_pumpedSeq, std::memory_order_release);
1529 }
1530 }
1531 ✗ return;
1532 }
1533
2/2
✓ Branch 0 taken 1272 times.
✓ Branch 1 taken 1 times.
1273 while (m_pumpRunning) {
1534
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 1247 times.
1272 if (m_pumpPaused.load(std::memory_order_acquire)) {
1535 // Nothing is being output, so there is nothing to hold on the
1536 // panel. Clear the command word and stop: the PRU picks it up at
1537 // the end of the frame it is in and parks in its command-wait
1538 // loop, which holds the display off, and the OE PRU parks the
1539 // same way once the brightness handshake stops. SendData writes
1540 // the command back on the first frame after StartingOutput.
1541 //
1542 // This is only reached at a frame boundary, and the ring is much
1543 // smaller than one frame, so the PRU has at most the tail of the
1544 // frame just written left to consume. It ends up exactly where
1545 // the pump stopped and the two stay aligned in the byte stream
1546 // across the pause.
1547
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 24 times.
25 if (!m_pumpParked.load(std::memory_order_relaxed)) {
1548 1 pruData->pixelsPerStride = 0;
1549 1 __sync_synchronize();
1550 1 m_pumpParked.store(true, std::memory_order_release);
1551 }
1552
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1553 25 continue;
1554 }
1555 1247 m_pumpParked.store(false, std::memory_order_relaxed);
1556 // frames stream back to back; a new frame from SendData is picked up
1557 // at the frame boundary so the PRU always sees whole frames
1558 1247 uint8_t* src = m_frontBuffer.load(std::memory_order_acquire);
1559
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 1208 times.
1247 if (!src) {
1560
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 std::this_thread::sleep_for(std::chrono::milliseconds(1));
1561 39 continue;
1562 }
1563 1208 uint32_t srcOff = 0;
1564
5/6
✓ Branch 0 taken 255532 times.
✓ Branch 1 taken 1208 times.
✓ Branch 2 taken 255532 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 255532 times.
✓ Branch 5 taken 1208 times.
256740 while (srcOff < m_frameBytes && m_pumpRunning) {
1565
1/2
✓ Branch 0 taken 255532 times.
✗ Branch 1 not taken.
255532 uint32_t n = m_ring.write(src + srcOff, std::min(PUMP_BLOCK_SIZE, m_frameBytes - srcOff));
1566
2/2
✓ Branch 0 taken 30791 times.
✓ Branch 1 taken 224741 times.
255532 if (n == 0) {
1567 // ring full; a full ring lasts ~480us at the max drain rate
1568 // so a short sleep cannot underrun the PRU
1569 30791 struct timespec ts = { 0, 150000 };
1570
1/2
✓ Branch 0 taken 30791 times.
✗ Branch 1 not taken.
30791 nanosleep(&ts, nullptr);
1571 30791 continue;
1572 30791 }
1573 224741 srcOff += n;
1574 }
1575 }
1576 }
1577 2 void BBShiftPanelManager::StopPRU(bool wait) {
1578 // A halted core cannot see the stop command, and a parked one is already
1579 // sitting on the check, so let them run first
1580 2 UnparkPRUs();
1581 // Send the stop command
1582
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pru) {
1583 1 pruData->command = PWM_COMMAND_HALT;
1584 1 pruData->result = PWM_COMMAND_HALT;
1585 }
1586 2 __asm__ __volatile__("" ::
1587 : "memory");
1588
1589
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pru) {
1590 1 int cnt = 0;
1591 // the pump keeps feeding the ring here so the PRU can reach the end
1592 // of the frame, which is where the halt command is checked
1593
4/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
1 while (wait && cnt < 25 && pruData->result == PWM_COMMAND_HALT) {
1594 ✗ std::this_thread::sleep_for(std::chrono::milliseconds(1));
1595 ✗ cnt++;
1596 ✗ __asm__ __volatile__("" ::
1597 : "memory");
1598 }
1599 }
1600 // stop the pump before the PRU memory mappings go away
1601 2 m_pumpRunning = false;
1602
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (m_pumpThread.joinable()) {
1603 1 m_pumpThread.join();
1604 }
1605
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pru) {
1606 1 pru->stop();
1607
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 delete pru;
1608 1 pru = nullptr;
1609 // pruData points into the mapping that just went away. Code that
1610 // finds it set assumes the PRU is up (startingOutput() sends the
1611 // panel init through pru on that basis), and after a failed rebuild
1612 // the surviving matrices keep calling in.
1613 1 pruData = nullptr;
1614
1615
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (pwmPru) {
1616 ✗ pwmPru->stop();
1617 ✗ delete pwmPru;
1618 ✗ pwmPru = nullptr;
1619 }
1620 }
1621 2 }
1622
1623 1 int BBShiftPanelOutput::Close(void) {
1624 1 LogDebug(VB_CHANNELOUT, "BBShiftPanelOutput::Close()\n");
1625
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!m_autoCreatedModelName.empty()) {
1626 1 PixelOverlayManager::INSTANCE.removeAutoOverlayModel(m_autoCreatedModelName);
1627 1 m_autoCreatedModelName.clear();
1628 }
1629 1 releaseToManager();
1630 1 return ChannelOutput::Close();
1631 }
1632
1633 53 void BBShiftPanelOutput::releaseToManager() {
1634
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 51 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
53 if (m_registered && !m_closed) {
1635 1 m_closed = true;
1636 1 BBShiftPanelManager::INSTANCE.closeMember();
1637 }
1638 53 }
1639
1640 1 void BBShiftPanelManager::closeMember() {
1641
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 if (m_openMembers > 0 && --m_openMembers) {
1642 // the pins stay muxed while another matrix on the cape still needs them
1643 ✗ return;
1644 }
1645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!m_refreshWarning.empty()) {
1646 ✗ WarningHolder::RemoveWarning(m_refreshWarning);
1647 ✗ m_refreshWarning.clear();
1648 }
1649 // release only the pins this cape muxed; other drivers (a future
1650 // panels + strings combo cape) may own the rest
1651
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1 times.
20 for (auto& pinName : m_configuredPins) {
1652
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 const PinCapabilities& pin = PinCapabilities::getPinByName(pinName);
1653
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 pin.releasePin();
1654 }
1655
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!singlePRU) {
1656 // if not using a single PRU, then we need to change the OE pin to the other PRU
1657 ✗ const PinCapabilities& pin = PinCapabilities::getPinByName(m_oePin);
1658 ✗ pin.releasePin();
1659 }
1660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (isPWMPanel()) {
1661 ✗ for (auto& pinName : PRU0_PWM_PINS) {
1662 ✗ const PinCapabilities& pin = PinCapabilities::getPinByName(pinName);
1663 ✗ pin.releasePin();
1664 }
1665 }
1666 1 m_configuredPins.clear();
1667 }
1668
1669 4 void BBShiftPanelManager::runBackgroundTasks() {
1670 4 ++bgThreadCount;
1671
2/2
✓ Branch 0 taken 1408 times.
✓ Branch 1 taken 4 times.
1412 while (bgThreadsRunning) {
1672 1408 std::function<void()> task;
1673 {
1674
1/2
✓ Branch 0 taken 1408 times.
✗ Branch 1 not taken.
1408 std::unique_lock<std::mutex> lock(bgTaskMutex);
1675
1/2
✓ Branch 0 taken 1408 times.
✗ Branch 1 not taken.
3752 bgTaskCondVar.wait_for(lock, std::chrono::milliseconds(100), [&]() { return !bgTasks.empty(); });
1676
2/2
✓ Branch 0 taken 1390 times.
✓ Branch 1 taken 18 times.
1408 if (!bgTasks.empty()) {
1677
1/2
✓ Branch 0 taken 1390 times.
✗ Branch 1 not taken.
1390 task = bgTasks.front();
1678 1390 bgTasks.pop();
1679 }
1680 1408 }
1681
2/2
✓ Branch 0 taken 1390 times.
✓ Branch 1 taken 18 times.
1408 if (task) {
1682
1/2
✓ Branch 0 taken 1390 times.
✗ Branch 1 not taken.
1390 task();
1683 }
1684 1408 }
1685 4 --bgThreadCount;
1686 4 }
1687
1688 230 void BBShiftPanelManager::processTasks(std::atomic<int>& counter) {
1689 // This must always drain to zero, even during shutdown: the queued tasks
1690 // reference the calling frame's stack (counter, results) and this object,
1691 // so returning while any are queued or running leaves them with dangling
1692 // references. The pool threads exit without draining the queue, but this
1693 // loop executes leftover tasks itself so it cannot deadlock.
1694
1/2
✓ Branch 0 taken 230 times.
✗ Branch 1 not taken.
230 std::unique_lock<std::mutex> lock(bgTaskMutex);
1695
2/2
✓ Branch 0 taken 1140 times.
✓ Branch 1 taken 230 times.
1370 while (counter > 0) {
1696 1140 std::function<void()> task;
1697
2/2
✓ Branch 0 taken 565 times.
✓ Branch 1 taken 575 times.
1140 if (!bgTasks.empty()) {
1698
1/2
✓ Branch 0 taken 565 times.
✗ Branch 1 not taken.
565 task = bgTasks.front();
1699 565 bgTasks.pop();
1700 }
1701
1/2
✓ Branch 0 taken 1140 times.
✗ Branch 1 not taken.
1140 lock.unlock();
1702
2/2
✓ Branch 0 taken 565 times.
✓ Branch 1 taken 575 times.
1140 if (task) {
1703
1/2
✓ Branch 0 taken 565 times.
✗ Branch 1 not taken.
565 task();
1704 } else {
1705
1/2
✓ Branch 0 taken 575 times.
✗ Branch 1 not taken.
575 std::this_thread::sleep_for(std::chrono::microseconds(100));
1706 }
1707
1/2
✓ Branch 0 taken 1140 times.
✗ Branch 1 not taken.
1140 lock.lock();
1708 1140 }
1709 230 }
1710
1711 115 void BBShiftPanelOutput::PrepData(unsigned char* channelData) {
1712 115 LogExcess(VB_CHANNELOUT, "BBShiftPanelOutput::PrepData(%p)\n", channelData);
1713
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 115 times.
115 if (!m_registered) {
1714 ✗ return;
1715 }
1716 115 ++m_inFlight;
1717 115 BBShiftPanelManager::INSTANCE.prepMember(this, channelData);
1718 115 --m_inFlight;
1719 }
1720
1721 // Scatters this matrix's channels into its own byte lanes of the shared 16 bit
1722 // intermediate. Members never touch each other's lanes, so this is safe to run
1723 // for each matrix in turn with no coordination beyond the frame boundary below.
1724 115 void BBShiftPanelOutput::scatterFrame(unsigned char* channelData) {
1725 115 auto& mgr = BBShiftPanelManager::INSTANCE;
1726 115 uint16_t* dest = mgr.currentChannelData;
1727
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 115 times.
115 if (!dest) {
1728 ✗ return;
1729 }
1730
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 m_matrix->OverlaySubMatrices(channelData);
1731 115 channelData += m_startChannel;
1732
1733
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 std::unique_lock<std::mutex> lock(mgr.bgTaskMutex);
1734 115 size_t total = m_scatterOffsets.size();
1735 115 size_t start = 0;
1736 115 std::atomic<int> counter(0);
1737
2/2
✓ Branch 0 taken 115 times.
✓ Branch 1 taken 115 times.
230 while (start < total) {
1738 // each chunk writes a contiguous region of the output since the map
1739 // is sorted by destination
1740 115 size_t end = std::min(start + 64 * 1024, total);
1741 115 ++counter;
1742
2/4
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 115 times.
✗ Branch 3 not taken.
115 mgr.bgTasks.push([this, channelData, start, end, dest, &counter]() {
1743 115 const uint32_t* offs = m_scatterOffsets.data();
1744 115 const uint32_t* srcs = m_scatterSrc.data();
1745
2/2
✓ Branch 0 taken 706560 times.
✓ Branch 1 taken 115 times.
706675 for (size_t x = start; x < end; x++) {
1746 706560 dest[offs[x]] = gammaCurve[channelData[srcs[x]]];
1747 }
1748 115 --counter;
1749 115 });
1750 115 start = end;
1751 }
1752
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 lock.unlock();
1753 115 mgr.bgTaskCondVar.notify_all();
1754
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 mgr.processTasks(counter);
1755 115 }
1756
1757 // One frame's worth of work for the whole cape. Every member scatters into
1758 // the shared intermediate first; the bit-plane pass is expensive and produces
1759 // a single frame for all of them, so it runs once, when the last member has
1760 // arrived. Doing it here rather than in SendData keeps it out of the way of
1761 // the other outputs' wire sends.
1762 115 void BBShiftPanelManager::prepMember(BBShiftPanelOutput* m, unsigned char* channelData) {
1763
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 std::lock_guard<std::mutex> memberLock(m_memberLock);
1764
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 if (!m->m_stopping) {
1765
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 m->scatterFrame(channelData);
1766 }
1767 // A stopping member contributes nothing but must still be counted, or the
1768 // frame boundary would never be reached again and output would stall.
1769
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 115 times.
115 if (++m_preppedThisFrame < (int)m_members.size()) {
1770 ✗ return;
1771 }
1772 115 m_preppedThisFrame = 0;
1773
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 115 times.
115 if (!bgThreadsRunning) {
1774 ✗ return;
1775 }
1776
2/4
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 115 times.
115 if (isPWMPanel()) {
1777 ✗ if (!PrepDataPWM()) {
1778 ✗ return; // dropped: the PRU didn't take the previous command
1779 }
1780 } else {
1781
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 PrepDataShift();
1782 }
1783 115 m_framePrepped = true;
1784
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 }
1785
1786 ✗ bool BBShiftPanelManager::PrepDataPWM() {
1787 ✗ uint8_t* buf = outputBuffers[currOutputBuffer];
1788
1789 ✗ std::unique_lock<std::mutex> lock(bgTaskMutex);
1790 ✗ std::atomic<int> counter(0);
1791 ✗ for (int curRow = 0; curRow < numRows; curRow++) {
1792 // Map the pixels for this row
1793 ✗ ++counter;
1794 ✗ bgTasks.push([this, curRow, buf, &counter]() {
1795 ✗ uint32_t start = curRow * rowLen;
1796 ✗ uint32_t end = start + rowLen;
1797
1798 ✗ if (m_numOutputSlots == 16) {
1799 ✗ ispc::MapPixelsForPWM16(currentChannelData, start, end, (uint16_t*)buf);
1800 } else {
1801 ✗ ispc::MapPixelsForPWM(currentChannelData, start, end, (uint16_t*)buf);
1802 }
1803 ✗ --counter;
1804 ✗ });
1805 }
1806 ✗ lock.unlock();
1807 ✗ bgTaskCondVar.notify_all();
1808 ✗ processTasks(counter);
1809
1810 /*
1811 for (int x = 0; x < 48; x++) {
1812 printf("%04x ", currentChannelData[x]);
1813 }
1814 printf("\n");
1815 for (int x = 0; x < 96; x += 6) {
1816 printf("%02x %02x %02x %02x %02x %02x\n", buf[x], buf[x + 1], buf[x + 2], buf[x + 3], buf[x + 4], buf[x + 5]);
1817 }
1818 printf("\n");
1819 for (int x = 48; x < 96; x++) {
1820 printf("%04x ", currentChannelData[x]);
1821 }
1822 printf("\n");
1823 for (int x = 96; x < (96 * 2); x += 6) {
1824 printf("%02x %02x %02x %02x %02x %02x\n", buf[x], buf[x + 1], buf[x + 2], buf[x + 3], buf[x + 4], buf[x + 5]);
1825 }
1826 printf("\n");
1827 */
1828
1829 // wait for the PRU to take the previous command before queueing the next
1830 ✗ if (!waitForPWMCommandTaken()) {
1831 ✗ return false;
1832 }
1833
1834 // hand the frame to the pump thread
1835 ✗ m_frontBuffer.store(buf, std::memory_order_release);
1836 ✗ uint32_t seq = m_pumpSeq.fetch_add(1, std::memory_order_release) + 1;
1837 ✗ m_pumpCV.notify_one();
1838 // Hold the DATA command until the ring is primed. Starting the PRU
1839 // against an empty ring left it in RINGWAIT for the pump's wakeup
1840 // latency, and an FM6363C reads that gap as its row scan running away
1841 // from the data. Filling the ring takes ~65us; the loop bound is only a
1842 // backstop so a stopped pump cannot hang the output thread.
1843 ✗ for (int i = 0; i < 4000 && m_pumpRunning && bgThreadsRunning &&
1844 ✗ m_pumpPrimed.load(std::memory_order_acquire) != seq;
1845 ++i) {
1846 ✗ std::this_thread::yield();
1847 }
1848
1849 ✗ pruData->numBlocks = rowLen / 16;
1850 ✗ pruData->numRows = numRows;
1851 ✗ pruData->cmd = PWM_COMMAND_DATA;
1852 ✗ __asm__ __volatile__("" ::
1853 : "memory");
1854 ✗ return true;
1855 ✗ }
1856
1857 // The PRU clears the command word when it takes a command. These waits run on
1858 // the output thread holding outputThreadLock and m_memberLock, so a PRU that
1859 // stopped answering (a wedged or crashed firmware) must not hold them forever:
1860 // that stopped all output, wedged the next config reload and kept fppd from
1861 // shutting down. Give it a few frame periods, then drop the frame. Once it
1862 // has missed one, later frames don't wait at all until it answers again.
1863 ✗ bool BBShiftPanelManager::waitForPWMCommandTaken() {
1864 ✗ int limit = m_pwmPruStalled ? 0 : 1000; // 100ms
1865 ✗ for (int cnt = 0; pruData->command && bgThreadsRunning && cnt < limit; cnt++) {
1866 ✗ std::this_thread::sleep_for(std::chrono::microseconds(100));
1867 ✗ __asm__ __volatile__("" ::
1868 : "memory");
1869 }
1870 ✗ if (!pruData->command) {
1871 ✗ if (m_pwmPruStalled) {
1872 ✗ LogInfo(VB_CHANNELOUT, "BBShiftPanel: the PRU is taking PWM panel frames again\n");
1873 ✗ m_pwmPruStalled = false;
1874 ✗ if (!m_pwmStallWarning.empty()) {
1875 ✗ WarningHolder::RemoveWarning(m_pwmStallWarning);
1876 ✗ m_pwmStallWarning.clear();
1877 }
1878 }
1879 ✗ return bgThreadsRunning;
1880 }
1881 ✗ if (bgThreadsRunning && !m_pwmPruStalled) {
1882 ✗ m_pwmPruStalled = true;
1883 ✗ LogErr(VB_CHANNELOUT, "BBShiftPanel: the PRU stopped taking PWM panel frames; dropping frames until it answers\n");
1884 ✗ m_pwmStallWarning = "BBShiftPanel: the PRU stopped responding; LED panel frames are being dropped. May require a reboot.";
1885 ✗ WarningHolder::AddWarning(m_pwmStallWarning);
1886 }
1887 ✗ return false;
1888 }
1889
1890 115 void BBShiftPanelManager::PrepDataShift() {
1891 // rebuild() refuses a geometry with more rows than this
1892 std::array<std::array<uint16_t*, 16>, MAX_SCAN_ROWS> results;
1893
1894 115 uint32_t bytesPerPixel = (m_numOutputSlots * 6 * 2) / 16;
1895 115 uint32_t strideLen = rowLen * bytesPerPixel;
1896 115 uint8_t* base = outputBuffers[currOutputBuffer];
1897 115 int numSlots = m_strideSchedule.size();
1898
2/2
✓ Branch 0 taken 2070 times.
✓ Branch 1 taken 115 times.
2185 for (int s = 0; s < numSlots; s++) {
1899
2/2
✓ Branch 0 taken 1150 times.
✓ Branch 1 taken 920 times.
2070 if (!m_strideSchedule[s].primary) {
1900 1150 continue;
1901 }
1902 920 int b = m_strideSchedule[s].bit;
1903
2/2
✓ Branch 0 taken 14720 times.
✓ Branch 1 taken 920 times.
15640 for (int r = 0; r < numRows; r++) {
1904
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14720 times.
14720 uint32_t idx = m_outputByRow ? (r * numSlots + s) : (s * numRows + r);
1905 14720 results[r][b] = (uint16_t*)(base + idx * strideLen);
1906 }
1907 }
1908
1909 /*
1910 int len = rowLen * numRows * 6 * 8
1911 uint16_t *d = data;
1912 for (int x = 0; x < len; x += 8) {
1913 for (int y = 0; y < 8; y++) {
1914 uint16_t d2 = *d;
1915 uint16_t mask = 0x1;
1916 uint8_t bit = 0x1 << y;
1917 for (int pos = 0; pos < bits; pos++) {
1918 if (d2 & mask) {
1919 results[pos][x] |= bit;
1920 }
1921 mask <<= 1;
1922 }
1923 ++d;
1924 }
1925 }
1926 */
1927 // Use ISPC generated code for the above. It's about 9x faster
1928
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 std::unique_lock<std::mutex> lock(bgTaskMutex);
1929 115 std::atomic<int> counter(0);
1930
2/2
✓ Branch 0 taken 1840 times.
✓ Branch 1 taken 115 times.
1955 for (int curRow = 0; curRow < numRows; curRow++) {
1931 // Map the pixels for this row
1932 1840 ++counter;
1933
2/4
✓ Branch 0 taken 1840 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1840 times.
✗ Branch 3 not taken.
1840 bgTasks.push([this, curRow, strideLen, base, &results, &counter]() {
1934 1840 uint32_t start = curRow * rowLen * 6 * m_numOutputSlots;
1935 1840 uint32_t end = start + (rowLen * 6 * m_numOutputSlots);
1936 1840 ispc::MapPixelsByDepth16(currentChannelData, start, end, m_colorDepth,
1937 1840 results[curRow][0], results[curRow][1],
1938 1840 results[curRow][2], results[curRow][3],
1939 1840 results[curRow][4], results[curRow][5],
1940 1840 results[curRow][6], results[curRow][7],
1941 1840 results[curRow][8], results[curRow][9],
1942 1840 results[curRow][10], results[curRow][11],
1943 1840 results[curRow][12], results[curRow][13],
1944 1840 results[curRow][14], results[curRow][15]);
1945 // fill in the duplicated strides for bits that display as multiple pulses
1946
2/2
✓ Branch 0 taken 18400 times.
✓ Branch 1 taken 1840 times.
20240 for (auto& dup : m_dupCopies[curRow]) {
1947 18400 memcpy(base + dup.first, base + dup.second, strideLen);
1948 }
1949 1840 --counter;
1950 1840 });
1951 }
1952
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 lock.unlock();
1953 115 bgTaskCondVar.notify_all();
1954
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 processTasks(counter);
1955 115 }
1956
1957 113 int BBShiftPanelOutput::SendData(unsigned char* channelData) {
1958 113 LogExcess(VB_CHANNELOUT, "BBShiftPanelOutput::SendData(%p)\n", channelData);
1959
3/6
✓ Branch 0 taken 113 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 113 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 113 times.
113 if (!m_registered || m_stopping) {
1960 ✗ return m_channelCount;
1961 }
1962 113 ++m_inFlight;
1963 113 BBShiftPanelManager::INSTANCE.publishFrame();
1964 113 --m_inFlight;
1965 113 return m_channelCount;
1966 }
1967
1968 // Hands the frame the last PrepData built to the pump. Every member calls
1969 // this, but the frame is shared, so only the first call after a prep does
1970 // anything - the rest are no-ops. This is deliberately cheap: a pointer
1971 // store and a couple of PRU words, nothing that would hold up the other
1972 // outputs waiting to send.
1973 113 void BBShiftPanelManager::publishFrame() {
1974
1/2
✓ Branch 0 taken 113 times.
✗ Branch 1 not taken.
113 std::lock_guard<std::mutex> memberLock(m_memberLock);
1975
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 113 times.
113 if (!m_framePrepped) {
1976 ✗ return;
1977 }
1978 113 m_framePrepped = false;
1979
1980
2/4
✓ Branch 0 taken 113 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 113 times.
✗ Branch 3 not taken.
113 if (!isPWMPanel()) {
1981 // hand the just-prepared frame to the pump thread; it picks it up at
1982 // its next frame boundary so the PRU always sees whole frames
1983 113 m_frontBuffer.store(outputBuffers[currOutputBuffer], std::memory_order_release);
1984 113 pruData->numStrides = m_strideSchedule.size() * numRows;
1985 113 pruData->pixelsPerStride = rowLen;
1986 } else {
1987 ✗ if (!waitForPWMCommandTaken()) {
1988 ✗ return; // dropped: the PRU didn't take the frame's DATA command
1989 }
1990
1991 // Send the command to setup the registers
1992 ✗ pruData->numBlocks = rowLen / 16;
1993 ✗ pruData->numRows = numRows;
1994 ✗ if (const PWMChipSeq* seq = activePWMSeq()) {
1995 // rotate the config sequence one word per frame as a continuous
1996 // refresh; the vsync is part of the FM6373 register upload so
1997 // PWM_COMMAND_SYNC is not set for this family
1998 ✗ writeFM6373SeqWord(m_pwmSeqIdx);
1999 ✗ m_pwmSeqIdx = (m_pwmSeqIdx + 1) % seq->len;
2000 ✗ pruData->cmd = PWM_COMMAND_REGISTERS | PWM_COMMAND_STARTGCLK;
2001 ✗ } else if (m_addressingMode == ADDRESSING_MODE_DP3364) {
2002 // same one-register-per-frame rotation; the datasheet frames it
2003 // as the intended way to refresh all 15 registers without paying
2004 // for a full upload every frame. The VSYNC is the first LE pulse
2005 // of the DP3364S register upload, so PWM_COMMAND_SYNC is not set.
2006 ✗ writeDP3364SeqWord(m_pwmSeqIdx);
2007 ✗ m_pwmSeqIdx = (m_pwmSeqIdx + 1) % DP3364_SEQ_LEN;
2008 ✗ pruData->cmd = PWM_COMMAND_REGISTERS | PWM_COMMAND_STARTGCLK;
2009 } else {
2010 ✗ pruData->cmd = PWM_COMMAND_REGISTERS | PWM_COMMAND_SYNC | PWM_COMMAND_STARTGCLK;
2011 }
2012 }
2013
2014 113 __asm__ __volatile__("" ::
2015 : "memory");
2016 113 currOutputBuffer = (currOutputBuffer + 1) % NUM_OUTPUT_BUFFERS;
2017
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 113 times.
113 while (outputBuffers[currOutputBuffer] == nullptr) {
2018 ✗ currOutputBuffer = (currOutputBuffer + 1) % NUM_OUTPUT_BUFFERS;
2019 }
2020
1/2
✓ Branch 0 taken 113 times.
✗ Branch 1 not taken.
113 }
2021 288 inline int mapRow(int row, int mode) {
2022
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 288 times.
288 if (mode == ADDRESSING_MODE_DIRECT) {
2023 ✗ switch (row) {
2024 ✗ case 0:
2025 ✗ return 0x0E;
2026 ✗ case 1:
2027 ✗ return 0x0D;
2028 ✗ case 2:
2029 ✗ return 0x0B;
2030 ✗ case 3:
2031 ✗ return 0x07;
2032 }
2033 }
2034 288 return row;
2035 }
2036
2037 ✗ static int outputRegData(int curidx, uint8_t* odata, uint16_t r, uint16_t g, uint16_t b, int numOutputs = 8) {
2038 // int sidx = curidx;
2039 ✗ int bytesPerClock = numOutputs == 16 ? 12 : 6;
2040 ✗ for (int x = 0; x < 16; x++) {
2041 ✗ odata[curidx] = r & 0x8000 ? 0xFF : 0x00;
2042 ✗ curidx++;
2043 ✗ odata[curidx] = g & 0x8000 ? 0xFF : 0x00;
2044 ✗ curidx++;
2045 ✗ odata[curidx] = b & 0x8000 ? 0xFF : 0x00;
2046 ✗ curidx++;
2047 ✗ odata[curidx] = r & 0x8000 ? 0xFF : 0x00;
2048 ✗ curidx++;
2049 ✗ odata[curidx] = g & 0x8000 ? 0xFF : 0x00;
2050 ✗ curidx++;
2051 ✗ odata[curidx] = b & 0x8000 ? 0xFF : 0x00;
2052 ✗ curidx++;
2053 ✗ if (numOutputs == 16) {
2054 // Duplicate for outputs 8-15
2055 ✗ odata[curidx] = r & 0x8000 ? 0xFF : 0x00;
2056 ✗ curidx++;
2057 ✗ odata[curidx] = g & 0x8000 ? 0xFF : 0x00;
2058 ✗ curidx++;
2059 ✗ odata[curidx] = b & 0x8000 ? 0xFF : 0x00;
2060 ✗ curidx++;
2061 ✗ odata[curidx] = r & 0x8000 ? 0xFF : 0x00;
2062 ✗ curidx++;
2063 ✗ odata[curidx] = g & 0x8000 ? 0xFF : 0x00;
2064 ✗ curidx++;
2065 ✗ odata[curidx] = b & 0x8000 ? 0xFF : 0x00;
2066 ✗ curidx++;
2067 }
2068 ✗ r <<= 1;
2069 ✗ g <<= 1;
2070 ✗ b <<= 1;
2071 }
2072 /*
2073 if (sidx == 0) {
2074 printf("%04X %04X %04X\n", r, g, b);
2075 for (int x = 0; x < 16; x++) {
2076 printf(" %02x %02x %02x %02x %02x %02x\n",
2077 odata[sidx], odata[sidx + 1], odata[sidx + 2],
2078 odata[sidx + 3], odata[sidx + 4], odata[sidx + 5]);
2079 sidx += 6;
2080 }
2081 }
2082 */
2083 ✗ return curidx;
2084 }
2085
2086 ✗ bool BBShiftPanelManager::setupPWMRegisters() {
2087 // This runs from Init(), on the main loop while outputs load. The waits
2088 // for the PRU to take a command had no limit, so a PRU that stopped
2089 // answering (firmware that failed to load, or wedged) hung fppd at
2090 // startup. Same budget as the panel init registers above.
2091 ✗ auto pruIdle = [this]() {
2092 ✗ for (int cnt = 0; pruData->command && cnt < 2500; cnt++) {
2093 ✗ std::this_thread::sleep_for(std::chrono::microseconds(100));
2094 ✗ __asm__ __volatile__("" ::
2095 : "memory");
2096 }
2097 ✗ if (pruData->command) {
2098 ✗ LogWarn(VB_CHANNELOUT, "BBShiftPanel: PWM panel registers were not sent (PRU did not respond)\n");
2099 ✗ return false;
2100 }
2101 ✗ return true;
2102 ✗ };
2103 /*
2104 // FM6363 from colorlight + logic analyzer
2105 // Confirmed via "advanced" tab in LEDVision
2106 Reg 1: 0x0970: b0000100101110000
2107 Reg 2: 0xFF9B R: b1111111110011011 (default values)
2108 0xF39B G: b1111001110011011
2109 0xDF9B B: b1101111110011011
2110 Reg 2: 0xFE01 R: b1111110000000001 (with current stripped off)
2111 0xF201 G: b1111000000000001
2112 0xDE01 B: b1101110000000001
2113 Reg 3: 0x4007 b0100000000000111
2114 Reg 4: 0x0040 b0000000001000000
2115 Reg 5: 0x0000 b0000000000000000
2116 */
2117 static uint16_t conf_6363[] = {
2118 // R/G/B triplets
2119 0x0070, 0x0070, 0x0070,
2120 0xFC01, 0xF001, 0xDC01,
2121 0x4007, 0x4007, 0x4007,
2122 0x0040, 0x0040, 0x0040,
2123 0x0000, 0x0000, 0x0000
2124 };
2125
2126 // create the "data" array of for all outputs of r/g/b triplets for the registers
2127 // 16 clocks * bytesPerClock * 5 registers
2128 ✗ int bytesPerClock = m_numOutputSlots == 16 ? 12 : 6;
2129 uint8_t odata[192 * 6];
2130
2131 ✗ if (m_addressingMode == ADDRESSING_MODE_DP3364) {
2132 // DP3364S: one config register per frame, so only the single rotating
2133 // word slot is used (the VSYNC and PRE_ACT that precede it are bare LE
2134 // pulses with the data lines "not care" - see OUTPUT_REGISTERS_DP3364
2135 // in the asm). Clock the whole sequence through before the first
2136 // frame, then SendData keeps rotating it as the continuous refresh the
2137 // datasheet asks for.
2138 ✗ setupGCLKConfig();
2139 ✗ pruData->numBlocks = rowLen / 16;
2140 ✗ pruData->numRows = numRows;
2141 ✗ m_pwmSeqIdx = 0;
2142 ✗ for (int i = 0; i < DP3364_SEQ_LEN; i++) {
2143 ✗ if (!pruIdle()) {
2144 ✗ return false;
2145 }
2146 ✗ writeDP3364SeqWord(i);
2147 ✗ pruData->cmd = PWM_COMMAND_REGISTERS;
2148 ✗ __asm__ __volatile__("" ::
2149 : "memory");
2150 // the command word clears when the PRU *starts* the upload, so
2151 // give it time to finish before rewriting the rotating slot
2152 ✗ std::this_thread::sleep_for(std::chrono::milliseconds(1));
2153 }
2154 // and that it took the last one
2155 ✗ return pruIdle();
2156 }
2157
2158 ✗ if (const PWMChipSeq* seq = activePWMSeq()) {
2159 // FM6373 family: the per-frame words are the 0x00AA/0x01AA
2160 // write-enable pair, one word of the config register sequence, an
2161 // optional chip specific word, and the 0x0055/0x0155 commit pair (see
2162 // OUTPUT_REGISTERS_FM6373 in the asm). Load the fixed slots once,
2163 // then clock the entire sequence through the rotating slot so the
2164 // panel is fully configured before the first frame; SendData keeps
2165 // rotating it afterwards as a continuous refresh (DMD_STM32 and
2166 // kingdo9 both do the same).
2167 ✗ int idx = outputRegData(0, odata, 0x00AA, 0x00AA, 0x00AA, m_numOutputSlots);
2168 ✗ idx = outputRegData(idx, odata, 0x01AA, 0x01AA, 0x01AA, m_numOutputSlots);
2169 ✗ idx = outputRegData(idx, odata, seq->r[0], seq->g[0], seq->b[0], m_numOutputSlots);
2170 ✗ if (seq->slots == 6) {
2171 ✗ idx = outputRegData(idx, odata, seq->extraWord, seq->extraWord, seq->extraWord, m_numOutputSlots);
2172 }
2173 ✗ idx = outputRegData(idx, odata, 0x0055, 0x0055, 0x0055, m_numOutputSlots);
2174 ✗ idx = outputRegData(idx, odata, 0x0155, 0x0155, 0x0155, m_numOutputSlots);
2175 ✗ pru->memcpyToPRU((uint8_t*)&pruData->registers[0], &odata[0], idx);
2176
2177 // An imported profile was picked for this panel deliberately, so it
2178 // is the authority on its own scan rate and the built-in capture's
2179 // rate says nothing about it.
2180 ✗ bool haveScan = m_haveProfile || ((int)numRows == seq->defaultScan);
2181 ✗ for (int v = 0; v < seq->variantCount && !haveScan; v++) {
2182 ✗ haveScan = (seq->variants[v].scan == (int)numRows);
2183 }
2184 ✗ if (!haveScan) {
2185 // These tables are per-capture, not per-chip: on this family the
2186 // current, subfield and tail registers move with the scan rate as
2187 // well as the scan count itself, so an unmatched rate is a
2188 // starting point rather than a correct configuration.
2189 ✗ LogWarn(VB_CHANNELOUT, "BBShiftPanel: no register table captured at 1/%u scan for this panel type; using the 1/%d table with only the scan count adjusted\n",
2190 numRows, seq->defaultScan);
2191 }
2192
2193 ✗ setupGCLKConfig();
2194 ✗ pruData->numBlocks = rowLen / 16;
2195 ✗ pruData->numRows = numRows;
2196 ✗ m_pwmSeqIdx = 0;
2197 ✗ for (int i = 0; i < seq->len; i++) {
2198 ✗ if (!pruIdle()) {
2199 ✗ return false;
2200 }
2201 ✗ writeFM6373SeqWord(i);
2202 ✗ pruData->cmd = PWM_COMMAND_REGISTERS;
2203 ✗ __asm__ __volatile__("" ::
2204 : "memory");
2205 // the command word clears when the PRU *starts* the upload, so
2206 // give it time to finish before rewriting the rotating slot
2207 ✗ std::this_thread::sleep_for(std::chrono::milliseconds(1));
2208 }
2209 // and that it took the last one
2210 ✗ return pruIdle();
2211 }
2212
2213 // register 1 contains the number of scan lines (rows)
2214 ✗ uint16_t rn = ((numRows - 1) << 8) & 0x3F00;
2215 int curidx;
2216 ✗ if (const FixedRegRow* regs = fixedRegDefaults(m_addressingMode)) {
2217 // FM6353 / ICND2153: five words in latch order 4, 6, 8, 10, 2 (the
2218 // latch length selects the register). An imported set replaces the
2219 // built-in one whole; either way the scan count is patched into the
2220 // first word, the same as the FM6373 family does for its profiles.
2221 uint16_t w[3][5];
2222 ✗ for (int c = 0; c < 3; c++) {
2223 ✗ for (int i = 0; i < 5; i++) {
2224 ✗ w[c][i] = m_haveFixedRegs ? m_fixedRegs[c][i] : regs[c][i];
2225 }
2226 ✗ w[c][0] = (w[c][0] & ~0x3F00) | rn;
2227 }
2228 ✗ curidx = 0;
2229 ✗ for (int i = 0; i < 5; i++) {
2230 ✗ curidx = outputRegData(curidx, odata, w[0][i], w[1][i], w[2][i], m_numOutputSlots);
2231 }
2232 ✗ LogDebug(VB_CHANNELOUT, "BBShiftPanel: %s registers R %04x %04x %04x %04x %04x\n",
2233 m_haveFixedRegs ? m_profName.c_str() : "built-in",
2234 w[0][0], w[0][1], w[0][2], w[0][3], w[0][4]);
2235 } else {
2236 ✗ curidx = outputRegData(0, odata, conf_6363[0] | rn, conf_6363[1] | rn, conf_6363[2] | rn, m_numOutputSlots);
2237
2238 // register 2 contains adjustments for current
2239
2240 ✗ uint16_t b = 205; // stick with default brightness for now
2241 ✗ if (m_brightness >= 5) {
2242 ✗ b = (m_brightness - 5) * 10;
2243 ✗ if (m_brightness > 8) {
2244 ✗ b *= (245 - 64) * 2;
2245 } else {
2246 ✗ b *= (205 - 64) * 2;
2247 }
2248 ✗ b /= 100;
2249 ✗ b += 64;
2250 ✗ b <<= 1;
2251 ✗ b &= 0x1FE;
2252 ✗ b |= 0x200;
2253 } else {
2254 ✗ b = (m_brightness - 1) * 10;
2255 ✗ b *= 100;
2256 ✗ b /= 40;
2257 ✗ b *= (255 - 64);
2258 ✗ b /= 100;
2259 ✗ b += 64;
2260 ✗ b <<= 1;
2261 ✗ b &= 0x1FE;
2262 }
2263
2264 ✗ curidx = outputRegData(curidx, odata, conf_6363[3] | b, conf_6363[4] | b, conf_6363[5] | b, m_numOutputSlots);
2265 ✗ curidx = outputRegData(curidx, odata, conf_6363[6], conf_6363[7], conf_6363[8], m_numOutputSlots);
2266 ✗ curidx = outputRegData(curidx, odata, conf_6363[9], conf_6363[10], conf_6363[11], m_numOutputSlots);
2267 ✗ curidx = outputRegData(curidx, odata, conf_6363[12], conf_6363[13], conf_6363[14], m_numOutputSlots);
2268 }
2269
2270 ✗ pru->memcpyToPRU(&pruData->registers[0], &odata[0], curidx);
2271
2272 ✗ if (!pruIdle()) {
2273 ✗ return false;
2274 }
2275
2276 // Send the command to setup the registers
2277 ✗ setupGCLKConfig();
2278 ✗ pruData->numBlocks = rowLen / 16;
2279 ✗ pruData->numRows = numRows;
2280 ✗ pruData->cmd = PWM_COMMAND_REGISTERS;
2281 ✗ __asm__ __volatile__("" ::
2282 : "memory");
2283 ✗ return pruIdle();
2284 }
2285
2286 // Adopt an imported register profile, if the config carried one and this chip
2287 // is one whose upload grammar we drive from a PWMChipSeq. The grammar itself
2288 // (slot count, extra word, middle LAT burst, which register holds the scan
2289 // count) is a property of the chip, not of the capture, so it is taken from
2290 // the built-in entry and only the word lists are replaced.
2291 1 void BBShiftPanelManager::buildRegisterProfile(const PanelParams& p) {
2292 1 m_haveProfile = false;
2293 1 m_haveFixedRegs = false;
2294 1 m_profName.clear();
2295
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (int c = 0; c < 3; c++) {
2296 3 m_profWords[c].clear();
2297 }
2298
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (p.regProfile[0].empty()) {
2299 1 return;
2300 }
2301 ✗ if (const FixedRegRow* regs = fixedRegDefaults(m_addressingMode)) {
2302 // FM6353 / ICND2153: the profile is the fixed register words in
2303 // upload order. Four is what a receiving card's software shows
2304 // (registers 1-4); the fifth, latch-2 word keeps its built-in value
2305 // unless given.
2306 ✗ size_t n = p.regProfile[0].size();
2307 ✗ if (n != 4 && n != 5) {
2308 ✗ LogWarn(VB_CHANNELOUT, "BBShiftPanel: a register profile for this panel type needs 4 or 5 words per color, got %d; using the built-in registers\n",
2309 (int)n);
2310 ✗ return;
2311 }
2312 ✗ for (int c = 0; c < 3; c++) {
2313 ✗ for (int i = 0; i < 5; i++) {
2314 ✗ m_fixedRegs[c][i] = i < (int)n ? p.regProfile[c][i] : regs[c][i];
2315 }
2316 }
2317 ✗ m_haveFixedRegs = true;
2318 ✗ m_profName = p.regProfileName;
2319 ✗ LogInfo(VB_CHANNELOUT, "BBShiftPanel: using imported register profile '%s' (%d words per color)\n",
2320 m_profName.c_str(), (int)n);
2321 ✗ return;
2322 }
2323 ✗ const PWMChipSeq* base = pwmChipSeqFor(m_addressingMode);
2324 ✗ if (!base) {
2325 // FM6363C drives its registers from a different code path that has
2326 // no word list to override.
2327 ✗ LogWarn(VB_CHANNELOUT, "BBShiftPanel: a register profile was configured but this panel type does not use one; ignoring it\n");
2328 ✗ return;
2329 }
2330 ✗ for (int c = 0; c < 3; c++) {
2331 ✗ m_profWords[c] = p.regProfile[c];
2332 }
2333 ✗ m_profSeq = *base;
2334 ✗ m_profSeq.r = m_profWords[0].data();
2335 ✗ m_profSeq.g = m_profWords[1].data();
2336 ✗ m_profSeq.b = m_profWords[2].data();
2337 ✗ m_profSeq.len = (int)m_profWords[0].size();
2338 // numRows is not known yet here (computeGeometry runs later), so the
2339 // scan-mismatch warning is suppressed at its own site via m_haveProfile
2340 // rather than by faking a defaultScan.
2341 ✗ m_profSeq.variants = nullptr;
2342 ✗ m_profSeq.variantCount = 0;
2343 ✗ m_haveProfile = true;
2344 ✗ m_profName = p.regProfileName;
2345 ✗ LogInfo(VB_CHANNELOUT, "BBShiftPanel: using imported register profile '%s' (%d words per color)\n",
2346 m_profName.c_str(), m_profSeq.len);
2347 }
2348
2349 ✗ const PWMChipSeq* BBShiftPanelManager::activePWMSeq() const {
2350 ✗ if (m_haveProfile) {
2351 ✗ return &m_profSeq;
2352 }
2353 ✗ return pwmChipSeqFor(m_addressingMode);
2354 }
2355
2356 ✗ void BBShiftPanelManager::writeFM6373SeqWord(int idx) {
2357 // rewrite the rotating register slot (slot 3 of 5) with sequence word
2358 // idx. Only safe while no register upload is in flight: called from
2359 // SendData after the previous DATA command was consumed (the PRU is
2360 // uploading frame data, which never reads the register slots, and the
2361 // previous REGISTERS upload completed before that DATA was dispatched)
2362 // and from the serialized init loop in setupPWMRegisters.
2363 ✗ const PWMChipSeq* seq = activePWMSeq();
2364 ✗ const uint16_t* sr = seq->r;
2365 ✗ const uint16_t* sg = seq->g;
2366 ✗ const uint16_t* sb = seq->b;
2367 ✗ for (int v = 0; v < seq->variantCount; v++) {
2368 ✗ if (seq->variants[v].scan == (int)numRows) {
2369 ✗ sr = seq->variants[v].r;
2370 ✗ sg = seq->variants[v].g;
2371 ✗ sb = seq->variants[v].b;
2372 ✗ break;
2373 }
2374 }
2375 ✗ uint16_t rw = sr[idx];
2376 ✗ uint16_t gw = sg[idx];
2377 ✗ uint16_t bw = sb[idx];
2378 ✗ if ((rw >> 8) == seq->scanReg) {
2379 // the scan row count lives in the low 6 bits; the upper 2 are a chip
2380 // constant carried over from the table (DMD_STM32 patches the same
2381 // entry for the FM6373)
2382 ✗ rw = gw = bw = (uint16_t)(((uint32_t)seq->scanReg << 8) | (rw & 0xC0) | ((numRows - 1) & 0x3F));
2383 }
2384 ✗ int slotSize = 16 * (m_numOutputSlots == 16 ? 12 : 6);
2385 uint8_t buf[192 * 2];
2386 ✗ int len = outputRegData(0, buf, rw, gw, bw, m_numOutputSlots);
2387 ✗ if (slotSize & 63) {
2388 // A slot that is not a whole number of 64 byte units shares a unit
2389 // with the one after it, so extend the write to cover that slot with
2390 // its own (constant) value rather than leave a partial unit: the
2391 // commit word for a five slot chip, the extra pre-commit word for a
2392 // six slot one.
2393 ✗ uint16_t next = seq->slots == 6 ? seq->extraWord : 0x0055;
2394 ✗ len = outputRegData(len, buf, next, next, next, m_numOutputSlots);
2395 }
2396 ✗ pru->memcpyToPRU((uint8_t*)&pruData->registers[0] + 2 * slotSize, buf, len);
2397 ✗ }
2398
2399 ✗ void BBShiftPanelManager::writeDP3364SeqWord(int idx) {
2400 // rewrite the single rotating register slot with sequence word idx. Same
2401 // in-flight rules as writeFM6373SeqWord: only safe while no register
2402 // upload is running.
2403 ✗ uint16_t rw = DP3364_SEQ_R[idx];
2404 ✗ uint16_t gw = DP3364_SEQ_G[idx];
2405 ✗ uint16_t bw = DP3364_SEQ_B[idx];
2406 ✗ if ((rw >> 8) == 0x02) {
2407 // scan row count
2408 ✗ rw = gw = bw = 0x0200 | ((numRows - 1) & 0x3F);
2409 ✗ } else if ((rw >> 8) == 0x08) {
2410 // linear output current multiplier. The captured 0x7f is the value
2411 // the panel was designed around, so it is full brightness and this
2412 // only ever scales down from it; the per-color trim in 0x09/0x0b is
2413 // left alone so the white balance does not move with brightness.
2414 ✗ uint16_t cur = std::clamp(0x7F * m_brightness / 10, 8, 0x7F);
2415 ✗ rw = gw = bw = 0x0800 | cur;
2416 }
2417 ✗ int slotSize = 16 * (m_numOutputSlots == 16 ? 12 : 6);
2418 uint8_t buf[192 * 2];
2419 ✗ int len = outputRegData(0, buf, rw, gw, bw, m_numOutputSlots);
2420 ✗ if (slotSize & 63) {
2421 // A slot that is not a whole number of 64 byte units shares a unit
2422 // with the one after it, so pad with a second copy of the word to keep
2423 // the write on a unit boundary. Nothing reads the padding - DP3364S
2424 // only sends one word per frame.
2425 ✗ len = outputRegData(len, buf, rw, gw, bw, m_numOutputSlots);
2426 }
2427 ✗ pru->memcpyToPRU((uint8_t*)&pruData->registers[0], buf, len);
2428 ✗ }
2429
2430 ✗ void BBShiftPanelManager::setupGCLKConfig() {
2431 // Configuration for the GCLK program on the other PRU (see the rowConfig
2432 // register in BBShiftPanel_gclk.asm): [0] = blanking loops between GCLK
2433 // packets (the brightness knob), [1] = row select mode, [2]/[3] = GCLK
2434 // pulses in the first packet after a restart / every packet after
2435 ✗ pwmPru->data_ram[0] = m_brightness > 8 ? 3 : 11 - m_brightness;
2436 ✗ if (m_addressingMode == ADDRESSING_MODE_DP3364) {
2437 // DP3364S generates its own GCLK from an internal PLL, so this side
2438 // only emits the per-row ROW strobe (which replaces OE on this chip)
2439 // and advances the row driver. Unlike the FM6373 the addressing
2440 // dropdown is honored: a panel with more than 32 scan rows cannot be
2441 // addressed by the 5 SEL lines at all and needs the token shift
2442 // register, which is what the row driver chips on these panels want.
2443 // Brightness comes from config register 0x08, not from blanking time.
2444 // [2] = W4 ROW pulse width in 100ns units (W12 is emitted as 3x
2445 // this; the chip only has to tell the two apart)
2446 // [3] = row period in us
2447 ✗ int dclkNs = m_numOutputSlots == 16 ? DCLK_NS_16 : DCLK_NS_8;
2448 ✗ int minLine = std::max({ dp3364MinLineDCLKs(DP3364_SEQ_R, DP3364_SEQ_LEN),
2449 ✗ dp3364MinLineDCLKs(DP3364_SEQ_G, DP3364_SEQ_LEN),
2450 ✗ dp3364MinLineDCLKs(DP3364_SEQ_B, DP3364_SEQ_LEN) });
2451 ✗ pwmPru->data_ram[1] = 4 | (m_pwmDirectRow ? 1 : 0);
2452 // W4 is 4 DCLK periods; the pulse is emitted as a time because the
2453 // DCLK comes from the other PRU with no phase relationship to this one
2454 ✗ pwmPru->data_ram[2] = std::clamp((4 * dclkNs + 99) / 100, 2, 40);
2455 // The datasheet's line time is a minimum, and the row scan free runs,
2456 // so a row that is too short simply will not have finished displaying.
2457 // 2x the minimum is the margin for that; the scan still revisits every
2458 // row far faster than the eye at any supported scan count.
2459 ✗ pwmPru->data_ram[3] = std::clamp((2 * minLine * dclkNs + 999) / 1000, 4, 255);
2460 // [4] = PWM display group count, config register 0x03 + 1. One W12
2461 // ROW pulse is emitted per (groups x rows) cycle, so this has to
2462 // track whatever DP3364_SEQ_* sets 0x03 to.
2463 ✗ pwmPru->data_ram[4] = (DP3364_SEQ_R[1] & 0x7F) + 1;
2464 ✗ return;
2465 }
2466 ✗ if (activePWMSeq()) {
2467 // FM6373 family: single OE pulse per row. kingdo9 gives ICND1065L
2468 // and SM16380SH the same OE style, so they run this scan too.
2469 // Brightness comes from the chip's config registers, not the blanking
2470 // time. [2] = opener pulse width us, [3] = row period us (~128 DCLKs
2471 // at the data clock rate, so the scan rate is about the same while
2472 // uploading and while free-running between frames)
2473 //
2474 // Row transport: the DP32019B boards this family was written against
2475 // put a binary row number on SEL0-4, and that stayed the only choice
2476 // here for a while. Some ICND1065L panels instead wire A/B/C to a
2477 // token shift register with D/E grounded (GH #2955), which is the
2478 // only way to reach more scan rows than the SEL lines can count.
2479 // Direct stays the default so those existing boards are untouched;
2480 // bit 0 clear picks the shift register and only an explicit ABC Shift
2481 // addressing selection does that.
2482 ✗ pwmPru->data_ram[1] = 2 | (m_pwmShiftRow ? 0 : 1);
2483 // OE timing. The reference implementation denominates these in DCLK
2484 // periods rather than in time, and they are the first thing its users
2485 // tune on a panel that will not light, so take DCLK counts here too
2486 // and convert. The defaults are its defaults (oe_clk_length 4,
2487 // first_oe_clk_length 12), which at either data clock rate land on
2488 // about the fixed 600ns / 2us this used to emit.
2489 ✗ int dclk = m_numOutputSlots == 16 ? DCLK_NS_16 : DCLK_NS_8;
2490 ✗ pwmPru->data_ram[2] = m_oeFirstClkLength
2491 ✗ ? std::clamp((m_oeFirstClkLength * dclk + 999) / 1000, 1, 255)
2492 : 2;
2493 ✗ pwmPru->data_ram[3] = m_oeRowPeriodUs;
2494 // byte 5 is in 100ns units; see oeWidth in BBShiftPanel_gclk.asm
2495 ✗ pwmPru->data_ram[5] = m_oeClkLength
2496 ✗ ? std::clamp((m_oeClkLength * dclk + 99) / 100, 1, 255)
2497 : 6;
2498 ✗ LogDebug(VB_CHANNELOUT, "BBShiftPanel: OE pulse %d (100ns units), opener %dus, row period %dus (DCLK %dns)\n",
2499 pwmPru->data_ram[5], pwmPru->data_ram[2], pwmPru->data_ram[3], dclk);
2500 ✗ return;
2501 }
2502 ✗ pwmPru->data_ram[1] = m_pwmDirectRow ? 1 : 0;
2503 ✗ if (m_addressingMode == ADDRESSING_MODE_FM6353C ||
2504 ✗ m_addressingMode == ADDRESSING_MODE_ICND2153) {
2505 // DMD_STM32 GCLK_NUM: FM6353 row switching takes 138 GCLK pulses
2506 // (uniform; no first-packet extension is documented for this chip).
2507 // kingdo9's ICND2153 capture independently lands on 138 pulses per
2508 // scan, though it drives them as a half-rate waveform (one pulse per
2509 // two clock slots) that this GCLK program does not reproduce.
2510 // Both are settable from the LED Panels page for panels that do not
2511 // advance rows on 138; 0 means unset.
2512 ✗ pwmPru->data_ram[2] = m_gclkFirstPulses ? m_gclkFirstPulses : 138;
2513 ✗ pwmPru->data_ram[3] = m_gclkPulses ? m_gclkPulses : 138;
2514 ✗ LogDebug(VB_CHANNELOUT, "BBShiftPanel: GCLK pulses first %d, each row %d\n",
2515 pwmPru->data_ram[2], pwmPru->data_ram[3]);
2516 } else {
2517 // FM6363: 74 pulses per row, first packet after a restart is 78
2518 // (logic analyzer capture; independently confirmed by the
2519 // kingdo9/rpi-rgb-led-matrix_pwm_experiment measurements)
2520 ✗ pwmPru->data_ram[2] = 78;
2521 ✗ pwmPru->data_ram[3] = 74;
2522 }
2523 }
2524
2525 1 uint32_t BBShiftPanelManager::computeMaxBrightnessCycles() {
2526 // Scale the row length by the amount of data actually shifted per pixel
2527 // clock: 16 output slots shift twice the bytes of 8 slots, so the same
2528 // physical row takes twice as long to shift out
2529 1 uint32_t effLen = rowLen * m_numOutputSlots / 8;
2530 // Longer rows take longer to shift, so they get a longer maximum on-time
2531 // to keep the duty cycle (brightness) up at the cost of refresh rate.
2532 // This is a smooth ramp so that adding one panel cannot step the
2533 // brightness; it passes through 0x8800 at 256, 0xA800 at 384, 0xC800 at
2534 // 512 and 0xE800 at 640 (the values the old stepped defaults used).
2535 1 uint32_t maxBright = 64 * effLen + 18432;
2536 // The ramp and its bounds are tuned for 16 scan rows. With fewer rows
2537 // each row owns a larger share of the frame so the on-times may grow
2538 // (1/8 scan); with more rows they must shrink or the refresh tanks
2539 // (1/32 scan P2.5 panels) - scale the bounds by 16/numRows so all scan
2540 // ratios get the same refresh contract.
2541 1 maxBright = std::clamp(maxBright, 0x8800u * 16 / numRows, 0xE800u * 16 / numRows);
2542
4/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
2 if (FileExists(FPP_DIR_MEDIA("/config/panel_timing.txt"))) {
2543 ✗ std::string v = GetFileContents(FPP_DIR_MEDIA("/config/panel_timing.txt"));
2544 ✗ if (!v.empty()) {
2545 ✗ maxBright = std::stoi(v, nullptr, 16);
2546 }
2547 ✗ }
2548 1 return maxBright;
2549 }
2550
2551 1 void BBShiftPanelManager::buildStrideSchedule() {
2552 1 m_strideSchedule.clear();
2553 1 m_dupCopies.clear();
2554
2555
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 uint32_t maxBright = computeMaxBrightnessCycles();
2556 // Approximate PRU cycles to shift one full stride out to the panels
2557 // (per-pixel cost including the amortized data load overhead). In the
2558 // two-PRU configuration the OE PRU prefetches the ring blocks and hands
2559 // them over through the scratchpad, which shortens the load (measured:
2560 // 80.0 -> 83.2Hz on a 4-panel 12-bit chain, ~42 cycles/pixel effective)
2561
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1 bool pruPrefetch = !singlePRU && !isPWMPanel();
2562
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 uint32_t cyclesPerPixel = (m_numOutputSlots == 16) ? (pruPrefetch ? 42 : 48)
2563 : (pruPrefetch ? 25 : 26);
2564 1 uint32_t shiftCycles = rowLen * cyclesPerPixel + 100;
2565
2566 // Capacity limit: the PRU brightness table holds 768 stride entries
2567 // (24 slots at 32 rows, so 1/32 scan panels keep a split-pulse budget)
2568 1 uint32_t bytesPerPixel = (m_numOutputSlots * 6 * 2) / 16;
2569 1 uint32_t strideLen = rowLen * bytesPerPixel;
2570 1 int maxSlots = 768 / (int)numRows;
2571
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (maxSlots < m_colorDepth) {
2572 // no room for splitting, the base schedule has to fit regardless
2573 ✗ maxSlots = m_colorDepth;
2574 }
2575
2576 // Decide how many pulses each bit is displayed as. A bit whose on-time
2577 // is >= k * shift time can be shown as k shorter pulses spread across the
2578 // frame at zero cost in frame time (the shifting still fits under each
2579 // pulse), which multiplies the perceived refresh rate. The count rounds
2580 // to nearest: pieces slightly shorter than the shift time cost a little
2581 // dead time (bounded by half a shift per bit) but move that bit's light
2582 // to a much higher frequency - on longer chains the second MSB would
2583 // otherwise sit unsplit at the frame rate carrying a quarter of the light.
2584 1 int budget = maxSlots - m_colorDepth;
2585
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
2 std::vector<int> pieces(m_colorDepth, 1);
2586
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 for (int b = m_colorDepth - 1; b >= 0 && budget > 0; b--) {
2587 5 uint32_t on = (m_brightness * maxBright / 10) >> (m_colorDepth - 1 - b);
2588 5 int k = std::min((int)((on + shiftCycles / 2) / shiftCycles), 4);
2589 5 k = std::min(k, budget + 1);
2590
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
5 if (k <= 1) {
2591 // lower bits have shorter on-times, no more splitting is useful
2592 1 break;
2593 }
2594 4 pieces[b] = k;
2595 4 budget -= k - 1;
2596 }
2597
2598 // Place the pieces on the timeline. Each bit's pieces are spread evenly
2599 // across the frame with a half-spacing phase offset so that every pixel
2600 // value, not just full white, sees its light distributed across the whole
2601 // frame (e.g. 4 pieces land at 1/8, 3/8, 5/8, 7/8 and interleave with the
2602 // 2-piece bit at 1/4, 3/4). Bits place MSB first; a collision moves to
2603 // the nearest free slot.
2604 1 int n = 0;
2605
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 for (int b = 0; b < m_colorDepth; b++) {
2606 8 n += pieces[b];
2607 }
2608
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 m_strideSchedule.assign(n, StrideSlot{ 0, false, 0 });
2609
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
2 std::vector<bool> used(n, false);
2610
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 for (int b = m_colorDepth - 1; b >= 0; b--) {
2611 8 uint32_t on = (m_brightness * maxBright / 10) >> (m_colorDepth - 1 - b);
2612 8 uint32_t per = on / pieces[b];
2613
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 8 times.
26 for (int i = 0; i < pieces[b]; i++) {
2614 18 int target = (int)(((i + 0.5f) * n) / pieces[b]);
2615 18 int slot = target;
2616
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 for (int d = 0; d < n; d++) {
2617 // search outward from the target, alternating below/above
2618
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 38 times.
64 int cand = (target + ((d & 1) ? (n - (d + 1) / 2) : (d / 2))) % n;
2619
3/4
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 46 times.
64 if (!used[cand]) {
2620 18 slot = cand;
2621 18 break;
2622 }
2623 }
2624
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 used[slot] = true;
2625 // keep the total on-time exact, first piece takes the remainder
2626
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 10 times.
18 uint32_t t = (i == 0) ? (on - per * (pieces[b] - 1)) : per;
2627 18 m_strideSchedule[slot] = { (uint8_t)b, false, t };
2628 }
2629 }
2630 1 bool seen[16] = { false };
2631
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1 times.
19 for (auto& slot : m_strideSchedule) {
2632 18 slot.primary = !seen[slot.bit];
2633 18 seen[slot.bit] = true;
2634 }
2635
2636 // Frame buffer copies needed for the non-primary (duplicated) strides
2637
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 m_dupCopies.assign(numRows, {});
2638 int primarySlot[16];
2639
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1 times.
19 for (int s = 0; s < n; s++) {
2640
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 10 times.
18 if (m_strideSchedule[s].primary) {
2641 8 primarySlot[m_strideSchedule[s].bit] = s;
2642 }
2643 }
2644
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1 times.
19 for (int s = 0; s < n; s++) {
2645
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 10 times.
18 if (m_strideSchedule[s].primary) {
2646 8 continue;
2647 }
2648 10 int ps = primarySlot[m_strideSchedule[s].bit];
2649
2/2
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 10 times.
170 for (int r = 0; r < numRows; r++) {
2650
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 160 times.
160 uint32_t dstIdx = m_outputByRow ? (r * n + s) : (s * numRows + r);
2651
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 160 times.
160 uint32_t srcIdx = m_outputByRow ? (r * n + ps) : (ps * numRows + r);
2652
1/2
✓ Branch 0 taken 160 times.
✗ Branch 1 not taken.
160 m_dupCopies[r].emplace_back(dstIdx * strideLen, srcIdx * strideLen);
2653 }
2654 }
2655
2656 // log what this schedule works out to
2657 1 uint64_t rowCycles = 0;
2658 1 uint64_t onCycles = 0;
2659
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1 times.
19 for (auto& slot : m_strideSchedule) {
2660 18 rowCycles += std::max(shiftCycles, slot.onTime) + 60;
2661 18 onCycles += slot.onTime;
2662 }
2663 1 uint64_t frameCycles = rowCycles * numRows;
2664 1 float refresh = 250000000.0f / (float)frameCycles;
2665
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogInfo(VB_CHANNELOUT, "BBShiftPanel: %d strides/row (%d bit color, MSB split %d ways), est refresh %d Hz, duty %d%%\n",
2666 n, m_colorDepth, pieces[m_colorDepth - 1], (int)refresh, (int)(onCycles * 100 / rowCycles));
2667 // a rebuild with other geometry replaces the estimate, so the banner for
2668 // the previous one has to come down first or it stays up for good
2669
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!m_refreshWarning.empty()) {
2670 ✗ WarningHolder::RemoveWarning(m_refreshWarning);
2671 ✗ m_refreshWarning.clear();
2672 }
2673
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (refresh < 60.0f) {
2674 ✗ m_refreshWarning = "LED panel refresh rate is only " + std::to_string((int)refresh) + "Hz and may flicker; reduce the color depth or panels per output";
2675 ✗ WarningHolder::AddWarning(m_refreshWarning);
2676 }
2677 1 }
2678
2679 1 void BBShiftPanelManager::setupBrightnessValues() {
2680 1 uint32_t* cur = &pruData->brightness[0];
2681 1 int n = m_strideSchedule.size();
2682 288 auto writeEntry = [&](int s, int r) {
2683 288 int mappedRow = mapRow(r, m_addressingMode);
2684 288 cur[0] = m_strideSchedule[s].onTime;
2685 288 cur[1] = (mappedRow << 24) & 0x7F000000;
2686
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 288 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
288 if (m_outputByRow && m_outputBlankData && (s == 0)) {
2687 ✗ cur[1] |= 0x80000000;
2688 }
2689 // printf("Brightness[%d %d] = %08x %08x\n", s, r, cur[0], cur[1]);
2690 288 cur += 2;
2691 289 };
2692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (m_outputByRow) {
2693 ✗ for (int r = 0; r < numRows; r++) {
2694 ✗ for (int s = 0; s < n; s++) {
2695 ✗ writeEntry(s, r);
2696 }
2697 }
2698 } else {
2699
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1 times.
19 for (int s = 0; s < n; s++) {
2700
2/2
✓ Branch 0 taken 288 times.
✓ Branch 1 taken 18 times.
306 for (int r = 0; r < numRows; r++) {
2701 288 writeEntry(s, r);
2702 }
2703 }
2704 }
2705 1 }
2706
2707 // Maps this matrix's channels onto the shared frame. The lane a pixel lands
2708 // in is fixed by which cape output its panel is on (outputPin/outputBank from
2709 // the cape pinout), which is what lets two matrices share one frame without
2710 // coordinating: they simply never compute the same destination offset.
2711 1 bool BBShiftPanelOutput::buildScatterMap() {
2712 1 auto& mgr = BBShiftPanelManager::INSTANCE;
2713 1 const int m_panelWidth = mgr.m_panelWidth;
2714 1 const int m_panelHeight = mgr.m_panelHeight;
2715 1 const uint32_t rowLen = mgr.rowLen;
2716 1 const uint32_t maxRowLen = mgr.maxRowLen;
2717 1 const int m_numOutputSlots = mgr.m_numOutputSlots;
2718
2719 // Full height layouts map every row directly and have no interleave; a
2720 // handler for them cannot exist (scan == height leaves no room for the
2721 // folded columns it checks for), so only the split layout asks for one.
2722 1 PanelInterleaveHandler* handler = nullptr;
2723
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!mgr.m_dataLayout) {
2724
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 handler = PanelInterleaveHandler::createHandler(mgr.m_panelInterleave, m_panelWidth, m_panelHeight, mgr.m_panelScan);
2725
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!handler) {
2726 ✗ LogErr(VB_CHANNELOUT, "Failed to create panel interleave handler\n");
2727 ✗ return false;
2728 }
2729 }
2730
2731
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 uint32_t* channelOffsets = new uint32_t[m_channelCount];
2732 1 memset(channelOffsets, 0xFF, m_channelCount * sizeof(uint32_t));
2733
2734 1 int pixelStride = m_numOutputSlots * 6;
2735 1 int totalRowLen = rowLen * pixelStride;
2736
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 for (int output = 0; output < mgr.m_numOutputs; output++) {
2737 8 int panelsOnOutput = m_panelMatrix->m_outputPanels[output].size();
2738 // For 16 outputs, bank1 data starts at offset 48 (= 8 slots * 6 colors)
2739 // so that ISPC's 16-lane reduce_add packs bank0/bank1 pairs correctly:
2740 // bank0 channels occupy slots 0-47, bank1 channels occupy slots 48-95
2741 8 int outputIdx = mgr.outputPin[output] + mgr.outputBank[output] * 48;
2742
2743
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
9 for (int i = 0; i < panelsOnOutput; i++) {
2744 1 int panel = m_panelMatrix->m_outputPanels[output][i];
2745 1 int c = m_panelMatrix->m_panels[panel].chain;
2746 // the chain position is measured from the far end of the LONGEST
2747 // chain on the cape, so a matrix with a shorter chain than another
2748 // simply shifts the leading pixels off the end of its own chain
2749 1 int chain = mgr.m_longestChain - c - 1;
2750 1 int xOff = chain * maxRowLen;
2751
2752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (mgr.m_dataLayout) {
2753 // Full height layout: lane 1 and lane 2 carry the two halves
2754 // of the SAME row. Layout 2 puts the left half on lane 1,
2755 // layout 1 swaps them. Every row is its own scan address, so
2756 // yOut is just y and the row is half a panel wide.
2757 ✗ const int half = m_panelWidth / 2;
2758 ✗ const int leftLane1 = (mgr.m_dataLayout == 2);
2759 ✗ for (int y = 0; y < m_panelHeight; y++) {
2760 ✗ int yw = y * m_panelWidth * 3;
2761 ✗ for (int x = 0; x < half; ++x) {
2762 ✗ int lane1x = leftLane1 ? x : x + half;
2763 ✗ int lane2x = leftLane1 ? x + half : x;
2764 ✗ uint32_t r1 = m_panelMatrix->m_panels[panel].pixelMap[yw + lane1x * 3];
2765 ✗ uint32_t g1 = m_panelMatrix->m_panels[panel].pixelMap[yw + lane1x * 3 + 1];
2766 ✗ uint32_t b1 = m_panelMatrix->m_panels[panel].pixelMap[yw + lane1x * 3 + 2];
2767
2768 ✗ uint32_t r2 = m_panelMatrix->m_panels[panel].pixelMap[yw + lane2x * 3];
2769 ✗ uint32_t g2 = m_panelMatrix->m_panels[panel].pixelMap[yw + lane2x * 3 + 1];
2770 ✗ uint32_t b2 = m_panelMatrix->m_panels[panel].pixelMap[yw + lane2x * 3 + 2];
2771
2772 ✗ int yOut = y;
2773 ✗ int xOut = x + xOff;
2774 ✗ if (mgr.isPWMPanel()) {
2775 ✗ int xo2 = xOut % 16;
2776 ✗ int xo3 = xOut / 16;
2777 ✗ xOut = xo2 * (rowLen / 16) + xo3;
2778 }
2779 ✗ channelOffsets[r1] = yOut * totalRowLen + xOut * pixelStride + outputIdx;
2780 ✗ channelOffsets[g1] = yOut * totalRowLen + xOut * pixelStride + outputIdx + 8;
2781 ✗ channelOffsets[b1] = yOut * totalRowLen + xOut * pixelStride + outputIdx + 16;
2782 ✗ channelOffsets[r2] = yOut * totalRowLen + xOut * pixelStride + outputIdx + 24;
2783 ✗ channelOffsets[g2] = yOut * totalRowLen + xOut * pixelStride + outputIdx + 32;
2784 ✗ channelOffsets[b2] = yOut * totalRowLen + xOut * pixelStride + outputIdx + 40;
2785 }
2786 }
2787 ✗ continue;
2788 ✗ }
2789
2790
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1 times.
17 for (int y = 0; y < (m_panelHeight / 2); y++) {
2791 16 int yw1 = y * m_panelWidth * 3;
2792 16 int yw2 = (y + (m_panelHeight / 2)) * m_panelWidth * 3;
2793
2794
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 16 times.
1040 for (int x = 0; x < m_panelWidth; ++x) {
2795 1024 uint32_t r1 = m_panelMatrix->m_panels[panel].pixelMap[yw1 + x * 3];
2796 1024 uint32_t g1 = m_panelMatrix->m_panels[panel].pixelMap[yw1 + x * 3 + 1];
2797 1024 uint32_t b1 = m_panelMatrix->m_panels[panel].pixelMap[yw1 + x * 3 + 2];
2798
2799 1024 uint32_t r2 = m_panelMatrix->m_panels[panel].pixelMap[yw2 + x * 3];
2800 1024 uint32_t g2 = m_panelMatrix->m_panels[panel].pixelMap[yw2 + x * 3 + 1];
2801 1024 uint32_t b2 = m_panelMatrix->m_panels[panel].pixelMap[yw2 + x * 3 + 2];
2802 1024 int yOut = y;
2803 1024 int xOut = x;
2804 1024 handler->map(xOut, yOut);
2805 1024 xOut += xOff;
2806
2807
2/4
✓ Branch 0 taken 1024 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1024 times.
1024 if (mgr.isPWMPanel()) {
2808 // For PWM panels, the first of each group of 16 pixels is out first,
2809 // then the second of each group of 16, etc...
2810 ✗ int xo2 = xOut % 16;
2811 ✗ int xo3 = xOut / 16;
2812
2813 ✗ xOut = xo2 * (rowLen / 16) + xo3;
2814 }
2815 // Color stride is always 8: within each bank, 8 slots per color channel.
2816 // For 8 outputs: outputIdx=pin(0-7), channels at +0,+8,+16,+24,+32,+40
2817 // For 16 outputs bank0: same; bank1: outputIdx=pin+48, same offsets
2818 1024 channelOffsets[r1] = yOut * totalRowLen + xOut * pixelStride + outputIdx;
2819 1024 channelOffsets[g1] = yOut * totalRowLen + xOut * pixelStride + outputIdx + 8;
2820 1024 channelOffsets[b1] = yOut * totalRowLen + xOut * pixelStride + outputIdx + 16;
2821 1024 channelOffsets[r2] = yOut * totalRowLen + xOut * pixelStride + outputIdx + 24;
2822 1024 channelOffsets[g2] = yOut * totalRowLen + xOut * pixelStride + outputIdx + 32;
2823 1024 channelOffsets[b2] = yOut * totalRowLen + xOut * pixelStride + outputIdx + 40;
2824 }
2825 }
2826 }
2827 }
2828
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 delete handler;
2829
2830 // Build the scatter map sorted by destination offset so the prep loop
2831 // writes sequentially; the random accesses become byte reads, which the
2832 // cache handles far better than random read-modify-write stores.
2833 // Channels not covered by any panel (partially invalid layouts, unused
2834 // canvas regions) still have the 0xFFFFFFFF fill from above and are left
2835 // out of the map; writing through the marker used to crash. The stable
2836 // sort keeps the original channel order for duplicated offsets so the
2837 // last writer still wins.
2838 1 std::vector<std::pair<uint32_t, uint32_t>> map;
2839
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 map.reserve(m_channelCount);
2840 1 uint32_t unmapped = 0;
2841
2/2
✓ Branch 0 taken 6144 times.
✓ Branch 1 taken 1 times.
6145 for (int x = 0; x < m_channelCount; x++) {
2842
1/2
✓ Branch 0 taken 6144 times.
✗ Branch 1 not taken.
6144 if (channelOffsets[x] != 0xFFFFFFFF) {
2843
1/2
✓ Branch 0 taken 6144 times.
✗ Branch 1 not taken.
6144 map.emplace_back(channelOffsets[x], x);
2844 } else {
2845 ✗ ++unmapped;
2846 }
2847 }
2848
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (unmapped) {
2849 ✗ LogWarn(VB_CHANNELOUT, "BBShiftPanel: %u of %u channels are not mapped to any panel; check the panel layout\n",
2850 unmapped, m_channelCount);
2851 }
2852
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 std::stable_sort(map.begin(), map.end(),
2853 45051 [](const std::pair<uint32_t, uint32_t>& a, const std::pair<uint32_t, uint32_t>& b) { return a.first < b.first; });
2854
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 m_scatterOffsets.resize(map.size());
2855
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 m_scatterSrc.resize(map.size());
2856
2/2
✓ Branch 0 taken 6144 times.
✓ Branch 1 taken 1 times.
6145 for (size_t x = 0; x < map.size(); x++) {
2857 6144 m_scatterOffsets[x] = map[x].first;
2858 6144 m_scatterSrc[x] = map[x].second;
2859 }
2860
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 delete[] channelOffsets;
2861 1 return true;
2862 1 }
2863
2864 1 void BBShiftPanelOutput::setupGamma() {
2865 1 auto& mgr = BBShiftPanelManager::INSTANCE;
2866 // The OE on-time is shared by every matrix on the cape, so a matrix that
2867 // asked for less brightness than the cape ended up running at makes up the
2868 // difference here. With one matrix, or with matrices that agree, the
2869 // ratio is 1.0 and the table is unchanged.
2870 1 float brightnessScale = 1.0f;
2871
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (mgr.m_brightness > 0 && m_brightness < mgr.m_brightness) {
2872 ✗ brightnessScale = (float)m_brightness / (float)mgr.m_brightness;
2873 }
2874
2875 1 int colorDepth = mgr.m_colorDepth;
2876
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (mgr.isPWMPanel()) {
2877 // we are outputting 16 bit data as that's what the
2878 // PWM registers require, but only the bottom 12 bits are used
2879 ✗ colorDepth = 12;
2880 }
2881
2882 1 GammaLUT::BuildForColorDepth(gammaCurve, GammaLUT::Clamp(m_gamma, 2.2f), colorDepth, brightnessScale);
2883 /*
2884 for (int x = 0; x < 256; x++) {
2885 printf("%d: %04x\n", x, gammaCurve[x]);
2886 }
2887 */
2888 1 }
2889
2890 ✗ void BBShiftPanelOutput::OverlayTestData(unsigned char* channelData, int cycleNum, float percentOfCycle, int testType, const Json::Value& config) {
2891 ✗ for (int output = 0; output < BBShiftPanelManager::INSTANCE.numOutputs(); output++) {
2892 ✗ int panelsOnOutput = m_panelMatrix->m_outputPanels[output].size();
2893 ✗ for (int i = 0; i < panelsOnOutput; i++) {
2894 ✗ int panel = m_panelMatrix->m_outputPanels[output][i];
2895
2896 ✗ m_panelMatrix->m_panels[panel].drawTestPattern(channelData + m_startChannel, cycleNum, percentOfCycle, testType);
2897 }
2898 }
2899 ✗ }
2900
2901 2 void BBShiftPanelOutput::GetRequiredChannelRanges(const std::function<void(int, int)>& addRange) {
2902 2 addRange(m_startChannel, m_startChannel + m_channelCount - 1);
2903 2 }
2904
2905 1 void BBShiftPanelOutput::DumpConfig(void) {
2906 1 LogDebug(VB_CHANNELOUT, "BBShiftPanelOutput::DumpConfig()\n");
2907 1 LogDebug(VB_CHANNELOUT, " Width : %d\n", m_width);
2908 1 LogDebug(VB_CHANNELOUT, " Height : %d\n", m_height);
2909 1 LogDebug(VB_CHANNELOUT, " Longest Chain : %d\n", m_longestChain);
2910 1 LogDebug(VB_CHANNELOUT, " Inverted Data : %d\n", m_invertedData);
2911 1 LogDebug(VB_CHANNELOUT, " Brightness : %d\n", m_brightness);
2912 1 LogDebug(VB_CHANNELOUT, " Gamma : %f\n", m_gamma);
2913 1 BBShiftPanelManager::INSTANCE.dumpConfig();
2914
2915 1 ChannelOutput::DumpConfig();
2916 1 }
2917
2918 1 void BBShiftPanelManager::dumpConfig() {
2919
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogDebug(VB_CHANNELOUT, " Shared cape config (%d matri%s):\n",
2920 (int)m_members.size(), m_members.size() == 1 ? "x" : "ces");
2921 1 LogDebug(VB_CHANNELOUT, " Panel Width : %d\n", m_panelWidth);
2922 1 LogDebug(VB_CHANNELOUT, " Panel Height : %d\n", m_panelHeight);
2923 1 LogDebug(VB_CHANNELOUT, " Color Depth : %d\n", m_colorDepth);
2924 1 LogDebug(VB_CHANNELOUT, " Longest Chain : %d\n", m_longestChain);
2925 1 LogDebug(VB_CHANNELOUT, " Cape Brightness: %d\n", m_brightness);
2926 1 LogDebug(VB_CHANNELOUT, " Output Rows : %d\n", numRows);
2927 1 LogDebug(VB_CHANNELOUT, " Output Length : %d\n", rowLen);
2928 1 LogDebug(VB_CHANNELOUT, " Num Outputs : %d (%d slots)\n", m_numOutputs, m_numOutputSlots);
2929
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 LogDebug(VB_CHANNELOUT, " Addressing Mode: %d %s\n", m_addressingMode, isPWMPanel() ? "PWM" : "Shift");
2930
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!m_strideSchedule.empty()) {
2931 1 std::string sched;
2932
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1 times.
19 for (auto& slot : m_strideSchedule) {
2933
7/12
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 18 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 18 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 18 times.
✗ Branch 11 not taken.
18 sched += " " + std::to_string(slot.bit) + (slot.primary ? "" : "*") + "/" + std::to_string(slot.onTime);
2934 }
2935
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogDebug(VB_CHANNELOUT, " Stride Schedule:%s\n", sched.c_str());
2936 1 }
2937 1 }
2938