From 365b86357825ebed7012377a5684269760326fd2 Mon Sep 17 00:00:00 2001 From: Kenneth Aloysius Date: Fri, 19 Jan 2018 08:02:32 +1100 Subject: [PATCH] Backlighting for JJ40 and underglow initialisation code (#2260) * Cleanup Mechmini keymap. Once the custom RGB function is defined, there is no need to manually handle RGB code. * Change default to KEYMAP_MIT, not KEYMAP_OFFSET * Add custom RGB code for JJ40 * Reset Mechmini advertised power draw to 500. Will have to test actual maximum power draw later. * RGB working on JJ40. * Fix: saturation increase/decrease flipped * Add new directory for my custom keymap with RGB keycodes * Swap LAlt and LGUI * Update JJ40 max power draw with measured value * Update: fun40 rules.mk to enable underglow; earlier failed Travis CI * Fix: init RGB LEDs on boot. Also added HHKB-like keymap for XD60. * Super rudimentary backlight test, init RGB LEDs on boot * Backlighting works - stays on for now * Toggling working * Now can override backlight.c functions. Problem was functions in backlight.c weren't called before due to a lack of matrix_scan_quantum() in matrix.c * Timers not working * Delete global.h * Cleanup * Compiles * Good sign: LEDs stop working again * Handle timer1 overflow * Progress: fix: forgot to init * Backlighting fully working now except breathing. * Revert keymap to original keycodes * Update XD60 keymap README * Update JJ40 keymap with backlight toggles * Breathing working just fine. * Update references * Add backlight_set() call * Cleanup code to disable backlight * Fix: does not compile * Fix: missing call to rgblight_task. * Testing with BACKLIGHT_BREATHING * Cleanup * Cleanup comments * More commenting cleanup. * Do not enable BACKLIGHT_BREATHING by default --- common_features.mk | 3 + keyboards/jj40/backlight.c | 212 ++++++++++++++++++++ keyboards/jj40/backlight_custom.h | 15 ++ keyboards/jj40/breathing_custom.h | 140 +++++++++++++ keyboards/jj40/config.h | 6 +- keyboards/jj40/jj40.c | 42 +++- keyboards/jj40/jj40.h | 2 + keyboards/jj40/keymaps/default/keymap.c | 17 -- keyboards/jj40/keymaps/krusli/config.h | 8 + keyboards/jj40/keymaps/krusli/keymap.c | 23 +-- keyboards/jj40/matrix.c | 36 ++-- keyboards/jj40/rules.mk | 8 +- keyboards/jj40/usbconfig.h | 2 +- keyboards/xd60/keymaps/krusli/HHKB-like.txt | 0 keyboards/xd60/keymaps/krusli/keymap.c | 62 ++++++ keyboards/xd60/keymaps/krusli/readme.md | 2 + quantum/quantum.c | 12 +- tmk_core/common/backlight.c | 2 +- 18 files changed, 528 insertions(+), 64 deletions(-) create mode 100644 keyboards/jj40/backlight.c create mode 100644 keyboards/jj40/backlight_custom.h create mode 100644 keyboards/jj40/breathing_custom.h create mode 100644 keyboards/jj40/keymaps/krusli/config.h create mode 100644 keyboards/xd60/keymaps/krusli/HHKB-like.txt create mode 100644 keyboards/xd60/keymaps/krusli/keymap.c create mode 100644 keyboards/xd60/keymaps/krusli/readme.md diff --git a/common_features.mk b/common_features.mk index a67cf230f3..1c0b3546a1 100644 --- a/common_features.mk +++ b/common_features.mk @@ -155,6 +155,9 @@ endif ifeq ($(strip $(BACKLIGHT_ENABLE)), yes) ifeq ($(strip $(VISUALIZER_ENABLE)), yes) CIE1931_CURVE = yes + endif + ifeq ($(strip $(BACKLIGHT_CUSTOM_DRIVER)), yes) + OPT_DEFS += -DBACKLIGHT_CUSTOM_DRIVER endif endif diff --git a/keyboards/jj40/backlight.c b/keyboards/jj40/backlight.c new file mode 100644 index 0000000000..079c410ff5 --- /dev/null +++ b/keyboards/jj40/backlight.c @@ -0,0 +1,212 @@ +/** + * Backlighting code for PS2AVRGB boards (ATMEGA32A) + * Kenneth A. (github.com/krusli | krusli.me) + */ + +#include "backlight.h" +#include "quantum.h" + +#include +#include + +#include "backlight_custom.h" +#include "breathing_custom.h" + +// DEBUG +#include +#include + +// Port D: digital pins of the AVR chipset +#define NUMLOCK_PORT (1 << 1) // 1st pin of Port D (digital) +#define CAPSLOCK_PORT (1 << 2) // 2nd pin +#define BACKLIGHT_PORT (1 << 4) // 4th pin +#define SCROLLLOCK_PORT (1 << 6) // 6th pin + +#define TIMER_CLK_DIV64 0x03 ///< Timer clocked at F_CPU/64 +#define TIMER1PRESCALE TIMER_CLK_DIV64 ///< timer 1 prescaler default + +#define TIMER_PRESCALE_MASK 0x07 ///< Timer Prescaler Bit-Mask + +#define PWM_MAX 0xFF +#define TIMER_TOP 255 // 8 bit PWM + +extern backlight_config_t backlight_config; + +/** + * References + * Port Registers: https://www.arduino.cc/en/Reference/PortManipulation + * TCCR1A: https://electronics.stackexchange.com/questions/92350/what-is-the-difference-between-tccr1a-and-tccr1b + * Timers: http://www.avrbeginners.net/architecture/timers/timers.html + * 16-bit timer setup: http://sculland.com/ATmega168/Interrupts-And-Timers/16-Bit-Timer-Setup/ + * PS2AVRGB firmware: https://github.com/showjean/ps2avrU/tree/master/firmware + */ + +// @Override +// turn LEDs on and off depending on USB caps/num/scroll lock states. +void led_set_user(uint8_t usb_led) { + if (usb_led & (1 << USB_LED_NUM_LOCK)) { + // turn on + DDRD |= NUMLOCK_PORT; + PORTD |= NUMLOCK_PORT; + } else { + // turn off + DDRD &= ~NUMLOCK_PORT; + PORTD &= ~NUMLOCK_PORT; + } + + if (usb_led & (1 << USB_LED_CAPS_LOCK)) { + DDRD |= CAPSLOCK_PORT; + PORTD |= CAPSLOCK_PORT; + } else { + DDRD &= ~CAPSLOCK_PORT; + PORTD &= ~CAPSLOCK_PORT; + } + + if (usb_led & (1 << USB_LED_SCROLL_LOCK)) { + DDRD |= SCROLLLOCK_PORT; + PORTD |= SCROLLLOCK_PORT; + } else { + DDRD &= ~SCROLLLOCK_PORT; + PORTD &= ~SCROLLLOCK_PORT; + } +} + +#ifdef BACKLIGHT_ENABLE + +// sets up Timer 1 for 8-bit PWM +void timer1PWMSetup(void) { // NOTE ONLY CALL THIS ONCE + // default 8 bit mode + TCCR1A &= ~(1 << 1); // cbi(TCCR1A,PWM11); <- set PWM11 bit to HIGH + TCCR1A |= (1 << 0); // sbi(TCCR1A,PWM10); <- set PWM10 bit to LOW + + // clear output compare value A + // outb(OCR1AH, 0); + // outb(OCR1AL, 0); + + // clear output comparator registers for B + OCR1BH = 0; // outb(OCR1BH, 0); + OCR1BL = 0; // outb(OCR1BL, 0); +} + +bool is_init = false; +void timer1Init(void) { + // timer1SetPrescaler(TIMER1PRESCALE) + // set to DIV/64 + (TCCR1B) = ((TCCR1B) & ~TIMER_PRESCALE_MASK) | TIMER1PRESCALE; + + // reset TCNT1 + TCNT1H = 0; // outb(TCNT1H, 0); + TCNT1L = 0; // outb(TCNT1L, 0); + + // TOIE1: Timer Overflow Interrupt Enable (Timer 1); + TIMSK |= _BV(TOIE1); // sbi(TIMSK, TOIE1); + + is_init = true; +} + +void timer1UnInit(void) { + // set prescaler back to NONE + (TCCR1B) = ((TCCR1B) & ~TIMER_PRESCALE_MASK) | 0x00; // TIMERRTC_CLK_STOP + + // disable timer overflow interrupt + TIMSK &= ~_BV(TOIE1); // overflow bit? + + setPWM(0); + + is_init = false; +} + + +// handle TCNT1 overflow +//! Interrupt handler for tcnt1 overflow interrupt +ISR(TIMER1_OVF_vect, ISR_NOBLOCK) +{ + // sei(); + // handle breathing here + #ifdef BACKLIGHT_BREATHING + if (is_breathing()) { + custom_breathing_handler(); + } + #endif + + // TODO call user defined function +} + +// enable timer 1 PWM +// timer1PWMBOn() +void timer1PWMBEnable(void) { + // turn on channel B (OC1B) PWM output + // set OC1B as non-inverted PWM + TCCR1A |= _BV(COM1B1); + TCCR1A &= ~_BV(COM1B0); +} + +// disable timer 1 PWM +// timer1PWMBOff() +void timer1PWMBDisable(void) { + TCCR1A &= ~_BV(COM1B1); + TCCR1A &= ~_BV(COM1B0); +} + +void enableBacklight(void) { + DDRD |= BACKLIGHT_PORT; // set digital pin 4 as output + PORTD |= BACKLIGHT_PORT; // set digital pin 4 to high +} + +void disableBacklight(void) { + // DDRD &= ~BACKLIGHT_PORT; // set digital pin 4 as input + PORTD &= ~BACKLIGHT_PORT; // set digital pin 4 to low +} + +void startPWM(void) { + timer1Init(); + timer1PWMBEnable(); + enableBacklight(); +} + +void stopPWM(void) { + timer1UnInit(); + disableBacklight(); + timer1PWMBDisable(); +} + +void b_led_init_ports(void) { + /* turn backlight on/off depending on user preference */ + #if BACKLIGHT_ON_STATE == 0 + // DDRx register: sets the direction of Port D + // DDRD &= ~BACKLIGHT_PORT; // set digital pin 4 as input + PORTD &= ~BACKLIGHT_PORT; // set digital pin 4 to low + #else + DDRD |= BACKLIGHT_PORT; // set digital pin 4 as output + PORTD |= BACKLIGHT_PORT; // set digital pin 4 to high + #endif + + timer1PWMSetup(); + startPWM(); + + #ifdef BACKLIGHT_BREATHING + breathing_enable(); + #endif +} + +void b_led_set(uint8_t level) { + if (level > BACKLIGHT_LEVELS) { + level = BACKLIGHT_LEVELS; + } + + setPWM((int)(TIMER_TOP * (float) level / BACKLIGHT_LEVELS)); +} + +// called every matrix scan +void b_led_task(void) { + // do nothing for now +} + +void setPWM(uint16_t xValue) { + if (xValue > TIMER_TOP) { + xValue = TIMER_TOP; + } + OCR1B = xValue; // timer1PWMBSet(xValue); +} + +#endif // BACKLIGHT_ENABLE diff --git a/keyboards/jj40/backlight_custom.h b/keyboards/jj40/backlight_custom.h new file mode 100644 index 0000000000..7210be840e --- /dev/null +++ b/keyboards/jj40/backlight_custom.h @@ -0,0 +1,15 @@ +/** + * Backlighting code for PS2AVRGB boards (ATMEGA32A) + * Kenneth A. (github.com/krusli | krusli.me) + */ + +#ifndef BACKLIGHT_CUSTOM_H +#define BACKLIGHT_CUSTOM_H + +#include +void b_led_init_ports(void); +void b_led_set(uint8_t level); +void b_led_task(void); +void setPWM(uint16_t xValue); + +#endif // BACKLIGHT_CUSTOM_H diff --git a/keyboards/jj40/breathing_custom.h b/keyboards/jj40/breathing_custom.h new file mode 100644 index 0000000000..71416b1b45 --- /dev/null +++ b/keyboards/jj40/breathing_custom.h @@ -0,0 +1,140 @@ +/** + * Breathing effect code for PS2AVRGB boards (ATMEGA32A) + * Works in conjunction with `backlight.c`. + * + * Code adapted from `quantum.c` to register with the existing TIMER1 overflow + * handler in `backlight.c` instead of setting up its own timer. + * Kenneth A. (github.com/krusli | krusli.me) + */ + +#ifdef BACKLIGHT_ENABLE +#ifdef BACKLIGHT_BREATHING + +#include "backlight_custom.h" + +#ifndef BREATHING_PERIOD +#define BREATHING_PERIOD 6 +#endif + +#define breathing_min() do {breathing_counter = 0;} while (0) +#define breathing_max() do {breathing_counter = breathing_period * 244 / 2;} while (0) + +// TODO make this share code with quantum.c + +#define BREATHING_NO_HALT 0 +#define BREATHING_HALT_OFF 1 +#define BREATHING_HALT_ON 2 +#define BREATHING_STEPS 128 + +static uint8_t breathing_period = BREATHING_PERIOD; +static uint8_t breathing_halt = BREATHING_NO_HALT; +static uint16_t breathing_counter = 0; + +static bool breathing = false; + +bool is_breathing(void) { + return breathing; +} + +// See http://jared.geek.nz/2013/feb/linear-led-pwm +static uint16_t cie_lightness(uint16_t v) { + if (v <= 5243) // if below 8% of max + return v / 9; // same as dividing by 900% + else { + uint32_t y = (((uint32_t) v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare + // to get a useful result with integer division, we shift left in the expression above + // and revert what we've done again after squaring. + y = y * y * y >> 8; + if (y > 0xFFFFUL) // prevent overflow + return 0xFFFFU; + else + return (uint16_t) y; + } +} + +void breathing_enable(void) { + breathing = true; + breathing_counter = 0; + breathing_halt = BREATHING_NO_HALT; + // interrupt already registered +} + +void breathing_pulse(void) { + if (get_backlight_level() == 0) + breathing_min(); + else + breathing_max(); + breathing_halt = BREATHING_HALT_ON; + // breathing_interrupt_enable(); + breathing = true; +} + +void breathing_disable(void) { + breathing = false; + // backlight_set(get_backlight_level()); + b_led_set(get_backlight_level()); // custom implementation of backlight_set() +} + +void breathing_self_disable(void) +{ + if (get_backlight_level() == 0) + breathing_halt = BREATHING_HALT_OFF; + else + breathing_halt = BREATHING_HALT_ON; +} + +void breathing_toggle(void) { + if (is_breathing()) + breathing_disable(); + else + breathing_enable(); +} + +void breathing_period_set(uint8_t value) +{ + if (!value) + value = 1; + breathing_period = value; +} + +void breathing_period_default(void) { + breathing_period_set(BREATHING_PERIOD); +} + +void breathing_period_inc(void) +{ + breathing_period_set(breathing_period+1); +} + +void breathing_period_dec(void) +{ + breathing_period_set(breathing_period-1); +} + +/* To generate breathing curve in python: + * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)] + */ +static const uint8_t breathing_table[BREATHING_STEPS] PROGMEM = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 17, 20, 24, 28, 32, 36, 41, 46, 51, 57, 63, 70, 76, 83, 91, 98, 106, 113, 121, 129, 138, 146, 154, 162, 170, 178, 185, 193, 200, 207, 213, 220, 225, 231, 235, 240, 244, 247, 250, 252, 253, 254, 255, 254, 253, 252, 250, 247, 244, 240, 235, 231, 225, 220, 213, 207, 200, 193, 185, 178, 170, 162, 154, 146, 138, 129, 121, 113, 106, 98, 91, 83, 76, 70, 63, 57, 51, 46, 41, 36, 32, 28, 24, 20, 17, 15, 12, 10, 8, 6, 5, 4, 3, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + +// Use this before the cie_lightness function. +static inline uint16_t scale_backlight(uint16_t v) { + return v / BACKLIGHT_LEVELS * get_backlight_level(); +} + +void custom_breathing_handler(void) { + uint16_t interval = (uint16_t) breathing_period * 244 / BREATHING_STEPS; + // resetting after one period to prevent ugly reset at overflow. + breathing_counter = (breathing_counter + 1) % (breathing_period * 244); + uint8_t index = breathing_counter / interval % BREATHING_STEPS; + + if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) || + ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1))) + { + // breathing_interrupt_disable(); + } + + setPWM(cie_lightness(scale_backlight((uint16_t) pgm_read_byte(&breathing_table[index]) * 0x0101U))); +} + +#endif // BACKLIGHT_BREATHING +#endif // BACKLIGHT_ENABLE diff --git a/keyboards/jj40/config.h b/keyboards/jj40/config.h index 3152f22beb..7fac1ed5b9 100644 --- a/keyboards/jj40/config.h +++ b/keyboards/jj40/config.h @@ -34,8 +34,8 @@ along with this program. If not, see . /* COL2ROW or ROW2COL */ #define DIODE_DIRECTION COL2ROW -#define BACKLIGHT_PIN B6 -#define BACKLIGHT_LEVELS 3 +#define BACKLIGHT_LEVELS 12 +// #define BACKLIGHT_BREATHING // works, but BL_TOGG might not work #define TAPPING_TOGGLE 3 @@ -45,7 +45,7 @@ along with this program. If not, see . // The RGB_DI_PIN value seems to be shared between all PS2AVRGB boards. // The same pin is used on the JJ40, at least. #define RGBLED_NUM 5 -#define RGB_DI_PIN E2 +#define RGB_DI_PIN E2 // NOTE: for PS2AVRGB boards, underglow commands are sent via I2C to 0xB0. #define RGBLIGHT_ANIMATIONS /* key combination for command */ diff --git a/keyboards/jj40/jj40.c b/keyboards/jj40/jj40.c index 6044e83fdf..9c1775d6a1 100644 --- a/keyboards/jj40/jj40.c +++ b/keyboards/jj40/jj40.c @@ -1,5 +1,6 @@ /* Copyright 2017 Luiz Ribeiro +Modified 2018 Kenneth A. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -24,8 +25,34 @@ along with this program. If not, see . #include "i2c.h" -// custom RGB driver +#include "backlight.h" +#include "backlight_custom.h" + extern rgblight_config_t rgblight_config; + +// for keyboard subdirectory level init functions +// @Override +void matrix_init_kb(void) { + // call user level keymaps, if any + // matrix_init_user(); +} + +#ifdef BACKLIGHT_ENABLE +/// Overrides functions in `quantum.c` +void backlight_init_ports(void) { + b_led_init_ports(); +} + +void backlight_task(void) { + b_led_task(); +} + +void backlight_set(uint8_t level) { + b_led_set(level); +} +#endif + +// custom RGB driver void rgblight_set(void) { if (!rgblight_config.enable) { for (uint8_t i=0; i. #include "keycode.h" #include "action.h" +void matrix_init_user(void); // TODO port this to other PS2AVRGB boards + #define KEYMAP_GRID( \ K01, K02, K03, K04, K05, K06, K07, K08, K09, K010, K011, K012, \ K11, K12, K13, K14, K15, K16, K17, K18, K19, K110, K111, K112, \ diff --git a/keyboards/jj40/keymaps/default/keymap.c b/keyboards/jj40/keymaps/default/keymap.c index 480486a18c..e02dd902a7 100644 --- a/keyboards/jj40/keymaps/default/keymap.c +++ b/keyboards/jj40/keymaps/default/keymap.c @@ -1,20 +1,3 @@ -/* -Copyright 2017 Luiz Ribeiro - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - #include "jj40.h" #include "action_layer.h" diff --git a/keyboards/jj40/keymaps/krusli/config.h b/keyboards/jj40/keymaps/krusli/config.h new file mode 100644 index 0000000000..11cafbefcb --- /dev/null +++ b/keyboards/jj40/keymaps/krusli/config.h @@ -0,0 +1,8 @@ +#ifndef CONFIG_USER_H +#define CONFIG_USER_H + +#include "../../config.h" + +#define PREVENT_STUCK_MODIFIERS + +#endif diff --git a/keyboards/jj40/keymaps/krusli/keymap.c b/keyboards/jj40/keymaps/krusli/keymap.c index 59e3988e11..5a985199e4 100644 --- a/keyboards/jj40/keymaps/krusli/keymap.c +++ b/keyboards/jj40/keymaps/krusli/keymap.c @@ -1,20 +1,3 @@ -/* -Copyright 2017 Luiz Ribeiro - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - #include "jj40.h" #include "action_layer.h" @@ -62,7 +45,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { KC_TILD, KC_EXLM, KC_AT, KC_HASH, KC_DLR, KC_PERC, KC_CIRC, KC_AMPR, KC_ASTR, KC_LPRN, KC_RPRN, KC_BSPC, \ KC_DEL, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_UNDS, KC_PLUS, KC_LCBR, KC_RCBR, KC_PIPE, \ _______, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, _______, _______,_______, _______, _______, \ - _______, _______, _______, _______, _______, _______, _______, KC_MNXT, KC_VOLD, KC_VOLU, KC_MPLY \ + BL_TOGG, BL_STEP, BL_BRTG, _______, _______, _______, _______, KC_MNXT, KC_VOLD, KC_VOLU, KC_MPLY \ ), /* Raise @@ -73,13 +56,13 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { * |------+------+------+------+------+------|------+------+------+------+------+------| * | | F7 | F8 | F9 | F10 | F11 | F12 | RGB | RGB | RGB | RGB |Enter | * |------+------+------+------+------+------+------+------+------+------+------+------| - * | | RGB | RGB | RGB | RGB | | | Next | Vol- | Vol+ | Play | + * | RGB | RGB | RGB | RGB | | | | Next | Vol- | Vol+ | Play | * `-----------------------------------------------------------------------------------' */ [_RAISE] = KEYMAP( \ KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_BSPC, \ KC_DEL, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_MINS, KC_EQL, KC_LBRC, KC_RBRC, KC_BSLS, \ _______, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, RGB_TOG, RGB_MOD, RGB_VAD, RGB_VAI, _______, \ - _______, RGB_HUD, RGB_HUI, RGB_SAD, RGB_SAI, _______, _______, KC_MNXT, KC_VOLD, KC_VOLU, KC_MPLY \ + RGB_HUD, RGB_HUI, RGB_SAD, RGB_SAI, _______, _______, _______, KC_MNXT, KC_VOLD, KC_VOLU, KC_MPLY \ ) }; diff --git a/keyboards/jj40/matrix.c b/keyboards/jj40/matrix.c index 140026013f..a9e9cb5397 100644 --- a/keyboards/jj40/matrix.c +++ b/keyboards/jj40/matrix.c @@ -29,6 +29,9 @@ static uint8_t debouncing = DEBOUNCE; static matrix_row_t matrix[MATRIX_ROWS]; static matrix_row_t matrix_debouncing[MATRIX_ROWS]; +void matrix_set_row_status(uint8_t row); +uint8_t bit_reverse(uint8_t x); + void matrix_init(void) { // all outputs for rows high DDRB = 0xFF; @@ -47,18 +50,8 @@ void matrix_init(void) { matrix[row] = 0x00; matrix_debouncing[row] = 0x00; } -} -void matrix_set_row_status(uint8_t row) { - DDRB = (1 << row); - PORTB = ~(1 << row); -} - -uint8_t bit_reverse(uint8_t x) { - x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa); - x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc); - x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0); - return x; + matrix_init_quantum(); // missing from original port by Luiz } uint8_t matrix_scan(void) { @@ -93,11 +86,30 @@ uint8_t matrix_scan(void) { } } - matrix_scan_user(); + matrix_scan_quantum(); // also missing in original PS2AVRGB implementation return 1; } +void matrix_scan_kb(void) { + // Looping keyboard code goes here + // This runs every cycle (a lot) + matrix_scan_user(); +}; + +// declarations +void matrix_set_row_status(uint8_t row) { + DDRB = (1 << row); + PORTB = ~(1 << row); +} + +uint8_t bit_reverse(uint8_t x) { + x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa); + x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc); + x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0); + return x; +} + inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; } diff --git a/keyboards/jj40/rules.mk b/keyboards/jj40/rules.mk index cdc898c0fa..670967fbd9 100644 --- a/keyboards/jj40/rules.mk +++ b/keyboards/jj40/rules.mk @@ -36,11 +36,13 @@ MOUSEKEY_ENABLE = no EXTRAKEY_ENABLE = yes CONSOLE_ENABLE = no COMMAND_ENABLE = yes -BACKLIGHT_ENABLE = no + +BACKLIGHT_ENABLE = yes +BACKLIGHT_CUSTOM_DRIVER = yes RGBLIGHT_ENABLE = yes RGBLIGHT_CUSTOM_DRIVER = yes -DISABLE_WS2812 = yes # TODO check if this is necessary +# DISABLE_WS2812 = no KEY_LOCK_ENABLE = yes @@ -51,7 +53,7 @@ OPT_DEFS = -DDEBUG_LEVEL=0 # custom matrix setup CUSTOM_MATRIX = yes -SRC = matrix.c i2c.c +SRC = matrix.c i2c.c backlight.c # programming options PROGRAM_CMD = ./util/atmega32a_program.py $(TARGET).hex diff --git a/keyboards/jj40/usbconfig.h b/keyboards/jj40/usbconfig.h index 520d08f611..ad97e7f0a3 100644 --- a/keyboards/jj40/usbconfig.h +++ b/keyboards/jj40/usbconfig.h @@ -119,7 +119,7 @@ section at the end of this file). * device is powered from the USB bus. */ // max power draw with maxed white underglow measured at 120 mA (peaks) -#define USB_CFG_MAX_BUS_POWER 150 +#define USB_CFG_MAX_BUS_POWER 100 /* Set this variable to the maximum USB bus power consumption of your device. * The value is in milliamperes. [It will be divided by two since USB * communicates power requirements in units of 2 mA.] diff --git a/keyboards/xd60/keymaps/krusli/HHKB-like.txt b/keyboards/xd60/keymaps/krusli/HHKB-like.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/keyboards/xd60/keymaps/krusli/keymap.c b/keyboards/xd60/keymaps/krusli/keymap.c new file mode 100644 index 0000000000..71f6e3c2e7 --- /dev/null +++ b/keyboards/xd60/keymaps/krusli/keymap.c @@ -0,0 +1,62 @@ +#include "xd60.h" +#include "action_layer.h" + +/* HHKB-like layout for standard 60% layout with split RShift and split backspace */ + +const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { + + // 0: Base Layer + KEYMAP( + KC_ESC, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSLS, KC_GRV, \ + KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSPC, \ + KC_LCTL, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_NO, KC_ENT, \ + KC_LSFT, KC_NO, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_RSFT, F(0), \ + KC_LCTL, KC_LALT, KC_LGUI, KC_SPC, KC_RGUI, KC_RALT, F(0), KC_RCTL, KC_RCTL), + + // 1: Function Layer + /* Layer HHKB: HHKB mode (HHKB Fn) + |------+-----+-----+-----+----+----+----+----+-----+-----+-----+-----+-------+-------+-----| + | Pwr | F1 | F2 | F3 | F4 | F5 | F6 | F7 | F8 | F9 | F10 | F11 | F12 | Ins | Del | + |------+-----+-----+-----+----+----+----+----+-----+-----+-----+-----+-------+-------+-----| + | Caps | | | | | | | | Psc | Slk | Pus | Up | | Backs | | + |------+-----+-----+-----+----+----+----+----+-----+-----+-----+-----+-------+-------+-----| + | | VoD | VoU | Mut | | | * | / | Hom | PgU | Lef | Rig | Enter | | | + |------+-----+-----+-----+----+----+----+----+-----+-----+-----+-----+-------+-------+-----| + | | | | | | | + | - | End | PgD | Dow | | | | | + |------+-----+-----+-----+----+----+----+----+-----+-----+-----+-----+-------+-------+-----| + + |------+------+----------------------+------+------+ + | **** | **** | ******************** | **** | **** | + |------+------+----------------------+------+------+ + */ + KEYMAP( + RESET, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_F13, KC_F14, \ + KC_CAPS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_PSCR, KC_SLCK, KC_PAUS, KC_UP, KC_TRNS, KC_BSPC, \ + KC_TRNS, KC_VOLD, KC_VOLU, KC_MUTE, KC_TRNS, KC_TRNS, KC_PAST, KC_PSLS, KC_HOME, KC_PGUP, KC_LEFT, KC_RGHT, KC_NO, KC_ENT, \ + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_PPLS, KC_PMNS, KC_END, KC_PGDN, KC_DOWN, KC_TRNS, KC_TRNS, KC_TRNS, \ + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, F(0), KC_TRNS, KC_TRNS), +}; + +// Custom Actions +const uint16_t PROGMEM fn_actions[] = { + [0] = ACTION_LAYER_MOMENTARY(1), // to Fn overlay +}; + +// Macros +const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) { + + // MACRODOWN only works in this function + switch(id) { + case 0: + if (record->event.pressed) { register_code(KC_RSFT); } + else { unregister_code(KC_RSFT); } + break; + } + + return MACRO_NONE; +}; + +// Loop +void matrix_scan_user(void) { + // Empty +}; diff --git a/keyboards/xd60/keymaps/krusli/readme.md b/keyboards/xd60/keymaps/krusli/readme.md new file mode 100644 index 0000000000..7b6bce9c48 --- /dev/null +++ b/keyboards/xd60/keymaps/krusli/readme.md @@ -0,0 +1,2 @@ +# krusli's HHKB-style keymap for the XD60 +HHKB-style keymap for the XD60. The board was not built with split backspace but still has Backspace and |\\ swapped. diff --git a/quantum/quantum.c b/quantum/quantum.c index 88617412c9..65fa259614 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c @@ -138,13 +138,13 @@ void reset_keyboard(void) { clear_keyboard(); #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) process_midi_all_notes_off(); -#endif +#endif #if defined(AUDIO_ENABLE) music_all_notes_off(); uint16_t timer_start = timer_read(); PLAY_SONG(goodbye_song); shutdown_user(); - while(timer_elapsed(timer_start) < 250) + while(timer_elapsed(timer_start) < 250) wait_ms(1); stop_all_notes(); #else @@ -885,6 +885,7 @@ void backlight_set(uint8_t level) {} uint8_t backlight_tick = 0; +#ifndef BACKLIGHT_CUSTOM_DRIVER void backlight_task(void) { if ((0xFFFF >> ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2))) & (1 << backlight_tick)) { #if BACKLIGHT_ON_STATE == 0 @@ -905,9 +906,12 @@ void backlight_task(void) { } backlight_tick = backlight_tick + 1 % 16; } +#endif #ifdef BACKLIGHT_BREATHING -#error "Backlight breathing only available with hardware PWM. Please disable." + #ifndef BACKLIGHT_CUSTOM_DRIVER + #error "Backlight breathing only available with hardware PWM. Please disable." + #endif #endif #else // pwm through timer @@ -935,6 +939,7 @@ static inline void set_pwm(uint16_t val) { OCR1x = val; } +#ifndef BACKLIGHT_CUSTOM_DRIVER __attribute__ ((weak)) void backlight_set(uint8_t level) { if (level > BACKLIGHT_LEVELS) @@ -952,6 +957,7 @@ void backlight_set(uint8_t level) { } void backlight_task(void) {} +#endif // BACKLIGHT_CUSTOM_DRIVER #ifdef BACKLIGHT_BREATHING diff --git a/tmk_core/common/backlight.c b/tmk_core/common/backlight.c index 4d2491b988..12f75b38ac 100644 --- a/tmk_core/common/backlight.c +++ b/tmk_core/common/backlight.c @@ -94,4 +94,4 @@ void backlight_level(uint8_t level) uint8_t get_backlight_level(void) { return backlight_config.level; -} \ No newline at end of file +}