FantomXR Posted November 7, 2018 Report Share Posted November 7, 2018 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 Quote Link to comment Share on other sites More sharing options...
Antichambre Posted November 7, 2018 Report Share Posted November 7, 2018 (edited) 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 November 7, 2018 by Antichambre task note Quote Link to comment Share on other sites More sharing options...
FantomXR Posted November 7, 2018 Author Report Share Posted November 7, 2018 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! Quote Link to comment Share on other sites More sharing options...
Antichambre Posted November 7, 2018 Report Share Posted November 7, 2018 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. Quote Link to comment Share on other sites More sharing options...
FantomXR Posted November 8, 2018 Author Report Share Posted November 8, 2018 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. Quote Link to comment Share on other sites More sharing options...
Antichambre Posted November 8, 2018 Report Share Posted November 8, 2018 Great! there's both solution now in you post, Hihi One day it will be interesting that I look a little in NG Quote Link to comment Share on other sites More sharing options...
weasel Posted March 3, 2020 Report Share Posted March 3, 2020 @FantomXR you're my daddy. everything i wanna search on the forum i always find a thread where you found the answer 5 years ago. Quote Link to comment Share on other sites More sharing options...
FantomXR Posted March 4, 2020 Author Report Share Posted March 4, 2020 I'm happy to help! :-) Quote Link to comment Share on other sites More sharing options...
Zam Posted March 4, 2020 Report Share Posted March 4, 2020 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 Quote Link to comment Share on other sites More sharing options...
FantomXR Posted March 5, 2020 Author Report Share Posted March 5, 2020 Great improvement! Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.