From bea45d696a698dccdef1962386b940b501aec6d9 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sat, 4 Jan 2020 18:25:10 +0100 Subject: [PATCH 1/2] device/Raise: Remove two unused methods from an internal class The (internal) `::dygma::raise::Hand` class had two methods that weren't used by Kaleidoscope at all: `.setAllLEDsTo` and `.setOneLEDTo`. Since they're unused, remove them. We weren't going to use them anyway, because they immediately sync to the LED driver, while we want to do that in `syncLeds()`, and in there only. Signed-off-by: Gergely Nagy --- src/kaleidoscope/device/dygma/raise/Hand.cpp | 19 ------------------- src/kaleidoscope/device/dygma/raise/Hand.h | 2 -- 2 files changed, 21 deletions(-) diff --git a/src/kaleidoscope/device/dygma/raise/Hand.cpp b/src/kaleidoscope/device/dygma/raise/Hand.cpp index 33d9b387..b10a9143 100644 --- a/src/kaleidoscope/device/dygma/raise/Hand.cpp +++ b/src/kaleidoscope/device/dygma/raise/Hand.cpp @@ -228,25 +228,6 @@ void Hand::sendLEDBank(uint8_t bank) { uint8_t result = twi_.writeTo(data, ELEMENTS(data)); } -void Hand::setAllLEDsTo(cRGB color) { - uint8_t data[] = {TWI_CMD_LED_SET_ALL_TO, - pgm_read_byte(&gamma8[color.r]), - pgm_read_byte(&gamma8[color.g]), - pgm_read_byte(&gamma8[color.b]) - }; - twi_.writeTo(data, ELEMENTS(data)); -} - -void Hand::setOneLEDTo(byte led, cRGB color) { - uint8_t data[] = {TWI_CMD_LED_SET_ONE_TO, - led, - pgm_read_byte(&gamma8[color.r]), - pgm_read_byte(&gamma8[color.g]), - pgm_read_byte(&gamma8[color.b]) - }; - twi_.writeTo(data, ELEMENTS(data)); -} - } } } diff --git a/src/kaleidoscope/device/dygma/raise/Hand.h b/src/kaleidoscope/device/dygma/raise/Hand.h index 261fb6ed..543d89ab 100644 --- a/src/kaleidoscope/device/dygma/raise/Hand.h +++ b/src/kaleidoscope/device/dygma/raise/Hand.h @@ -74,8 +74,6 @@ class Hand { bool moreKeysWaiting(); void sendLEDData(); void sendLEDBank(uint8_t bank); - void setOneLEDTo(byte led, cRGB color); - void setAllLEDsTo(cRGB color); keydata_t getKeyData(); bool readKeys(); uint8_t controllerAddress(); From 5024ff4066f8223ee0fb36a3ebf38bc6f9bd4197 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sat, 4 Jan 2020 18:57:05 +0100 Subject: [PATCH 2/2] LEDControl: Implement global brightness controls The new `.setBrightness()` and `.getBrightness()` methods control the brightness of the LEDs, by dispatching them to the LED drivers. We dispatch to the drivers so that nothing else needs to be aware of brightness control. Plugins will always set the unadjusted colors, and anything and anyone who reads colors, will also get the unadjusted values. Pushing the adjustment down to the driver level makes everything smooth, and since we do gamma correction there anyway, it makes sense to do brightness adjustment at the same place, too. Fixes #775. Signed-off-by: Gergely Nagy --- .../LEDs/LED-Brightness/LED-Brightness.ino | 77 +++++++++++++++++++ src/kaleidoscope/device/dygma/Raise.cpp | 17 +++- src/kaleidoscope/device/dygma/Raise.h | 6 +- src/kaleidoscope/device/dygma/raise/Hand.cpp | 8 +- src/kaleidoscope/device/dygma/raise/Hand.h | 8 ++ src/kaleidoscope/device/keyboardio/Imago.cpp | 23 ++++-- src/kaleidoscope/device/keyboardio/Imago.h | 12 ++- .../device/keyboardio/Model01.cpp | 12 ++- src/kaleidoscope/device/keyboardio/Model01.h | 4 +- src/kaleidoscope/driver/led/Base.h | 3 + src/kaleidoscope/plugin/LEDControl.h | 7 ++ 11 files changed, 162 insertions(+), 15 deletions(-) create mode 100644 examples/LEDs/LED-Brightness/LED-Brightness.ino diff --git a/examples/LEDs/LED-Brightness/LED-Brightness.ino b/examples/LEDs/LED-Brightness/LED-Brightness.ino new file mode 100644 index 00000000..5f184150 --- /dev/null +++ b/examples/LEDs/LED-Brightness/LED-Brightness.ino @@ -0,0 +1,77 @@ +/* -*- mode: c++ -*- + * LED-Brightness.ino -- Example to show LED brightness control + * Copyright (C) 2020 Keyboard.io, Inc + * + * 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, version 3. + * + * 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 +#include +#include + +// *INDENT-OFF* +KEYMAPS( + [0] = KEYMAP_STACKED + ( + Key_LEDEffectNext, Key_1, Key_2, Key_3, Key_4, Key_5, Key_LEDEffectNext, + Key_Backtick, Key_Q, Key_W, Key_E, Key_R, Key_T, Key_Tab, + Key_PageUp, Key_A, Key_S, Key_D, Key_F, Key_G, + Key_PageDown, Key_Z, Key_X, Key_C, Key_V, Key_B, Key_Escape, + + Key_LeftControl, Key_Backspace, Key_LeftGui, Key_LeftShift, + M(0), + + Key_skip, Key_6, Key_7, Key_8, Key_9, Key_0, Key_skip, + Key_Enter, Key_Y, Key_U, Key_I, Key_O, Key_P, Key_Equals, + Key_H, Key_J, Key_K, Key_L, Key_Semicolon, Key_Quote, + Key_skip, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash, Key_Minus, + + Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl, + M(1)), +) +// *INDENT-ON* + +KALEIDOSCOPE_INIT_PLUGINS(LEDControl, + Macros, + LEDRainbowWaveEffect); + +const macro_t *macroAction(uint8_t macroIndex, uint8_t keyState) { + if (keyToggledOn(keyState)) { + uint8_t brightness = LEDControl.getBrightness(); + + if (macroIndex == 0) { + if (brightness > 10) + brightness -= 10; + else + brightness = 0; + } else if (macroIndex == 1) { + if (brightness < 245) + brightness += 10; + else + brightness = 255; + } + + LEDControl.setBrightness(brightness); + } + + return MACRO_NONE; +} + +void setup() { + Kaleidoscope.setup(); + LEDRainbowWaveEffect.brightness(255); +} + +void loop() { + Kaleidoscope.loop(); +} diff --git a/src/kaleidoscope/device/dygma/Raise.cpp b/src/kaleidoscope/device/dygma/Raise.cpp index 42465d45..012ba8e3 100644 --- a/src/kaleidoscope/device/dygma/Raise.cpp +++ b/src/kaleidoscope/device/dygma/Raise.cpp @@ -1,7 +1,7 @@ /* -*- mode: c++ -*- * kaleidoscope::device::dygma::Raise -- Kaleidoscope device plugin for Dygma Raise - * Copyright (C) 2017-2019 Keyboard.io, Inc - * Copyright (C) 2017-2019 Dygma Lab S.L. + * Copyright (C) 2017-2020 Keyboard.io, Inc + * Copyright (C) 2017-2020 Dygma Lab S.L. * * 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 @@ -144,6 +144,19 @@ constexpr uint8_t RaiseLEDDriver::led_map[][RaiseLEDDriverProps::led_count + 1]; constexpr uint8_t RaiseLEDDriverProps::key_led_map[]; +void RaiseLEDDriver::setBrightness(uint8_t brightness) { + RaiseHands::leftHand.setBrightness(brightness); + RaiseHands::rightHand.setBrightness(brightness); + for (uint8_t i = 0; i < LED_BANKS; i++) { + isLEDChangedLeft[i] = true; + isLEDChangedRight[i] = true; + } +} + +uint8_t RaiseLEDDriver::getBrightness() { + return RaiseHands::leftHand.getBrightness(); +} + void RaiseLEDDriver::syncLeds() { // left and right sides for (uint8_t i = 0; i < LED_BANKS; i ++) { diff --git a/src/kaleidoscope/device/dygma/Raise.h b/src/kaleidoscope/device/dygma/Raise.h index 5d59edc1..921da20e 100644 --- a/src/kaleidoscope/device/dygma/Raise.h +++ b/src/kaleidoscope/device/dygma/Raise.h @@ -1,7 +1,7 @@ /* -*- mode: c++ -*- * kaleidoscope::device::dygma::Raise -- Kaleidoscope device plugin for Dygma Raise - * Copyright (C) 2017-2019 Keyboard.io, Inc - * Copyright (C) 2017-2019 Dygma Lab S.L. + * Copyright (C) 2017-2020 Keyboard.io, Inc + * Copyright (C) 2017-2020 Dygma Lab S.L. * * 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 @@ -61,6 +61,8 @@ class RaiseLEDDriver : public kaleidoscope::driver::led::Base brightness_adjustment_) + c -= brightness_adjustment_; + else + c = 0; + + data[i + 1] = pgm_read_byte(&gamma8[c]); } uint8_t result = twi_.writeTo(data, ELEMENTS(data)); } diff --git a/src/kaleidoscope/device/dygma/raise/Hand.h b/src/kaleidoscope/device/dygma/raise/Hand.h index 543d89ab..bc2e3a5b 100644 --- a/src/kaleidoscope/device/dygma/raise/Hand.h +++ b/src/kaleidoscope/device/dygma/raise/Hand.h @@ -81,10 +81,18 @@ class Hand { return twi_.crc_errors(); } + void setBrightness(uint8_t brightness) { + brightness_adjustment_ = 255 - brightness; + } + uint8_t getBrightness() { + return 255 - brightness_adjustment_; + } + LEDData_t led_data; bool online = false; private: + uint8_t brightness_adjustment_ = 0; int ad01_; TWI twi_; keydata_t key_data_; diff --git a/src/kaleidoscope/device/keyboardio/Imago.cpp b/src/kaleidoscope/device/keyboardio/Imago.cpp index 8721016f..e5e965bd 100644 --- a/src/kaleidoscope/device/keyboardio/Imago.cpp +++ b/src/kaleidoscope/device/keyboardio/Imago.cpp @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-Hardware-Keyboardio-Imago -- Imago hardware support for Kaleidoscope - * Copyright (C) 2018, 2019 Keyboard.io, Inc + * Copyright (C) 2018, 2019, 2020 Keyboard.io, Inc * * This program is free software: you can redistribute it and/or modify * it under the terms of version 3 of the GNU General Public License as @@ -98,6 +98,15 @@ cRGB ImagoLEDDriver::getCrgbAt(uint8_t i) { return led_data[i]; } +uint8_t ImagoLEDDriver::adjustBrightness(uint8_t value) { + if (value > brightness_adjustment_) + value -= brightness_adjustment; + else + value = 0; + + return value; +} + void ImagoLEDDriver::syncLeds() { // if (!isLEDChanged) // return; @@ -110,9 +119,9 @@ void ImagoLEDDriver::syncLeds() { selectRegister(LED_REGISTER_DATA0); for (auto i = 1; i < LED_REGISTER_DATA0_SIZE; i += 3) { - data[i] = led_data[last_led].b; - data[i + 1] = led_data[last_led].g; - data[i + 2] = led_data[last_led].r; + data[i] = adjustBrightness(led_data[last_led].b); + data[i + 1] = adjustBrightness(led_data[last_led].g); + data[i + 2] = adjustBrightness(led_data[last_led].r); last_led++; } @@ -130,9 +139,9 @@ void ImagoLEDDriver::syncLeds() { selectRegister(LED_REGISTER_DATA1); for (auto i = 1; i < LED_REGISTER_DATA1_SIZE; i += 3) { - data[i] = led_data[last_led].b; - data[i + 1] = led_data[last_led].g; - data[i + 2] = led_data[last_led].r; + data[i] = adjustBrightness(led_data[last_led].b); + data[i + 1] = adjustBrightness(led_data[last_led].g); + data[i + 2] = adjustBrightness(led_data[last_led].r); last_led++; } diff --git a/src/kaleidoscope/device/keyboardio/Imago.h b/src/kaleidoscope/device/keyboardio/Imago.h index b6801ede..ccf45943 100644 --- a/src/kaleidoscope/device/keyboardio/Imago.h +++ b/src/kaleidoscope/device/keyboardio/Imago.h @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-Hardware-Keyboardio-Imago -- Imago hardware support for Kaleidoscope - * Copyright (C) 2018, 2019 Keyboard.io, Inc + * Copyright (C) 2018, 2019, 2020 Keyboard.io, Inc * * This program is free software: you can redistribute it and/or modify * it under the terms of version 3 of the GNU General Public License as @@ -58,12 +58,22 @@ class ImagoLEDDriver : public kaleidoscope::driver::led::Base