From ee079a41f1450ef567e15699275596ebe058236a Mon Sep 17 00:00:00 2001 From: fishsoupisgood Date: Fri, 2 Oct 2020 11:10:08 +0100 Subject: async handing of colour --- keyboards/ymdk/ymd09_32a/ymd09_32a.c | 12 ++++++++++-- quantum/rgblight.c | 11 +++++++++-- quantum/rgblight.h | 1 + tmk_core/common/raw_hid.h | 1 + tmk_core/protocol/vusb/vusb.c | 29 ++++++++++++----------------- 5 files changed, 33 insertions(+), 21 deletions(-) diff --git a/keyboards/ymdk/ymd09_32a/ymd09_32a.c b/keyboards/ymdk/ymd09_32a/ymd09_32a.c index 870593e6a..62487e017 100644 --- a/keyboards/ymdk/ymd09_32a/ymd09_32a.c +++ b/keyboards/ymdk/ymd09_32a/ymd09_32a.c @@ -16,6 +16,7 @@ */ #include "ymd09_32a.h" +static int rgb_update=0; void raw_hid_receive( uint8_t *data, uint8_t length ) { @@ -23,12 +24,19 @@ void raw_hid_receive( uint8_t *data, uint8_t length ) switch ( data[0] ) { case RAW_COMMAND_CHANGE_COLOR: // 0x05 0x04 0xLL 0xRR 0xGG 0xBB -// rgblight_mode_noeeprom(RGBLIGHT_MODE_STATIC_LIGHT); - rgblight_setrgb_at(data[2],data[3],data[4],data[1]); + rgblight_setrgb_at_noupdate(data[2],data[3],data[4],data[1]); + rgb_update=1; break; } } +void raw_hid_dispatch(void) +{ + if (!rgb_update) return; + rgb_update = 0; + rgblight_set(); +} + void keyboard_pre_init_kb(void) { led_init_ports(); keyboard_pre_init_user(); diff --git a/quantum/rgblight.c b/quantum/rgblight.c index 76bb6eb8c..bba50037e 100644 --- a/quantum/rgblight.c +++ b/quantum/rgblight.c @@ -551,9 +551,9 @@ void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) { rgblight_set(); } -void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index) { +int rgblight_setrgb_at_noupdate(uint8_t r, uint8_t g, uint8_t b, uint8_t index) { if (!rgblight_config.enable || index >= RGBLED_NUM) { - return; + return -1; } led[index].r = r; @@ -562,6 +562,13 @@ void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index) { #ifdef RGBW led[index].w = 0; #endif + return 0; +} + + +void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index) { + if (rgblight_setrgb_at_noupdate(r, g, b, index)) + return; rgblight_set(); } diff --git a/quantum/rgblight.h b/quantum/rgblight.h index c3a9e94b7..436d4d8bd 100644 --- a/quantum/rgblight.h +++ b/quantum/rgblight.h @@ -291,6 +291,7 @@ void rgblight_set_clipping_range(uint8_t start_pos, uint8_t num_leds); void rgblight_set_effect_range(uint8_t start_pos, uint8_t num_leds); /* direct operation */ +int rgblight_setrgb_at_noupdate(uint8_t r, uint8_t g, uint8_t b, uint8_t index); void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index); void rgblight_sethsv_at(uint8_t hue, uint8_t sat, uint8_t val, uint8_t index); void rgblight_setrgb_range(uint8_t r, uint8_t g, uint8_t b, uint8_t start, uint8_t end); diff --git a/tmk_core/common/raw_hid.h b/tmk_core/common/raw_hid.h index c579157f1..1e4be6a92 100644 --- a/tmk_core/common/raw_hid.h +++ b/tmk_core/common/raw_hid.h @@ -2,6 +2,7 @@ #define _RAW_HID_H_ void raw_hid_receive(uint8_t *data, uint8_t length); +void raw_hid_dispatch(void); void raw_hid_send(uint8_t *data, uint8_t length); diff --git a/tmk_core/protocol/vusb/vusb.c b/tmk_core/protocol/vusb/vusb.c index 7d1a0a7a2..c32b595bc 100644 --- a/tmk_core/protocol/vusb/vusb.c +++ b/tmk_core/protocol/vusb/vusb.c @@ -107,10 +107,8 @@ void vusb_transfer_keyboard(void) { * RAW HID *------------------------------------------------------------------*/ #ifdef RAW_ENABLE -# define RAW_BUFFER_SIZE 8 # define RAW_EPSIZE 8 -static uint8_t raw_output_buffer[RAW_BUFFER_SIZE]; static uint8_t raw_output_received_bytes = 0; #if 0 @@ -140,11 +138,15 @@ __attribute__((weak)) void raw_hid_receive(uint8_t *data, uint8_t length) { // so users can opt to not handle data coming in. } +__attribute__((weak)) void raw_hid_dispatch(void) { + // Users should #include "raw_hid.h" in their own code + // and implement this function there. Leave this as weak linkage + // so users can opt to not handle data coming in. +} + + void raw_hid_task(void) { - if (raw_output_received_bytes == RAW_BUFFER_SIZE) { - raw_hid_receive(raw_output_buffer, RAW_BUFFER_SIZE); - raw_output_received_bytes = 0; - } + raw_hid_dispatch(); } #endif @@ -340,16 +342,9 @@ uchar usbFunctionWrite(uchar *data, uchar len) { void usbFunctionWriteOut(uchar *data, uchar len) { #ifdef RAW_ENABLE // Data from host must be divided every 8bytes - if (len != 8) { - dprint("RAW: invalid length\n"); - raw_output_received_bytes = 0; - return; - } + raw_hid_receive(data, len); - for (uint8_t i = 0; i < 8; i++) { - raw_output_buffer[i] = data[i]; - } - raw_output_received_bytes = len; + raw_output_received_bytes = 1; #endif } @@ -491,14 +486,14 @@ const PROGMEM uchar raw_hid_report[] = { 0x09, 0x62, // Usage (Vendor Defined) 0x15, 0x00, // Logical Minimum (0) 0x26, 0xFF, 0x00, // Logical Maximum (255) - 0x95, RAW_BUFFER_SIZE, // Report Count + 0x95, RAW_EPSIZE, // Report Count 0x75, 0x08, // Report Size (8) 0x81, 0x02, // Input (Data, Variable, Absolute) // Data from host 0x09, 0x63, // Usage (Vendor Defined) 0x15, 0x00, // Logical Minimum (0) 0x26, 0xFF, 0x00, // Logical Maximum (255) - 0x95, RAW_BUFFER_SIZE, // Report Count + 0x95, RAW_EPSIZE, // Report Count 0x75, 0x08, // Report Size (8) 0x91, 0x02, // Output (Data, Variable, Absolute) 0xC0 // End Collection -- cgit v1.2.3