Sign in to follow this  
Followers 0
FantomXR

Push & Hold - function?

6 posts in this topic

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
1 hour ago, Antichambre said:

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

Yes... I'd need to do this in NG.... hmmm

Thanks for your help!

Share this post


Link to post
Share on other sites

I did not see that it was in the NG tree before having already written it. I really do not know anything about NG sorry.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Great! there's both solution now in you post, Hihi
One day it will be interesting that I look a little in NG :blush:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0