DCS-BIOS switch init - ED Forums
 


Notices

Reply
 
Thread Tools Display Modes
Old 12-29-2018, 04:10 AM   #1
WhoMadeWho
Junior Member
 
WhoMadeWho's Avatar
 
Join Date: Oct 2016
Posts: 75
Default DCS-BIOS switch init

Hello,

I'm wondering if anyone has found a way to sync **current** switch positions into DCS-BIOS. Problem is I need to cycle switches back and forth before DCS-BIOS knows where my switch is set.

I recall reading something would be added into DCS-BIOS v2 but I know the author is busy with school.

Any ideas? Thanks all!

Code:
/*
  Tell DCS-BIOS to use a serial connection and use interrupt-driven
  communication. The main program will be interrupted to prioritize
  processing incoming data.
  
  This should work on any Arduino that has an ATMega328 controller
  (Uno, Pro Mini, many others).
 */
#define DCSBIOS_IRQ_SERIAL

#include "DcsBios.h"

// GUN/PAC GUNARM - SAFE - ARM
DcsBios::Switch3Pos ahcpGunpac("AHCP_GUNPAC", 2, 3);
// GND = Gray, 2 = White, 3 = Blue

// Master Arm TRAIN - SAFE - ARM
DcsBios::Switch3Pos ahcpMasterArm("AHCP_MASTER_ARM", 4, 5);
// GND = Green + Purple, 4 = White, 5 = Orange

// Laser Arm TRAIN - SAFE - ARM
DcsBios::Switch3Pos ahcpLaserArm("AHCP_LASER_ARM", 6, 7);
// GND = Purple + White, 6 = Blue, 7 = Orange

// TGP OFF - ON
DcsBios::Switch2Pos ahcpTgp("AHCP_TGP", 8);
// GND = Brown, 8 = Yellow

// Altimeter Source RADAR - DELTA - BARO
DcsBios::Switch3Pos ahcpAltSce("AHCP_ALT_SCE", 9, 10);

void setup() {
  DcsBios::setup();
}

void loop() {
  DcsBios::loop();
}
WhoMadeWho is offline   Reply With Quote
Old 12-29-2018, 10:39 AM   #2
Blue73
Member
 
Blue73's Avatar
 
Join Date: Oct 2010
Location: Perth, Australia
Posts: 395
Default

I had resolved this issue by adding a member function "pollInputCurrent " to switches.h. Unfortunately it's been overwritten, but from memory it was just the same as pollInput except it doesn't check for a state change. I called the function for each control during Setup(), something like this,

Edit: I also called it within onAcftNameChange() and called it every 40000 cycles in case it was missed during init.

Code:
class Switch2Pos : PollingInput {
        private:
            const char* msg_;
            char pin_;
            char lastState_;
            bool reverse_;
            void init_(const char* msg, char pin, bool reverse) {
                msg_ = msg;
                pin_ = pin;
                pinMode(pin_, INPUT_PULLUP);
                lastState_ = digitalRead(pin_);
                reverse_ = reverse;
            }
            void pollInput() {
                char state = digitalRead(pin_);
                if (reverse_) state = !state;
                if (state != lastState_) {
                    if (tryToSendDcsBiosMessage(msg_, state == HIGH ? "0" : "1")) {
                        lastState_ = state;
                    }
                }
            }
            
            
        public:
            Switch2Pos(const char* msg, char pin, bool reverse) { init_(msg, pin, reverse); }
            Switch2Pos(const char* msg, char pin) { init_(msg, pin, false); }

            void pollInputCurrent() 
            {
                char state = digitalRead(pin_);
                if (tryToSendDcsBiosMessage(msg_, state == HIGH ? "0" : "1")) {
                        lastState_ = state;
                    }
            }    
            
        };
For your code

Code:
void setup() {   

DcsBios::setup();

 ahcpTgp.pollInputCurrent();
  //... do for all control types, add member function to all classes.

  }
Quote:
Originally Posted by WhoMadeWho View Post
Hello,


