qmk_firmware

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

bajjak.c (6703B)


      1 /*
      2 Copyright 2012 Jun Wako <wakojun@gmail.com>
      3 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
      4 Copyright 2015 ZSA Technology Labs Inc (@zsa)
      5 Copyright 2020 Christopher Courtney <drashna@live.com> (@drashna)
      6 Copyright 2021 Gary Kong <kongkm88@gmail.com> (@garykong)
      7 
      8 This program is free software: you can redistribute it and/or modify
      9 it under the terms of the GNU General Public License as published by
     10 the Free Software Foundation, either version 2 of the License, or
     11 (at your option) any later version.
     12 
     13 This program is distributed in the hope that it will be useful,
     14 but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16 GNU General Public License for more details.
     17 
     18 You should have received a copy of the GNU General Public License
     19 along with this program.  If not, see <http://www.gnu.org/licenses/>.
     20 */
     21 
     22 #include "bajjak.h"
     23 
     24 bool bajjak_left_led_1 = 0;
     25 bool bajjak_left_led_2 = 0;
     26 bool bajjak_left_led_3 = 0;
     27 
     28 extern inline void bajjak_board_led_on(void);
     29 extern inline void bajjak_right_led_1_on(void);
     30 extern inline void bajjak_right_led_2_on(void);
     31 extern inline void bajjak_right_led_3_on(void);
     32 extern inline void bajjak_right_led_on(uint8_t led);
     33 
     34 extern inline void bajjak_board_led_off(void);
     35 extern inline void bajjak_right_led_1_off(void);
     36 extern inline void bajjak_right_led_2_off(void);
     37 extern inline void bajjak_right_led_3_off(void);
     38 extern inline void bajjak_right_led_off(uint8_t led);
     39 
     40 extern inline void bajjak_led_all_on(void);
     41 extern inline void bajjak_led_all_off(void);
     42 
     43 extern inline void bajjak_right_led_1_set(uint8_t n);
     44 extern inline void bajjak_right_led_2_set(uint8_t n);
     45 extern inline void bajjak_right_led_3_set(uint8_t n);
     46 extern inline void bajjak_right_led_set(uint8_t led, uint8_t n);
     47 
     48 extern inline void bajjak_led_all_set(uint8_t n);
     49 
     50 keyboard_config_t keyboard_config;
     51 
     52 bool i2c_initialized = 0;
     53 i2c_status_t mcp23018_status = 0x20;
     54 
     55 void matrix_init_kb(void) {
     56    // keyboard LEDs (see "PWM on ports OC1(A|B|C)" in "teensy-2-0.md")
     57     TCCR1A = 0b10101001;  // set and configure fast PWM
     58     TCCR1B = 0b00001001;  // set and configure fast PWM
     59 
     60     // (tied to Vcc for hardware convenience)
     61     gpio_set_pin_input(B4); // set B(4) as input, internal pull-up disabled
     62 
     63     // unused pins - D4, D5, E6
     64     // set as input with internal pull-up enabled
     65     gpio_set_pin_input_high(D4);
     66     gpio_set_pin_input_high(D5);
     67     gpio_set_pin_input_high(E6);
     68 
     69     keyboard_config.raw = eeconfig_read_kb();
     70     bajjak_led_all_set((uint8_t)keyboard_config.led_level * 255 / 4 );
     71     bajjak_blink_all_leds();
     72 
     73     matrix_init_user();
     74 }
     75 
     76 void bajjak_blink_all_leds(void)
     77 {
     78     bajjak_led_all_off();
     79     bajjak_led_all_set(LED_BRIGHTNESS_DEFAULT);
     80     bajjak_right_led_1_on();
     81     wait_ms(50);
     82     bajjak_right_led_2_on();
     83     wait_ms(50);
     84     bajjak_right_led_3_on();
     85     wait_ms(50);
     86 #ifdef LEFT_LEDS
     87     bajjak_left_led_1_on();
     88     wait_ms(50);
     89     if (!mcp23018_status) {
     90       mcp23018_status = bajjak_left_leds_update();
     91     }
     92     bajjak_left_led_2_on();
     93     wait_ms(50);
     94     if (!mcp23018_status) {
     95       mcp23018_status = bajjak_left_leds_update();
     96     }
     97 #endif
     98     bajjak_right_led_1_off();
     99     wait_ms(50);
    100     bajjak_right_led_2_off();
    101     wait_ms(50);
    102     bajjak_right_led_3_off();
    103 #ifdef LEFT_LEDS
    104     wait_ms(50);
    105     bajjak_left_led_1_off();
    106     if (!mcp23018_status) {
    107       mcp23018_status = bajjak_left_leds_update();
    108     }
    109     wait_ms(50);
    110     bajjak_left_led_2_off();
    111     if (!mcp23018_status) {
    112       mcp23018_status = bajjak_left_leds_update();
    113     }
    114 #endif
    115 
    116     //bajjak_led_all_on();
    117     //wait_ms(333);
    118     bajjak_led_all_off();
    119 }
    120 
    121 uint8_t init_mcp23018(void) {
    122     mcp23018_status = 0x20;
    123 
    124     // I2C subsystem
    125 
    126     // uint8_t sreg_prev;
    127     // sreg_prev=SREG;
    128     // cli();
    129 
    130     if (i2c_initialized == 0) {
    131         i2c_init();  // on pins D(1,0)
    132         i2c_initialized = true;
    133         wait_ms(1000);
    134     }
    135     // i2c_init(); // on pins D(1,0)
    136     // wait_ms(1000);
    137 
    138     // set pin direction
    139     // - unused  : input  : 1
    140     // - input   : input  : 1
    141     // - driving : output : 0
    142     uint8_t data[] = {0b00000000, 0b00111111};
    143     mcp23018_status = i2c_write_register(I2C_ADDR, IODIRA, data, 2, BAJJAK_EZ_I2C_TIMEOUT);
    144 
    145     if (!mcp23018_status) {
    146         // set pull-up
    147         // - unused  : on  : 1
    148         // - input   : on  : 1
    149         // - driving : off : 0
    150         mcp23018_status = i2c_write_register(I2C_ADDR, IODIRA, data, 2, BAJJAK_EZ_I2C_TIMEOUT);
    151     }
    152 
    153 #ifdef LEFT_LEDS
    154     if (!mcp23018_status) mcp23018_status = bajjak_left_leds_update();
    155 #endif // LEFT_LEDS
    156 
    157     // SREG=sreg_prev;
    158 
    159     return mcp23018_status;
    160 }
    161 
    162 #ifdef LEFT_LEDS
    163 uint8_t bajjak_left_leds_update(void) {
    164     if (mcp23018_status) { // if there was an error
    165         return mcp23018_status;
    166     }
    167 #define LEFT_LED_1_SHIFT        7       // in MCP23018 port A
    168 #define LEFT_LED_2_SHIFT        7       // in MCP23018 port B
    169 
    170     // set logical value (doesn't matter on inputs)
    171     // - unused  : hi-Z : 1
    172     // - input   : hi-Z : 1
    173     // - driving : hi-Z : 1
    174     uint8_t data[2];
    175     data[0] = 0b11111111 & ~(bajjak_left_led_1<<LEFT_LED_1_SHIFT);
    176     data[1] = 0b11111111 & ~(bajjak_left_led_2<<LEFT_LED_2_SHIFT);
    177     mcp23018_status = i2c_write_register(I2C_ADDR, OLATA, data, 2, BAJJAK_EZ_I2C_TIMEOUT);
    178 
    179     return mcp23018_status;
    180 }
    181 #endif
    182 
    183 
    184 #ifdef SWAP_HANDS_ENABLE
    185 __attribute__ ((weak))
    186 // swap-hands action needs a matrix to define the swap
    187 const keypos_t hand_swap_config[MATRIX_ROWS][MATRIX_COLS] = {
    188     /* Left hand, matrix positions */
    189     {{0, 13}, {1, 13}, {2, 13}, {3, 13}, {4, 13}, {5, 13}, {6, 13}},
    190     {{0, 12}, {1, 12}, {2, 12}, {3, 12}, {4, 12}, {5, 12}, {6, 12}},
    191     {{0, 11}, {1, 11}, {2, 11}, {3, 11}, {4, 11}, {5, 11}, {6, 11}},
    192     {{0, 10}, {1, 10}, {2, 10}, {3, 10}, {4, 10}, {5, 10}, {6, 10}},
    193     {{0, 9}, {1, 9}, {2, 9}, {3, 9}, {4, 9}, {5, 9}, {6, 9}},
    194     {{0, 8}, {1, 8}, {2, 8}, {3, 8}, {4, 8}, {5, 8}, {6, 8}},
    195     {{0, 7}, {1, 7}, {2, 7}, {3, 7}, {4, 7}, {5, 7}, {6, 7}},
    196     /* Right hand, matrix positions */
    197     {{0, 6}, {1, 6}, {2, 6}, {3, 6}, {4, 6}, {5, 6}, {6, 6}},
    198     {{0, 5}, {1, 5}, {2, 5}, {3, 5}, {4, 5}, {5, 5}, {6, 5}},
    199     {{0, 4}, {1, 4}, {2, 4}, {3, 4}, {4, 4}, {5, 4}, {6, 4}},
    200     {{0, 3}, {1, 3}, {2, 3}, {3, 3}, {4, 3}, {5, 3}, {6, 3}},
    201     {{0, 2}, {1, 2}, {2, 2}, {3, 2}, {4, 2}, {5, 2}, {6, 2}},
    202     {{0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1}},
    203     {{0, 0}, {1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}},
    204 };
    205 #endif
    206 
    207 void eeconfig_init_kb(void) {  // EEPROM is getting reset!
    208     keyboard_config.raw = 0;
    209     keyboard_config.led_level = 4;
    210     eeconfig_update_kb(keyboard_config.raw);
    211     eeconfig_init_user();
    212 }