From 64b7cfe735339193ae78fe0f13029b0e027af7a7 Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Tue, 29 Oct 2019 22:53:11 +0000 Subject: [PATCH] Refactor ps2avrgb i2c ws2812 to core (#7183) * Refactor ps2avrgb i2c ws2812 to core * Refactor jj40 to use ws2812 i2c driver * Refactor ps2avrgb template to use ws2812 i2c driver * Add ws2812 stub files * clang-format and driver config * Add ws2812 driver docs * Fix default config values * Update tmk_core/protocol/vusb/main.c Co-Authored-By: Drashna Jaelre --- common_features.mk | 24 ++++++++++++++++-- docs/_summary.md | 1 + docs/ws2812_driver.md | 33 ++++++++++++++++++++++++ drivers/arm/ws2812.c | 1 + drivers/arm/ws2812_pwm.c | 1 + drivers/arm/ws2812_spi.c | 1 + drivers/avr/ws2812_i2c.c | 31 +++++++++++++++++++++++ keyboards/jj40/jj40.c | 37 --------------------------- keyboards/jj40/rules.mk | 4 +-- quantum/template/ps2avrgb/keyboard.c | 38 ---------------------------- quantum/template/ps2avrgb/rules.mk | 4 +-- tmk_core/protocol/vusb/main.c | 9 +++++++ 12 files changed, 101 insertions(+), 83 deletions(-) create mode 100644 docs/ws2812_driver.md create mode 100644 drivers/arm/ws2812.c create mode 100644 drivers/arm/ws2812_pwm.c create mode 100644 drivers/arm/ws2812_spi.c create mode 100644 drivers/avr/ws2812_i2c.c diff --git a/common_features.mk b/common_features.mk index 83b2b51ae..7bb9187bb 100644 --- a/common_features.mk +++ b/common_features.mk @@ -112,7 +112,7 @@ ifeq ($(strip $(RGBLIGHT_ENABLE)), yes) ifeq ($(strip $(RGBLIGHT_CUSTOM_DRIVER)), yes) OPT_DEFS += -DRGBLIGHT_CUSTOM_DRIVER else - SRC += ws2812.c + WS2812_DRIVER_REQUIRED = yes endif endif @@ -176,7 +176,7 @@ endif ifeq ($(strip $(RGB_MATRIX_ENABLE)), WS2812) OPT_DEFS += -DWS2812 - SRC += ws2812.c + WS2812_DRIVER_REQUIRED = yes endif ifeq ($(strip $(RGB_MATRIX_CUSTOM_KB)), yes) @@ -262,6 +262,26 @@ ifneq ($(strip $(BACKLIGHT_ENABLE)), no) endif endif +VALID_WS2812_DRIVER_TYPES := bitbang pwm spi i2c + +WS2812_DRIVER ?= bitbang +ifeq ($(strip $(WS2812_DRIVER_REQUIRED)), yes) + ifeq ($(filter $(WS2812_DRIVER),$(VALID_WS2812_DRIVER_TYPES)),) + $(error WS2812_DRIVER="$(WS2812_DRIVER)" is not a valid WS2812 driver) + endif + + ifeq ($(strip $(WS2812_DRIVER)), bitbang) + SRC += ws2812.c + else + SRC += ws2812_$(strip $(WS2812_DRIVER)).c + endif + + # add extra deps + ifeq ($(strip $(WS2812_DRIVER)), i2c) + QUANTUM_LIB_SRC += i2c_master.c + endif +endif + ifeq ($(strip $(CIE1931_CURVE)), yes) OPT_DEFS += -DUSE_CIE1931_CURVE LED_TABLES = yes diff --git a/docs/_summary.md b/docs/_summary.md index 65bfa11f0..dc6e88f95 100644 --- a/docs/_summary.md +++ b/docs/_summary.md @@ -98,6 +98,7 @@ * [ISP Flashing Guide](isp_flashing_guide.md) * [ARM Debugging Guide](arm_debugging.md) * [I2C Driver](i2c_driver.md) + * [WS2812 Driver](ws2812_driver.md) * [GPIO Controls](internals_gpio_control.md) * [Proton C Conversion](proton_c_conversion.md) diff --git a/docs/ws2812_driver.md b/docs/ws2812_driver.md new file mode 100644 index 000000000..6fa5d324c --- /dev/null +++ b/docs/ws2812_driver.md @@ -0,0 +1,33 @@ +# WS2812 Driver +This driver powers the [RGB Lighting](feature_rgblight.md) and [RGB Matrix](feature_rgb_matrix.md) features. + +Currently QMK supports the following addressable LEDs on AVR microcontrollers (however, the white LED in RGBW variants is not supported): + + WS2811, WS2812, WS2812B, WS2812C, etc. + SK6812, SK6812MINI, SK6805 + +These LEDs are called "addressable" because instead of using a wire per color, each LED contains a small microchip that understands a special protocol sent over a single wire. The chip passes on the remaining data to the next LED, allowing them to be chained together. In this way, you can easily control the color of the individual LEDs. + +## Driver configuration + +### Bitbang +Default driver, the absence of configuration assumes this driver. To configure it, add this to your rules.mk: + +```make +WS2812_DRIVER = bitbang +``` + +!> ARM does not yet support WS2182. Progress is being made, but we are not quite there, yet. + +### I2C +Targeting boards where WS2812 support is offloaded to a 2nd MCU. Currently the driver is limited to AVR given the known consumers are ps2avrGB/BMC. To configure it, add this to your rules.mk: + +```make +WS2812_DRIVER = i2c +``` + +Configure the hardware via your config.h: +```c +#define WS2812_ADDRESS 0xb0 // default: 0xb0 +#define WS2812_TIMEOUT 100 // default: 100 +``` diff --git a/drivers/arm/ws2812.c b/drivers/arm/ws2812.c new file mode 100644 index 000000000..2094e5009 --- /dev/null +++ b/drivers/arm/ws2812.c @@ -0,0 +1 @@ +#error("NOT SUPPORTED") \ No newline at end of file diff --git a/drivers/arm/ws2812_pwm.c b/drivers/arm/ws2812_pwm.c new file mode 100644 index 000000000..2094e5009 --- /dev/null +++ b/drivers/arm/ws2812_pwm.c @@ -0,0 +1 @@ +#error("NOT SUPPORTED") \ No newline at end of file diff --git a/drivers/arm/ws2812_spi.c b/drivers/arm/ws2812_spi.c new file mode 100644 index 000000000..2094e5009 --- /dev/null +++ b/drivers/arm/ws2812_spi.c @@ -0,0 +1 @@ +#error("NOT SUPPORTED") \ No newline at end of file diff --git a/drivers/avr/ws2812_i2c.c b/drivers/avr/ws2812_i2c.c new file mode 100644 index 000000000..8525a026c --- /dev/null +++ b/drivers/avr/ws2812_i2c.c @@ -0,0 +1,31 @@ +#include "ws2812.h" +#include "i2c_master.h" + +#ifndef WS2812_ADDRESS +# define WS2812_ADDRESS 0xb0 +#endif + +#ifndef WS2812_TIMEOUT +# define WS2812_TIMEOUT 100 +#endif + +void ws2812_init(void) { i2c_init(); } + +// Setleds for standard RGB +void ws2812_setleds(LED_TYPE *ledarray, uint16_t leds) { + static bool s_init = false; + if (!s_init) { + ws2812_init(); + s_init = true; + } + + i2c_transmit(WS2812_ADDRESS, (uint8_t *)ledarray, sizeof(LED_TYPE) * leds, WS2812_TIMEOUT); +} + +// Setleds for SK6812RGBW +void ws2812_setleds_rgbw(LED_TYPE *ledarray, uint16_t leds) { +// not supported - for now error out if its enabled +#ifdef RGBW +# error "RGBW not supported" +#endif +} diff --git a/keyboards/jj40/jj40.c b/keyboards/jj40/jj40.c index 26cfa6c06..894ed4907 100644 --- a/keyboards/jj40/jj40.c +++ b/keyboards/jj40/jj40.c @@ -17,40 +17,3 @@ along with this program. If not, see . */ #include "jj40.h" - -#ifdef RGBLIGHT_ENABLE - -#include -#include "i2c_master.h" -#include "rgblight.h" - -extern rgblight_config_t rgblight_config; - -void matrix_init_kb(void) { - i2c_init(); - // call user level keymaps, if any - matrix_init_user(); -} -// custom RGB driver -void rgblight_set(void) { - if (!rgblight_config.enable) { - memset(led, 0, 3 * RGBLED_NUM); - } - - i2c_transmit(0xb0, (uint8_t*)led, 3 * RGBLED_NUM, 100); -} - -bool rgb_init = false; - -void matrix_scan_kb(void) { - // if LEDs were previously on before poweroff, turn them back on - if (rgb_init == false && rgblight_config.enable) { - i2c_transmit(0xb0, (uint8_t*)led, 3 * RGBLED_NUM, 100); - rgb_init = true; - } - - rgblight_task(); - matrix_scan_user(); -} - -#endif diff --git a/keyboards/jj40/rules.mk b/keyboards/jj40/rules.mk index b0bf574bd..8e0e8c864 100644 --- a/keyboards/jj40/rules.mk +++ b/keyboards/jj40/rules.mk @@ -40,7 +40,7 @@ SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend NKRO_ENABLE = no # USB Nkey Rollover BACKLIGHT_ENABLE = yes # Enable keyboard backlight functionality on B7 by default RGBLIGHT_ENABLE = yes # Enable keyboard RGB underglow -RGBLIGHT_CUSTOM_DRIVER = yes +WS2812_DRIVER = i2c MIDI_ENABLE = no # MIDI support (+2400 to 4200, depending on config) UNICODE_ENABLE = no # Unicode BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID @@ -48,6 +48,4 @@ AUDIO_ENABLE = no # Audio output on port C6 FAUXCLICKY_ENABLE = no # Use buzzer to emulate clicky switches HD44780_ENABLE = no # Enable support for HD44780 based LCDs (+400) -SRC += i2c_master.c - LAYOUTS = ortho_4x12 planck_mit diff --git a/quantum/template/ps2avrgb/keyboard.c b/quantum/template/ps2avrgb/keyboard.c index efc851748..73cd668f8 100644 --- a/quantum/template/ps2avrgb/keyboard.c +++ b/quantum/template/ps2avrgb/keyboard.c @@ -16,44 +16,6 @@ #include "%KEYBOARD%.h" -#ifdef RGBLIGHT_ENABLE - -# include -# include "i2c_master.h" -# include "rgblight.h" - -extern rgblight_config_t rgblight_config; - -void matrix_init_kb(void) { - i2c_init(); - // call user level keymaps, if any - matrix_init_user(); -} - -// custom RGB driver -void rgblight_set(void) { - if (!rgblight_config.enable) { - memset(led, 0, 3 * RGBLED_NUM); - } - - i2c_transmit(0xb0, (uint8_t*)led, 3 * RGBLED_NUM, 100); -} - -bool rgb_init = false; - -void matrix_scan_kb(void) { - // if LEDs were previously on before poweroff, turn them back on - if (rgb_init == false && rgblight_config.enable) { - i2c_transmit(0xb0, (uint8_t*)led, 3 * RGBLED_NUM, 100); - rgb_init = true; - } - - rgblight_task(); - matrix_scan_user(); -} - -#endif - // Optional override functions below. // You can leave any or all of these undefined. // These are only required if you want to perform custom actions. diff --git a/quantum/template/ps2avrgb/rules.mk b/quantum/template/ps2avrgb/rules.mk index bda115db5..ec57b03dc 100644 --- a/quantum/template/ps2avrgb/rules.mk +++ b/quantum/template/ps2avrgb/rules.mk @@ -19,8 +19,6 @@ CONSOLE_ENABLE = yes COMMAND_ENABLE = yes BACKLIGHT_ENABLE = no RGBLIGHT_ENABLE = yes -RGBLIGHT_CUSTOM_DRIVER = yes +WS2812_DRIVER = i2c OPT_DEFS = -DDEBUG_LEVEL=0 - -SRC += i2c_master.c diff --git a/tmk_core/protocol/vusb/main.c b/tmk_core/protocol/vusb/main.c index f8322d94a..8cc736497 100644 --- a/tmk_core/protocol/vusb/main.c +++ b/tmk_core/protocol/vusb/main.c @@ -20,6 +20,11 @@ #include "timer.h" #include "uart.h" #include "debug.h" +#include "rgblight_reconfig.h" + +#if (defined(RGB_MIDI) | defined(RGBLIGHT_ANIMATIONS)) & defined(RGBLIGHT_ENABLE) +# include "rgblight.h" +#endif #define UART_BAUD_RATE 115200 @@ -94,6 +99,10 @@ int main(void) { // To prevent failing to configure NOT scan keyboard during configuration if (usbConfiguration && usbInterruptIsReady()) { keyboard_task(); + +#if defined(RGBLIGHT_ANIMATIONS) && defined(RGBLIGHT_ENABLE) + rgblight_task(); +#endif } vusb_transfer_keyboard(); }