I'm wondering if anyone has found a way to sync **current** switch positions into DCS-BIOS. Problem is I need to cycle switches back and forth before DCS-BIOS knows where my switch is set.

I recall reading something would be added into DCS-BIOS v2 but I know the author is busy with school.

Any ideas? Thanks all!

Code:
/*
  Tell DCS-BIOS to use a serial connection and use interrupt-driven
  communication. The main program will be interrupted to prioritize
  processing incoming data.
  
  This should work on any Arduino that has an ATMega328 controller
  (Uno, Pro Mini, many others).
 */
#define DCSBIOS_IRQ_SERIAL

#include "DcsBios.h"

// GUN/PAC GUNARM - SAFE - ARM
DcsBios::Switch3Pos ahcpGunpac("AHCP_GUNPAC", 2, 3);
// GND = Gray, 2 = White, 3 = Blue

// Master Arm TRAIN - SAFE - ARM
DcsBios::Switch3Pos ahcpMasterArm("AHCP_MASTER_ARM", 4, 5);
// GND = Green + Purple, 4 = White, 5 = Orange

// Laser Arm TRAIN - SAFE - ARM
DcsBios::Switch3Pos ahcpLaserArm("AHCP_LASER_ARM", 6, 7);
// GND = Purple + White, 6 = Blue, 7 = Orange

// TGP OFF - ON
DcsBios::Switch2Pos ahcpTgp("AHCP_TGP", 8);
// GND = Brown, 8 = Yellow

// Altimeter Source RADAR - DELTA - BARO
DcsBios::Switch3Pos ahcpAltSce("AHCP_ALT_SCE", 9, 10);

void setup() {
  DcsBios::setup();
}

void loop() {
  DcsBios::loop();
}

Last edited by Blue73; 12-29-2018 at 12:13 PM.
Blue73 is offline   Reply With Quote
Old 12-29-2018, 08:07 PM   #3
WhoMadeWho
Junior Member
 
WhoMadeWho's Avatar
 
Join Date: Oct 2016
Posts: 75
Default

Thanks Blue73 for the reply! I tried inserting your code but get an error at compile time - "Expected class-name before '{' token".. Can you tell me where I went wrong? Sorry, I'm probably missing something really obvious..

Code:
#define DCSBIOS_IRQ_SERIAL

#include "DcsBios.h"

// Start class from Blue73
class Switch2Pos : PollingInput {
        private:
            const char* msg_;
            char pin_;
            char lastState_;
            bool reverse_;
            void init_(const char* msg, char pin, bool reverse) {
                msg_ = msg;
                pin_ = pin;
                pinMode(pin_, INPUT_PULLUP);
                lastState_ = digitalRead(pin_);
                reverse_ = reverse;
            }
            void pollInput() {
                char state = digitalRead(pin_);
                if (reverse_) state = !state;
                if (state != lastState_) {
                    if (tryToSendDcsBiosMessage(msg_, state == HIGH ? "0" : "1")) {
                        lastState_ = state;
                    }
                }
            }
            
            
        public:
            Switch2Pos(const char* msg, char pin, bool reverse) { init_(msg, pin, reverse); }
            Switch2Pos(const char* msg, char pin) { init_(msg, pin, false); }

            void pollInputCurrent() 
            {
                char state = digitalRead(pin_);
                if (tryToSendDcsBiosMessage(msg_, state == HIGH ? "0" : "1")) {
                        lastState_ = state;
                    }
            }    
            
        };
// End class from Blue73

// TGP OFF - ON
DcsBios::Switch2Pos ahcpTgp("AHCP_TGP", 8);
// GND = Brown, 8 = Yellow

void setup() {
  DcsBios::setup();

 ahcpTgp.pollInputCurrent();
 
}

void loop() {
  DcsBios::loop();

 }
WhoMadeWho is offline   Reply With Quote
Old 12-30-2018, 01:42 AM   #4
Blue73
Member
 
Blue73's Avatar
 
Join Date: Oct 2010
Location: Perth, Australia
Posts: 395
Default

