Jump to content
The simFlight Network Forums

rotaries.lua


Recommended Posts

Hi!

Does anyone have the rotaries.lua script. It is mentioned in the documentation, but it is not in the download package or installation file. I just wired up a rotary encoder board to my console, and would like to try the virtual buttons for faster rotation of the OBS and HDG. I found some info in the forum, but would like to compare, if any changes.

 

Thanks

Arild E

post-16873-0-40124400-1380277206_thumb.j

post-16873-0-51266300-1380277213_thumb.j

Link to comment
Share on other sites

Does anyone have the rotaries.lua script. It is mentioned in the documentation, but it is not in the download package or installation file. I just wired up a rotary encoder board to my console, and would like to try the virtual buttons for faster rotation of the OBS and HDG. I found some info in the forum, but would like to compare, if any changes.

 

Oops! I wonder how that file dropped out? It certainly used to be there. I've put it back again now. Please re-download the Lua package from the Lua thread in the Download Links subforum.

 

Pete

Link to comment
Share on other sites

I have tried to make this work with my Desktop Aviator encoder panel, but no luck. FSUIPC does not read anything else but the real buttons. I have tried different changes according to other posts, and also other related scripts like IPCready.lua.

 

The encoders are Panasonic type at they are set at 88 ms according to the manual for the 2090 board. However, I did check the site, and it is recently changed to 104 ms. I will test this later.

 

The script was modified for the encoder pairs on my board, and the vendor and product from the HID log.

I have added the script to the AUTO section (FSUPC.ini), and also added buttons for start and kill.

 

Are there anything else I need to do with the FSUIPC.ini. I should probably declare ignore buttons, but I think this is not essential before I can make it read fast and slow.

 

Any advice for problem solving is welcome.

 

Regard

Arild

Link to comment
Share on other sites

Any advice for problem solving is welcome.

 

Regard

Arild

 

Try using the log/debug lua facilities to see what is going on -- see the Logging tab. Even if the timing is alll wrong, if you have the correct device and vendor details you must be getting a virtual button indication in the Buttons & Switches tab. Have you checked the FSUIPC log to see if you are getting a Lua error?

 

Pete

Link to comment
Share on other sites

Hi Pete

 

Thank you for your excellent support. It is probably me not understanding the concept fully. But I have read the forums trying to do it right. But something is missing.

 

Here is the log:

********* FSUIPC4, Version 4.92 by Pete Dowson *********
Running inside FSX on Windows 7
Module base=6E6C0000
User Name="Arild Elverum"
User Addr="arild@xxx"
FSUIPC4 Key is provided
WideFS7 Key is provided
       16 System time = 01/10/2013 07:32:04
       31 FLT UNC path = "\\ARILDS-PC\FSPLANS\"
       78 Trying to connect to SimConnect Acc/SP2 Oct07 ...
       94 FS UNC path = "\\ARILDS-PC\Microsoft Flight Simulator X\"
      999 LogOptions=80000000 00000001
      999 SIM1 Frictions access gained
      999 Wind smoothing fix is fully installed
      999 G3D.DLL fix attempt installed ok
      999 SimConnect_Open succeeded: waiting to check version okay
      999 Trying to use SimConnect Acc/SP2 Oct07
     3261 Running in "Microsoft Flight Simulator X", Version: 10.0.61637.0 (SimConnect: 10.0.61259.0)
     3261 Initialising SimConnect data requests now
     3261 FSUIPC Menu entry added
     3339 \\ARILDS-PC\Microsoft Flight Simulator X\FLIGHTS\OTHER\FLTSIM.FLT
     3339 \\ARILDS-PC\Microsoft Flight Simulator X\SimObjects\Airplanes\Aircreation_582SL\Aircreation_582SL.AIR
    25522 System time = 01/10/2013 07:32:30, Simulator time = 07:32:08 (14:32Z)
    25522 Aircraft="Aircreation582SL red"
    26520 Starting everything now ...
    76378 Advanced Weather Interface Enabled
    78703 LUA.0: beginning "C:\Program Files (x86)\Microsoft Games\Microsoft Flight Simulator X\Modules\Rotaries.lua"
    78703 LUA.0: ...es\Microsoft Flight Simulator X\Modules\Rotaries.lua:1
    78703 LUA.0: Global: ipcPARAM = 0
    78703 LUA.0: ...es\Microsoft Flight Simulator X\Modules\Rotaries.lua:2
    78703 LUA.0: Global: Vendor = 0x16C0
    78703 LUA.0: ...es\Microsoft Flight Simulator X\Modules\Rotaries.lua:3
    78718 LUA.0: Global: Product = 0x27CB
    78718 LUA.0: ...es\Microsoft Flight Simulator X\Modules\Rotaries.lua:10
    78718 LUA.0: Global: Device = 0
    78718 LUA.0: ...es\Microsoft Flight Simulator X\Modules\Rotaries.lua:16
    78718 LUA.0: Global: Rotaries = table: 0D15F4A0
    78812 LUA.0: ...es\Microsoft Flight Simulator X\Modules\Rotaries.lua:18
    78828 LUA.0: Global: dev = 0
    78828 LUA.0: ...es\Microsoft Flight Simulator X\Modules\Rotaries.lua:19
    78828 LUA.0: Could not open HID
    78828 LUA.0: ...es\Microsoft Flight Simulator X\Modules\Rotaries.lua:20
   133756 \\ARILDS-PC\Microsoft Flight Simulator X\SimObjects\Airplanes\Maule_M7_260C\Maule_M7_260C.AIR
   133943 Aircraft="Maule M7 260C paint1"
   180634 Sim stopped: average frame rate for last 46 secs = 21.9 fps
   190945 System time = 01/10/2013 07:35:15, Simulator time = 07:33:08 (14:33Z)
   190945 *** FSUIPC log file being closed
