qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

v2.c (3844B)


      1 /* Copyright 2017 MechMerlin <mechmerlin@gmail.com>
      2  *
      3  * This program is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published by
      5  * the Free Software Foundation, either version 2 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * This program is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     15  */
     16 #include "quantum.h"
     17 #include "indicator_leds.h"
     18 
     19 enum BACKLIGHT_AREAS {
     20   BACKLIGHT_ALPHA    = 0b0000001,
     21   BACKLIGHT_EXTRA    = 0b0000010,
     22   BACKLIGHT_MODNUM   = 0b0000100,
     23   BACKLIGHT_FROW     = 0b0001000,
     24   BACKLIGHT_RGB      = 0b0010000,
     25   BACKLIGHT_SWITCH   = 0b0001111
     26 };
     27 
     28 uint8_t backlight_rgb_r = 255;
     29 uint8_t backlight_rgb_g = 0;
     30 uint8_t backlight_rgb_b = 0;
     31 uint8_t backlight_os_state = 0;
     32 uint32_t backlight_layer_state = 0;
     33 
     34 void backlight_toggle_rgb(bool enabled)
     35 {
     36   if(enabled) {
     37     uint8_t rgb[17][3] = {
     38       {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
     39       {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
     40       {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
     41       {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
     42       {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
     43       {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
     44       {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
     45       {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
     46       {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
     47       {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
     48       {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
     49       {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
     50       {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
     51       {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
     52       {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
     53       {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
     54       {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b}
     55     };
     56     backlight_set_rgb(rgb);
     57   } else {
     58     uint8_t rgb[17][3] = {
     59       {0, 0, 0},
     60       {0, 0, 0},
     61       {0, 0, 0},
     62       {0, 0, 0},
     63       {0, 0, 0},
     64       {0, 0, 0},
     65       {0, 0, 0},
     66       {0, 0, 0},
     67       {0, 0, 0},
     68       {0, 0, 0},
     69       {0, 0, 0},
     70       {0, 0, 0},
     71       {0, 0, 0},
     72       {0, 0, 0},
     73       {0, 0, 0},
     74       {0, 0, 0},
     75       {0, 0, 0}
     76     };
     77     backlight_set_rgb(rgb);
     78   }
     79 }
     80 
     81 void backlight_set_rgb(uint8_t cfg[17][3])
     82 {
     83   cli();
     84   for(uint8_t i = 0; i < 17; ++i) {
     85     send_color(cfg[i][0], cfg[i][1], cfg[i][2], Device_PCBRGB);
     86   }
     87   sei();
     88   show();
     89 }
     90 
     91 void backlight_set(uint8_t level) {
     92   level & BACKLIGHT_ALPHA ? (PORTB |= 0b00000010) : (PORTB &= ~0b00000010);
     93   level & BACKLIGHT_EXTRA ? (PORTB |= 0b00000100) : (PORTB &= ~0b00000100);
     94   level & BACKLIGHT_MODNUM ? (PORTB |= 0b00001000) : (PORTB &= ~0b00001000);
     95   level & BACKLIGHT_FROW ? (PORTE |= 0b01000000) : (PORTE &= ~0b01000000);
     96   level & BACKLIGHT_RGB ? backlight_toggle_rgb(true) : backlight_toggle_rgb(false);
     97 }
     98 
     99 // Port from backlight_update_state
    100 bool led_update_kb(led_t led_state) {
    101   bool res = led_update_user(led_state);
    102   if(res) {
    103     bool status[7] = {
    104       backlight_os_state & 2,
    105       backlight_os_state & 4,
    106       backlight_os_state & 1,
    107       backlight_layer_state & (1<<1),
    108       backlight_layer_state & (1<<2),
    109       backlight_layer_state & (1<<3),
    110       backlight_layer_state & (1<<4)
    111     };
    112     indicator_leds_set(status);
    113     backlight_os_state & 2 ? (PORTB &= ~0b00000001) : (PORTB |= 0b00000001);
    114     backlight_os_state & 4 ? (PORTB &= ~0b00010000) : (PORTB |= 0b00010000);
    115   }
    116   return res;
    117 }