Jump to content
The simFlight Network Forums

Displaying ADF frequency (offsets 02D4 and 02D6)

Recommended Posts

Since on aircraft I'm flying the ADF frequency is barely visible, especially the fractions part, I've written this piece of "dirty" code to display it on the screen:

-- Converts decimal values stored at offsets 02D4 & 02D6 to hex
function dec2hex(num)

    local a = {}
    local b = {}
    local i = 0
    num = tonumber(num)
    while num > 0 do
         local q = math.floor(num / 16)  -->> find quotient
	 local r = num - q * 16          -->> find remainder

	 if r == 10 then r = "A"                -->> this IF part is not required anyway due to offset limits but it makes the converter sort of complete!
	    elseif r == 11 then r = "B"
	    elseif r == 12 then r = "C"
	    elseif r == 13 then r = "D"
	    elseif r == 14 then r = "E"
	    elseif r == 15 then r = "F"

         num = q
         i = i + 1
         a[i] = r

   -- reverse the array elements
   for j = 1,i do
      b[j] = a[i]
      i = i - 1

   -- join the array elements to a string
   return table.concat(b,"")


adf = ipc.readUW(0x02D4)
adf_f = ipc.readUW(0x02D6)

if adf_f == 0 then                             -->> Do I need to modify this IF part?!
   adf = dec2hex(adf)
elseif adf_f == 256 then
   adf = dec2hex(adf) + 1000
elseif adf_f > 256 then
   adf = dec2hex(adf) + 1000 + (adf_f - 256)/10
elseif adf_f > 0 and adf_f < 256 then
   adf = dec2hex(adf) + adf_f/10

ipc.display("ADF = "..adf.." kHz")

The code goes into a WHILE loop and so far works ok. What I really wonder is how I can modify the IF section indicated above to read hi / low bytes at offset 02D6 properly (instead of doing this "dirty" hex / decimal addition) and amend to the value of offset 02D4 converted to hex?


I'm sure the answer is really simple but I can't figure the better approach yet...


Link to comment
Share on other sites

Maybe this will help - referring to THIS post & then splitting the upper and fractions into their own variable by reading them as byte, per offset,  instead of word. This will help in reading ADF1 at offsets 0x034C & 0x0356 - not contiguous.  


Quick & dirty, not tested at all. The formatting may be off due to not testing.

adf2_main = ipc.readUW(0x02D4)
adf2_frac = ipc.readUB(0x02D6)
adf2_upper = ipc.readUB(0x02D7)

adf2_main = string.format("%3X", adf2_main)
adf2_frac = string.format("%01X", adf2_frac)
adf2_upper = string.format("%1X", adf2_upper)

ipc.display("ADF2 = " .. adf2_upper .. adf2_main .. "." .. adf2_frac .. " kHz")
  • Upvote 1
Link to comment
Share on other sites

The power of C libraries... :) Amazing solution, thanks a million!!!
I took the liberty to slightly modify your code - otherwise I see a blank spot instead of a zero:

adf2_main = string.format("%03X", adf2_main) 

Thanks again, Roman!!!

Link to comment
Share on other sites

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.