diff --git a/ArduSub/Sub.h b/ArduSub/Sub.h index 93069a5a15..2843343249 100644 --- a/ArduSub/Sub.h +++ b/ArduSub/Sub.h @@ -646,6 +646,7 @@ private: 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 clear_input_hold(); diff --git a/ArduSub/joystick.cpp b/ArduSub/joystick.cpp index ace614359a..8fd285920b 100644 --- a/ArduSub/joystick.cpp +++ b/ArduSub/joystick.cpp @@ -76,6 +76,9 @@ void Sub::transform_manual_control_to_rc_override(int16_t x, int16_t y, int16_t for (uint8_t i = 0 ; i < 16 ; 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); } } @@ -348,6 +351,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; @@ -359,6 +367,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; case JSButton::button_function_t::k_relay_3_on: relay.on(2); break; @@ -370,6 +383,11 @@ void Sub::handle_jsbutton_press(uint8_t button, bool shift, bool held) relay.toggle(2); } break; + case JSButton::button_function_t::k_relay_3_momentary: + if (!held) { + relay.on(2); + } + break; case JSButton::button_function_t::k_relay_4_on: relay.on(3); break; @@ -381,6 +399,11 @@ void Sub::handle_jsbutton_press(uint8_t button, bool shift, bool held) relay.toggle(3); } break; + case JSButton::button_function_t::k_relay_4_momentary: + if (!held) { + relay.on(3); + } + break; //////////////////////////////////////////////// // Servo functions @@ -517,6 +540,25 @@ 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; + case JSButton::button_function_t::k_relay_3_momentary: + relay.off(2); + break; + case JSButton::button_function_t::k_relay_4_momentary: + relay.off(3); + break; + } +} + JSButton* Sub::get_button(uint8_t index) { // Help to access appropriate parameter