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"
end

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

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

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

end

if adf_f == 0 then                             -->> Do I need to modify this IF part?!
end

ipc.sleep(50)
```

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...

Thanks!

##### 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)

```
• 1
##### 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!!!

##### Share on other sites

This should be more efficient, it will get all the ADF1 & 2 data in one read and place the data into the variables as needed.

`adf2_main, adf2_frac, adf2_upper, adf1_main, adf1_frac, adf1_upper = ipc.readStruct(0x02D4, 1UW, 2UB, 0x034C, 1UW, 0x0356, 2UB)`

## Create an account

Register a new account