The new function needs to be inserted into the DCSBIOS library rather than your code. Once done all you do is call the new function within your setup.

My libraries are in the following folder,

C:\Users\<username>\Documents\Arduino\libraries\dcs-bios-arduino-library-0.2.11\src\internal

Can you PM me your email address so I can send you the updated switches.h file? The functions are slightly different for switch3pos and switchmultipos. I could be more elegant and rewrite his function to accommodate this variation but instead left it as is and repeated much of his code leaving out the delta check.

Edit: I could post here but not sure of the T&C's.


cheers

John

Last edited by Blue73; 12-30-2018 at 01:45 AM.
Blue73 is offline   Reply With Quote
Old 12-31-2018, 03:02 AM   #5
AndrewW
Member
 
Join Date: Jan 2011
Location: Brisbane, Australia
Posts: 543
Default

Quote:
Originally Posted by Blue73 View Post

Edit: I could post here but not sure of the T&C's.
Could you please post here? This is something that is sort after by a lot of people, myself included
__________________
Intel i7 6700K @ 4 GHz / ASUS Z170 Pro Gaming / 32 Gb G.Skill DDR4 / Gigabyte RTX2080 OC Gaming / 512 Gb Samsung 850 EVO System SSD / 512 Gb Samsung 850 EVO DCS SSD / Win 10 x64 Pro

A-10C, AV-8B, F-5E, F-86F, FC3, FW190D-9, Hawk, Ka50 2, P-51D, Spitfire, UH-1H, F/A-18C, F-14, F-16.

Nevada, Normandy, Persian Gulf.
AndrewW is online now   Reply With Quote
Old 12-31-2018, 03:36 AM   #6
Blue73
Member
 
Blue73's Avatar
 
Join Date: Oct 2010
Location: Perth, Australia
Posts: 395
Default

Hi Andrew,

Here are my additions/changes made to switches.h. For your main code I've found regular polling is the best way to update the controls.

Unzip/Copy this file to your Arduino DCSBIOS library location, backup up the old one first.
Location: C:\Users\<username>\Documents\Arduino\libraries\dcs-bios-arduino-library-0.2.11\src\internal

Switches.h.zip

Sample Code

Code:
DcsBios::Switch3Pos flapSw("FLAP_SW", 9, 10);

unsigned int g_iCounter = 0;

void PollAllControls()
{
   flapSw.pollInputCurrent();
  
}

void loop()
{
  DcsBios::loop();
 
  if (++g_iCounter > 60000)
  { g_iCounter = 0;
    PollAllControls();
  }
   
}
Note: Doesn't work for toggle type controls as sending a repeated '1' to the sim tells it to toggle the control. Luckily these are rare.

cheers

John

Last edited by Blue73; 12-31-2018 at 03:44 AM.
Blue73 is offline   Reply With Quote
Old 12-31-2018, 05:05 AM   #7
WhoMadeWho
Junior Member
 
WhoMadeWho's Avatar
 
Join Date: Oct 2016
Posts: 75
Default

Thanks again to Blue73 for the code! After adding his custom Switches.h and modifying my sketch, I was finally able to get DCS-BIOS to reflect the state of my switches at startup.

Code from my sketch (Master Arm panel from the A10)

Code:
/*
  Tell DCS-BIOS to use a serial connection and use interrupt-driven
  communication. The main program will be interrupted to prioritize
  processing incoming data.
  
  This should work on any Arduino that has an ATMega328 controller
  (Uno, Pro Mini, many others).
 */
#define DCSBIOS_IRQ_SERIAL
#include "DcsBios.h"

//// WARNING
//// WARNING - using custom Switches.h so switch init reflects current switch positons at init
//// WARNING 


// GUN/PAC GUNARM - SAFE - ARM
DcsBios::Switch3Pos ahcpGunpac("AHCP_GUNPAC", 2, 3);
// GND = Gray, 2 = White, 3 = Blue

// Master Arm TRAIN - SAFE - ARM
DcsBios::Switch3Pos ahcpMasterArm("AHCP_MASTER_ARM", 4, 5);
// GND = Green + Purple, 4 = White, 5 = Orange

