Previous Thread
Next Thread
Print Thread
Rate This Thread
Hop To
#4422100 - 05/22/18 01:18 PM Activate a layer by code  
Joined: May 2001
Posts: 468
Joao Muas Offline
Member
Joao Muas  Offline
Member

Joined: May 2001
Posts: 468
Portugal
Hi again.

I got another question:

Is it possible to activate a U M or D layer by code?
I mean not by pressing one of its assigned keys?

Thanks in advance.

Inline advert (2nd and 3rd post)

#4422182 - 05/22/18 09:48 PM Re: Activate a layer by code [Re: Joao Muas]  
Joined: Jul 2016
Posts: 61
Drakoz Offline
Junior Member
Drakoz  Offline
Junior Member

Joined: Jul 2016
Posts: 61
Yes. See the example below.

I am using the Warthog Throttle MSP button for my IO button, and the Warthog Throttle Boat Switch (BSF, BSB) for my U, M, and D modes.

You set the mode (I, O, U, M, or D) by setting the Throttle[<button>] value equal to 1 or 0 (1 is pressed, 0 is not pressed). Then call the EventHandler. But understand that EventHandle() in the bottom of your script file is not the master Event Handler. Instead, we have to call DefEventHandler() which is in target.tmh. DefEventHandler() does a few checks, and then calls EvenHandle() and EventHandle() then calls DefaultMapping() which handles all events. In this case, we are tricking TARGET into thinking that the MSP or Boat Switch changed.

In my example, I just used EXEC() routines connected to other buttons to fire off the change in IO or UMD. You can confirm it changed by pressing TG1. Also, if you actually press the IO or UMD buttons, it will override this of course because it will set a new IO or UMD mode.

Code
// Set Shift mode to I
MapKey(&Joystick, S1, EXEC("Throttle[MSP]=1; DefEventHandler(EV_HID_INPUT_DATA, &Throttle, MSP);"));
// Set Shift mode to O
MapKey(&Joystick, S2, EXEC("Throttle[MSP]=0; DefEventHandler(EV_HID_INPUT_DATA, &Throttle, MSP);"));

// Set UMD mode to U
MapKey(&Joystick, H1U, EXEC("Throttle[BSF]=1; DefEventHandler(EV_HID_INPUT_DATA, &Throttle, BSF);"));
// Set UMD mode to M (reset U mode)
MapKey(&Joystick, H1D, EXEC("Throttle[BSF]=0; DefEventHandler(EV_HID_INPUT_DATA, &Throttle, BSF);")); 
// Set UMD mode to M (reset D mode)
MapKey(&Joystick, H1L, EXEC("Throttle[BSB]=0; DefEventHandler(EV_HID_INPUT_DATA, &Throttle, BSB);")); 
// Set UMD mode to D
MapKey(&Joystick, H1R, EXEC("Throttle[BSB]=1; DefEventHandler(EV_HID_INPUT_DATA, &Throttle, BSB);"));

// Use TG1 to test the result
MapKeyIOUMD(&Joystick, TG1, 
	'4',	// IU
	'3',	// OU
	'2',	// IM
	'1',	// OM
	'6',	// ID
	'5'	// OD
);


For Resetting the U or D mode to M, any action that sets M mode will reset either U or D.

It may work just as well to call EventHandle() directly (e.g. EventHandle(EV_HID_INPUT_DATA, &Throttle, MSP);), but probably better to call DefEvenHandler() as this is what TARGET does for all new events. Also, in this example, EV_HID_INPUT_DATA has no impact on the result. You could probably leave it as 0 and it would still work. But EV_HID_INPUT_DATA seems to be the proper value for a generic controller input change. See hid.tmh for the definition of EV_HID_INPUT_DATA, though it is not clearly explained. I think this is related to a legacy feature, or something that was not completely implemented. EventHandle() completely ignores it.

