123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- // Button handling for IR remotes NEC code
- // - These DO NOT send a toggle bit on every new button press, BUT they send a 0 bit NEC code as repetition instead of the
- // button code, so it is easy to differ between hold and re-pressing
- // - if a Button configured as BUTTONMODE_ONCE the repetitions will be ignored
- // - On re-pressing, this function will send the button again as fast as possible
- // - If a button is held down, the remote will go on sending the 0 bit repeate code code every 70-100ms or so.
- // This function will then:
- // - BUTTONMODE_REPEAT:
- // 1. ignore the first couple of repetition codes to prevent multi triggering on single button presses
- // 2. send repeats as they come in from the remote
- // - BUTTONMODE_REPEAT_EXTENDED:
- // 1. ignore the first couple of repetition codes to prevent multi triggering on single button presses
- // 2. start with some slow repeats
- // 3. switch to medium repeat rate
- // 4. switch to fast repeat rate OR HOLD if configured
- // - BUTTONMODE_HOLD:
- // - press the key and dont release it until no more repetitions come in from the RC, then release
- // configuration variables for NEC button handling
- uint8_t handleButton_NEC_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_NEC_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_NEC_slowRepeats = 0; // amount of slow repeats to send
- uint8_t handleButton_NEC_slowRepeatsNumFastRepeats = 4; // how many fast repeats (=sending rate of the RC remote) represent one slow repeat?
- uint8_t handleButton_NEC_mediumRepeats = 5; // amount of medium repeats to send
- uint8_t handleButton_NEC_mediumRepeatsNumFastRepeats = 2; // how many fast repeats (=sending rate of the RC remote) represent one medium repeat?
- bool handleButton_NEC_useHoldInsteadOfFastRepeat = true;
- // hold button mode (key via USB keyboard emulation is not release until the RC stops sending repeats + this timeout)
- uint8_t handleButton_NEC_holdButton_releaseTimeout = 120; // release keys after a button was held (_btnMode=BUTTON_HOLD) after this ms if triggered by handleButton_NEC function
- // global variables for NEC button handling
- unsigned int handleButton_NEC_btnRepeatsCounter;
- uint8_t handleButton_NEC_btnSlowRepeatsCounter, handleButton_NEC_btnSlowRepeatsCounter2;
- uint8_t handleButton_NEC_btnMediumRepeatsCounter, handleButton_NEC_btnMediumRepeatsCounter2;
- uint8_t handleButton_NEC_lastButton = 0;
- uint8_t handleButton_NEC_lastButtonMode = 0;
- void handleButton_NEC(uint8_t _btn, uint8_t _btnMode) {
- bool _repeatMode = false;
- bool _extRepeatMode = false;
- bool _holdMode = false;
- if (_btn == BTN_REPETITION && _btnMode == BUTTONMODE_LAST) {
- if (handleButton_NEC_lastButtonMode == BUTTONMODE_REPEAT) _repeatMode = true;
- else if (handleButton_NEC_lastButtonMode == BUTTONMODE_EXTENDED_REPEAT) _extRepeatMode = true;
- else if (handleButton_NEC_lastButtonMode == BUTTONMODE_HOLD) _holdMode = true;
- }
- else {
- if (_btnMode == BUTTONMODE_REPEAT) _repeatMode = true;
- else if (_btnMode == BUTTONMODE_EXTENDED_REPEAT) _extRepeatMode = true;
- else if (_btnMode == BUTTONMODE_HOLD) {
- _holdMode = true;
- handleHoldButton_setTimeout(handleButton_NEC_holdButton_releaseTimeout);
- }
- }
- // single/first button press
- if (_btn != BTN_REPETITION) {
- handleButton_NEC_lastButtonMode = _btnMode;
- if (_btn == BTN_UNKNOWN) {
- // reset lastButton to 0 / BTN_UNKNOWN to avoid triggering a key if an unknown/unimplemented button is held
- handleButton_NEC_lastButton = BTN_UNKNOWN;
- }
- else if (_btn == BTN_POWER) {
- handleButton_NEC_lastButton = _btn;
- handlePowerButton();
- }
- else if (_btn == BTN_TASKSWITCH) {
- handleButton_NEC_lastButton = _btn;
- handleTaskSwitchButton();
- }
- else {
- handleButton_NEC_lastButton = _btn;
- if (_btnMode == BUTTONMODE_HOLD) {
- releaseAllKeys();
- sendKey(_btn, true);
- }
- else sendKey(_btn, false);
- }
- handleButton_NEC_btnRepeatsCounter = 0;
- handleButton_NEC_btnSlowRepeatsCounter = 0;
- handleButton_NEC_btnMediumRepeatsCounter = 0;
- }
- // BUTTONMODE_REPEAT (direct send a keypress on every repeat received from the RC, except of the first ones)
- else if (_btn == BTN_REPETITION && _repeatMode) {
- if (handleButton_NEC_lastButton == BTN_POWER) {
- handlePowerButton();
- }
- else if (handleButton_NEC_lastButton == BTN_TASKSWITCH) {
- handleTaskSwitchButton();
- }
- else {
- handleButton_NEC_btnRepeatsCounter++;
- if (debug2) {
- Serial.print(F("REP #"));
- Serial.println(handleButton_NEC_btnRepeatsCounter);
- }
- if (handleButton_NEC_btnRepeatsCounter > handleButton_NEC_ignoreFirstRepeats) {
- // filter out 1st repetitions
- sendKey(handleButton_NEC_lastButton, false);
- }
- }
- }
- // BUTTONMODE_EXTENDED_REPEAT (first slow, then getting faster)
- else if (_btn == BTN_REPETITION && _extRepeatMode) {
- if (handleButton_NEC_btnRepeatsCounter >= handleButton_NEC_ignoreFirstRepeats_Ext) {
- if (handleButton_NEC_btnSlowRepeatsCounter == 0) {
- handleButton_NEC_btnSlowRepeatsCounter++;
- handleButton_NEC_btnSlowRepeatsCounter2 = 0;
- if (debug2) {
- Serial.print(F("NEC BUTTON REPEAT SLOW #"));
- Serial.println(handleButton_NEC_btnSlowRepeatsCounter);
- }
- sendKey(handleButton_NEC_lastButton, false);
- }
- else if (handleButton_NEC_btnSlowRepeatsCounter < handleButton_NEC_slowRepeats && handleButton_NEC_slowRepeats > 0) {
- if (handleButton_NEC_btnSlowRepeatsCounter2 < handleButton_NEC_slowRepeatsNumFastRepeats) {
- handleButton_NEC_btnSlowRepeatsCounter2++;
- }
- else {
- handleButton_NEC_btnSlowRepeatsCounter++;
- handleButton_NEC_btnSlowRepeatsCounter2 = 0;
- if (debug2) {
- Serial.print(F("NEC BUTTON REPEAT SLOW #"));
- Serial.println(handleButton_NEC_btnSlowRepeatsCounter);
- }
- if (handleButton_NEC_btnSlowRepeatsCounter <= (handleButton_NEC_slowRepeats - 1)) {
- // do not send keypress on last medium repeat before going on with fast repeat mode
- sendKey(handleButton_NEC_lastButton, false);
- }
- }
- }
- else if (handleButton_NEC_btnMediumRepeatsCounter <= handleButton_NEC_mediumRepeats && handleButton_NEC_mediumRepeats > 0) {
- if (handleButton_NEC_btnMediumRepeatsCounter2 < handleButton_NEC_mediumRepeatsNumFastRepeats) {
- handleButton_NEC_btnMediumRepeatsCounter2++;
- }
- else {
- handleButton_NEC_btnMediumRepeatsCounter++;
- handleButton_NEC_btnMediumRepeatsCounter2 = 0;
- if (debug2) {
- Serial.print(F("NEC BUTTON REPEAT MEDIUM #"));
- Serial.println(handleButton_NEC_btnMediumRepeatsCounter);
- }
- if (handleButton_NEC_btnMediumRepeatsCounter <= (handleButton_NEC_mediumRepeats - 1)) {
- // do not send keypress on last medium repeat before going on with fast repeat mode
- sendKey(handleButton_NEC_lastButton, false);
- }
- }
- }
- else {
- if (debug2) {
- Serial.println(F("NEC BUTTON REPEAT FAST"));
- }
- if (handleButton_NEC_useHoldInsteadOfFastRepeat) {
- if (handleHoldButton_active()) handleHoldButton();
- else {
- sendKey(handleButton_NEC_lastButton, true);
- }
- }
- else sendKey(handleButton_NEC_lastButton, false);
- }
- }
- handleButton_NEC_btnRepeatsCounter++;
- }
- // HOLD
- else if ((_btn == BTN_REPETITION) && (_holdMode || _extRepeatMode)) {
- handleHoldButton();
- }
- }
|