Jump to content
The simFlight Network Forums

Recommended Posts

Posted

Documentation for the Lua control function in MobiFlight seems to be a bit thin, I only found this: https://www.mobiflight.com/forum/message/10232.html

It is lacking the LuaValue function.

I have found that alternating turns of the encoders for MHz and kHz "unfreezes" the frequencies and they do advance instead of just moving one notch back or one notch forth,

I could not assert it through the error logging (problably, because it is not an error), but the reason might be that if turning to increase the value, the feed value is set to N. And when increasing further, the same feed value is being sent.

Event.param, if I have understood it correctly, monitors if the value changes. But this is not the case when the same value is being sent.

By the way, I am monitoring both offsets 0x0D6C and 0x0D70, and the feed value is shown only in 0x0D6C.

I have tried to adapt the script to "event.offset" so 0x0D6C is monitored and the script is run when it changes, but this also does not help, because if a turn in let's say the right direction always sends 10, then the offset or the param does not change.

I have also tried to reset 0x0D6C to 0 after every input, but it does not work, yet. I will keep trying.

Posted
14 minutes ago, shorthauler said:

Documentation for the Lua control function in MobiFlight seems to be a bit thin, I only found this: https://www.mobiflight.com/forum/message/10232.html

It is lacking the LuaValue function.

Yes, as is the FSUIPC offset documentation for offset 0x0D70 (which is where that comes from). As I said, this is an omission error but it works - as I keep saying, you MUST use LuaValue, and not just Lua.  Please read the FSUIPC offset documentation if using FSUIPC offsets (via MobiFlight).

14 minutes ago, shorthauler said:

I have found that alternating turns of the encoders for MHz and kHz "unfreezes" the frequencies and they do advance instead of just moving one notch back or one notch forth,

But using what - Lua or LuaValue?

14 minutes ago, shorthauler said:

I could not assert it through the error logging (problably, because it is not an error), but the reason might be that if turning to increase the value, the feed value is set to N. And when increasing further, the same feed value is being sent.

It is not error logging (errors are always logged), but debug logging you need. This will show you every line executed in the lua script. 

14 minutes ago, shorthauler said:

Event.param, if I have understood it correctly, monitors if the value changes. But this is not the case when the same value is being sent.

 Event.param does not monitor for value changes, but is called whenever LuaValue is called on the script, using the parameter provided, which when called via 0x0D70 will be the value in 0x0D6C.

14 minutes ago, shorthauler said:

I will keep trying.

The ONLY thing you need to try is using LuaValue to call the script.

 

Posted

Thanks a lot for your advice. Yes, I am using LuaValue to call the script. Let me describe in my words what is happening:

Turning the left encoder clockwise sends a feed value of 10 to 0x0D6C. I am monitoring this offset, so I can see that 10 is stored there. As per the script, the MHz value increases by 1 MHz. Any further clockwise turn does not increase the MHz value any further. It remains as it is. I can, however decrease the MHz value by 1 MHz (counterclockwise turn, feed value = 20). But after this, I cannot decrease any further.

However, this is possible: Clockwise turn with left encoder increases the MHz value by 1. Then,  with a turn (clockwise resp. counterclockwise) increase resp. decrease the kHz value by 50 kHz. Only after having done so, I can increase the MHz value further as it it had been reset.

It seems to me that once a certain feed value is set, running the script again and sending the same feed value via LuaValue does not do anything (the log file does not even produce further lines).

Subsequent increments of decrements of MHz are only possible if there has been an increment or decrement of the kHz in between.

 

Posted

Rather than using LuaValue and event.param, switch to using flags.

Change the  event.param call to use event.flag, and add 1 call for each flag used (the flag replaces the feed_value/ ipcPARAM value):

--
-- function to convert to BCD
--
function convertToBCD(freq)					
    local cleanFreq = freq:gsub("%.", "")  -- Remove decimal (e.g., "109.2" → "0920")
    local decimalNumber = tonumber(cleanFreq) -- Convert to number
		
    if not decimalNumber then return 0 end -- Return 0 if conversion fails

    local bcd = 0
    local shift = 0

    -- Convert each decimal digit to BCD format
    while decimalNumber > 0 do
        local digit = decimalNumber % 10
        bcd = bcd + (digit * (16 ^ shift)) -- Use base 16 shift instead of bitwise left shift
        decimalNumber = math.floor(decimalNumber / 10)
        shift = shift + 1
    end

    return bcd
end

function updateNav1(flag)
    currentNav1BCD = ipc.readUW(0x0350) -- read the current NAV1 frequency
    currentNav1 = string.format("%04X", currentNav1BCD)  -- convert BCD to number (as string)

    if flag==1 then  -- if turned right, the left encoder (MHz) sends the value 10 to offset 0x0D6C
        -- the following lines increase the MHz value by 1 MHz and wrap the frequency around, substracting 900, when 117 MHz is reached
        if tonumber(currentNav1) >= 1700 then
          newNav1 = currentNav1 - 900  -- currentNav1 will be converted to a number
        else
          newNav1 = currentNav1 + 100
        end
    elseif flag==2 then    -- if turned left, the left encoder (MHz) sends the value 20 to offset 0x0D6C
	    -- the following lines decrease the MHz value by 1 MHz and wrap the frequency around, adding 900, when 108.95 MHz is reached
        if tonumber(currentNav1) <= 895 then 
          newNav1 = currentNav1 + 900  -- currentNav1 will be converted to a number
        else
          newNav1 = currentNav1 - 100
        end
    elseif flag==3 then    -- if turned right, the right encoder (kHz) sends the value 30 to offset 0x0D6C
        if string.find(currentNav1, "%d%d95") then	-- this line checks whether the frequency ends with 95 and ...
            newNav1 = currentNav1 - 95		-- ... if so, substractss 95 kHz so as to prevent the frequency from carrying (changing the MHz value) when digit wraps. So you can turn the encoder for the kHz value seperately without increasing the MHz value.
        else
            newNav1 = currentNav1 + 5
        end
    else    -- if turned left, the right encoder (kHz) sends the value 40 to offset 0x0D6C, which for now is "else"
        if string.find(currentNav1, "%d%d00") then	-- this line checks whether the frequency ends with 00 and ...
            newNav1 = currentNav1 + 95		-- ... if so, adds 95 kHz so as to prevent the frequency from carrying (changing the MHz value) when digit wraps. So you can turn the encoder for the kHz value seperately without decreasing the MHz value.
        else
            newNav1 = currentNav1 - 5
        end
    end

    newNav1_BCD = convertToBCD(tostring(newNav1))	-- convert to BCD
    ipc.control(65708, newNav1_BCD)	-- sending BCD to FSUIPC
end
                                      
event.flag(1,"updateNav1")
event.flag(2,"updateNav1")
event.flag(3,"updateNav1")
event.flag(4,"updateNav1")
ipc.log("Nav1 update lua script now running")

Then write the flag value (1,2,3 or 4) to offset 0x0D6C, and use the LuaToggle control when writing to offset 0x0D70.

John 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.