I assume you could also use the above to change a controller axis value. For example we could change the Warthog Throttle Friction axis with Throttle[THR_FC] = 32000, and call DefEventHandler() as above, and you will trick TARGET into thinking that the Friction Control axis has just moved to 32000. I did not test this, but it should work. Of course, if you then move the Friction Control axis, it will immediately reset TARGET to see the actual position of the axis.

This is another way to adjust an axis as per your previous question in the Set an axis to a particular value topic. The difference is you asked how to change a DirectX axis directly, which is what I explained in that topic. But this method would change a Thrustmaster device axis directly, and if that aixs was mapped to a DirectX axis, it would of course change the DirectX axis as well.

Regards,
Michael

#4422209 - 05/23/18 01:45 AM Re: Activate a layer by code [Re: Joao Muas]  
Joined: May 2001
Posts: 468
Joao Muas Offline
Member
Joao Muas  Offline
Member

Joined: May 2001
Posts: 468
Portugal

Drakoz,
Thank you very much!!

I understand what you did, but I wanted your code to work without this definition
Code
SetShiftButton(&Joystick, MSP, &Joystick, BSF, BSB);
which you omitted, but from your words has to be there somewhere...

I don't want to trick TARGET into thinking that the MSP or Boat Switch has changed...
I want TARGET to activate U,M or D without any preset keys for that effect wink

#4422229 - 05/23/18 08:56 AM Re: Activate a layer by code [Re: Joao Muas]  
Joined: Jul 2016
Posts: 61
Drakoz Offline
Junior Member
Drakoz  Offline
Junior Member

Joined: Jul 2016
Posts: 61
You can create virtual buttons to achieve this. Here is an updated code example using VBI, VBU, and VBD (buttons I created for the Warthog Joystick) as virtual IO and UMD buttons. This code example is a complete program. Copy and paste into the TARGET script editor and it will run. I did this to point out that the defines for VBI, VBU and VBD must be outside of main(), or must be in your .ttm file. This example does not use a .ttm.

You have a maximum of 52 real + virtual buttons which are numbered 0 through 51. This maximum is defined by MAXKEYDATA in target.tmh. The Warthog Throttle uses all 52 of them, but the Warthog Joystick and most other TARGET supported devices use less than 52 buttons. You can see this in the defines.tmh file. Look at the "Warthog Joystick interface" definition. It has slots 0 -18, assigned to real buttons, and 29 - 32 assigned to virtual buttons. So it is safe to use slots 40, 41, and 42. So I assigned VBI, VBU and VBD as 40, 41, and 42 and used them for the Warthog Joystick.

If Joystick[VBI] = 1, then the virtual button is "pressed", and = 0 means released. Now, everything you can do with a real button, you can do with VBI, VBU and VBD, including assigning them as the IO Shift and UMD Mode buttons .

Code
include "target.tmh"

// Define virtual buttons for IO Shift and UMD mode for Warthog Joystick
define VBI	40	// Mode I virtual button
define VBU	41	// Mode U virtual button
define VBD	42	// Mode D virtual button

int main()
{
	if(Init(&EventHandle)) return 1;
	
	SetShiftButton(&Joystick, VBI, &Joystick, VBU, VBD, 0);		// IO Shift and UMD Setup (%DEV1, I button, $DEV, U, D, Toggle settings)

	MapKey(&Joystick, S1, EXEC("Joystick[VBI]=1; DefEventHandler(EV_HID_INPUT_DATA, &Joystick, VBI);"));
	MapKey(&Joystick, S2, EXEC("Joystick[VBI]=0; DefEventHandler(EV_HID_INPUT_DATA, &Joystick, VBI);"));
	
	MapKey(&Joystick, H1U, EXEC("Joystick[VBU]=1; DefEventHandler(EV_HID_INPUT_DATA, &Joystick, VBU);"));
	MapKey(&Joystick, H1D, EXEC("Joystick[VBU]=0; DefEventHandler(EV_HID_INPUT_DATA, &Joystick, VBU);")); 
	MapKey(&Joystick, H1L, EXEC("Joystick[VBD]=0; DefEventHandler(EV_HID_INPUT_DATA, &Joystick, VBD);")); 
	MapKey(&Joystick, H1R, EXEC("Joystick[VBD]=1; DefEventHandler(EV_HID_INPUT_DATA, &Joystick, VBD);")); 

	// Test the result
	MapKeyIOUMD(&Joystick, TG1, 
		'4',		// IU
		'3',		// OU
		'2',		// IM
		'1',		// OM
		'6',		// ID
		'5'		// OD
	);		
}

