Stumped on the Cessna 172 G1000 STBY BATT switch in the TEST position

Questions about deployment and use of Air Manager Instruments

Moderators: russ, Ralph

Message
Author
XPLOU
Posts: 13
Joined: Sat Dec 17, 2022 4:27 am

Stumped on the Cessna 172 G1000 STBY BATT switch in the TEST position

#1 Post by XPLOU »

I'm rather new to X-Plane and very new to Air Manager but I have coded proferssionally for most of my life so thought I would take a stab at reproducing a C172 G1000 switch panel.

Note that I have never touched LUA in my life so there was a bit of a learning curve there. I also had to learn all of the AM functions but I watched all of Tony's videos (very helpful) and have read quite a bit of forum posts.

Master and Avionics rockers as well as the basic two-way toggle switches were easy. The Land/Recog/Tax 3-way switch was a tad harder but managed that ok. And now I am left with just the STBY BATT thjree way switch but that has a unique behaviour in that you can push it up to ARM and it will stay there, you can put it in off and it will stay there, but to run the test you have to push it down and keep it down for the time you want to run the test, then you take your finger off of it and it will slide itself back into off.

It's that TEST position that I've been playing with to get it to work.

Maybe someone has already done this switch panel already in Air Manager but I have not found it. I've tried finding a similar switch on the downloadable panels and instruments to see if I could find a coding example but haven't had any luck there, either.

I have attached a picture of the panel for refrerence.
SwitchPanel.jpg
You can see a real-life switch being TEST actuated in this video at about the 8:13 minute mark.

https://www.youtube.com/watch?v=2go2vw1DP0A
SwitchPanel.jpg
Maybe someone can point me to an already coded panel or switch and barring that might have an idea on how to deal with coding this up.

OH, major note - I am NOT trying to create a physical panel (like the pictured one). This is an all digital panel.

SimPassion
Posts: 5339
Joined: Thu Jul 27, 2017 12:22 am

Re: Stumped on the Cessna 172 G1000 STBY BATT switch in the TEST position

#2 Post by SimPassion »

Hi @XPLOU,

the simple way is to use the included feature press / release :

https://siminnovations.com/wiki/index.p ... Switch_add

for example, this one :

Code: Select all

switch_id = switch_add(img_pos_0, img_pos_1, img_pos_n, x, y, width, height, position_callback, pressed_callback, released_callback)
though could be the one with "Mode" in the list

So, when going to ARM position, the release_callback will not be included in the switch_add statement
when going the TEST position, we would use the switch_add statement and include the release_callback at the end (indeed the release_callback function should be added to handle code to release the switch visually, so to return to the OFF position)

User avatar
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Stumped on the Cessna 172 G1000 STBY BATT switch in the TEST position

#3 Post by Sling »

The pictures confused a little. Are you implementing a hardware panel or just an Air Manager panel. Is this for the default XP12 C172?

SimPassion
Posts: 5339
Joined: Thu Jul 27, 2017 12:22 am

Re: Stumped on the Cessna 172 G1000 STBY BATT switch in the TEST position

#4 Post by SimPassion »

Sling wrote: Sun Jan 08, 2023 3:47 am The pictures confused a little. Are you implementing a hardware panel or just an Air Manager panel. Is this for the default XP12 C172?
The OP stated for the C172 G1000 and only Air Manager panel

XPLOU
Posts: 13
Joined: Sat Dec 17, 2022 4:27 am

Re: Stumped on the Cessna 172 G1000 STBY BATT switch in the TEST position

#5 Post by XPLOU »

Not hardware. It's actually the airfoil labs C172 NG Digital, but could also be the standard 172 G1000. It's the same switch panel design. Just different datarefs and functions, I'll guess although I haven't looked. I'll fire up the other aircraft and see if it behaves the same way.

I have learned that the switch callbacks go in this order: pressed_callback, callback, released_callback
which makes sense.

pressed_callback and released_callback do not have any parameters that I've been able to see (adding one and trying to txt_set it crashed the code so I am assuming no parameters), so no pressed_callback(position) or pressed_callback(position, direction). It seems to just be a parameterless pressed or released trigger.

In light of that I only have:

pressed_callback()
callback(position, direction)
released_callback()

to work with. It has made it hard to detect direction.

position is easy using switch_get_position within pressed_callback(), so I at least know the switch is currently in the OFF position and I "might" be pushing it down toward the TEST position, but without "direction" available in pressed_callback() that threw a wrench into the thing.