Average frame rate for running time of 61 secs = 22.0 fps
G3D fix: Passes 9758, Null pointers 0, Bad pointers 0, Separate instances 0
Memory managed: 50 Allocs, 50 Freed
********* FSUIPC Log file closed ***********

 

LUA Script:

 

Vendor = "0x16C0"
Product = "0x27CB"
Device = 0  -- Multiple devices of the same name need increasing Device numbers

-- Use separate Lua plug-ins for separate cards!

-- NOTE: this can handle up to 16 rotaries only, using the first 64 "virtual buttons"
-- List the pairs here:

Rotaries = { 0.1,6,7,10,11}

-- Example { 31, 30, 25, 22 } would be 31+30 for one, 25+22 for the next, and so on
-- Which is clockwise and which counterclockwise doesn't matter -- you'll get
-- fast and slow of each in any case

dev, rd, wrf, wr, init = com.openhid(Vendor, Product, Device, Report)

if dev == 0 then
   ipc.log("Could not open HID")
   ipc.exit()
end

-- Set the boundary time in milliseconds between
-- "fast" (shorter) and "slow" (longer) signalling

FastTimeLimit = 60 -- Adjust to taste

-- Polling time in milliseconds: should be much shorter than
-- the boundary time

Pollrate = 20  -- same as 50 times per second

-- Initialise variables used to keep track of things
LastTimes = {}
Buttons = 0
PrevButtons = 0
Diff = 0

-- This function will be called by a time event, set at the end of the program before exit.
function poll(Time)
 -- read any data available from the device (only need most recent report)
  data, n = com.readlast(dev, rd)

 if n ~= 0 then
   -- Data was returned, so get the status of all the possible "buttons" we are using
   -- one value of 32 bits
  Buttons = com.gethidbuttons(dev, data)           

   -- See if any changes occurred:
   Diff = logic.Xor(Buttons, PrevButtons)
   PrevButtons = Buttons
         
    if Diff ~= 0 then
     offset = 0x3340
     buttonbit = 1
    j = 1
     while Rotaries[j] ~= nil do
    mask = logic.Shl(1, Rotaries[j]-1)      
    if logic.And(Diff,mask) ~= 0 then
     -- This one changed
       -- See if changed in less than the "boundary" time
       thisbutton = buttonbit
       if (LastTimes[j] ~= nil) and ((Time - LastTimes[j]) < FastTimeLimit) then
           thisbutton = buttonbit + buttonbit  -- Use next higher button for fast
         end
         LastTimes[j] = Time
                       
        -- Toggle a virtual button accordingly
         ipc.togglebitsUB(offset, thisbutton)
    end
    
    j = j + 1
    buttonbit = 4 * buttonbit
    if logic.And(j,15) == 0 then
       buttonbit = 1
       offset = offset + 4
    end
    end
  end
 end
end

event.timer(Pollrate, "poll")

Link to comment
Share on other sites

Here is the log:

 

Well, the reason it doesn't work is clear enough:

 

    78828 LUA.0: Could not open HID

 

That error would have been logged even without you enabling the trace/debug option. You need to remember to always look at the log if things don't do what you expect.

 

So, the device identification is wrong. Let's look at that:

 

Vendor = "0x16C0"

Product = "0x27CB"

 

Now there's the problem. There are two ways of identifying the device -- by names, as in the original examples:

 

Vendor = "GoFlight"

