qmk_firmware

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

giabalanai.c (8806B)


      1 /* Copyright 2020 3araht
      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 
     17 #include "giabalanai.h"
     18 
     19 // the velocity difference from the velocity of the root note.
     20 #define UNISON_VELOCITY_OFFSET 30
     21 
     22 #ifdef RGBLIGHT_ENABLE
     23 #    define NO_LED 255
     24 /* Conversion map from keylocation (MATRIX_ROWS x2(split) x MATRIX_COLS) to led IDs.
     25   led IDs are the number starts "0" from upper left corner of left side,
     26   enumerated from left to right, from top to bottom.
     27   Then emumeration follows to the right side, starting from "60".
     28 
     29   Note that the conversion from physical LED serial alighment to
     30     the led IDs is done with RGBLIGHT_LED_MAP beforehand.                          */
     31 
     32 #    ifdef GIABARINAIX2
     33 const uint8_t PROGMEM convert_key_to_led[] =
     34 {
     35     0,  12,  24,  36,  48,  11,  23,  35,  47,  59,
     36     1,  13,  25,  37,  49,  10,  22,  34,  46,  58,
     37     2,  14,  26,  38,  50,  9,   21,  33,  45,  57,
     38     3,  15,  27,  39,  51,  8,   20,  32,  44,  56,
     39     4,  16,  28,  40,  52,  7,   19,  31,  43,  55,
     40     5,  17,  29,  41,  53,  6,   18,  30,  42,  54,
     41   119, 107,  95,  83,  71, 108,  96,  84,  72,  60,
     42   118, 106,  94,  82,  70, 109,  97,  85,  73,  61,
     43   117, 105,  93,  81,  69, 110,  98,  86,  74,  62,
     44   116, 104,  92,  80,  68, 111,  99,  87,  75,  63,
     45   115, 103,  91,  79,  67, 112, 100,  88,  76,  64,
     46   114, 102,  90,  78,  66, 113, 101,  89,  77,  65
     47 };
     48 
     49 #    else
     50 const uint8_t PROGMEM convert_key_to_led[] =
     51 {
     52 0,      12,    24,    36,    48,      11,    23,    35,     47,     59,
     53 1,      13,    25,    37,    49,      10,    22,    34,     46,     58,
     54 2,      14,    26,    38,    50,      9,     21,    33,     45,     57,
     55 3,      15,    27,    39,    51,      8,     20,    32,     44,     56,
     56 4,      16,    28,    40,    52,      7,     19,    31,     43,     55,
     57 5,      17,    29,    41,    53,      6,     18,    30,     42,     54,
     58 
     59 85,     86,    87,    88,    89,      90,    91,    NO_LED, NO_LED, NO_LED,
     60 98,     99,    100,   101,   102,     103,   104,   NO_LED, NO_LED, NO_LED,
     61 NO_LED, 111,   112,   113,   114,     115,   116,   NO_LED, NO_LED, NO_LED,
     62 NO_LED, 97,    96,    95,    94,      93,    92,    NO_LED, NO_LED, NO_LED,
     63 NO_LED, 110,   109,   108,   107,     106,   105,   NO_LED, NO_LED, NO_LED,
     64 NO_LED, 122,   121,   120,   119,     118,   117,   NO_LED, NO_LED, NO_LED
     65 };
     66 
     67 /* Top 2 rows on the right side (LED:60-84) are
     68  duplicates of the bottom 2 rows (LED:85-122).
     69  LED:97 = Encoder,
     70  LED:110 don't have a duplicate on the top row,
     71  LED:72 is used when r20 is pressed (not a duplicate) */
     72 const uint8_t PROGMEM convert_key_to_led2[] =
     73 {
     74 NO_LED, NO_LED, NO_LED, NO_LED, NO_LED,   NO_LED, NO_LED, NO_LED, NO_LED, NO_LED,
     75 NO_LED, NO_LED, NO_LED, NO_LED, NO_LED,   NO_LED, NO_LED, NO_LED, NO_LED, NO_LED,
     76 NO_LED, NO_LED, NO_LED, NO_LED, NO_LED,   NO_LED, NO_LED, NO_LED, NO_LED, NO_LED,
     77 NO_LED, NO_LED, NO_LED, NO_LED, NO_LED,   NO_LED, NO_LED, NO_LED, NO_LED, NO_LED,
     78 NO_LED, NO_LED, NO_LED, NO_LED, NO_LED,   NO_LED, NO_LED, NO_LED, NO_LED, NO_LED,
     79 NO_LED, NO_LED, NO_LED, NO_LED, NO_LED,   NO_LED, NO_LED, NO_LED, NO_LED, NO_LED,
     80 
     81 NO_LED, NO_LED, NO_LED, NO_LED, NO_LED,   NO_LED, NO_LED, NO_LED, NO_LED, NO_LED,
     82 60,     61,     62,     63,     64,       65,     66,     NO_LED, NO_LED, NO_LED,
     83 72,     73,     74,     75,     76,       77,     78,     NO_LED, NO_LED, NO_LED,
     84 NO_LED, NO_LED, NO_LED, NO_LED, NO_LED,   NO_LED, NO_LED, NO_LED, NO_LED, NO_LED,
     85 NO_LED, NO_LED, 71,     70,     69,       68,     67,     NO_LED, NO_LED, NO_LED,
     86 NO_LED, 84,     83,     82,     81,       80,     79,     NO_LED, NO_LED, NO_LED
     87 };
     88 #    endif
     89 #endif  //  RGBLIGHT_ENABLE
     90 
     91 void my_process_midi4Bass(uint8_t channel, keyrecord_t *record,
     92                           uint8_t *chord_status, uint8_t chord, uint16_t root_note, bool is_single_bass) {
     93     uint8_t velocity = midi_config.velocity;
     94     if (record->event.pressed) {
     95         if (chord_status[chord] == MIDI_INVALID_NOTE) {
     96             uint8_t note = midi_compute_note(root_note);
     97             if (is_single_bass) {
     98                 midi_send_noteon(&midi_device, channel, note, velocity);
     99             } else {
    100                 midi_send_noteon(&midi_device, channel, note, velocity);
    101                 midi_send_noteon(&midi_device, channel, note + 12, velocity);  // +1 Octave
    102             }
    103             dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity);
    104             chord_status[chord] = note;  // store root_note status.
    105         }
    106     } else {
    107         uint8_t note = chord_status[chord];
    108         if (note != MIDI_INVALID_NOTE) {
    109             if (is_single_bass) {
    110                 midi_send_noteoff(&midi_device, channel, note, velocity);
    111             } else {
    112                 midi_send_noteoff(&midi_device, channel, note, velocity);
    113                 midi_send_noteoff(&midi_device, channel, note + 12, velocity);  // +1 Octave
    114             }
    115             dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
    116         }
    117         chord_status[chord] = MIDI_INVALID_NOTE;
    118     }
    119 }
    120 
    121 void my_process_midi4TriadChords(uint8_t channel, keyrecord_t *record,
    122                                  uint8_t *chord_status, uint8_t chord, uint16_t root_note,
    123                                  int8_t offset1, int8_t offset2, int8_t offset3) {
    124     uint8_t velocity = midi_config.velocity;
    125     if (record->event.pressed) {
    126         if (chord_status[chord] == MIDI_INVALID_NOTE) {
    127             uint8_t note = midi_compute_note(root_note);
    128             midi_send_noteon(&midi_device, channel, note + offset1, velocity);
    129             midi_send_noteon(&midi_device, channel, note + offset2, velocity);
    130             midi_send_noteon(&midi_device, channel, note + offset3, velocity);
    131             dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity);
    132             chord_status[chord] = note;  // store root_note status.
    133         }
    134     } else {
    135         uint8_t note = chord_status[chord];
    136         if (note != MIDI_INVALID_NOTE) {
    137             midi_send_noteoff(&midi_device, channel, note + offset1, velocity);
    138             midi_send_noteoff(&midi_device, channel, note + offset2, velocity);
    139             midi_send_noteoff(&midi_device, channel, note + offset3, velocity);
    140             dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
    141         }
    142     chord_status[chord] = MIDI_INVALID_NOTE;
    143     }
    144 }
    145 
    146 void my_process_midi(uint8_t channel, uint16_t keycode, keyrecord_t *record, uint8_t *my_tone_status, int8_t offset, bool melody_unison_suppress) {
    147 
    148     uint8_t tone     = keycode - MIDI_TONE_MIN;
    149 
    150     uint8_t melody_unison_velocity;
    151     if (melody_unison_suppress) {
    152         if (midi_config.velocity > UNISON_VELOCITY_OFFSET){
    153             melody_unison_velocity = midi_config.velocity - UNISON_VELOCITY_OFFSET;
    154         } else {
    155             melody_unison_velocity = 0;
    156         }
    157     } else {
    158         melody_unison_velocity = midi_config.velocity;
    159     }
    160 
    161     if (record->event.pressed) {
    162         if (my_tone_status[tone] == MIDI_INVALID_NOTE) {
    163             uint8_t note = midi_compute_note(keycode);
    164             midi_send_noteon(&midi_device, channel, note + offset, melody_unison_velocity);
    165             dprintf("midi noteon channel:%d note:%d tone:%d velocity:%d\n", channel, note, tone, melody_unison_velocity);
    166             my_tone_status[tone] = note;  // store root_note status.
    167         }
    168     } else {
    169         uint8_t note = my_tone_status[tone];
    170         if (note != MIDI_INVALID_NOTE) {
    171             midi_send_noteoff(&midi_device, channel, note + offset, melody_unison_velocity);
    172             dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, melody_unison_velocity);
    173         }
    174     my_tone_status[tone] = MIDI_INVALID_NOTE;
    175     }
    176 }
    177 
    178 #ifdef RGBLIGHT_ENABLE
    179 void keylight_manager(keyrecord_t *record, uint8_t hue, uint8_t sat, uint8_t val, uint8_t keylocation) {
    180     if (keylocation == NO_LED) {
    181         return;  // do nothing.
    182 #    ifdef CONSOLE_ENABLE
    183         uprintf("keylight_manager, NO_LED\n");
    184 #    endif
    185     }
    186 
    187     if (record->event.pressed) {
    188         rgblight_sethsv_at(hue, sat, val, keylocation);
    189     } else {
    190         rgblight_sethsv_at(HSV_BLACK, keylocation);
    191     }
    192 }
    193 
    194 #endif  // RGBLIGHT_ENABLE
    195