Cockpit lights logic for warning lights, power and lights test function - MobiFlight/MobiFlight-Connector GitHub Wiki
An aircraft warning light reacts to two things (or well, three): electrical power, the actual thing it is a warning for, like "Engine 1 is on fire", and in many cases a lights test button that is useful during the preflight checks to see that all warning lights are working.
For obvious reasons the warning light test button cannot set the engine on fire for real, so that the fire warning light can be checked, so there is often a separate, parallel logic using a different simulator variable just for the test case. Some aircraft, like the PMDG 737 for MSFS2020 have the simulator variables for warning lights also react to the light test function, but many times this is not the case, and the warning light objects in the virtual cockpits check for the light test internally, and this is not visible to MobiFlight directly.
Additional checks
Most of the time we thus need to do the same logic in MobiFlight: either read the "light test" state from the simulator, if it is available, or do such state variable ourselves using a mobiflight variable. We also need to check that the aircraft panel actually is powered up. These can differ per airplane a bit, but "Microsoft > Generic > Miscellaneous > CIRCUIT GENERAL PANEL ON" is a good starting point. This will also make sure your cockpit is "cold and dark" when power is off, all lights work only when you have power.
[!NOTE]
You will learn a new concept: Config references. A config reference lets you use the value of another output config item in your logic.
Config References
While the actual config refers to a simvar like our "ENGINE IS ON FIRE!!", config references lets you bring in additional things you can compare and check your simvar against.
Output configuration without a LED becomes handy
While outputs usually are tied to actual leds or displays, it's often also useful to just configure some simulator variables as "outputs" but just leave the LED / display hardware unconfigured. They will still work for our purposes. You can see their state in the "Flight Sim value" column, and they should be either 0 or 1 depending on the state of panel power, and whether the "lights test" button is pressed. And by using those configs from other configs, you can create more complicated schemes for things like electricity and so on.
Use these two things as additional config references for your warning light, you can very easily take care of the two additional cases your warning light should react to.
Now, the image above has a lot to unpack, but pay attention to two things:
- the Config references and their symbols A and B, and
- Transform Modifier with the cryptic code we will explain next
The config references are on the bottom: panel is powered and light test. Those are given two symbols, they are by default special characters like ! and # but they can be changed, and I find A and B to be more clear in the example. Just remember the symbols cannot be characters that are used in other function names etc in the Transform, like if you use Round() you should not use "R" as a reference name.
In the Transform field, "$" refers always to the sim variable the current config is referring to, the "ENG ON FIRE" state in this case.
Then we have logical OR, represented by the pipe character "|" between $ and A, basically stating "light this light up if engine is on fire, OR, if lights test is pressed".
The final multiplication is with the logical state of power: If panel power is zero, multiplication results in a zero, so there is no light no matter how much fire there is on the engine, or if the test button is being pressed.
The transform field uses NCalc mathematical expression library that has quite a few useful functions and operators, you can check out the NCalc GitHub page for more documentation on the syntax.