// Laser Arm TRAIN - SAFE - ARM
DcsBios::Switch3Pos ahcpLaserArm("AHCP_LASER_ARM", 6, 7);
// GND = Purple + White, 6 = Blue, 7 = Orange

// TGP OFF - ON
DcsBios::Switch2Pos ahcpTgp("AHCP_TGP", 8);
// GND = Brown, 8 = Yellow

// Altimeter Source RADAR - DELTA - BARO
DcsBios::Switch3Pos ahcpAltSce("AHCP_ALT_SCE", 9, 10);
// GND = Gray, 9 = Blue, 10 = Purple

// Hud Mode NIGHT - DAY
DcsBios::Switch2Pos ahcpHudDaynight("AHCP_HUD_DAYNIGHT", 11);
// GND = Green, 11 = Orange

// Hud Mode STBY - NORM
DcsBios::Switch2Pos ahcpHudMode("AHCP_HUD_MODE", 12);
// GND = White, 12 = Yellow

// CICU OFF - ON
DcsBios::Switch2Pos ahcpCicu("AHCP_CICU", 14);
// GND = White, 14 = Purple 

// JTRS OFF - ON
DcsBios::Switch2Pos ahcpJtrs("AHCP_JTRS", 15);
// GND = Orange, 15 = White

// IFFCC OFF - TEST - ON
DcsBios::Switch3Pos ahcpIffcc("AHCP_IFFCC", 16, 17);
// GND = Brown + Green,  16 = Yellow, 17 = Blue

unsigned int g_iCounter = 0;
void PollAllControls()
{
  ahcpGunpac.pollInputCurrent();
  ahcpMasterArm.pollInputCurrent();
  ahcpLaserArm.pollInputCurrent();
  ahcpTgp.pollInputCurrent();
  ahcpAltSce.pollInputCurrent();
  ahcpHudDaynight.pollInputCurrent();
  ahcpHudMode.pollInputCurrent();
  ahcpCicu.pollInputCurrent();
  ahcpJtrs.pollInputCurrent();
  ahcpIffcc.pollInputCurrent();
}

void setup() {
  DcsBios::setup();


}

void loop() {
  DcsBios::loop();
  if (++g_iCounter > 60000)
  {   g_iCounter = 0;
      PollAllControls();
  } 
 }
WhoMadeWho is offline   Reply With Quote
Old 12-31-2018, 07:48 AM   #8
AndrewW
Member
 
Join Date: Jan 2011
Location: Brisbane, Australia
Posts: 543
Default

Thanks Blue, that works a treat.
__________________
Intel i7 6700K @ 4 GHz / ASUS Z170 Pro Gaming / 32 Gb G.Skill DDR4 / Gigabyte RTX2080 OC Gaming / 512 Gb Samsung 850 EVO System SSD / 512 Gb Samsung 850 EVO DCS SSD / Win 10 x64 Pro

A-10C, AV-8B, F-5E, F-86F, FC3, FW190D-9, Hawk, Ka50 2, P-51D, Spitfire, UH-1H, F/A-18C, F-14, F-16.

Nevada, Normandy, Persian Gulf.
AndrewW is online now   Reply With Quote
Old 12-31-2018, 09:22 AM   #9
Blue73
Member
 
Blue73's Avatar
 
Join Date: Oct 2010
Location: Perth, Australia
Posts: 395
Default

Nice


Quote:
Originally Posted by AndrewW View Post
Thanks Blue, that works a treat.
Blue73 is offline   Reply With Quote
Old 01-01-2019, 09:28 AM   #10
Hansolo
Senior Member
 
Hansolo's Avatar
 
Join Date: Aug 2012
Location: Denmark
Posts: 1,701
Default

Blue73, that is a very sweet solution sir. Thanks a lot for sharing. Happy New Year sir.

Cheers
Solo
__________________
Hansolo is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT. The time now is 07:34 AM. vBulletin Skin by ForumMonkeys. Powered by vBulletin®.
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.