Skip to content

Commit

Permalink
Add SETSINGLE_AG_MESSAGE
Browse files Browse the repository at this point in the history
  • Loading branch information
Tetopia committed Jan 18, 2024
1 parent 2bc36ac commit 9b990e7
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 8 deletions.
59 changes: 52 additions & 7 deletions examples/KerbalSimpitAllCommandDemo/KerbalSimpitAllCommandDemo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ void setup() {
// |------------------------------------------------------------|
// | Other default Action Groups: Gear, Light, SAS, RCS, Breaks |
// |------------------------------------------------------------|
/* Old way of activating and deactivating action groups.
Does not support all Action Groups for KSP2.
Use SETSINGLE_AG_MESSAGE instead as shown below.
mySimpit.printToKSP("Step : Act & Deact Gear", PRINT_TO_SCREEN);
while (digitalRead(CONTINUE_TEST_PIN) == HIGH)
{
Expand All @@ -176,6 +180,31 @@ void setup() {
}
}
WaitForContinueButtonReleased();
*/

mySimpit.printToKSP("Step : Act & Deact Gear", PRINT_TO_SCREEN);
while (digitalRead(CONTINUE_TEST_PIN) == HIGH)
{
if(digitalRead(ACTION_INPUT_PIN) == LOW && lastInputButtonState == HIGH) //Button got pressed down
{
lastInputButtonState = LOW;
setSingleActionGroupMessage ag_msg(ADVANCED_GEAR_ACTION, AG_ACTION_ACTIVATE);
mySimpit.send(SETSINGLE_AG_MESSAGE, ag_msg);
delay(100);
}
else if(digitalRead(ACTION_INPUT_PIN) == HIGH && lastInputButtonState == LOW) //Button was released
{
lastInputButtonState = HIGH;
setSingleActionGroupMessage ag_msg(ADVANCED_GEAR_ACTION, AG_ACTION_DEACTIVATE);
mySimpit.send(SETSINGLE_AG_MESSAGE, ag_msg);
delay(100);
}
}
WaitForContinueButtonReleased();

/* Old way of toggling action groups.
Does not support all Action Groups for KSP2.
Use SETSINGLE_AG_MESSAGE instead as shown below.
mySimpit.printToKSP("Step : Toggle Gear", PRINT_TO_SCREEN);
while (digitalRead(CONTINUE_TEST_PIN) == HIGH)
Expand All @@ -187,12 +216,25 @@ void setup() {
WaitForActionButtonReleased();
}
WaitForContinueButtonReleased();
*/

mySimpit.printToKSP("Step : Toggle Gear", PRINT_TO_SCREEN);
while (digitalRead(CONTINUE_TEST_PIN) == HIGH)
{
if(digitalRead(ACTION_INPUT_PIN) == LOW)
{
setSingleActionGroupMessage ag_msg(ADVANCED_GEAR_ACTION, AG_ACTION_TOGGLE);
mySimpit.send(SETSINGLE_AG_MESSAGE, ag_msg);
}
WaitForActionButtonReleased();
}
WaitForContinueButtonReleased();

//The others work the same, to make the code shorter they are in this separate function
TestActionGroup("Light", LIGHT_ACTION);
TestActionGroup("SAS", SAS_ACTION);
TestActionGroup("RCS", RCS_ACTION);
TestActionGroup("Brakes", BRAKES_ACTION);
TestActionGroup("Light", ADVANCED_LIGHT_ACTION);
TestActionGroup("SAS", ADVANCED_SAS_ACTION);
TestActionGroup("RCS", ADVANCED_RCS_ACTION);
TestActionGroup("Brakes", ADVANCED_BRAKES_ACTION);

// |----------|
// | Rotation |
Expand Down Expand Up @@ -654,13 +696,15 @@ void TestActionGroup(String name, byte actionGroupIndex)
if(digitalRead(ACTION_INPUT_PIN) == LOW && lastInputButtonState == HIGH) //Button got pressed down
{
lastInputButtonState = LOW;
mySimpit.activateAction(actionGroupIndex);
setSingleActionGroupMessage ag_msg(actionGroupIndex, AG_ACTION_ACTIVATE);
mySimpit.send(SETSINGLE_AG_MESSAGE, ag_msg);
delay(100);
}
else if(digitalRead(ACTION_INPUT_PIN) == HIGH && lastInputButtonState == LOW) //Button was released
{
lastInputButtonState = HIGH;
mySimpit.deactivateAction(actionGroupIndex);
setSingleActionGroupMessage ag_msg(actionGroupIndex, AG_ACTION_DEACTIVATE);
mySimpit.send(SETSINGLE_AG_MESSAGE, ag_msg);
delay(100);
}
}
Expand All @@ -671,7 +715,8 @@ void TestActionGroup(String name, byte actionGroupIndex)
{
if(digitalRead(ACTION_INPUT_PIN) == LOW)
{
mySimpit.toggleAction(actionGroupIndex);
setSingleActionGroupMessage ag_msg(actionGroupIndex, AG_ACTION_TOGGLE);
mySimpit.send(SETSINGLE_AG_MESSAGE, ag_msg);
}
WaitForActionButtonReleased();
}
Expand Down
24 changes: 23 additions & 1 deletion src/KerbalSimpitMessageTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,14 @@ enum InboundPackets
AGDEACTIVATE_MESSAGE = 14,
/** Toggle the given standard Action Group(s). */
AGTOGGLE_MESSAGE = 15,
/** Set one of the standard Action Group(s).
The payload should be a single byte,
with the first 6 bits as which action group to set as in the AdvancedActionGroupIndexes enum
and the last 2 bits the state/action of the action group as in the ActionGroupSettings enum
in the AutopilotMode enum.*/
SETSINGLE_AG_MESSAGE = 58,
/** Set one of the custom Action Group(s). */
SETSINGLE_CAG_MESSAGE = 59,
/** Send vessel rotation commands.
If set and not 0, those command will supersede SAS command. The SAS will appear not the work when the jostick is in use.
If this is the case for you, make sure you are sending 0 command to KSP on all used axis.*/
Expand Down Expand Up @@ -395,7 +403,7 @@ enum ActionGroupIndexes
};