Before I knew the callback trigger sequence I started down the path of a global variable that I would just do a
globalDirection = direction
within the callback(position, direction)
and use that in the pressed_callback() to detect if the switch was being pressed downward (-1) but since pressed_callback() comes before callback(position, direction) that ended up being a lagging value :-(

XPLOU
Posts: 13
Joined: Sat Dec 17, 2022 4:27 am

Re: Stumped on the Cessna 172 G1000 STBY BATT switch in the TEST position

#6 Post by XPLOU »

I checked and the Cessna Skyhawk (G1000) has the same panel and same switch that behaves the same way so it's not specific to the airfoil labs C172 NG Digital. The solution should be similar for either.

SimPassion
Posts: 5339
Joined: Thu Jul 27, 2017 12:22 am

Re: Stumped on the Cessna 172 G1000 STBY BATT switch in the TEST position

#7 Post by SimPassion »

XPLOU wrote: Sun Jan 08, 2023 4:15 am I checked and the Cessna Skyhawk (G1000) has the same panel and same switch that behaves the same way so it's not specific to the airfoil labs C172 NG Digital. The solution should be similar for either.
Otherwise using button_add with 3 click spots, we should have something like this :

Code: Select all

function btn_up_clicked()
	-- some code
end

function btn_ctr_clicked()
	-- some code
end

function btn_dwn_clicked()
	-- some code
end

function btn_dwn_released()
	-- some code
end

btn_switch_up = button_add(nil,nil,  4, 83,51,16,btn_up_clicked,nil)
btn_switch_ctr = button_add(nil,nil, 4,100,51,16,btn_ctr_clicked,nil)
btn_switch_dwn = button_add(nil,nil, 4,117,51,16,btn_dwn_clicked,btn_dwn_released)

User avatar
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Stumped on the Cessna 172 G1000 STBY BATT switch in the TEST position

#8 Post by Sling »

Oops my mistake on the hardware thing I missed the footnote at the very bottom of the original post. I have a panel for this that works with the default aircraft but I don’t have the add-on aircraft. If it’s using a different dataref it is absolutely relevant to how this functions. I’ll explain. The default aircraft uses 2 different commands for this operation. What exactly this add-on is using needs to be understood before a solution can be implemented. If they use a single dataref for instance it will require a slightly different solution. I personally don’t like using switch_add for 3 position up/down or left/right switches because it forces knobster use when there should be a way to ignore for knobster when it’s not a rotary. That may also be different to what you already have.

Can you tell us what dataref(s) are used?
Last edited by Sling on Sun Jan 08, 2023 4:32 am, edited 1 time in total.

SimPassion
Posts: 5339
Joined: Thu Jul 27, 2017 12:22 am

Re: Stumped on the Cessna 172 G1000 STBY BATT switch in the TEST position

#9 Post by SimPassion »

Here's a sample @XPLOU which works in a different way using a timer and button_add

XPLOU
Posts: 13
Joined: Sat Dec 17, 2022 4:27 am

Re: Stumped on the Cessna 172 G1000 STBY BATT switch in the TEST position

#10 Post by XPLOU »

Code.JPG
More background: Cessna 172 G1000 Normal Procedure calls for the STBY BATT switch to be held down in the TEST position for 10 seconds, if using the most recent checklist, then switched to ARM if the LED stayed on for the entire test.

I do have a Knobster and a touchscreen but prefer to use the touch screen for simple toggle switches like this one.

=======================================================
@Sling

In the AirfoilLabs C172 NG Digital the dataRef is: C172/cockpit/stbyBatt

The values are:

TEST POSITION = - 1
OFF POSITION = 0
ARM POSITION = 1

When you hold the switch down in the TEST position in the sim the dataref does switch to -1 and stays there as long as you hold the switch down. Once you release the switch it returns itself to the OFF position and the dataref returns to a 0 value.
Flipping the switch up does not require a constant push and sets the dataref to 1. You then need to manually flip the switch back to OFF and the dataref returns to 0.

They also have two functions defined:
C172/cockpit/stbyBattInc
C172/cockpit/stbyBattDec
which do exactly what they say, which led to this obvious and simple attempt which I purposely made more wordy for the example. I also didn't bother worrying yet about moving the switch back up, I just wanted to see if it would even move down into TEST. ( I had nice indents but they got stripped so I'll paste a screenshot)
------------------------------------------------------
function stbyBatt_callback(position, direction)
if position == 2 then
-- CURRENTLY IN THE ARM POSITION
if direction == -1 then
-- SWITCH IS BEING MOVED DOWN TO THE OFF POSITION
xpl_command("C172/cockpit/stbyBattDec") -- WORKS. AM AND SIM SWITCHES REACT PROPERLY. C172/cockpit/stbyBatt DATAREF VALUE CHANGES TO 0.
end
elseif position == 1 then
-- CURRENTLY IN THE OFF POSITION
if direction == 1 then
-- SWITCH IS BEING MOVED UP TO THE ARM POSITION
xpl_command("C172/cockpit/stbyBattInc") -- WORKS. AM AND SIM SWITCHES REACT PROPERLY. C172/cockpit/stbyBatt DATAREF VALUE CHANGES TO 1.
else
-- SWITCH IS BEING MOVED DOWN TO THE TEST POSITION
xpl_command("C172/cockpit/stbyBattDec") -- DOES NOT WORK. NO EFFECT IN EITHER THE AM OR SIM SWITCHES. C172/cockpit/stbyBatt DATAREF VALUE STAYS AT 0.
-- I assume their internal code that forces the automatic return to the OFF position kicks in immediately or I'm simply overlooking something.
end
end
end

stbyBatt_id = switch_add("stby_batt_test.png", "stby_batt_off.png", "stby_batt_arm.png", 0, 0, 256, 165, "VERTICAL", stbyBatt_callback)
------------------------------------------------------
I looked at the Cessna Skyhawk (G1000) the implementation is very different. That aircraft has a function that is called in the TEST position. So, for now, I'm not going to worry about making this work in that aircraft yet and am focusing on the AirfoilLabs version.

sim/annunciator/test_all_annunciators
And when that is called a whole raft of datarefs under sim/cockpit/warnings/ flip to 1:
=======================================================
@SimPassion

I had briefly thought of that well before I even started on this one but I quickly figured there must have already been support for a 3-way toggle switch, and with the Land 3-way being pretty easy to implement I figured this STBY BATT one wouldn't be too hard, either. What a rabbit hole it turned out to be. Even though I got the Land 3-way working I wasn't happy with not being able to control it via the touch screen.

After playing around with this thing for far too long I do believe your example of the three-button approach and "ditch the switch_add" is the best way to go to achieve the desired result. Thanks for the example :-)

I'll implement the three-button approach unless anyone has a better option?

Post Reply