Push & Hold - function?

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

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
 

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!

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.

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.

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

you’re my daddy. everything i wanna search on the forum i always find a thread where you found the answer 5 years ago.

I’m happy to help! :slight_smile:

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

 

Great improvement! Thanks!