/** Advanced Action Group Indexes
These are used to get the velues in the ADVANCED_ACTIONSTATUS_MESSAGE. */
These are used to get the values in the ADVANCED_ACTIONSTATUS_MESSAGE. */
enum AdvancedActionGroupIndexes
{
/** Index for the Stage action group. */
Expand All @@ -418,6 +426,20 @@ enum AdvancedActionGroupIndexes
ADVANCED_RADIATOR_ACTION = 8
};

/** Action Group Settings for single action groups
These are used to set the values in the SETSINGLE_AG_MESSAGE and SETSINGLE_CAG_MESSAGE. */
enum ActionGroupSettings
{
/** Don't do anything with this action group. */
AG_ACTION_NOTHING = 0,
/** Activate the action group. */
AG_ACTION_ACTIVATE = 1,
/** Deactivate the action group. */
AG_ACTION_DEACTIVATE = 2,
/** Toggle the action group. */
AG_ACTION_TOGGLE = 3
};

/** Advanced Action Group States
These are the values returned by advancedActionStatusMessage::get_action_status in the ADVANCED_ACTIONSTATUS_MESSAGE. */
enum AdvancedActionGroupStates
Expand Down
5 changes: 5 additions & 0 deletions src/PayloadStructs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ flightStatusMessage parseFlightStatusMessage(byte msg[]){

// Helper functions for using bitmask

setSingleActionGroupMessage::setSingleActionGroupMessage(byte advancedActionGroupIndex, ActionGroupSettings setting){
this->actionGroupAndSetting = advancedActionGroupIndex<<2;
this->actionGroupAndSetting |= setting;
}

rotationMessage::rotationMessage(){
this->mask = 0;
}
Expand Down
8 changes: 8 additions & 0 deletions src/PayloadStructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ struct advancedActionStatusMessage {
byte getActionStatus(byte groupIndex) { return (status >> (groupIndex * 2)) & 3; }
} __attribute__((packed));

/** A message to set a single action group. */
struct setSingleActionGroupMessage {
byte actionGroupAndSetting; /**< 6 bits of which action group to set and 2 bits of what to to with said action group. */

/** param: index of the action group as it is defined in AdvancedActionGroupIndexes or a number from 0 to 9 for custom action groups.*/
setSingleActionGroupMessage(byte advancedActionGroupIndex, ActionGroupSettings setting);
} __attribute__((packed));

/** A deltaV information message. */
struct deltaVMessage {
float stageDeltaV; /**< DeltaV of the current stage. */
Expand Down

0 comments on commit 9b990e7

Please sign in to comment.