int EventHandle(int type, alias o, int x)
{
	DefaultMapping(&o, x);
}


If you want to use a different Thrustmaster device, make sure the numbers you choose do not conflict with the device definition in defines.tmh.

I assume if you increase MAXKEYDATA in target.tmh to a larger value, you can create more virtual buttons, up to 256 total buttons (real + virtual) because the joy1[], joy2[], joy3[], etc. arrays defined in hid.tmh allow for up to 256 buttons total. Note, joy1[], joy2[], etc. are the actual arrays which &Joystick, &Throttle, &HCougar, etc. point to. See my post on Re: How to detect Shift key for more details on these arrays. Understanding them goes a long way toward understanding how to do all these more advanced TARGET scripts.

#4422242 - 05/23/18 12:59 PM Re: Activate a layer by code [Re: Drakoz]  
Joined: May 2001
Posts: 468
Joao Muas Offline
Member
Joao Muas  Offline
Member

Joined: May 2001
Posts: 468
Portugal
Great!

I got to try something similar before using flags...

Code
SetShiftButton(&Joystick, S3, &Joystick, flag1, flag2);

Then set

flag1=1 and flag2=0 for mode U
flag1=0 and flag2=0 for mode M
falg1=0 and flag2=1 for mode D

of course it didn't work, because I missed the EXEC with the DefEventHandler...

After this, I got around the problem using conditional ifs to create different 3 states (layers) for each button, but not the coded UMD feature, because I failed to control the SetShiftButton as per the above. So the script became a complete program by itself. Your solution is much better, because you are still using the already coded UMD feature, so your button programming can follow the current syntax. Much easier smile

I won't bother to increase MAXKEYDATA, I only want these 3 virtual buttons...

And the reason is, I'm using a T16000, which only has pushbuttons, no dogfight or boat switch, so like this it's easy to forget in which layer you are at. There is no visual of tactile clue.
So what I want is to assign 3 pushbuttons to set U, M, or D by on by every push, cancelling whatever state you are in. This way if you forget where you are, you just press the desired layer button once and you are in control.

Next might be... to print an overlay message on the screen telling you which mode is selected? smile This way you might even use only one pushbutton to sequence between the 3 states...

I will try this out tonight. Thank you again!!

#4422343 - 05/23/18 11:04 PM Re: Activate a layer by code [Re: Joao Muas]  
Joined: Jul 2016
Posts: 61
Drakoz Offline
Junior Member
Drakoz  Offline
Junior Member

Joined: Jul 2016
Posts: 61
Re: using your flag1 and flag2... In case it wasn't obvious.... When flag1 or flag2 = 0, that is the equivalent of saying TS1 on the T16000. When flag1 or flag2 = 1, that is the equivalent of saying TS2 on your T16000. This would be TG1 and S2 respectively for the Warthog Joystick. As shown in defines.tmh, TS1 = TG1 = 0, and TS2 = S2 = 1. When I define VBI, VBU and VBU, I am simply defining three new numbers (buttons) which are not previously assigned for the T16000 or Warthog Joystick. This was probably obvious, but I wanted to point it out because this is critical toward understanding defines.tmh and all the stuff I've been posting here.

Regarding printing out a message, it isn't probably what you want, but you know you can print out messages in the TARGET script editor console, right? Do it as follows:

Code
printf("This is my message:  Var1=%d, Var2=%d\xa", Var1, Var2);


It mostly follows normal C language printf() nomenclature, except you can't use \n for a carriage return. You have to use \xa which is the HEX ASCII code (0xA) for a carriage return. I use this regularly for debugging messages, and it doesn't cause any slow down in my games or the processing of the TARGET script even when I spew thousands of lines of information like giving positional updates on the movement of an axis.

