123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- // Button handling for RC5 and RC6 Remotes
- // - these send a toggle bit on every new button press, so it is easy to differ hold and re-pressing
- // - on re-pressing this function will send the button again immediately. it uses the _currPrefix which has to be passed by the calling function
- // and which must be only the single byte of the RCx code containing the toggle bit
- // - if a button is held down, the remote will not toggle this bit and goes on sending the same code every 100ms or so
- // this function will then, after some ignored codes, start with some slow repeats, then switch to medium repeat rate and finally repeat with the rate of the incoming codes
- // configuration variables for RCx button handling
- uint8_t handleButton_RCx_ignoreFirstRepeats = 2; // repeats to ignore when a button is beeing held (the first one is always sent), both for BUTTONMODE_REPEAT and BUTTONMODE_FILTERED_REPEAT
- uint8_t handleButton_RCx_ignoreFirstRepeats_Ext = 6; // repeats to ignore when a button is beeing held (the first one is always sent), both for BUTTONMODE_REPEAT and BUTTONMODE_FILTERED_REPEAT
- uint8_t handleButton_RCx_slowRepeats = 0; // amount of slow repeats to send
- uint8_t handleButton_RCx_slowRepeatsNumFastRepeats = 4; // how many fast repeats (=sending rate of the RC remote) represent one slow repeat?
- uint8_t handleButton_RCx_mediumRepeats = 5; // amount of medium repeats to send
- uint8_t handleButton_RCx_mediumRepeatsNumFastRepeats = 2; // how many fast repeats (=sending rate of the RC remote) represent one medium repeat?
- bool handleButton_RCx_useHoldInsteadOfFastRepeat = true;
- // hold button mode (key via USB keyboard emulation is not release until the RC stops sending repeats + this timeout)
- uint8_t handleButton_RC5_holdButton_releaseTimeout = 150; // release keys after a button was held (_btnMode=BUTTON_HOLD) after this ms if triggered by handleButton_RCx function
- uint8_t handleButton_RC6_holdButton_releaseTimeout = 120; // release keys after a button was held (_btnMode=BUTTON_HOLD) after this ms if triggered by handleButton_RCx function
- // global variables for RCx button handling
- unsigned int handleButton_RCx_btnRepeatsCounter;
- uint8_t handleButton_RCx_lastButton, handleButton_RCx_lastPrefix;
- uint8_t handleButton_RCx_btnSlowRepeatsCounter, handleButton_RCx_btnSlowRepeatsCounter2;
- uint8_t handleButton_RCx_btnMediumRepeatsCounter, handleButton_RCx_btnMediumRepeatsCounter2;
- // the function
- //void handleButton_RCx(uint8_t _btn, uint8_t _btnMode, uint8_t _currPrefix) {
- // handleButton_RCx(_btn, _btnMode, _currPrefix, REMOTETYPE_RC6);
- //}
- void handleButton_RCx(uint8_t _btn, uint8_t _btnMode, uint8_t _currPrefix, uint8_t _remoteType) {
- if (_btn != handleButton_RCx_lastButton || (_btn == handleButton_RCx_lastButton) && (_currPrefix != handleButton_RCx_lastPrefix)) {
- if (debug2) {
- Serial.println(F("RCx BUTTON INITIAL"));
- }
- if (_btn == BTN_POWER) {
- handlePowerButton();
- }
- else if (_btn == BTN_TASKSWITCH) {
- handleTaskSwitchButton();
- }
- else {
- if (_remoteType == REMOTETYPE_RC6) handleHoldButton_setTimeout(handleButton_RC6_holdButton_releaseTimeout);
- else if (_remoteType == REMOTETYPE_RC5) handleHoldButton_setTimeout(handleButton_RC5_holdButton_releaseTimeout);
-
- //if (_btnMode == BUTTONMODE_HOLD || _btnMode == BUTTONMODE_EXTENDED_REPEAT && !handleTaskSwitchButton_isActive()) {
- //if (_btnMode == BUTTONMODE_HOLD && !handleTaskSwitchButton_isActive()) {
- if (_btnMode == BUTTONMODE_HOLD) {
- releaseAllKeys();
- sendKey(_btn, true);
- handleHoldButton();
- }
- else sendKey(_btn, false);
- }
- handleButton_RCx_btnRepeatsCounter = 0;
- handleButton_RCx_btnSlowRepeatsCounter = 0;
- handleButton_RCx_btnMediumRepeatsCounter = 0;
- }
- // BUTTONMODE_REPEAT (direct send a keypress on every repeat received from the RC, except of the first ones)
- else if ((_btn == handleButton_RCx_lastButton) && (_currPrefix == handleButton_RCx_lastPrefix) && _btnMode == BUTTONMODE_REPEAT) {
- if (_btn == BTN_POWER) {
- handlePowerButton();
- }
- else if (_btn == BTN_TASKSWITCH) {
- handleTaskSwitchButton();
- }
- else {
- handleButton_RCx_btnRepeatsCounter++;
- if (debug2) {
- Serial.print("REP #");
- Serial.println(handleButton_RCx_btnRepeatsCounter);
- }
- if (handleButton_RCx_btnRepeatsCounter > handleButton_RCx_ignoreFirstRepeats) {
- // filter out 1st repetition
- sendKey(_btn, false);
- }
- }
- }
- // BUTTONMODE_EXTENDED_REPEAT (first slow, then getting faster)
- else if ((_btn == handleButton_RCx_lastButton) && (_currPrefix == handleButton_RCx_lastPrefix) && _btnMode == BUTTONMODE_EXTENDED_REPEAT) {
- if (handleButton_RCx_btnRepeatsCounter >= handleButton_RCx_ignoreFirstRepeats_Ext) {
- if (handleButton_RCx_btnSlowRepeatsCounter == 0) {
- handleButton_RCx_btnSlowRepeatsCounter++;
- handleButton_RCx_btnSlowRepeatsCounter2 = 0;
- if (debug2) {
- Serial.print(F("RCx BUTTON REPEAT SLOW #"));
- Serial.println(handleButton_RCx_btnSlowRepeatsCounter);
- }
- sendKey(_btn, false);
- }
- else if (handleButton_RCx_btnSlowRepeatsCounter <= handleButton_RCx_slowRepeats && handleButton_RCx_slowRepeats > 0) {
- if (handleButton_RCx_btnSlowRepeatsCounter2 < handleButton_RCx_slowRepeatsNumFastRepeats) {
- handleButton_RCx_btnSlowRepeatsCounter2++;
- }
- else {
- handleButton_RCx_btnSlowRepeatsCounter++;
- handleButton_RCx_btnSlowRepeatsCounter2 = 0;
- if (debug2) {
- Serial.print(F("RCx BUTTON REPEAT SLOW #"));
- Serial.println(handleButton_RCx_btnSlowRepeatsCounter);
- }
- if (handleButton_RCx_btnSlowRepeatsCounter <= (handleButton_RCx_slowRepeats - 1)) {
- // do not send keypress on last medium repeat before going on with fast repeat mode
- sendKey(_btn, false);
- }
- }
- }
- else if (handleButton_RCx_btnMediumRepeatsCounter <= handleButton_RCx_mediumRepeats && handleButton_RCx_mediumRepeats > 0) {
- if (handleButton_RCx_btnMediumRepeatsCounter2 < handleButton_RCx_mediumRepeatsNumFastRepeats) {
- handleButton_RCx_btnMediumRepeatsCounter2++;
- }
- else {
- handleButton_RCx_btnMediumRepeatsCounter++;
- handleButton_RCx_btnMediumRepeatsCounter2 = 0;
- if (debug2) {
- Serial.print(F("RCx BUTTON REPEAT MEDIUM #"));
- Serial.println(handleButton_RCx_btnMediumRepeatsCounter);
- }
- if (handleButton_RCx_btnMediumRepeatsCounter <= (handleButton_RCx_mediumRepeats - 1)) {
- // do not send keypress on last medium repeat before going on with fast repeat mode
- sendKey(_btn, false);
- }
- }
- }
- else {
- if (debug2) {
- Serial.println(F("RCx BUTTON REPEAT FAST"));
- }
- if(handleButton_RCx_useHoldInsteadOfFastRepeat) {
- if(handleHoldButton_active()) handleHoldButton();
- else {
- sendKey(_btn, true);
- }
- }
- else sendKey(_btn, false);
- }
- }
- handleButton_RCx_btnRepeatsCounter++;
- }
- // BUTTONMODE_HOLD
- else if ((_btn == handleButton_RCx_lastButton) && (_currPrefix == handleButton_RCx_lastPrefix) && _btnMode == BUTTONMODE_HOLD) {
- handleHoldButton();
- }
- handleButton_RCx_lastPrefix = _currPrefix;
- handleButton_RCx_lastButton = _btn;
- }
|