]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - quantum/quantum.c
Added pin C6 to hardware PWM backlight
[qmk_firmware.git] / quantum / quantum.c
index bd95d5ea80881b9b6317a59c0ab054a9f9975546..b9934aee8faeb906f9f8e581200108539d802a8b 100644 (file)
@@ -147,7 +147,7 @@ void reset_keyboard(void) {
 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
   process_midi_all_notes_off();
 #endif
-#if defined(AUDIO_ENABLE)
+#if defined(AUDIO_ENABLE) && !defined(NO_MUSIC_MODE)
   music_all_notes_off();
   uint16_t timer_start = timer_read();
   PLAY_SONG(goodbye_song);
@@ -226,7 +226,13 @@ bool process_record_quantum(keyrecord_t *record) {
     // Must run first to be able to mask key_up events.
     process_key_lock(&keycode, record) &&
   #endif
+  #if defined(AUDIO_ENABLE) && defined(AUDIO_CLICKY)
+      process_clicky(keycode, record) &&
+  #endif //AUDIO_CLICKY
     process_record_kb(keycode, record) &&
+  #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_KEYPRESSES)
+    process_rgb_matrix(keycode, record) &&
+  #endif
   #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
     process_midi(keycode, record) &&
   #endif
@@ -236,7 +242,7 @@ bool process_record_quantum(keyrecord_t *record) {
   #ifdef STENO_ENABLE
     process_steno(keycode, record) &&
   #endif
-  #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
+  #if ( defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))) && !defined(NO_MUSIC_MODE)
     process_music(keycode, record) &&
   #endif
   #ifdef TAP_DANCE_ENABLE
@@ -304,7 +310,7 @@ bool process_record_quantum(keyrecord_t *record) {
     }
     return false;
   #endif
-  #ifdef RGBLIGHT_ENABLE
+  #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
   case RGB_TOG:
     if (record->event.pressed) {
       rgblight_toggle();
@@ -362,6 +368,16 @@ bool process_record_quantum(keyrecord_t *record) {
       rgblight_decrease_val();
     }
     return false;
+  case RGB_SPI:
+    if (record->event.pressed) {
+      rgblight_increase_speed();
+    }
+    return false;
+  case RGB_SPD:
+    if (record->event.pressed) {
+      rgblight_decrease_speed();
+    }
+    return false;
   case RGB_MODE_PLAIN:
     if (record->event.pressed) {
       rgblight_mode(1);
@@ -426,7 +442,12 @@ bool process_record_quantum(keyrecord_t *record) {
       }
     }
     return false;
-  #endif
+  case RGB_MODE_RGBTEST:
+    if (record->event.pressed) {
+      rgblight_mode(35);
+    }
+    return false;
+  #endif // defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
     #ifdef PROTOCOL_LUFA
     case OUT_AUTO:
       if (record->event.pressed) {
@@ -777,12 +798,14 @@ void set_single_persistent_default_layer(uint8_t default_layer) {
   default_layer_set(1U<<default_layer);
 }
 
+uint32_t update_tri_layer_state(uint32_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3) {
+  uint32_t mask12 = (1UL << layer1) | (1UL << layer2);
+  uint32_t mask3 = 1UL << layer3;
+  return (state & mask12) == mask12 ? (state | mask3) : (state & ~mask3);
+}
+
 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
-  if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
-    layer_on(layer3);
-  } else {
-    layer_off(layer3);
-  }
+  layer_state_set(update_tri_layer_state(layer_state, layer1, layer2, layer3));
 }
 
 void tap_random_base64(void) {
@@ -830,11 +853,20 @@ void matrix_init_quantum() {
   #ifdef AUDIO_ENABLE
     audio_init();
   #endif
+  #ifdef RGB_MATRIX_ENABLE
+    rgb_matrix_init();
+  #endif
   matrix_init_kb();
 }
 
+uint8_t rgb_matrix_task_counter = 0;
+
+#ifndef RGB_MATRIX_SKIP_FRAMES
+  #define RGB_MATRIX_SKIP_FRAMES 1
+#endif
+
 void matrix_scan_quantum() {
-  #ifdef AUDIO_ENABLE
+  #if defined(AUDIO_ENABLE)
     matrix_scan_music();
   #endif
 
@@ -850,23 +882,45 @@ void matrix_scan_quantum() {
     backlight_task();
   #endif
 
+  #ifdef RGB_MATRIX_ENABLE
+    rgb_matrix_task();
+    if (rgb_matrix_task_counter == 0) {
+      rgb_matrix_update_pwm_buffers();
+    }
+    rgb_matrix_task_counter = ((rgb_matrix_task_counter + 1) % (RGB_MATRIX_SKIP_FRAMES + 1));
+  #endif
+
   matrix_scan_kb();
 }
-
 #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
 
 static const uint8_t backlight_pin = BACKLIGHT_PIN;
 
 // depending on the pin, we use a different output compare unit
 #if BACKLIGHT_PIN == B7
-#  define COM1x1 COM1C1
-#  define OCR1x  OCR1C
+#  define TCCRxA TCCR1A
+#  define TCCRxB TCCR1B
+#  define COMxx1 COM1C1
+#  define OCRxx  OCR1C
+#  define ICRx   ICR1
 #elif BACKLIGHT_PIN == B6
-#  define COM1x1 COM1B1
-#  define OCR1x  OCR1B
+#  define TCCRxA TCCR1A
+#  define TCCRxB TCCR1B
+#  define COMxx1 COM1B1
+#  define OCRxx  OCR1B
+#  define ICRx   ICR1
 #elif BACKLIGHT_PIN == B5
-#  define COM1x1 COM1A1
-#  define OCR1x  OCR1A
+#  define TCCRxA TCCR1A
+#  define TCCRxB TCCR1B
+#  define COMxx1 COM1A1
+#  define OCRxx  OCR1A
+#  define ICRx   ICR1
+#elif BACKLIGHT_PIN == C6
+#  define TCCRxA TCCR3A
+#  define TCCRxB TCCR3B
+#  define COMxx1 COM1A1
+#  define OCRxx  OCR3A
+#  define ICRx   ICR3
 #else
 #  define NO_HARDWARE_PWM
 #endif
@@ -948,7 +1002,7 @@ static uint16_t cie_lightness(uint16_t v) {
 
 // range for val is [0..TIMER_TOP]. PWM pin is high while the timer count is below val.
 static inline void set_pwm(uint16_t val) {
-  OCR1x = val;
+       OCRxx = val;
 }
 
 #ifndef BACKLIGHT_CUSTOM_DRIVER
@@ -959,10 +1013,10 @@ void backlight_set(uint8_t level) {
 
   if (level == 0) {
     // Turn off PWM control on backlight pin
-    TCCR1A &= ~(_BV(COM1x1));
+    TCCRxA &= ~(_BV(COMxx1));
   } else {
     // Turn on PWM control of backlight pin
-    TCCR1A |= _BV(COM1x1);
+    TCCRxA |= _BV(COMxx1);
   }
   // Set the brightness
   set_pwm(cie_lightness(TIMER_TOP * (uint32_t)level / BACKLIGHT_LEVELS));
@@ -1110,11 +1164,10 @@ void backlight_init_ports(void)
   "In fast PWM mode, the compare units allow generation of PWM waveforms on the OCnx pins. Setting the COMnx1:0 bits to two will produce a non-inverted PWM [..]."
   "In fast PWM mode the counter is incremented until the counter value matches either one of the fixed values 0x00FF, 0x01FF, or 0x03FF (WGMn3:0 = 5, 6, or 7), the value in ICRn (WGMn3:0 = 14), or the value in OCRnA (WGMn3:0 = 15)."
   */
-
-  TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
-  TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
+  TCCRxA = _BV(COMxx1) | _BV(WGM11); // = 0b00001010;
+  TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
   // Use full 16-bit resolution. Counter counts to ICR1 before reset to 0.
-  ICR1 = TIMER_TOP;
+  ICRx = TIMER_TOP;
 
   backlight_init();
   #ifdef BACKLIGHT_BREATHING