Skip to content

Commit

Permalink
Add momentary relay button function
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxxzer committed May 5, 2017
1 parent a124987 commit 6c61087
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions ArduSub/Sub.h
Expand Up @@ -639,6 +639,7 @@ class Sub : public AP_HAL::HAL::Callbacks {
void init_joystick();
void transform_manual_control_to_rc_override(int16_t x, int16_t y, int16_t z, int16_t r, uint16_t buttons);
void handle_jsbutton_press(uint8_t button,bool shift=false,bool held=false);
void handle_jsbutton_release(uint8_t button, bool shift);
JSButton* get_button(uint8_t index);
void default_js_buttons(void);
void init_barometer(bool save);
Expand Down
27 changes: 26 additions & 1 deletion ArduSub/joystick.cpp
Expand Up @@ -74,9 +74,11 @@ void Sub::transform_manual_control_to_rc_override(int16_t x, int16_t y, int16_t
// Act if button is pressed
// Only act upon pressing button and ignore holding. This provides compatibility with Taranis as joystick.
for (uint8_t i = 0 ; i < 16 ; i++) {
if ((buttons & (1 << i))) {
if (buttons & (1 << i)) {
handle_jsbutton_press(i,shift,(buttons_prev & (1 << i)));
buttonDebounce = tnow_ms;
} else if (buttons_prev & (1 << i)) {
handle_jsbutton_release(i, shift);
}
}

Expand Down Expand Up @@ -338,6 +340,11 @@ void Sub::handle_jsbutton_press(uint8_t button, bool shift, bool held)
relay.toggle(0);
}
break;
case JSButton::button_function_t::k_relay_1_momentary:
if (!held) {
relay.on(0);
}
break;
case JSButton::button_function_t::k_relay_2_on:
relay.on(1);
break;
Expand All @@ -349,6 +356,11 @@ void Sub::handle_jsbutton_press(uint8_t button, bool shift, bool held)
relay.toggle(1);
}
break;
case JSButton::button_function_t::k_relay_2_momentary:
if (!held) {
relay.on(1);
}
break;

////////////////////////////////////////////////
// Servo functions
Expand Down Expand Up @@ -485,6 +497,19 @@ void Sub::handle_jsbutton_press(uint8_t button, bool shift, bool held)
}
}

void Sub::handle_jsbutton_release(uint8_t button, bool shift) {

// Act based on the function assigned to this button
switch (get_button(button)->function(shift)) {
case JSButton::button_function_t::k_relay_1_momentary:
relay.off(0);
break;
case JSButton::button_function_t::k_relay_2_momentary:
relay.off(1);
break;
}
}

JSButton* Sub::get_button(uint8_t index)
{
// Help to access appropriate parameter
Expand Down
4 changes: 4 additions & 0 deletions libraries/AP_JSButton/AP_JSButton.h
Expand Up @@ -57,6 +57,10 @@ class JSButton {
k_relay_2_on = 54, ///< trigger relay on
k_relay_2_off = 55, ///< trigger relay off
k_relay_2_toggle = 56, ///< trigger relay toggle

k_relay_1_momentary = 57, ///< relay toggle when button is pushed, and again when released
k_relay_2_momentary = 58,

// 57-60 reserved for future functions
k_servo_1_inc = 61, ///< increase servo output
k_servo_1_dec = 62, ///< decrease servo output
Expand Down

0 comments on commit 6c61087

Please sign in to comment.