Product = "RP48"

 

or by numbers. You seem to have chosen neither. Why didn't you just do as the example showed and use names?

 

0x16C0 and 0x27CB are numbers, but putting them in quotes "..." makes them strings and so names. But those certainly won't really be the names.

 

Either provide the numbers or the names, otherwise the device will not be identified.

 

 

Pete

Link to comment
Share on other sites

  • 6 months later...

Sorry to resurrect this thread, but I'm looking to play with the rotaries.lua as well. (Pete if you are reading this, please be gentle buddy, I've just got up following a night shift. I'm trying to get away from opening and killing mini scripts constantly to event driven programming - the way you prefer)

 

I've got a Saitek multi panel, and I can see it in rotaries.lua. The values from the panel appear to be presented as a single DWord. The knob which changes the value for ALT/VS/IAS etc, when turned clockwise or anticlockwise seems to read this as a button press, rather than a specific value. SPAD doesn't remember the position of the knob when I restart, so I'm assuming that SPAD is just recording the number of button presses and gives it a notional position in its GUI. Therefore the knob is simply a record of going clockwise or anticlockwise.

  	-- See if any changes occurred:
  	Diff = logic.Xor(Buttons, PrevButtons)
  	PrevButtons = Buttons
  	ipc.log(Buttons)       
  	ipc.log("Diff: " .. Diff)

Which gives me this in the log

 33208421 LUA: 66
 33208421 LUA: Diff: 0
 33208453 LUA: 2
 33208453 LUA: Diff: 64
 33208484 LUA: 2
 33208484 LUA: Diff: 0
 33208515 LUA: 34
 33208515 LUA: Diff: 32
 33208546 LUA: 34

In my mind this is the bits in the dword being toggled. Now, I've done a bit of RTFM, and there needs to be a mask applied to pick up which bits have actually changed. In the rotaries script, I'm assuming generating the mask is done here 

Rotaries = {}

This is where my mind gets broken, I've tried everything in that rotaries I can think of, 20,40 (hex values for 32/64) 32/64. Am I correct in assuming the rotaries array is actually expecting a value passed to it? i.e. The knob is at position 20, the knob is at position 21?

Link to comment
Share on other sites

Okay,

 

 

i'm having another attack of the stupids.......

Rotaries = {6,7,19,20}
Changer = {1,2,3,4,5}
Pushbuts = {8,9,10,11,12,13,14,15}
Spring = {17,18}
Switch = {16}

function poll(Time)
	-- read any data available from the device (only need most recent report)
  data, n = com.readlast(dev, rd)

	if n ~= 0 then
 		-- Data was returned, so get the status of all the possible "buttons" we are using
  	-- one value of 32 bits
		Buttons = com.gethidbuttons(dev, data)            

  	-- See if any changes occurred:
  	Diff = logic.Xor(Buttons, PrevButtons)
  	PrevButtons = Buttons
  	ipc.log(Buttons)       
  	ipc.log("Diff: " .. Diff)
    if Diff ~= 0 then 
    	offset = 0x3340
    	buttonbit = 1
  		j = 1
   		while Rotaries[j] ~= nil do
   				mask = logic.Shl(1, Rotaries[j]-1)
   				ipc.log("Mask: " .. mask)   			
				if logic.And(Diff,mask) ~= 0 then
				ipc.log("Change fired")
					-- This one changed
		   		-- See if changed in less than the "boundary" time
		   		thisbutton = buttonbit
    			if (LastTimes[j] ~= nil) and ((Time - LastTimes[j]) < FastTimeLimit) then
          	thisbutton = buttonbit + buttonbit  -- Use next higher button for fast
        	end
       		LastTimes[j] = Time
                      
      		-- Toggle a virtual button accordingly
       		ipc.togglebitsUB(offset, thisbutton)
				end
				
				j = j + 1
				buttonbit = 4 * buttonbit
				if logic.And(j,15) == 0 then
				   buttonbit = 1
				   offset = offset + 4
				end
  		end
		
		buttonbit = 1
		j = 1
		while Changer[j] ~= nil do
   				mask = logic.Shl(1, Changer[j]-1)
   				ipc.log("Mask: " .. mask)   			
				if logic.And(Diff,mask) ~= 0 then
					ipc.log("Changer fired")
					ipc.clearbitsUB(offset,1)
      				ipc.clearbitsUB(offset,2)
      				ipc.clearbitsUB(offset,4)
      				ipc.clearbitsUB(offset,8)
      				ipc.clearbitsUB(offset,16)
					-- This one changed
		   		-- See if changed in less than the "boundary" time
		   		ipc.log("Button bit: " .. buttonbit)
		   		thisbutton = buttonbit
    		--	if (LastTimes[j] ~= nil) and ((Time - LastTimes[j]) < FastTimeLimit) then
          	--thisbutton = buttonbit + buttonbit  -- Use next higher button for fast
        --	end
       	--	LastTimes[j] = Time
                      
      		-- Toggle a virtual button accordingly
      		
      		ipc.togglebitsUB(offset, thisbutton)
       		
				end
				
				j = j + 1
				buttonbit = 4 * buttonbit
				if logic.And(j,15) == 0 then
				   buttonbit = 1
				   offset = offset + 4
				end
  		end
	end
	end
