diff options
| author | John M Daly <jmdaly@gmail.com> | 2020-01-12 22:48:24 -0500 |
|---|---|---|
| committer | Joel Challis <git@zvecr.com> | 2020-01-13 03:48:24 +0000 |
| commit | be7d70b15c4b3db4ae2aa2ea7c67caa48c9ef50b (patch) | |
| tree | cfae1f30cee0902555372421b75c76fddc10ceca | |
| parent | b89e35bdd33b3953711de8b0be64c76b64e9701b (diff) | |
| download | qmk_firmware-be7d70b15c4b3db4ae2aa2ea7c67caa48c9ef50b.tar.gz qmk_firmware-be7d70b15c4b3db4ae2aa2ea7c67caa48c9ef50b.zip | |
Enable RGB underglow on the CO60 and SteamVan, switch backlight code to QMK built-in code (#7874)
* Add: RGB underglow support using SPI to SteamVan and CO60
* Update: Switch to QMK's baclight implementation for CO60 and SteamVan
* Fix: Remove now unnecessary backlighting code from keyboard files
23 files changed, 24 insertions, 922 deletions
diff --git a/keyboards/handwired/co60/rev6/config.h b/keyboards/handwired/co60/rev6/config.h index b1ab99fd9..a2c73853c 100644 --- a/keyboards/handwired/co60/rev6/config.h +++ b/keyboards/handwired/co60/rev6/config.h | |||
| @@ -55,6 +55,5 @@ | |||
| 55 | /* Backlight configuration | 55 | /* Backlight configuration |
| 56 | * Backlight LEDs on B8 | 56 | * Backlight LEDs on B8 |
| 57 | */ | 57 | */ |
| 58 | #define BACKLIGHT_LEVELS 24 | 58 | #define BACKLIGHT_PIN B8 |
| 59 | #define BACKLIGHT_BREATHING | 59 | #define BACKLIGHT_BREATHING |
| 60 | #define BREATHING_PERIOD 6 | ||
diff --git a/keyboards/handwired/co60/rev6/led.c b/keyboards/handwired/co60/rev6/led.c deleted file mode 100644 index fe28ce2e8..000000000 --- a/keyboards/handwired/co60/rev6/led.c +++ /dev/null | |||
| @@ -1,240 +0,0 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2012 Jun Wako <wakojun@gmail.com> | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 2 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include "hal.h" | ||
| 19 | #include "led_custom.h" | ||
| 20 | #include "rev6.h" | ||
| 21 | #include "printf.h" | ||
| 22 | |||
| 23 | static void breathing_callback(PWMDriver *pwmp); | ||
| 24 | |||
| 25 | static PWMConfig pwmCFG = { | ||
| 26 | 0xFFFF, /* PWM clock frequency */ | ||
| 27 | 256, /* PWM period (in ticks) 1S (1/10kHz=0.1mS 0.1ms*10000 ticks=1S) */ | ||
| 28 | NULL, /* No Callback */ | ||
| 29 | { | ||
| 30 | {PWM_OUTPUT_DISABLED, NULL}, | ||
| 31 | {PWM_OUTPUT_DISABLED, NULL}, | ||
| 32 | {PWM_OUTPUT_ACTIVE_HIGH, NULL}, /* Enable Channel 3 */ | ||
| 33 | {PWM_OUTPUT_DISABLED, NULL} | ||
| 34 | }, | ||
| 35 | 0, /* HW dependent part.*/ | ||
| 36 | 0 | ||
| 37 | }; | ||
| 38 | |||
| 39 | static PWMConfig pwmCFG_breathing = { | ||
| 40 | 0xFFFF, /* 10kHz PWM clock frequency */ | ||
| 41 | 256, /* PWM period (in ticks) 1S (1/10kHz=0.1mS 0.1ms*10000 ticks=1S) */ | ||
| 42 | breathing_callback, /* Breathing Callback */ | ||
| 43 | { | ||
| 44 | {PWM_OUTPUT_DISABLED, NULL}, | ||
| 45 | {PWM_OUTPUT_DISABLED, NULL}, | ||
| 46 | {PWM_OUTPUT_ACTIVE_HIGH, NULL}, /* Enable Channel 3 */ | ||
| 47 | {PWM_OUTPUT_DISABLED, NULL} | ||
| 48 | }, | ||
| 49 | 0, /* HW dependent part.*/ | ||
| 50 | 0 | ||
| 51 | }; | ||
| 52 | |||
| 53 | // See http://jared.geek.nz/2013/feb/linear-led-pwm | ||
| 54 | static uint16_t cie_lightness(uint16_t v) { | ||
| 55 | if (v <= 5243) // if below 8% of max | ||
| 56 | return v / 9; // same as dividing by 900% | ||
| 57 | else { | ||
| 58 | uint32_t y = (((uint32_t)v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare | ||
| 59 | // to get a useful result with integer division, we shift left in the expression above | ||
| 60 | // and revert what we've done again after squaring. | ||
| 61 | y = y * y * y >> 8; | ||
| 62 | if (y > 0xFFFFUL) // prevent overflow | ||
| 63 | return 0xFFFFU; | ||
| 64 | else | ||
| 65 | return (uint16_t)y; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | void backlight_init_ports(void) { | ||
| 70 | palSetPadMode(GPIOB, 8, PAL_MODE_ALTERNATE(2)); | ||
| 71 | pwmStart(&PWMD4, &pwmCFG); | ||
| 72 | if (kb_backlight_config.enable) { | ||
| 73 | if (kb_backlight_config.breathing) { | ||
| 74 | breathing_enable(); | ||
| 75 | } else { | ||
| 76 | backlight_set(kb_backlight_config.level); | ||
| 77 | } | ||
| 78 | } else { | ||
| 79 | backlight_set(0); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | void backlight_set(uint8_t level) { | ||
| 84 | uint32_t duty = (uint32_t)(cie_lightness(0xFFFF * (uint32_t)level / BACKLIGHT_LEVELS)); | ||
| 85 | if (level == 0) { | ||
| 86 | // Turn backlight off | ||
| 87 | // Disable channel 3 on PWM4 | ||
| 88 | pwmDisableChannel(&PWMD4, 2); | ||
| 89 | } else { | ||
| 90 | // Turn backlight on | ||
| 91 | if (!is_breathing()) { | ||
| 92 | // Enable channel 3 on PWM4 | ||
| 93 | pwmEnableChannel(&PWMD4, 2, PWM_FRACTION_TO_WIDTH(&PWMD4, 0xFFFF, duty)); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | uint8_t backlight_tick = 0; | ||
| 99 | |||
| 100 | void backlight_task(void) { | ||
| 101 | } | ||
| 102 | |||
| 103 | #define BREATHING_NO_HALT 0 | ||
| 104 | #define BREATHING_HALT_OFF 1 | ||
| 105 | #define BREATHING_HALT_ON 2 | ||
| 106 | #define BREATHING_STEPS 128 | ||
| 107 | |||
| 108 | static uint8_t breathing_period = BREATHING_PERIOD; | ||
| 109 | static uint8_t breathing_halt = BREATHING_NO_HALT; | ||
| 110 | static uint16_t breathing_counter = 0; | ||
| 111 | |||
| 112 | bool is_breathing(void) { | ||
| 113 | return PWMD4.config == &pwmCFG_breathing; | ||
| 114 | } | ||
| 115 | |||
| 116 | #define breathing_min() do {breathing_counter = 0;} while (0) | ||
| 117 | #define breathing_max() do {breathing_counter = breathing_period * 256 / 2;} while (0) | ||
| 118 | |||
| 119 | |||
| 120 | void breathing_interrupt_enable(void){ | ||
| 121 | pwmStop(&PWMD4); | ||
| 122 | pwmStart(&PWMD4, &pwmCFG_breathing); | ||
| 123 | chSysLockFromISR(); | ||
| 124 | pwmEnablePeriodicNotification(&PWMD4); | ||
| 125 | pwmEnableChannelI( | ||
| 126 | &PWMD4, | ||
| 127 | 2, | ||
| 128 | PWM_FRACTION_TO_WIDTH( | ||
| 129 | &PWMD4, | ||
| 130 | 0xFFFF, | ||
| 131 | 0xFFFF | ||
| 132 | ) | ||
| 133 | ); | ||
| 134 | chSysUnlockFromISR(); | ||
| 135 | } | ||
| 136 | |||
| 137 | void breathing_interrupt_disable(void){ | ||
| 138 | pwmStop(&PWMD4); | ||
| 139 | pwmStart(&PWMD4, &pwmCFG); | ||
| 140 | } | ||
| 141 | |||
| 142 | void breathing_enable(void) | ||
| 143 | { | ||
| 144 | breathing_counter = 0; | ||
| 145 | breathing_halt = BREATHING_NO_HALT; | ||
| 146 | breathing_interrupt_enable(); | ||
| 147 | } | ||
| 148 | |||
| 149 | void breathing_pulse(void) | ||
| 150 | { | ||
| 151 | if (kb_backlight_config.level == 0) | ||
| 152 | breathing_min(); | ||
| 153 | else | ||
| 154 | breathing_max(); | ||
| 155 | breathing_halt = BREATHING_HALT_ON; | ||
| 156 | breathing_interrupt_enable(); | ||
| 157 | } | ||
| 158 | |||
| 159 | void breathing_disable(void) | ||
| 160 | { | ||
| 161 | breathing_interrupt_disable(); | ||
| 162 | // Restore backlight level | ||
| 163 | backlight_set(kb_backlight_config.level); | ||
| 164 | } | ||
| 165 | |||
| 166 | void breathing_self_disable(void) | ||
| 167 | { | ||
| 168 | if (kb_backlight_config.level == 0) | ||
| 169 | breathing_halt = BREATHING_HALT_OFF; | ||
| 170 | else | ||
| 171 | breathing_halt = BREATHING_HALT_ON; | ||
| 172 | } | ||
| 173 | |||
| 174 | void breathing_toggle(void) { | ||
| 175 | if (is_breathing()){ | ||
| 176 | breathing_disable(); | ||
| 177 | } else { | ||
| 178 | breathing_enable(); | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | void breathing_period_set(uint8_t value) | ||
| 183 | { | ||
| 184 | if (!value) | ||
| 185 | value = 1; | ||
| 186 | breathing_period = value; | ||
| 187 | } | ||
| 188 | |||
| 189 | void breathing_period_default(void) { | ||
| 190 | breathing_period_set(BREATHING_PERIOD); | ||
| 191 | } | ||
| 192 | |||
| 193 | void breathing_period_inc(void) | ||
| 194 | { | ||
| 195 | breathing_period_set(breathing_period+1); | ||
| 196 | } | ||
| 197 | |||
| 198 | void breathing_period_dec(void) | ||
| 199 | { | ||
| 200 | breathing_period_set(breathing_period-1); | ||
| 201 | } | ||
| 202 | |||
| 203 | /* To generate breathing curve in python: | ||
| 204 | * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)] | ||
| 205 | */ | ||
| 206 | static const uint8_t breathing_table[BREATHING_STEPS] = {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}; | ||
| 207 | |||
| 208 | // Use this before the cie_lightness function. | ||
| 209 | static inline uint16_t scale_backlight(uint16_t v) { | ||
| 210 | return v / BACKLIGHT_LEVELS * kb_backlight_config.level; | ||
| 211 | } | ||
| 212 | |||
| 213 | static void breathing_callback(PWMDriver *pwmp) | ||
| 214 | { | ||
| 215 | (void)pwmp; | ||
| 216 | uint16_t interval = (uint16_t) breathing_period * 256 / BREATHING_STEPS; | ||
| 217 | // resetting after one period to prevent ugly reset at overflow. | ||
| 218 | breathing_counter = (breathing_counter + 1) % (breathing_period * 256); | ||
| 219 | uint8_t index = breathing_counter / interval % BREATHING_STEPS; | ||
| 220 | |||
| 221 | if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) || | ||
| 222 | ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1))) | ||
| 223 | { | ||
| 224 | breathing_interrupt_disable(); | ||
| 225 | } | ||
| 226 | |||
| 227 | uint32_t duty = cie_lightness(scale_backlight(breathing_table[index] * 256)); | ||
| 228 | |||
| 229 | chSysLockFromISR(); | ||
| 230 | pwmEnableChannelI( | ||
| 231 | &PWMD4, | ||
| 232 | 2, | ||
| 233 | PWM_FRACTION_TO_WIDTH( | ||
| 234 | &PWMD4, | ||
| 235 | 0xFFFF, | ||
| 236 | duty | ||
| 237 | ) | ||
| 238 | ); | ||
| 239 | chSysUnlockFromISR(); | ||
| 240 | } | ||
diff --git a/keyboards/handwired/co60/rev6/led_custom.h b/keyboards/handwired/co60/rev6/led_custom.h deleted file mode 100644 index 96c4d0c2b..000000000 --- a/keyboards/handwired/co60/rev6/led_custom.h +++ /dev/null | |||
| @@ -1,22 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * Copyright 2019 John M Daly <jmdaly@gmail.com> | ||
| 3 | * | ||
| 4 | * This program is free software: you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License as published by | ||
| 6 | * the Free Software Foundation, either version 2 of the License, or | ||
| 7 | * (at your option) any later version. | ||
| 8 | * | ||
| 9 | * This program is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | * GNU General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU General Public License | ||
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #pragma once | ||
| 19 | |||
| 20 | void backlight_task(void); | ||
| 21 | void breathing_interrupt_disable(void); | ||
| 22 | void breathing_interrupt_enable(void); | ||
diff --git a/keyboards/handwired/co60/rev6/rev6.c b/keyboards/handwired/co60/rev6/rev6.c index f597513b1..e2645871a 100644 --- a/keyboards/handwired/co60/rev6/rev6.c +++ b/keyboards/handwired/co60/rev6/rev6.c | |||
| @@ -15,25 +15,3 @@ | |||
| 15 | */ | 15 | */ |
| 16 | #include "rev6.h" | 16 | #include "rev6.h" |
| 17 | 17 | ||
| 18 | #include "backlight.h" | ||
| 19 | #include "led.h" | ||
| 20 | #include "printf.h" | ||
| 21 | |||
| 22 | backlight_levels_config_t kb_backlight_config = { | ||
| 23 | .enable = true, | ||
| 24 | .breathing = true, | ||
| 25 | .level = BACKLIGHT_LEVELS | ||
| 26 | }; | ||
| 27 | |||
| 28 | uint8_t *o_fb; | ||
| 29 | |||
| 30 | uint16_t counterst = 0; | ||
| 31 | |||
| 32 | void matrix_init_kb(void) { | ||
| 33 | matrix_init_user(); | ||
| 34 | backlight_init_ports(); | ||
| 35 | } | ||
| 36 | |||
| 37 | void matrix_scan_kb(void) { | ||
| 38 | matrix_scan_user(); | ||
| 39 | } | ||
diff --git a/keyboards/handwired/co60/rev6/rev6.h b/keyboards/handwired/co60/rev6/rev6.h index 7d11f19d3..1f0fe51e2 100644 --- a/keyboards/handwired/co60/rev6/rev6.h +++ b/keyboards/handwired/co60/rev6/rev6.h | |||
| @@ -126,21 +126,3 @@ | |||
| 126 | { KC_NO, K401, K402, KC_NO, K404, KC_NO, K406, KC_NO, K408, KC_NO, K410, K411, KC_NO, K413, KC_NO }, \ | 126 | { KC_NO, K401, K402, KC_NO, K404, KC_NO, K406, KC_NO, K408, KC_NO, K410, K411, KC_NO, K413, KC_NO }, \ |
| 127 | } | 127 | } |
| 128 | 128 | ||
| 129 | // Backlighting | ||
| 130 | typedef union { | ||
| 131 | uint8_t raw; | ||
| 132 | struct { | ||
| 133 | bool enable :1; | ||
| 134 | bool breathing : 1; | ||
| 135 | uint8_t level :6; | ||
| 136 | }; | ||
| 137 | } backlight_levels_config_t; | ||
| 138 | |||
| 139 | extern backlight_levels_config_t kb_backlight_config; | ||
| 140 | extern bool kb_backlight_breathing; | ||
| 141 | |||
| 142 | void backlight_init_ports(void); | ||
| 143 | void backlight_set(uint8_t level); | ||
| 144 | bool is_breathing(void); | ||
| 145 | void breathing_enable(void); | ||
| 146 | void breathing_disable(void); | ||
diff --git a/keyboards/handwired/co60/rev6/rules.mk b/keyboards/handwired/co60/rev6/rules.mk index fc7cabb10..b20c2efed 100644 --- a/keyboards/handwired/co60/rev6/rules.mk +++ b/keyboards/handwired/co60/rev6/rules.mk | |||
| @@ -1,9 +1,6 @@ | |||
| 1 | # MCU name | 1 | # MCU name |
| 2 | MCU = STM32F303 | 2 | MCU = STM32F303 |
| 3 | 3 | ||
| 4 | # Code for backlight breathing: | ||
| 5 | SRC += led.c | ||
| 6 | |||
| 7 | # Build Options | 4 | # Build Options |
| 8 | # comment out to disable the options. | 5 | # comment out to disable the options. |
| 9 | # | 6 | # |
diff --git a/keyboards/handwired/co60/rev7/config.h b/keyboards/handwired/co60/rev7/config.h index 1ccc12ad4..e13d28f50 100644 --- a/keyboards/handwired/co60/rev7/config.h +++ b/keyboards/handwired/co60/rev7/config.h | |||
| @@ -57,9 +57,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 57 | /* Backlight configuration | 57 | /* Backlight configuration |
| 58 | * Backlight LEDs on B8 | 58 | * Backlight LEDs on B8 |
| 59 | */ | 59 | */ |
| 60 | #define BACKLIGHT_LEVELS 24 | 60 | #define BACKLIGHT_PIN B8 |
| 61 | #define BACKLIGHT_BREATHING | 61 | #define BACKLIGHT_BREATHING |
| 62 | #define BREATHING_PERIOD 6 | 62 | |
| 63 | /* RGB underglow configuration */ | ||
| 64 | #define WS2812_SPI SPID1 | ||
| 65 | #define WS2812_SPI_MOSI_PAL_MODE 5 | ||
| 63 | 66 | ||
| 64 | #define RGBLIGHT_ANIMATIONS | 67 | #define RGBLIGHT_ANIMATIONS |
| 65 | 68 | ||
diff --git a/keyboards/handwired/co60/rev7/halconf.h b/keyboards/handwired/co60/rev7/halconf.h index 5e5d70219..7363a21a2 100644 --- a/keyboards/handwired/co60/rev7/halconf.h +++ b/keyboards/handwired/co60/rev7/halconf.h | |||
| @@ -153,7 +153,7 @@ | |||
| 153 | * @brief Enables the SPI subsystem. | 153 | * @brief Enables the SPI subsystem. |
| 154 | */ | 154 | */ |
| 155 | #if !defined(HAL_USE_SPI) || defined(__DOXYGEN__) | 155 | #if !defined(HAL_USE_SPI) || defined(__DOXYGEN__) |
| 156 | #define HAL_USE_SPI FALSE | 156 | #define HAL_USE_SPI TRUE |
| 157 | #endif | 157 | #endif |
| 158 | 158 | ||
| 159 | /** | 159 | /** |
diff --git a/keyboards/handwired/co60/rev7/led.c b/keyboards/handwired/co60/rev7/led.c deleted file mode 100644 index 13f8d9860..000000000 --- a/keyboards/handwired/co60/rev7/led.c +++ /dev/null | |||
| @@ -1,242 +0,0 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2012 Jun Wako <wakojun@gmail.com> | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 2 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include "hal.h" | ||
| 19 | #include "led_custom.h" | ||
| 20 | #include "rev7.h" | ||
| 21 | #include "printf.h" | ||
| 22 | |||
| 23 | static void breathing_callback(PWMDriver *pwmp); | ||
| 24 | |||
| 25 | static PWMConfig pwmCFG = { | ||
| 26 | 0xFFFF, /* PWM clock frequency */ | ||
| 27 | 256, /* PWM period (in ticks) 1S (1/10kHz=0.1mS 0.1ms*10000 ticks=1S) */ | ||
| 28 | NULL, /* No Callback */ | ||
| 29 | { | ||
| 30 | {PWM_OUTPUT_DISABLED, NULL}, | ||
| 31 | {PWM_OUTPUT_DISABLED, NULL}, | ||
| 32 | {PWM_OUTPUT_ACTIVE_HIGH, NULL}, /* Enable Channel 3 */ | ||
| 33 | {PWM_OUTPUT_DISABLED, NULL} | ||
| 34 | }, | ||
| 35 | 0, /* HW dependent part.*/ | ||
| 36 | 0 | ||
| 37 | }; | ||
| 38 | |||
| 39 | static PWMConfig pwmCFG_breathing = { | ||
| 40 | 0xFFFF, /* 10kHz PWM clock frequency */ | ||
| 41 | 256, /* PWM period (in ticks) 1S (1/10kHz=0.1mS 0.1ms*10000 ticks=1S) */ | ||
| 42 | breathing_callback, /* Breathing Callback */ | ||
| 43 | { | ||
| 44 | {PWM_OUTPUT_DISABLED, NULL}, | ||
| 45 | {PWM_OUTPUT_DISABLED, NULL}, | ||
| 46 | {PWM_OUTPUT_ACTIVE_HIGH, NULL}, /* Enable Channel 3 */ | ||
| 47 | {PWM_OUTPUT_DISABLED, NULL} | ||
| 48 | }, | ||
| 49 | 0, /* HW dependent part.*/ | ||
| 50 | 0 | ||
| 51 | }; | ||
| 52 | |||
| 53 | // See http://jared.geek.nz/2013/feb/linear-led-pwm | ||
| 54 | static uint16_t cie_lightness(uint16_t v) { | ||
| 55 | if (v <= 5243) // if below 8% of max | ||
| 56 | return v / 9; // same as dividing by 900% | ||
| 57 | else { | ||
| 58 | uint32_t y = (((uint32_t) v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare | ||
| 59 | // to get a useful result with integer division, we shift left in the expression above | ||
| 60 | // and revert what we've done again after squaring. | ||
| 61 | y = y * y * y >> 8; | ||
| 62 | if (y > 0xFFFFUL) // prevent overflow | ||
| 63 | return 0xFFFFU; | ||
| 64 | else | ||
| 65 | return (uint16_t) y; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | void backlight_init_ports(void) { | ||
| 71 | palSetPadMode(GPIOB, 8, PAL_MODE_ALTERNATE(2)); | ||
| 72 | pwmStart(&PWMD4, &pwmCFG); | ||
| 73 | if(kb_backlight_config.enable){ | ||
| 74 | if(kb_backlight_config.breathing){ | ||
| 75 | breathing_enable(); | ||
| 76 | } else{ | ||
| 77 | backlight_set(kb_backlight_config.level); | ||
| 78 | } | ||
| 79 | } else { | ||
| 80 | backlight_set(0); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | void backlight_set(uint8_t level) { | ||
| 85 | uint32_t duty = (uint32_t)(cie_lightness(0xFFFF * (uint32_t) level / BACKLIGHT_LEVELS)); | ||
| 86 | if (level == 0) { | ||
| 87 | // Turn backlight off | ||
| 88 | // Disable channel 3 on PWM4 | ||
| 89 | pwmDisableChannel(&PWMD4, 2); | ||
| 90 | } else { | ||
| 91 | // Turn backlight on | ||
| 92 | if(!is_breathing()){ | ||
| 93 | // Enable channel 3 on PWM4 | ||
| 94 | pwmEnableChannel(&PWMD4, 2, PWM_FRACTION_TO_WIDTH(&PWMD4,0xFFFF,duty)); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | uint8_t backlight_tick = 0; | ||
| 101 | |||
| 102 | void backlight_task(void) { | ||
| 103 | } | ||
| 104 | |||
| 105 | #define BREATHING_NO_HALT 0 | ||
| 106 | #define BREATHING_HALT_OFF 1 | ||
| 107 | #define BREATHING_HALT_ON 2 | ||
| 108 | #define BREATHING_STEPS 128 | ||
| 109 | |||
| 110 | static uint8_t breathing_period = BREATHING_PERIOD; | ||
| 111 | static uint8_t breathing_halt = BREATHING_NO_HALT; | ||
| 112 | static uint16_t breathing_counter = 0; | ||
| 113 | |||
| 114 | bool is_breathing(void) { | ||
| 115 | return PWMD4.config == &pwmCFG_breathing; | ||
| 116 | } | ||
| 117 | |||
| 118 | #define breathing_min() do {breathing_counter = 0;} while (0) | ||
| 119 | #define breathing_max() do {breathing_counter = breathing_period * 256 / 2;} while (0) | ||
| 120 | |||
| 121 | |||
| 122 | void breathing_interrupt_enable(void){ | ||
| 123 | pwmStop(&PWMD4); | ||
| 124 | pwmStart(&PWMD4, &pwmCFG_breathing); | ||
| 125 | chSysLockFromISR(); | ||
| 126 | pwmEnablePeriodicNotification(&PWMD4); | ||
| 127 | pwmEnableChannelI( | ||
| 128 | &PWMD4, | ||
| 129 | 2, | ||
| 130 | PWM_FRACTION_TO_WIDTH( | ||
| 131 | &PWMD4, | ||
| 132 | 0xFFFF, | ||
| 133 | 0xFFFF | ||
| 134 | ) | ||
| 135 | ); | ||
| 136 | chSysUnlockFromISR(); | ||
| 137 | } | ||
| 138 | |||
| 139 | void breathing_interrupt_disable(void){ | ||
| 140 | pwmStop(&PWMD4); | ||
| 141 | pwmStart(&PWMD4, &pwmCFG); | ||
| 142 | } | ||
| 143 | |||
| 144 | void breathing_enable(void) | ||
| 145 | { | ||
| 146 | breathing_counter = 0; | ||
| 147 | breathing_halt = BREATHING_NO_HALT; | ||
| 148 | breathing_interrupt_enable(); | ||
| 149 | } | ||
| 150 | |||
| 151 | void breathing_pulse(void) | ||
| 152 | { | ||
| 153 | if (kb_backlight_config.level == 0) | ||
| 154 | breathing_min(); | ||
| 155 | else | ||
| 156 | breathing_max(); | ||
| 157 | breathing_halt = BREATHING_HALT_ON; | ||
| 158 | breathing_interrupt_enable(); | ||
| 159 | } | ||
| 160 | |||
| 161 | void breathing_disable(void) | ||
| 162 | { | ||
| 163 | breathing_interrupt_disable(); | ||
| 164 | // Restore backlight level | ||
| 165 | backlight_set(kb_backlight_config.level); | ||
| 166 | } | ||
| 167 | |||
| 168 | void breathing_self_disable(void) | ||
| 169 | { | ||
| 170 | if (kb_backlight_config.level == 0) | ||
| 171 | breathing_halt = BREATHING_HALT_OFF; | ||
| 172 | else | ||
| 173 | breathing_halt = BREATHING_HALT_ON; | ||
| 174 | } | ||
| 175 | |||
| 176 | void breathing_toggle(void) { | ||
| 177 | if (is_breathing()){ | ||
| 178 | breathing_disable(); | ||
| 179 | } else { | ||
| 180 | breathing_enable(); | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | void breathing_period_set(uint8_t value) | ||
| 185 | { | ||
| 186 | if (!value) | ||
| 187 | value = 1; | ||
| 188 | breathing_period = value; | ||
| 189 | } | ||
| 190 | |||
| 191 | void breathing_period_default(void) { | ||
| 192 | breathing_period_set(BREATHING_PERIOD); | ||
| 193 | } | ||
| 194 | |||
| 195 | void breathing_period_inc(void) | ||
| 196 | { | ||
| 197 | breathing_period_set(breathing_period+1); | ||
| 198 | } | ||
| 199 | |||
| 200 | void breathing_period_dec(void) | ||
| 201 | { | ||
| 202 | breathing_period_set(breathing_period-1); | ||
| 203 | } | ||
| 204 | |||
| 205 | /* To generate breathing curve in python: | ||
| 206 | * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)] | ||
| 207 | */ | ||
| 208 | static const uint8_t breathing_table[BREATHING_STEPS] = {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}; | ||
| 209 | |||
| 210 | // Use this before the cie_lightness function. | ||
| 211 | static inline uint16_t scale_backlight(uint16_t v) { | ||
| 212 | return v / BACKLIGHT_LEVELS * kb_backlight_config.level; | ||
| 213 | } | ||
| 214 | |||
| 215 | static void breathing_callback(PWMDriver *pwmp) | ||
| 216 | { | ||
| 217 | (void)pwmp; | ||
| 218 | uint16_t interval = (uint16_t) breathing_period * 256 / BREATHING_STEPS; | ||
| 219 | // resetting after one period to prevent ugly reset at overflow. | ||
| 220 | breathing_counter = (breathing_counter + 1) % (breathing_period * 256); | ||
| 221 | uint8_t index = breathing_counter / interval % BREATHING_STEPS; | ||
| 222 | |||
| 223 | if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) || | ||
| 224 | ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1))) | ||
| 225 | { | ||
| 226 | breathing_interrupt_disable(); | ||
| 227 | } | ||
| 228 | |||
| 229 | uint32_t duty = cie_lightness(scale_backlight(breathing_table[index] * 256)); | ||
| 230 | |||
| 231 | chSysLockFromISR(); | ||
| 232 | pwmEnableChannelI( | ||
| 233 | &PWMD4, | ||
| 234 | 2, | ||
| 235 | PWM_FRACTION_TO_WIDTH( | ||
| 236 | &PWMD4, | ||
| 237 | 0xFFFF, | ||
| 238 | duty | ||
| 239 | ) | ||
| 240 | ); | ||
| 241 | chSysUnlockFromISR(); | ||
| 242 | } | ||
diff --git a/keyboards/handwired/co60/rev7/led_custom.h b/keyboards/handwired/co60/rev7/led_custom.h deleted file mode 100644 index 56e723db8..000000000 --- a/keyboards/handwired/co60/rev7/led_custom.h +++ /dev/null | |||
| @@ -1,22 +0,0 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2019 John M Daly <jmdaly@gmail.com> | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 2 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #pragma once | ||
| 19 | |||
| 20 | void backlight_task(void); | ||
| 21 | void breathing_interrupt_disable(void); | ||
| 22 | void breathing_interrupt_enable(void); | ||
diff --git a/keyboards/handwired/co60/rev7/mcuconf.h b/keyboards/handwired/co60/rev7/mcuconf.h index 69bf9185d..7ad013744 100644 --- a/keyboards/handwired/co60/rev7/mcuconf.h +++ b/keyboards/handwired/co60/rev7/mcuconf.h | |||
| @@ -210,7 +210,7 @@ | |||
| 210 | /* | 210 | /* |
| 211 | * SPI driver system settings. | 211 | * SPI driver system settings. |
| 212 | */ | 212 | */ |
| 213 | #define STM32_SPI_USE_SPI1 FALSE | 213 | #define STM32_SPI_USE_SPI1 TRUE |
| 214 | #define STM32_SPI_USE_SPI2 FALSE | 214 | #define STM32_SPI_USE_SPI2 FALSE |
| 215 | #define STM32_SPI_USE_SPI3 FALSE | 215 | #define STM32_SPI_USE_SPI3 FALSE |
| 216 | #define STM32_SPI_SPI1_DMA_PRIORITY 1 | 216 | #define STM32_SPI_SPI1_DMA_PRIORITY 1 |
diff --git a/keyboards/handwired/co60/rev7/rev7.c b/keyboards/handwired/co60/rev7/rev7.c index 3d0964d89..40fd7e664 100644 --- a/keyboards/handwired/co60/rev7/rev7.c +++ b/keyboards/handwired/co60/rev7/rev7.c | |||
| @@ -15,25 +15,3 @@ | |||
| 15 | */ | 15 | */ |
| 16 | #include "rev7.h" | 16 | #include "rev7.h" |
| 17 | 17 | ||
| 18 | #include "backlight.h" | ||
| 19 | #include "led.h" | ||
| 20 | #include "printf.h" | ||
| 21 | |||
| 22 | backlight_levels_config_t kb_backlight_config = { | ||
| 23 | .enable = true, | ||
| 24 | .breathing = true, | ||
| 25 | .level = BACKLIGHT_LEVELS | ||
| 26 | }; | ||
| 27 | |||
| 28 | uint8_t *o_fb; | ||
| 29 | |||
| 30 | uint16_t counterst = 0; | ||
| 31 | |||
| 32 | void matrix_init_kb(void) { | ||
| 33 | matrix_init_user(); | ||
| 34 | backlight_init_ports(); | ||
| 35 | } | ||
| 36 | |||
| 37 | void matrix_scan_kb(void) { | ||
| 38 | matrix_scan_user(); | ||
| 39 | } | ||
diff --git a/keyboards/handwired/co60/rev7/rev7.h b/keyboards/handwired/co60/rev7/rev7.h index 4ab2b8d72..fb9c7cb31 100644 --- a/keyboards/handwired/co60/rev7/rev7.h +++ b/keyboards/handwired/co60/rev7/rev7.h | |||
| @@ -126,21 +126,3 @@ | |||
| 126 | { KC_NO, K401, K402, KC_NO, K404, KC_NO, K406, KC_NO, K408, KC_NO, K410, K411, KC_NO, K413, KC_NO }, \ | 126 | { KC_NO, K401, K402, KC_NO, K404, KC_NO, K406, KC_NO, K408, KC_NO, K410, K411, KC_NO, K413, KC_NO }, \ |
| 127 | } | 127 | } |
| 128 | 128 | ||
| 129 | // Backlighting | ||
| 130 | typedef union { | ||
| 131 | uint8_t raw; | ||
| 132 | struct { | ||
| 133 | bool enable :1; | ||
| 134 | bool breathing : 1; | ||
| 135 | uint8_t level :6; | ||
| 136 | }; | ||
| 137 | } backlight_levels_config_t; | ||
| 138 | |||
| 139 | extern backlight_levels_config_t kb_backlight_config; | ||
| 140 | extern bool kb_backlight_breathing; | ||
| 141 | |||
| 142 | void backlight_init_ports(void); | ||
| 143 | void backlight_set(uint8_t level); | ||
| 144 | bool is_breathing(void); | ||
| 145 | void breathing_enable(void); | ||
| 146 | void breathing_disable(void); | ||
diff --git a/keyboards/handwired/co60/rev7/rules.mk b/keyboards/handwired/co60/rev7/rules.mk index 9daeaf047..50e4bb891 100644 --- a/keyboards/handwired/co60/rev7/rules.mk +++ b/keyboards/handwired/co60/rev7/rules.mk | |||
| @@ -1,8 +1,8 @@ | |||
| 1 | # MCU name | 1 | # MCU name |
| 2 | MCU = STM32F303 | 2 | MCU = STM32F303 |
| 3 | 3 | ||
| 4 | # Code for backlight breathing: | 4 | # Use SPI for RGB underglow: |
| 5 | SRC += led.c | 5 | WS2812_DRIVER = spi |
| 6 | 6 | ||
| 7 | # Build Options | 7 | # Build Options |
| 8 | # comment out to disable the options. | 8 | # comment out to disable the options. |
| @@ -15,7 +15,7 @@ CONSOLE_ENABLE = no # Console for debug(+400) | |||
| 15 | COMMAND_ENABLE = no # Commands for debug and configuration | 15 | COMMAND_ENABLE = no # Commands for debug and configuration |
| 16 | NKRO_ENABLE = yes # USB Nkey Rollover - if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work | 16 | NKRO_ENABLE = yes # USB Nkey Rollover - if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work |
| 17 | AUDIO_ENABLE = no | 17 | AUDIO_ENABLE = no |
| 18 | RGBLIGHT_ENABLE = no # Enable keyboard underlight functionality | 18 | RGBLIGHT_ENABLE = yes # Enable keyboard underlight functionality |
| 19 | MIDI_ENABLE = no # MIDI controls | 19 | MIDI_ENABLE = no # MIDI controls |
| 20 | UNICODE_ENABLE = no # Unicode | 20 | UNICODE_ENABLE = no # Unicode |
| 21 | BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID | 21 | BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID |
diff --git a/keyboards/handwired/steamvan/keymaps/jmdaly/keymap.c b/keyboards/handwired/steamvan/keymaps/jmdaly/keymap.c index 2bc543271..1c835bad1 100644 --- a/keyboards/handwired/steamvan/keymaps/jmdaly/keymap.c +++ b/keyboards/handwired/steamvan/keymaps/jmdaly/keymap.c | |||
| @@ -44,10 +44,10 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { | |||
| 44 | _______, KC_LGUI, _______, _______, _______, _______, _______, MO(_L4) | 44 | _______, KC_LGUI, _______, _______, _______, _______, _______, MO(_L4) |
| 45 | ), | 45 | ), |
| 46 | [_L3] = LAYOUT_standard( /* LAYER 3 */ | 46 | [_L3] = LAYOUT_standard( /* LAYER 3 */ |
| 47 | KC_MINS, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, _______, | 47 | KC_MINS, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, _______, |
| 48 | KC_EQL, KC_PIPE, KC_TILD, KC_UNDS, KC_PLUS, LSFT(KC_LBRC), LSFT(KC_RBRC), KC_4, KC_5, KC_6, KC_VOLU, KC_INS, | 48 | KC_EQL, KC_PIPE, KC_TILD, KC_UNDS, KC_PLUS, LSFT(KC_LBRC), LSFT(KC_RBRC), KC_4, KC_5, KC_6, KC_VOLU, KC_INS, |
| 49 | _______, _______, _______, KC_DQUO, _______, _______, KC_0, KC_1, KC_2, KC_3, KC_VOLD, _______, | 49 | _______, RGB_TOG, RGB_MOD, RGB_RMOD, KC_DQUO, _______, KC_0, KC_1, KC_2, KC_3, KC_VOLD, _______, |
| 50 | _______, _______, _______, _______, _______, _______, _______, _______ | 50 | _______, _______, _______, _______, _______, _______, _______, _______ |
| 51 | ), | 51 | ), |
| 52 | [_L4] = LAYOUT_standard( /* LAYER 4 */ | 52 | [_L4] = LAYOUT_standard( /* LAYER 4 */ |
| 53 | RESET, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_PGUP, _______, | 53 | RESET, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_PGUP, _______, |
diff --git a/keyboards/handwired/steamvan/rev1/config.h b/keyboards/handwired/steamvan/rev1/config.h index f10f42966..f47dc90c1 100644 --- a/keyboards/handwired/steamvan/rev1/config.h +++ b/keyboards/handwired/steamvan/rev1/config.h | |||
| @@ -57,9 +57,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 57 | /* Backlight configuration | 57 | /* Backlight configuration |
| 58 | * Backlight LEDs on B8 | 58 | * Backlight LEDs on B8 |
| 59 | */ | 59 | */ |
| 60 | #define BACKLIGHT_LEVELS 24 | 60 | #define BACKLIGHT_PIN B8 |
| 61 | #define BACKLIGHT_BREATHING | 61 | #define BACKLIGHT_BREATHING |
| 62 | #define BREATHING_PERIOD 6 | 62 | |
| 63 | #define WS2812_SPI SPID1 | ||
| 64 | #define WS2812_SPI_MOSI_PAL_MODE 5 | ||
| 63 | 65 | ||
| 64 | #define RGBLIGHT_ANIMATIONS | 66 | #define RGBLIGHT_ANIMATIONS |
| 65 | 67 | ||
diff --git a/keyboards/handwired/steamvan/rev1/halconf.h b/keyboards/handwired/steamvan/rev1/halconf.h index 5e5d70219..7363a21a2 100644 --- a/keyboards/handwired/steamvan/rev1/halconf.h +++ b/keyboards/handwired/steamvan/rev1/halconf.h | |||
| @@ -153,7 +153,7 @@ | |||
| 153 | * @brief Enables the SPI subsystem. | 153 | * @brief Enables the SPI subsystem. |
| 154 | */ | 154 | */ |
| 155 | #if !defined(HAL_USE_SPI) || defined(__DOXYGEN__) | 155 | #if !defined(HAL_USE_SPI) || defined(__DOXYGEN__) |
| 156 | #define HAL_USE_SPI FALSE | 156 | #define HAL_USE_SPI TRUE |
| 157 | #endif | 157 | #endif |
| 158 | 158 | ||
| 159 | /** | 159 | /** |
diff --git a/keyboards/handwired/steamvan/rev1/led.c b/keyboards/handwired/steamvan/rev1/led.c deleted file mode 100644 index 1b78fa2f8..000000000 --- a/keyboards/handwired/steamvan/rev1/led.c +++ /dev/null | |||
| @@ -1,242 +0,0 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2012 Jun Wako <wakojun@gmail.com> | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 2 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include "hal.h" | ||
| 19 | #include "led_custom.h" | ||
| 20 | #include "rev1.h" | ||
| 21 | #include "printf.h" | ||
| 22 | |||
| 23 | static void breathing_callback(PWMDriver *pwmp); | ||
| 24 | |||
| 25 | static PWMConfig pwmCFG = { | ||
| 26 | 0xFFFF, /* PWM clock frequency */ | ||
| 27 | 256, /* PWM period (in ticks) 1S (1/10kHz=0.1mS 0.1ms*10000 ticks=1S) */ | ||
| 28 | NULL, /* No Callback */ | ||
| 29 | { | ||
| 30 | {PWM_OUTPUT_DISABLED, NULL}, | ||
| 31 | {PWM_OUTPUT_DISABLED, NULL}, | ||
| 32 | {PWM_OUTPUT_ACTIVE_HIGH, NULL}, /* Enable Channel 3 */ | ||
| 33 | {PWM_OUTPUT_DISABLED, NULL} | ||
| 34 | }, | ||
| 35 | 0, /* HW dependent part.*/ | ||
| 36 | 0 | ||
| 37 | }; | ||
| 38 | |||
| 39 | static PWMConfig pwmCFG_breathing = { | ||
| 40 | 0xFFFF, /* 10kHz PWM clock frequency */ | ||
| 41 | 256, /* PWM period (in ticks) 1S (1/10kHz=0.1mS 0.1ms*10000 ticks=1S) */ | ||
| 42 | breathing_callback, /* Breathing Callback */ | ||
| 43 | { | ||
| 44 | {PWM_OUTPUT_DISABLED, NULL}, | ||
| 45 | {PWM_OUTPUT_DISABLED, NULL}, | ||
| 46 | {PWM_OUTPUT_ACTIVE_HIGH, NULL}, /* Enable Channel 3 */ | ||
| 47 | {PWM_OUTPUT_DISABLED, NULL} | ||
| 48 | }, | ||
| 49 | 0, /* HW dependent part.*/ | ||
| 50 | 0 | ||
| 51 | }; | ||
| 52 | |||
| 53 | // See http://jared.geek.nz/2013/feb/linear-led-pwm | ||
| 54 | static uint16_t cie_lightness(uint16_t v) { | ||
| 55 | if (v <= 5243) // if below 8% of max | ||
| 56 | return v / 9; // same as dividing by 900% | ||
| 57 | else { | ||
| 58 | uint32_t y = (((uint32_t) v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare | ||
| 59 | // to get a useful result with integer division, we shift left in the expression above | ||
| 60 | // and revert what we've done again after squaring. | ||
| 61 | y = y * y * y >> 8; | ||
| 62 | if (y > 0xFFFFUL) // prevent overflow | ||
| 63 | return 0xFFFFU; | ||
| 64 | else | ||
| 65 | return (uint16_t) y; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | void backlight_init_ports(void) { | ||
| 71 | palSetPadMode(GPIOB, 8, PAL_MODE_ALTERNATE(2)); | ||
| 72 | pwmStart(&PWMD4, &pwmCFG); | ||
| 73 | if(kb_backlight_config.enable){ | ||
| 74 | if(kb_backlight_config.breathing){ | ||
| 75 | breathing_enable(); | ||
| 76 | } else{ | ||
| 77 | backlight_set(kb_backlight_config.level); | ||
| 78 | } | ||
| 79 | } else { | ||
| 80 | backlight_set(0); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | void backlight_set(uint8_t level) { | ||
| 85 | uint32_t duty = (uint32_t)(cie_lightness(0xFFFF * (uint32_t) level / BACKLIGHT_LEVELS)); | ||
| 86 | if (level == 0) { | ||
| 87 | // Turn backlight off | ||
| 88 | // Disable channel 3 on PWM4 | ||
| 89 | pwmDisableChannel(&PWMD4, 2); | ||
| 90 | } else { | ||
| 91 | // Turn backlight on | ||
| 92 | if(!is_breathing()){ | ||
| 93 | // Enable channel 3 on PWM4 | ||
| 94 | pwmEnableChannel(&PWMD4, 2, PWM_FRACTION_TO_WIDTH(&PWMD4,0xFFFF,duty)); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | uint8_t backlight_tick = 0; | ||
| 101 | |||
| 102 | void backlight_task(void) { | ||
| 103 | } | ||
| 104 | |||
| 105 | #define BREATHING_NO_HALT 0 | ||
| 106 | #define BREATHING_HALT_OFF 1 | ||
| 107 | #define BREATHING_HALT_ON 2 | ||
| 108 | #define BREATHING_STEPS 128 | ||
| 109 | |||
| 110 | static uint8_t breathing_period = BREATHING_PERIOD; | ||
| 111 | static uint8_t breathing_halt = BREATHING_NO_HALT; | ||
| 112 | static uint16_t breathing_counter = 0; | ||
| 113 | |||
| 114 | bool is_breathing(void) { | ||
| 115 | return PWMD4.config == &pwmCFG_breathing; | ||
| 116 | } | ||
| 117 | |||
| 118 | #define breathing_min() do {breathing_counter = 0;} while (0) | ||
| 119 | #define breathing_max() do {breathing_counter = breathing_period * 256 / 2;} while (0) | ||
| 120 | |||
| 121 | |||
| 122 | void breathing_interrupt_enable(void){ | ||
| 123 | pwmStop(&PWMD4); | ||
| 124 | pwmStart(&PWMD4, &pwmCFG_breathing); | ||
| 125 | chSysLockFromISR(); | ||
| 126 | pwmEnablePeriodicNotification(&PWMD4); | ||
| 127 | pwmEnableChannelI( | ||
| 128 | &PWMD4, | ||
| 129 | 2, | ||
| 130 | PWM_FRACTION_TO_WIDTH( | ||
| 131 | &PWMD4, | ||
| 132 | 0xFFFF, | ||
| 133 | 0xFFFF | ||
| 134 | ) | ||
| 135 | ); | ||
| 136 | chSysUnlockFromISR(); | ||
| 137 | } | ||
| 138 | |||
| 139 | void breathing_interrupt_disable(void){ | ||
| 140 | pwmStop(&PWMD4); | ||
| 141 | pwmStart(&PWMD4, &pwmCFG); | ||
| 142 | } | ||
| 143 | |||
| 144 | void breathing_enable(void) | ||
| 145 | { | ||
| 146 | breathing_counter = 0; | ||
| 147 | breathing_halt = BREATHING_NO_HALT; | ||
| 148 | breathing_interrupt_enable(); | ||
| 149 | } | ||
| 150 | |||
| 151 | void breathing_pulse(void) | ||
| 152 | { | ||
| 153 | if (kb_backlight_config.level == 0) | ||
| 154 | breathing_min(); | ||
| 155 | else | ||
| 156 | breathing_max(); | ||
| 157 | breathing_halt = BREATHING_HALT_ON; | ||
| 158 | breathing_interrupt_enable(); | ||
| 159 | } | ||
| 160 | |||
| 161 | void breathing_disable(void) | ||
| 162 | { | ||
| 163 | breathing_interrupt_disable(); | ||
| 164 | // Restore backlight level | ||
| 165 | backlight_set(kb_backlight_config.level); | ||
| 166 | } | ||
| 167 | |||
| 168 | void breathing_self_disable(void) | ||
| 169 | { | ||
| 170 | if (kb_backlight_config.level == 0) | ||
| 171 | breathing_halt = BREATHING_HALT_OFF; | ||
| 172 | else | ||
| 173 | breathing_halt = BREATHING_HALT_ON; | ||
| 174 | } | ||
| 175 | |||
| 176 | void breathing_toggle(void) { | ||
| 177 | if (is_breathing()){ | ||
| 178 | breathing_disable(); | ||
| 179 | } else { | ||
| 180 | breathing_enable(); | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | void breathing_period_set(uint8_t value) | ||
| 185 | { | ||
| 186 | if (!value) | ||
| 187 | value = 1; | ||
| 188 | breathing_period = value; | ||
| 189 | } | ||
| 190 | |||
| 191 | void breathing_period_default(void) { | ||
| 192 | breathing_period_set(BREATHING_PERIOD); | ||
| 193 | } | ||
| 194 | |||
| 195 | void breathing_period_inc(void) | ||
| 196 | { | ||
| 197 | breathing_period_set(breathing_period+1); | ||
| 198 | } | ||
| 199 | |||
| 200 | void breathing_period_dec(void) | ||
| 201 | { | ||
| 202 | breathing_period_set(breathing_period-1); | ||
| 203 | } | ||
| 204 | |||
| 205 | /* To generate breathing curve in python: | ||
| 206 | * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)] | ||
| 207 | */ | ||
| 208 | static const uint8_t breathing_table[BREATHING_STEPS] = {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}; | ||
| 209 | |||
| 210 | // Use this before the cie_lightness function. | ||
| 211 | static inline uint16_t scale_backlight(uint16_t v) { | ||
| 212 | return v / BACKLIGHT_LEVELS * kb_backlight_config.level; | ||
| 213 | } | ||
| 214 | |||
| 215 | static void breathing_callback(PWMDriver *pwmp) | ||
| 216 | { | ||
| 217 | (void)pwmp; | ||
| 218 | uint16_t interval = (uint16_t) breathing_period * 256 / BREATHING_STEPS; | ||
| 219 | // resetting after one period to prevent ugly reset at overflow. | ||
| 220 | breathing_counter = (breathing_counter + 1) % (breathing_period * 256); | ||
| 221 | uint8_t index = breathing_counter / interval % BREATHING_STEPS; | ||
| 222 | |||
| 223 | if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) || | ||
| 224 | ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1))) | ||
| 225 | { | ||
| 226 | breathing_interrupt_disable(); | ||
| 227 | } | ||
| 228 | |||
| 229 | uint32_t duty = cie_lightness(scale_backlight(breathing_table[index] * 256)); | ||
| 230 | |||
| 231 | chSysLockFromISR(); | ||
| 232 | pwmEnableChannelI( | ||
| 233 | &PWMD4, | ||
| 234 | 2, | ||
| 235 | PWM_FRACTION_TO_WIDTH( | ||
| 236 | &PWMD4, | ||
| 237 | 0xFFFF, | ||
| 238 | duty | ||
| 239 | ) | ||
| 240 | ); | ||
| 241 | chSysUnlockFromISR(); | ||
| 242 | } | ||
diff --git a/keyboards/handwired/steamvan/rev1/led_custom.h b/keyboards/handwired/steamvan/rev1/led_custom.h deleted file mode 100644 index 56e723db8..000000000 --- a/keyboards/handwired/steamvan/rev1/led_custom.h +++ /dev/null | |||
| @@ -1,22 +0,0 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2019 John M Daly <jmdaly@gmail.com> | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 2 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #pragma once | ||
| 19 | |||
| 20 | void backlight_task(void); | ||
| 21 | void breathing_interrupt_disable(void); | ||
| 22 | void breathing_interrupt_enable(void); | ||
diff --git a/keyboards/handwired/steamvan/rev1/mcuconf.h b/keyboards/handwired/steamvan/rev1/mcuconf.h index 69bf9185d..7ad013744 100644 --- a/keyboards/handwired/steamvan/rev1/mcuconf.h +++ b/keyboards/handwired/steamvan/rev1/mcuconf.h | |||
| @@ -210,7 +210,7 @@ | |||
| 210 | /* | 210 | /* |
| 211 | * SPI driver system settings. | 211 | * SPI driver system settings. |
| 212 | */ | 212 | */ |
| 213 | #define STM32_SPI_USE_SPI1 FALSE | 213 | #define STM32_SPI_USE_SPI1 TRUE |
| 214 | #define STM32_SPI_USE_SPI2 FALSE | 214 | #define STM32_SPI_USE_SPI2 FALSE |
| 215 | #define STM32_SPI_USE_SPI3 FALSE | 215 | #define STM32_SPI_USE_SPI3 FALSE |
| 216 | #define STM32_SPI_SPI1_DMA_PRIORITY 1 | 216 | #define STM32_SPI_SPI1_DMA_PRIORITY 1 |
diff --git a/keyboards/handwired/steamvan/rev1/rev1.c b/keyboards/handwired/steamvan/rev1/rev1.c index 318864949..53f9a400f 100644 --- a/keyboards/handwired/steamvan/rev1/rev1.c +++ b/keyboards/handwired/steamvan/rev1/rev1.c | |||
| @@ -15,22 +15,3 @@ | |||
| 15 | */ | 15 | */ |
| 16 | #include "rev1.h" | 16 | #include "rev1.h" |
| 17 | 17 | ||
| 18 | #include "led.h" | ||
| 19 | #include "printf.h" | ||
| 20 | |||
| 21 | backlight_config_t kb_backlight_config = { | ||
| 22 | .enable = true, | ||
| 23 | .breathing = true, | ||
| 24 | .level = BACKLIGHT_LEVELS | ||
| 25 | }; | ||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | void matrix_init_kb(void) { | ||
| 30 | matrix_init_user(); | ||
| 31 | backlight_init_ports(); | ||
| 32 | } | ||
| 33 | |||
| 34 | void matrix_scan_kb(void) { | ||
| 35 | matrix_scan_user(); | ||
| 36 | } | ||
diff --git a/keyboards/handwired/steamvan/rev1/rev1.h b/keyboards/handwired/steamvan/rev1/rev1.h index 83f7ecb9c..1cb72277f 100644 --- a/keyboards/handwired/steamvan/rev1/rev1.h +++ b/keyboards/handwired/steamvan/rev1/rev1.h | |||
| @@ -16,7 +16,6 @@ | |||
| 16 | #pragma once | 16 | #pragma once |
| 17 | 17 | ||
| 18 | #include "quantum.h" | 18 | #include "quantum.h" |
| 19 | #include "backlight.h" | ||
| 20 | 19 | ||
| 21 | // This a shortcut to help you visually see your layout. | 20 | // This a shortcut to help you visually see your layout. |
| 22 | // There are a number of variations depending on the layout of your bottom row. | 21 | // There are a number of variations depending on the layout of your bottom row. |
| @@ -79,12 +78,3 @@ | |||
| 79 | { K30, K31, K32, K33, K34, KC_NO, K36, KC_NO, K38, K39, K3A, K3B } \ | 78 | { K30, K31, K32, K33, K34, KC_NO, K36, KC_NO, K38, K39, K3A, K3B } \ |
| 80 | } | 79 | } |
| 81 | 80 | ||
| 82 | // Backlighting | ||
| 83 | extern backlight_config_t kb_backlight_config; | ||
| 84 | extern bool kb_backlight_breathing; | ||
| 85 | |||
| 86 | void backlight_init_ports(void); | ||
| 87 | void backlight_set(uint8_t level); | ||
| 88 | bool is_breathing(void); | ||
| 89 | void breathing_enable(void); | ||
| 90 | void breathing_disable(void); | ||
diff --git a/keyboards/handwired/steamvan/rev1/rules.mk b/keyboards/handwired/steamvan/rev1/rules.mk index 471cb4c1e..797d7b35d 100644 --- a/keyboards/handwired/steamvan/rev1/rules.mk +++ b/keyboards/handwired/steamvan/rev1/rules.mk | |||
| @@ -1,8 +1,8 @@ | |||
| 1 | # MCU name | 1 | # MCU name |
| 2 | MCU = STM32F303 | 2 | MCU = STM32F303 |
| 3 | 3 | ||
| 4 | # Code for backlight breathing: | 4 | # Use SPI for RGB underglow: |
| 5 | SRC += led.c | 5 | WS2812_DRIVER = spi |
| 6 | 6 | ||
| 7 | # Build Options | 7 | # Build Options |
| 8 | # comment out to disable the options. | 8 | # comment out to disable the options. |
| @@ -15,7 +15,7 @@ CONSOLE_ENABLE = no # Console for debug(+400) | |||
| 15 | COMMAND_ENABLE = no # Commands for debug and configuration | 15 | COMMAND_ENABLE = no # Commands for debug and configuration |
| 16 | NKRO_ENABLE = yes # USB Nkey Rollover - if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work | 16 | NKRO_ENABLE = yes # USB Nkey Rollover - if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work |
| 17 | AUDIO_ENABLE = no | 17 | AUDIO_ENABLE = no |
| 18 | RGBLIGHT_ENABLE = no # Enable keyboard underlight functionality | 18 | RGBLIGHT_ENABLE = yes # Enable keyboard underlight functionality |
| 19 | MIDI_ENABLE = no # MIDI controls | 19 | MIDI_ENABLE = no # MIDI controls |
| 20 | UNICODE_ENABLE = no # Unicode | 20 | UNICODE_ENABLE = no # Unicode |
| 21 | BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID | 21 | BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID |
