Switch function change.

Let Sim Innovations know about your Air Manager experience and let us know about your dream feature addition

Moderators: Ralph, russ

Post Reply
Message
Author
User avatar
Keith Baxter
Posts: 2805
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Switch function change.

#1 Post by Keith Baxter » Sat Apr 03, 2021 5:17 pm

Hi,

Does AM now not return a position when a switch is changed? We have to code changes??


Keith
Last edited by Keith Baxter on Sat Apr 03, 2021 5:32 pm, edited 1 time in total.
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

User avatar
Keith Baxter
Posts: 2805
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Switch function change.

#2 Post by Keith Baxter » Sat Apr 03, 2021 5:21 pm

Hi,


I might add that I went to the API to check and found a Whole bunch of confusing GREEN text. Whoever s idea that was. IMHO is is a bum one.

Put code there without datarefs so that peeps can use to test and are simple to read.

I big step backwards i think.

Add all that confusing stuff in an addendum or something. But not among the code.

Something like this. As it was before, I think is better and easier to understand.

Code: Select all

function callback(position,direction)
  position=position+direction
  switch_set_position(switch_id,position)
  print("Switch Position  "..position)
end

switch_id=switch_add(my_image1,my_image2,20,20,80,80,callback)
Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

User avatar
Ralph
Posts: 5074
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: Switch function change.

#3 Post by Ralph » Sat Apr 03, 2021 5:46 pm

I'm not exactly sure what you mean, but yes you have to set the position of the switch with the code. This is because you want the switch to follow the state of the simulator. Or you can always set the switch position within your switch callback if you want to be independent of the simulator.

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

Re: Switch function change.

#4 Post by Sling » Sun Apr 04, 2021 12:19 am

@Keith Baxter
I think you have been using hardware switches too long. For soft switches it’s always been this way. Nothing has changed.

@Ralph
While on the subject this example needs updating to get rid of the 2 if’s because they are not required. It’s no wonder everyone seems to keep doing this because they are following the wiki example.

Existing.

Code: Select all

function dataref_callback(avionics)
  if avionics == 0 then
    switch_set_position(switch_id, 0)
  end

  if avionics == 1 then
    switch_set_position(switch_id, 1)
  end
end
Can change to.

Code: Select all

function dataref_callback(avionics)

    — avionics argument returned from sim will be 0 or 1.
    — We can therefore use this to drive the switch position directly
    switch_set_position(switch_id, avionics)
 
end

User avatar
Ralph
Posts: 5074
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: Switch function change.

#5 Post by Ralph » Sun Apr 04, 2021 5:43 am

You're right, that's a weird one :lol: Thanks.
I'll change it tomorrow or Tuesday, it's still Easter tomorrow in the Netherlands.

User avatar
jph
Posts: 644
Joined: Fri Apr 10, 2020 12:50 pm
Location: Portugal

Re: Switch function change.

#6 Post by jph » Sun Apr 04, 2021 9:49 am

Sling wrote:
Sun Apr 04, 2021 12:19 am

@Ralph
While on the subject this example needs updating to get rid of the 2 if’s because they are not required. It’s no wonder everyone seems to keep doing this because they are following the wiki example.

Existing.

Code: Select all

function dataref_callback(avionics)
  if avionics == 0 then
    switch_set_position(switch_id, 0)
  end

  if avionics == 1 then
    switch_set_position(switch_id, 1)
  end
end
Can change to.

Code: Select all

function dataref_callback(avionics)

    — avionics argument returned from sim will be 0 or 1.
    — We can therefore use this to drive the switch position directly
    switch_set_position(switch_id, avionics)
 
end
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Fully agreed Tony,
It might be worth adding to the wiki that, as you know, we can also invert the level returned in relation to the output for our dataref call using the following -
(It is basically the same code that I listed before but have moved the bitwise operator 'Exclusive OR' Boolean - XOR (~) to be directly applied to the return within the function (avionics in this case) rather than an assign to an intermediate variable prior to the call)

Code: Select all

function dataref_callback(avionics)

    — avionics argument returned from sim will be 0 or 1.
    — If the output follows the input at a binary level we can therefore use this to drive the switch position directly with -
    — switch_set_position(switch_id, avionics)
    
    — Or, if the inverse of the return is needed we can toggle the return with an XOR operator (~)  (Tilde)
    switch_set_position(switch_id, avionics ~ 1) -- Joe
end
It may well be useful as a general toggle as it can be used to replace yet another generalised common IF/Then toggle such as .
if value = 1 then value = 0
if value = 0 then value = 1

Joe.
“Everything in moderation, including moderation.”
“Experience is merely the name men gave to their mistakes.”
Joe (CISSP) -IT security consultant

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

Re: Switch function change.

#7 Post by Sling » Sun Apr 04, 2021 10:59 am

Absolutely. I made this same point just recently in reply to Ted’s iic question.

User avatar
jph
Posts: 644
Joined: Fri Apr 10, 2020 12:50 pm
Location: Portugal

Re: Switch function change.

#8 Post by jph » Sun Apr 04, 2021 11:36 am

Sling wrote:
Sun Apr 04, 2021 10:59 am
Absolutely. I made this same point just recently in reply to Ted’s iic question.
Hi Tony,
Yes, I noticed this AFTER I posted this. DOH :) :o

It would be really lovely to have a definitive code snippet repository. I cant seem to find one at all. All these little snippets WOULD really help. Your vids are amazing and so helpful. But having a copy / paste code library would advance AM to another level.
I have never really found the search function 'good' here, also, I think you posted a while back that a proper download manual of the API would be really good, I think it not only would be good, but I think it is bordering on absolutely essential to further AM and the hobby. I do appreciate that from a @Ralph and @Corjan point of view it may be that it is a WIP and may be available 'some time' ;) . But it is sorely needed, Even if, as it is updated it could be linked to the bootloader.exe and included in the download - or even - a specific - on demand - download - whatever - it just needs to be possible imho.
Is there ANY space at all to post code snippets in a truly searchable and meaningful format Tony ?. It would be so so helpful to all.
Maybe in the wiki ? maybe it is and I missed it ? .. it still needs to be a great reference. For example, the code we posted could certainly contain the non desirable IF THEN option, and then the option WITHOUT the IF THEN ELSE option as a matter of a learning exercise. A veritable compare with explanations.
Is It is ok, for example, me quoting XOR ? when possibly many do not grasp the concept, and, it CAN be done with IF THEN ELSE etc... Maybe a comparison is absolutely needed to allow this to become a staple and advance the benefits and simplicity ? AND explain WHY ?..

Joe
“Everything in moderation, including moderation.”
“Experience is merely the name men gave to their mistakes.”
Joe (CISSP) -IT security consultant

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

Re: Switch function change.

#9 Post by Sling » Sun Apr 04, 2021 2:18 pm

There is a code snippet tool built into AM. I’m not at the PC right now and I can’t quite remember how to access it. It may be in the right click context menu. Anyhow this lets you quickly paste in code snippets with the basic framework for each function. I think these are based on the wiki examples and the last time I looked they needed an update. Not sure if that’s planned for the AM 4.0 release.

User avatar
Ralph
Posts: 5074
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: Switch function change.

#10 Post by Ralph » Sun Apr 04, 2021 2:40 pm

Then it was a while ago that you looked ;)
They're indeed available in the code editor when you right click. And all the free instruments also work as examples.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests