12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- // 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 = 4; // repeats to ignore when a button is beeing held (the first one is always sent)
- uint8_t handleButton_RCx_slowRepeats = 4; // 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 slow repeat?
- // 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, bool _repeat, uint8_t _currPrefix) {
- if (_btn != handleButton_RCx_lastButton || (_btn == handleButton_RCx_lastButton) && (_currPrefix != handleButton_RCx_lastPrefix)) {
- if (debug2) {
- Serial.println(F("RCx BUTTON INITIAL"));
- }
- sendKey(_btn);
- handleButton_RCx_btnRepeatsCounter = 0;
- handleButton_RCx_btnSlowRepeatsCounter = 0;
- handleButton_RCx_btnMediumRepeatsCounter = 0;
- }
- else if ((_btn == handleButton_RCx_lastButton) && (_currPrefix == handleButton_RCx_lastPrefix) && _repeat) {
- if (handleButton_RCx_btnRepeatsCounter >= handleButton_RCx_ignoreFirstRepeats) {
- 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);
- }
- else if (handleButton_RCx_btnSlowRepeatsCounter < handleButton_RCx_slowRepeats) {
- 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);
- }
- sendKey(_btn);
- }
- }
- else if (handleButton_RCx_btnMediumRepeatsCounter <= handleButton_RCx_mediumRepeats) {
- 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);
- }
- sendKey(_btn);
- }
- }
- else {
- if (debug2) {
- Serial.println(F("RCx BUTTON REPEAT FAST"));
- }
- sendKey(_btn);
- }
- }
- handleButton_RCx_btnRepeatsCounter++;
- }
- handleButton_RCx_lastPrefix = _currPrefix;
- handleButton_RCx_lastButton = _btn;
- }
|