end

event.timer(Pollrate, "poll")

For some reason when I turn the changer button I'm getting duplicate toggling of the virtual buttons... I think that I'm not clearing the bits properly from the last change.... anyone have any ideas?

   861313 LUA: 1
   861313 LUA: Diff: 9
   861313 LUA: Mask: 32
   861313 LUA: Mask: 64
   861313 LUA: Mask: 262144
   861313 LUA: Mask: 524288
   861313 LUA: Mask: 1
   861313 LUA: Changer fired
   861469 LUA: Button bit: 1
   861500 LUA: Mask: 2
   861516 LUA: Mask: 4
   861516 LUA: Mask: 8
   861516 LUA: Changer fired
   861672 LUA: Button bit: 64
   861703 LUA: Mask: 16

Anyone help a lua newbie out?

Link to comment
Share on other sites

For a Lua "newbie" you seem to be constructing quite a complex bit of code. Without spending ages try to read through it all to see what you are trying to do, which isn't at all obvious without a lot of study, can you explain what your problem is and where in all that code you think you have things wrong? I don't even understand what the logging is supposed to mean, let alone the code.

 

BTW I can show you even more obscure code of my own, if you like. Even I don't understand it, at least without a lot of study, and i wrote it! I actually get on better reading C or ASM, I find Lua much more difficult to read.

 

Regards

Pete

Link to comment
Share on other sites

Pete,

 

 

sorry it was linked to my previous post. I've had about 5 hours sleep following a night duty so I'm basically coding sleep drunk :). 

 

My Saitek multi panel has a "changer" (I can't think of a better word for it) which selects ALT/IAS/HDG/CRS/VS. This comes back as bits from 1 to 5.

Changer = {1,2,3,4,5}

There is a knob which adjust each of the values, there is also a trim adjustment also.

Rotaries = {6,7,19,20}

Plus hold/indication buttons for AP/HDG/NAV/BACK/APR/VS.  (I haven't started work on those yet) Plus flap/auto throttle, but I've not started work on those as I think they will be fairly straight forward.

 

 

I've looked at your rotaries script which if I adapt I think will do the job. I have managed to do everything I wanted to do via joybuttons using an adapted version of your HIDDEMO. However, I don't really want to be opening and killing lua scripts; as you right have pointed out, its messy as hell. Your rotaries script takes account of speed of turn, which joybuttons in HIDDEMO doesn't seem to do.

 

 

I can read the buttons of the multi panel, which is presented as a DWORD.

 

I want to do the following: -

 

Initalise

Poll the device, grab the DWORD value

Determine what position the changer is in. Record it in an offset and store it.

Determine what position the auto throttle device is in. Record it in an offset and change the controls in FSX

 

Go into a polling loop

 

On button press

Determine which button has been pressed

 

If it is the changer, record it in an offset.

 

If it is the value knob,

read the changer offset then

change the appropriate value using L:Vars or appropriate ipc.control

 

If it is the trim control

change the appropriate value using L:Vars or appropriate ipc.control

 

If it is a button/autothrottle/flaps

change the appropriate value using L:Vars or appropriate ipc.control

 

 

I know that the ipc.control offsets are, I know what the L:Vars are and I know the logic I need to use them. Its just dealing with reading what button is being actually being pushed. Its as simple as that. Obviously the changer/buttons/autothrottle/flaps don't required slow/fast turn bits so I can trim that out.

 

I think I'm on the right track with this script.

 

I have just had an epiphany, that when the changer has turned, two bits per turn has toggled. So if I turn from ALT to HDG, the bit for ALT needs to be turned off and the bit for HDG needs to be turned on. This is why I was seeing two bit changes. :oops:

Link to comment
Share on other sites

I have just had an epiphany, that when the changer has turned, two bits per turn has toggled. So if I turn from ALT to HDG, the bit for ALT needs to be turned off and the bit for HDG needs to be turned on. This is why I was seeing two bit changes. :oops:

 

So will you be okay now?

 

Pete

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.