Jump to content

Push & Hold - function?


FantomXR

Recommended Posts

Hey people,

maybe I missed it or it is not possible. 
I'd like to set up a switch. This switch should have to functions. The first is executed as soon as I push it an release it immediately. The second one should be executed if I push the button and hold it for a given amount of time.

Any ideas?

Thanks,
Chris

Link to comment
Share on other sites


You can do something like this, I didn't test it, you will maybe have to correct it.

In your App.c

#define BUTT_PIN_NUM 0
#define HOLD_TIME 3000 // in ms cause APP_SRIO_ServicePrepare is called every ms
u8 butt_last_state;
u16 butt_hold_count;


/////////////////////////////////////////////////////////////////////////////
// This hook is called before the shift register chain is scanned
/////////////////////////////////////////////////////////////////////////////
void APP_SRIO_ServicePrepare(void)
{
	if(butt_last_state == 0){
		butt_hold_count++;
		if(butt_hold_count >= HOLD_TIME){  //3 second hold time
			// function#2 here
          	butt_last_state = 1; //avoid repetitive Function#2 and Function#1 to be triggered just after Function#2 
		}
	}else{
		butt_hold_count=0;
	}
}

/////////////////////////////////////////////////////////////////////////////
// This hook is called when a button has been toggled
// pin_value is 1 when button released, and 0 when button pressed
/////////////////////////////////////////////////////////////////////////////
void APP_DIN_NotifyToggle(u32 pin, u32 pin_value)
{
	if(pin == BUTT_PIN_NUM){
		if(pin_value == 1 && butt_last_state == 0){
			if(butt_hold_count < HOLD_TIME){
				// function#1 here
			}
		}
		butt_last_state = pin_value;
	}
}


If you need it in MBNG I don't know.

Note: If Function#2 need a lot of process you can use a task to do not delay your SRIO Process. tutorial 006_rtos_tasks
 

Edited by Antichambre
task note
Link to comment
Share on other sites

I solved it!

This is the NGC part:

EVENT_BUTTON id=1 type=meta meta=runsection:1 button_mode=OnOff if_equal=127

And this is the NGR part:

if ^section == 1 

    delay_ms 500

    if BUTTON:1 == 127
      SEND CC USB1 1 1 127
      exit
    endif

    if BUTTON:1 == 0
      SEND CC USB1 1 2 0
      exit
    endif

endif

This code wait's 500ms and checks if the button is still pressed. If yes it sends on channel 1, if not it sends on channel 2.

Link to comment
Share on other sites

  • 1 year later...

Hello

 

On 08/11/2018 at 11:49 AM, FantomXR said:

And this is the NGR part:


if ^section == 1 

    delay_ms 500

    if BUTTON:1 == 127
      SEND CC USB1 1 1 127
      exit
    endif

    if BUTTON:1 == 0
      SEND CC USB1 1 2 0
      exit
    endif

endif

This code wait's 500ms and checks if the button is still pressed. If yes it sends on channel 1, if not it sends on channel 2.

You can reduce the time for the short push to only wait the processing when long time press

 

if ^section == 1 

    delay_ms 50

    if BUTTON:1 == 0
      SEND CC USB1 1 2 0
      exit
    endif

    delay_ms 500

    if BUTTON:1 == 127
      SEND CC USB1 1 1 127
      exit
    endif

endif

By this you can handle both situation time in any time respond configuration you want

Note that first condition will in fact be the time you manually release the button (if shorter than second delay) + the first delay as the script will re-trig when button is off ( due to button_mode=onoff)

Best

Zam

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...