Using encoder to drive a value back and forth within a given range - MobiFlight/MobiFlight-Connector GitHub Wiki

Challenge

For many things, expecially in MSFS2020, but also in X-plane, there are presets that are tailored for a specific input value, and they have a built in "wrap around" for a value, like a heading selector wraps around to 0 when you increase the value from 359, or the altitude preselect might have some reasonable lower and upper limits.

But what if you need to have a range you define yourself, or if you need to input a value in the sim that does not have an INC and DEC event, but just a "set" event that just expects an input value and just expects it to be in the correct range? Or if you need to increase or decrease the value by more than just one, but the event code in the simulated aircraft does not expect the input parameter to be something else than 1 or -1?

This is when it might be handy to use a transform modifier, but this is not possible for MSFS2020 or X-plane presets.

Solution, part one: Reading the encoder into an intermediate variable

We are using a MobiFlight variable to store our trim setting, and safeguarding that the value stays within our desired range. MobiFlight Variable input allows for a Transform modifier in the input side, and we can make sure we are not trying to adjust the trim beyond the allowed range. We divide our problem into two halves and conquer each of them separately.

[!TIP] MobiFlight variables are a way to store information that is not sent to the simulator, but can be read by other configuration entries later. A bit like a postit note you might leave on your fridge door to let others know there is leftover pizza if someone is hungry. Whoever needs this information can look at the note and read what's stored in it.

Here are the requirements:

  • Our trim value needs to be between -100 and 100
  • Each click of the encoder should increment or decrement the value by one

So lets create an input configuration for our encoder first. We need to give our variable a name, lets call it RudderTrimSetting. This can be whatever you want to use.

Notice that under the "Value" field, it reads "Supports variable value($), input value (@) and placeholders (?,#, etc.)". This field also works like the "Transform" modifier in Output configs. So we can also write some NCalc formulas. NCalc is a bit like the formulas that you can input on a spreadsheet cells, and we are going to make a logical IF here. In these fields "$" means the current value of the config, and we will make the following formula to the "On Left" field:

if($ > -100, $-1, -100)

This basically is a if(check, then, else) function that checks IF our value ($) is greater than (>) -100 (our lowest limit), THEN decreases the value by one. ELSE it just sets the value to the lowest limit of -100. So we set it on the config:

image

We can then click the "Copy" button, and select the On Right tab to do the same for the other direction of rotation, but instead if decrementing the value, we increment:

if($ < 100, $+1, 100)

So we check if we are at the upper limit already, and if not, we add one. For more thorough explanation of NCalc syntax and what functions are available, please see the NCalc documentation.

To see our MobiFlight variable's value, we need to create an Output config for it. Add a new output config, and set its Sim Variable to "MobiFlight Variable" and pick our RudderTrimSetting from the dropdown list. When you press "Run" in MobiFlight, you can test the variable.

Here we can see that by turning the encoder left, to decrease the value, the lowest we can reach is -100 as we wanted. And the same works for turning right. image

Part two: Setting the simulator trim according to our variable value

The first part, like we did above, to see our value is super simple, we just set the Sim Variable to point to our MobiFlight Variable: image

We could show this value on a 7 segment display, but it would not make much sense in terms of flying the aircraft, so we need to do something else. And here we use a bit of a shortcut, that might feel strange at first, since we are in a Output configuration. We want, whenever the MobiFlight variable value changes, to send the value to the simulator's trim. So we select the Input Action type for our display. This basically creates an input that we can send to the simulator, and since we have already taken care of our value's range limits, we can just use a "set value" event preset, "Rudder Trim Set" in this case. I searched for "trim" and used the "Generic" aircraft to narrow down the search results, and picked this one that does expect the range in the -100 to 100 range like we set things up with.

image

There is just one issue - our trim valuye is already neatly contained within the -100 and 100 range, and the preset code expects wrong, as you can see in the screenshot marked with red.** So click the "Show preset code" checkbox**, and we need to edit the code to be just this:

@ (>K:RUDDER_TRIM_SET)

And everything should work. Let's test!

Variations

So it worked great, except that in the MSFS2020 Asobo Beechraft Baron the rudder trim seems to move left when the value increases, and right when it decreases, so my encoder works backwards. This can be solved by swapping around the OnLeft and OnRight events for example.

And if you want to increment faster or slower, just adjust the code in the OnLEft and OnRight events to add and subtract 2 or 4 for example instead of 1.