The best solution for feedback, though, may be audible feedback. Even though my Warthog has programmable LEDs, I can't see them normally, so I use beeps for feedback. I have a function called beep() that takes frequency and duration. There is one example where I cycle through 8 different possible modes with a single button and use an increasing frequency tone to tell me which state I am in. I first learned how to do this from here on SimHQ: http://SimHQ.com/forum/ubbthreads.php/topics/3852299. You have to write and compile your own Windows command line beep program and then call that program from within TARGET using the system() command. This command allows you to execute any windows command line program.

If you want to see a complete example, including get the wbeep.exe program I wrote, source code for wbeep.exe, the beep() function I use in TARGET, and documentation on how to use it, go download my Train Sim World TARGET script. I have all the files you need and explain how it works in the included PDF documentation (search for beep in the PDF). Much easier than trying to explain it all here.
https://forums.dovetailgames.com/th...warthog-throttle-saitek-tq-profile.3634/

#4422487 - 05/24/18 07:54 PM Re: Activate a layer by code [Re: Drakoz]  
Joined: May 2001
Posts: 468
Joao Muas Offline
Member
Joao Muas  Offline
Member

Joined: May 2001
Posts: 468
Portugal
This is how I did to setup UMD modes on single button press for theT16000, thanks to you smile

Code
include "target.tmh"

// Define virtual buttons for UMD modes for T16000 Joystick
define VBU	41	// Mode U virtual button
define VBD	42	// Mode D virtual button

int main()
{
	if(Init(&EventHandle)) return 1;
	
	SetShiftButton(&Joystick, B5, &Joystick, VBU, VBD); //UMD Setup, B5 is IO momentary

	//B13 is U mode, B12 is M mode, and B11 is D mode
	MapKey(&T16000, B11, EXEC("T16000[VBD]=1; DefEventHandler(EV_HID_INPUT_DATA, &T16000, VBD);")); // D mode selected
	MapKey(&T16000, B12, EXEC("T16000[VBU]=0; DefEventHandler(EV_HID_INPUT_DATA, &T16000, VBU);"
	                          "T16000[VBD]=0; DefEventHandler(EV_HID_INPUT_DATA, &T16000, VBD);")); // M mode selected
	MapKey(&T16000, B13, EXEC("T16000[VBU]=1; DefEventHandler(EV_HID_INPUT_DATA, &T16000, VBU);")); // U mode selected

	// Test the result with trigger
	MapKeyIOUMD(&T16000, TS1, 
		'4',		// IU
		'3',		// OU
		'2',		// IM
		'1',		// OM
		'6',		// ID
		'5'		// OD
	);		
}

int EventHandle(int type, alias o, int x)
{
	DefaultMapping(&o, x);
}


Works like a charm!!

Last edited by Joao Muas; 05/24/18 07:59 PM.

Moderated by  RacerGT 

Quick Search
Recent Articles
Support SimHQ

If you shop on Amazon use this Amazon link to support SimHQ
.
Social


Recent Topics
Carnival Cruise Ship Fire....... Again
by F4UDash4. 03/26/24 05:58 PM
Baltimore Bridge Collapse
by F4UDash4. 03/26/24 05:51 PM
The Oldest WWII Veterans
by F4UDash4. 03/24/24 09:21 PM
They got fired after this.
by Wigean. 03/20/24 08:19 PM
Grown ups joke time
by NoFlyBoy. 03/18/24 10:34 PM
Anyone Heard from Nimits?
by F4UDash4. 03/18/24 10:01 PM
RIP Gemini/Apollo astronaut Tom Stafford
by semmern. 03/18/24 02:14 PM
10 years after 3/8/2014
by NoFlyBoy. 03/17/24 10:25 AM
Copyright 1997-2016, SimHQ Inc. All Rights Reserved.

Powered by UBB.threads™ PHP Forum Software 7.6.0