Advertisement
hakbraley

Untitled

Aug 31st, 2021
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. /*  https://www.reddit.com/r/olkb/comments/oxlqyi/common_anode_rgb_led_control_with_qmk/
  2.  *  Code to include a specially wired button into the regular QMK keymap.
  3.  *  This should go into keymap.c
  4.  */
  5.  
  6. #ifdef ENC_BUTTON_PIN
  7.     #include "matrix.h"
  8.     #include "debounce.h"
  9.     extern matrix_row_t raw_matrix[MATRIX_ROWS];  // raw values
  10.     extern matrix_row_t matrix[MATRIX_ROWS];      // debounced values
  11. #endif  //ENC_BUTTON_PIN
  12.  
  13.  
  14. void keyboard_pre_init_user(void) {
  15.     setPinInput(ENC_BUTTON_PIN);  //set encoder pin as an input
  16. }
  17.  
  18. void matrix_scan_user(void) {
  19. #ifdef ENC_BUTTON_PIN
  20.     uint16_t prev_row_value = raw_matrix[ENC_ROW];          //save the current state of the row value
  21.    
  22.     if (readPin(ENC_BUTTON_PIN)) {                          //if encoder button is pressed (pin reads HIGH)
  23.         raw_matrix[ENC_ROW] |= 1 << ENC_COL;                //  set encoder button state in matrix
  24.     } else {                                                //if encoder is not pressed (pin reads LOW)
  25.         raw_matrix[ENC_ROW] &= ~(1 << ENC_COL);             //  clear encoder button state in matrix
  26.     }
  27.    
  28.     if ((prev_row_value != raw_matrix[ENC_ROW])) {          //if the encoder state was just changed
  29.         debounce(raw_matrix, matrix, MATRIX_ROWS, true);    //  debounce the matrix
  30.     }  
  31. #endif  //ENC_BUTTON_PIN
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement