GCC Code Coverage Report


Directory: ./
File: channeloutput/PixelString.h
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 10 10 100.0%
Functions: 5 5 100.0%
Branches: 4 6 66.7%

Line Branch Exec Source
1 #pragma once
2 /*
3 * This file is part of the Falcon Player (FPP) and is Copyright (C)
4 * 2013-2022 by the Falcon Player Developers.
5 *
6 * The Falcon Player (FPP) is free software, and is covered under
7 * multiple Open Source licenses. Please see the included 'LICENSES'
8 * file for descriptions of what files are covered by each license.
9 *
10 * This source file is covered under the LGPL v2.1 as described in the
11 * included LICENSE.LGPL file.
12 */
13
14 #include <cmath>
15 #include "fpp-json.h"
16 #include <list>
17 #include <stdint.h>
18 #include <string>
19 #include <vector>
20
21 #include "ColorOrder.h"
22
23 // Brightness/gamma lookup tables are a pure function of (brightness, gamma) -
24 // see ReadVirtualString - and it is very common for many ports to carry the
25 // same settings, a whole megatree being the obvious case. Handing those a
26 // single shared table instead of a private copy each keeps the tables the
27 // frame interleave is walking small enough to stay resident, and drops the
28 // memory from one table per virtual string to one per distinct setting.
29 //
30 // Sharing is also why inversion cannot flip a table in place any more: the
31 // inverted form is just a different cached table.
32 //
33 // Tables live for the life of the process. They are only ever built during
34 // output Init and are read-only afterwards, so the lock is uncontended.
35 class BrightnessLUTCache {
36 public:
37 // brightness is 0-100; inverted returns the complemented table used by a
38 // line that idles high. Never returns null.
39 static const uint8_t* get(int brightness, float gamma, bool inverted);
40
41 // The 16 bit form for parts like UCS8903. Sequence data is always 8 bit
42 // per channel, so this is where the extra range comes from: the same
43 // brightness/gamma curve evaluated to 65535 instead of 255. That is the
44 // whole point of a 16 bit part - at gamma 2.2 the low 8 bit inputs all
45 // round to the same output, and expanding at the wider width keeps them
46 // distinct. Simply duplicating the byte would buy nothing.
47 static const uint16_t* getWide(int brightness, float gamma, bool inverted);
48
49 // distinct tables built so far - diagnostics and tests
50 static size_t tableCount();
51 };
52
53 class VirtualString {
54 public:
55 VirtualString();
56 VirtualString(int receiverType, int receiverNum);
57 1730 ~VirtualString() {};
58
59 int channelsPerNode() const;
60 // Every channel the string occupies on the wire: its null nodes count,
61 // they are in the output map the same as pixels are.
62 int channelCount() const;
63
64 bool isSmartReceiver() const;
65 bool isFalconV5SmartReceiver() const;
66
67 int startChannel;
68 int pixelCount;
69 int groupCount;
70 int reverse;
71 FPPColorOrder colorOrder;
72 int startNulls;
73 int endNulls;
74 int zigZag;
75 int brightness;
76 float gamma;
77 // owned by BrightnessLUTCache and shared with every virtual string that
78 // has the same brightness/gamma/inversion - do not write through it
79 const uint8_t* brightnessMap;
80 // the 16 bit form of the same curve; only set on a 16 bit port
81 const uint16_t* brightnessMap16 = nullptr;
82
83 int8_t receiverNum;
84 uint8_t leadInCount;
85 uint8_t toggleCount;
86 uint8_t leadOutCount;
87 std::string description;
88
89 int* chMap;
90 int chMapCount;
91 };
92
93 class GPIOCommand {
94 public:
95 856 GPIOCommand(int p, int offset, int tp = 0, int bo = 0) :
96 856 port(p),
97 856 channelOffset(offset),
98 856 type(tp),
99 856 bitOffset(bo) {}
100 3907 ~GPIOCommand() {}
101
102 int port;
103 int channelOffset;
104 int type; // 0 for off, 1 for on
105 int bitOffset;
106 };
107
108 class PixelString {
109 public:
110 PixelString(bool supportsSmartReceivers = false);
111 ~PixelString();
112
113 int Init(Json::Value config, Json::Value* pinConfig = nullptr);
114 void DumpConfig(void);
115 void invertOutput();
116
117 // Pixel protocols whose line idles high and pulses low (the TM18xx
118 // family). The bit cell is otherwise identical to ws2811, so a driver
119 // that can flip the idle level per port drives them alongside normal
120 // strings at no cost - see m_isInverted.
121 static bool isInvertedProtocol(const std::string& protocol);
122
123 // TM1814's C1/C2 words: four 6 bit constant current levels in the chip's
124 // physical W,R,G,B pin order, then the bitwise complement of all four
125 // (the chip refuses to decode the frame without it). milliAmps is per
126 // channel and clamps to the 6.5-38mA the part supports.
127 static std::vector<uint8_t> buildTM1814Preamble(float milliAmps);
128 static bool protocolNeedsTM1814Preamble(const std::string& protocol);
129
130 // Bytes clocked per colour channel: 1 for the usual 8 bit parts, 2 for the
131 // 16 bit ones. The sequence still supplies 8 bits either way - see
132 // BrightnessLUTCache::getWide - so this only changes the wire length.
133 static int protocolBytesPerChannel(const std::string& protocol);
134
135 // The bit cell a protocol wants, in ns. t0/t1 are how long the line is
136 // driven for a zero and a one; period is the whole cell. These are the
137 // instants every port on a PRU shares, so a driver can only honour one set
138 // at a time - see BBShiftString, which derives the controller's timing
139 // from the ports in use and refuses to mix.
140 struct Timing {
141 int t0Ns;
142 int t1Ns;
143 int periodNs;
144 61 bool operator==(const Timing& o) const {
145
4/6
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 58 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 58 times.
✗ Branch 5 not taken.
61 return t0Ns == o.t0Ns && t1Ns == o.t1Ns && periodNs == o.periodNs;
146 }
147 };
148 static Timing protocolTiming(const std::string& protocol);
149
150 // Set the wire length from a count of colour channels. The licensed
151 // output clamps rebuild that count from the (reduced) virtual strings, so
152 // it is channels and excludes both the protocol preamble and any 16 bit
153 // widening - assigning it straight to m_outputBytes would quietly drop
154 // them, so route it through here.
155 void setPixelDataChannels(int channels);
156
157 // Drop a preamble the driver cannot honour and shorten the port back to
158 // plain pixel data, keeping m_outputBytes and the gpio commands in step.
159 // Without this the leading bytes would offset every pixel on the port.
160 void dropPreamble();
161
162 int m_portNumber;
163 int m_channelOffset;
164 // bytes clocked out on this port, including any protocol preamble
165 int m_outputBytes;
166 uint8_t* m_outputBuffer;
167
168 // Constant bytes the protocol requires ahead of the pixel data - TM1814's
169 // C1/C2 constant current words. Counted in m_outputBytes and parked
170 // permanently at the head of m_outputBuffer, so anything that fills the
171 // buffer just has to start at pixelDataStart() and the preamble comes
172 // along for free.
173 std::vector<uint8_t> m_preamble;
174 4252 uint8_t* pixelDataStart() const { return m_outputBuffer + m_preamble.size(); }
175
176 std::vector<VirtualString> m_virtualStrings;
177 std::vector<GPIOCommand> m_gpioCommands;
178
179 std::vector<int> m_outputMap;
180 const uint8_t** m_brightnessMaps;
181
182 enum class ReceiverType {
183 None = -1,
184 Standard = 0,
185 v1 = 1,
186 v2 = 2,
187 // Falcon differential receivers: FalconV5 is the full bidirectional
188 // protocol (config + queries + listen, needs cape listener support);
189 // FalconV4 only sends the config packet
190 FalconV5 = 3,
191 FalconV4 = 4
192 };
193 ReceiverType smartReceiverType = PixelString::ReceiverType::Standard;
194 bool m_isSmartReceiver = false;
195
196 // The line for this port idles high instead of low. Two independent
197 // things set this and they compose by xor:
198 //
199 // - the cape declaring "inverted" on the port, which compensates for a
200 // differential pair wired backwards (one batch of K16A-B boards had
201 // this on the second line of the first differential group). The far
202 // end sees a correct signal, so smart receivers are unaffected.
203 // - the port's protocol being one of the TM18xx family, which really
204 // does want an inverted line all the way to the pixels. A smart
205 // receiver in that path would see inverted data, so drivers refuse
206 // the combination.
207 //
208 // A TM18xx string on a backwards-wired port cancels out, which is what
209 // the hardware actually does.
210 bool m_isInverted = false;
211 // 1, or 2 for a 16 bit part. Set before the virtual strings are read so
212 // the wire length and the lookup tables agree.
213 int m_bytesPerChannel = 1;
214 // set when m_isInverted came from the protocol rather than the cape, so
215 // drivers can apply the smart receiver restriction to just that case
216 bool m_protocolInverted = false;
217
218 static void AutoCreateOverlayModels(const std::vector<PixelString*>& strings, std::list<std::string>& autoModelNames);
219
220 // returned buffer is owned by the PixelString and reused next frame
221 uint8_t* prepareOutput(uint8_t* channelData);
222
223 private:
224 void SetupMap(int vsOffset, const VirtualString& vs);
225 void FlipPixels(int offset1, int offset2, int chanCount);
226 void DumpMap(const char* msg);
227
228 int ReadVirtualString(Json::Value& vsc, VirtualString& vs) const;
229 void AddVirtualString(const VirtualString& vs);
230 void AddNullPixelString();
231
232 Json::Value m_pinConfig;
233 };
234