From 956c974aed187f172b38f5f39ec4cd91fa18fabd Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Tue, 26 Jan 2016 14:52:28 -0800 Subject: [PATCH 1/5] Trivial first library wrapper for the storage functions --- KeyboardioFirmware.h | 1 + KeyboardioFirmware.ino | 10 ++++++---- storage.cpp | 6 ++++-- storage.h | 7 +++++-- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/KeyboardioFirmware.h b/KeyboardioFirmware.h index ddec1e6d..d81c74a0 100644 --- a/KeyboardioFirmware.h +++ b/KeyboardioFirmware.h @@ -30,6 +30,7 @@ void setup(); #include "debouncing.h" #include "KeyboardioSX1509.h" #include "mouse_movement.h" +#include "storage.h" //extern uint8_t usbMaxPower; char x; diff --git a/KeyboardioFirmware.ino b/KeyboardioFirmware.ino index f2831e25..058d5d07 100644 --- a/KeyboardioFirmware.ino +++ b/KeyboardioFirmware.ino @@ -5,6 +5,8 @@ #include "KeyboardioFirmware.h" #include "HID-Project.h" + +KeyboardStorage Storage; void set_keymap(Key keymapEntry, byte matrixStateEntry) { if (keymapEntry.flags & SWITCH_TO_KEYMAP) { @@ -27,7 +29,7 @@ void set_keymap(Key keymapEntry, byte matrixStateEntry) { // switch keymap and stay there if (key_toggled_on(matrixStateEntry)) { temporary_keymap = primary_keymap = keymapEntry.rawKey; - save_primary_keymap(primary_keymap); + Storage.save_primary_keymap(primary_keymap); } } } @@ -68,13 +70,14 @@ void setup() { wdt_disable(); Serial.begin(115200); //usbMaxPower = 100; + Keyboard.begin(); Mouse.begin(); implementation_setup_leds(); led_bootup(); implementation_pins_setup(); - temporary_keymap = primary_keymap = load_primary_keymap(); + temporary_keymap = primary_keymap = Storage.load_primary_keymap(); } @@ -103,8 +106,7 @@ void handle_synthetic_key_press(byte switchState, Key mappedKey) { if (key_toggled_on (switchState)) { ConsumerControl.press(mappedKey.rawKey); } - } - else if (mappedKey.flags & IS_INTERNAL) { + } else if (mappedKey.flags & IS_INTERNAL) { if (key_toggled_on (switchState)) { if (mappedKey.rawKey == LED_TOGGLE) { next_led_mode(); diff --git a/storage.cpp b/storage.cpp index 32c354c9..9781be9b 100644 --- a/storage.cpp +++ b/storage.cpp @@ -1,10 +1,12 @@ #include "storage.h" -void save_primary_keymap(byte keymap) { + + +void KeyboardStorage::save_primary_keymap(byte keymap) { EEPROM.write(EEPROM_KEYMAP_LOCATION, keymap); } -byte load_primary_keymap() { +byte KeyboardStorage::load_primary_keymap() { byte keymap = EEPROM.read(EEPROM_KEYMAP_LOCATION); if (keymap >= KEYMAPS ) { return 0; // undefined positions get saved as 255 diff --git a/storage.h b/storage.h index a6db42fe..b3e77b1c 100644 --- a/storage.h +++ b/storage.h @@ -2,5 +2,8 @@ #include #include "KeyboardConfig.h" -void save_primary_keymap(byte keymap); -byte load_primary_keymap(); +class KeyboardStorage { + public: + byte load_primary_keymap(); + void save_primary_keymap(byte keymap); +}; From a134c07a83df8469178c54565e8124c8b467c152 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Tue, 26 Jan 2016 15:03:12 -0800 Subject: [PATCH 2/5] data type modernization --- KeyboardioFirmware.h | 8 ++------ storage.cpp | 6 +++--- storage.h | 4 ++-- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/KeyboardioFirmware.h b/KeyboardioFirmware.h index d81c74a0..1ca31709 100644 --- a/KeyboardioFirmware.h +++ b/KeyboardioFirmware.h @@ -39,15 +39,11 @@ char y; uint8_t matrixState[ROWS][COLS] = {0}; static const Key keymaps[KEYMAPS][ROWS][COLS] = { KEYMAP_LIST }; -byte primary_keymap = 0; -byte temporary_keymap = 0; - - - - // EEPROM related void save_primary_keymap(byte keymap); byte load_primary_keymap(); +uint8_t primary_keymap = 0; +uint8_t temporary_keymap = 0; diff --git a/storage.cpp b/storage.cpp index 9781be9b..177717f1 100644 --- a/storage.cpp +++ b/storage.cpp @@ -2,12 +2,12 @@ -void KeyboardStorage::save_primary_keymap(byte keymap) { +void KeyboardStorage::save_primary_keymap(uint8_t keymap) { EEPROM.write(EEPROM_KEYMAP_LOCATION, keymap); } -byte KeyboardStorage::load_primary_keymap() { - byte keymap = EEPROM.read(EEPROM_KEYMAP_LOCATION); +uint8_t KeyboardStorage::load_primary_keymap() { + uint8_t keymap = EEPROM.read(EEPROM_KEYMAP_LOCATION); if (keymap >= KEYMAPS ) { return 0; // undefined positions get saved as 255 } diff --git a/storage.h b/storage.h index b3e77b1c..9d4abc40 100644 --- a/storage.h +++ b/storage.h @@ -4,6 +4,6 @@ class KeyboardStorage { public: - byte load_primary_keymap(); - void save_primary_keymap(byte keymap); + uint8_t load_primary_keymap(); + void save_primary_keymap(uint8_t keymap); }; From 7bc2c42cfe7332910c3d6a6516b2f28ad8c75432 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Tue, 26 Jan 2016 15:03:20 -0800 Subject: [PATCH 3/5] remove function definitions that moved elsewhere --- KeyboardioFirmware.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/KeyboardioFirmware.h b/KeyboardioFirmware.h index 1ca31709..2b85f140 100644 --- a/KeyboardioFirmware.h +++ b/KeyboardioFirmware.h @@ -39,9 +39,6 @@ char y; uint8_t matrixState[ROWS][COLS] = {0}; static const Key keymaps[KEYMAPS][ROWS][COLS] = { KEYMAP_LIST }; -// EEPROM related -void save_primary_keymap(byte keymap); -byte load_primary_keymap(); uint8_t primary_keymap = 0; uint8_t temporary_keymap = 0; From 16411878aa0db979c069bce242dc8c95aeffdb55 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Tue, 26 Jan 2016 16:37:55 -0800 Subject: [PATCH 4/5] First pass of making led_control into a class --- KeyboardioFirmware.ino | 8 +++++--- led_control.cpp | 41 +++++++++++++++++++++-------------------- led_control.h | 19 ++++++++++++------- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/KeyboardioFirmware.ino b/KeyboardioFirmware.ino index 058d5d07..e03ddd90 100644 --- a/KeyboardioFirmware.ino +++ b/KeyboardioFirmware.ino @@ -7,6 +7,8 @@ #include "HID-Project.h" KeyboardStorage Storage; +LEDControl BlinkyLights; + void set_keymap(Key keymapEntry, byte matrixStateEntry) { if (keymapEntry.flags & SWITCH_TO_KEYMAP) { @@ -74,7 +76,7 @@ void setup() { Keyboard.begin(); Mouse.begin(); implementation_setup_leds(); - led_bootup(); + BlinkyLights.led_bootup(); implementation_pins_setup(); temporary_keymap = primary_keymap = Storage.load_primary_keymap(); @@ -86,7 +88,7 @@ void loop() { TS("about to scan the matrix") scan_matrix(); TS("updating LEDs"); - update_leds(temporary_keymap == NUMPAD_KEYMAP); + BlinkyLights.update_leds(temporary_keymap == NUMPAD_KEYMAP); } @@ -109,7 +111,7 @@ void handle_synthetic_key_press(byte switchState, Key mappedKey) { } else if (mappedKey.flags & IS_INTERNAL) { if (key_toggled_on (switchState)) { if (mappedKey.rawKey == LED_TOGGLE) { - next_led_mode(); + BlinkyLights.next_led_mode(); } } } else if (mappedKey.flags & IS_SYSCTL) { diff --git a/led_control.cpp b/led_control.cpp index ab1c8e0c..2f2bdceb 100644 --- a/led_control.cpp +++ b/led_control.cpp @@ -33,17 +33,17 @@ static uint8_t current_chase_counter = 0; // End RGB stuff -void set_key_color(byte row, byte col, cRGB color) { +void LEDControl::set_key_color(byte row, byte col, cRGB color) { implementation_led_set_crgb_at(row, col, color); } -cRGB get_key_color(byte row, byte col) { +cRGB LEDControl::get_key_color(byte row, byte col) { return implementation_get_key_color(row, col); } -void initialize_led_mode(uint8_t mode) { +void LEDControl::initialize_led_mode(uint8_t mode) { set_all_leds_to(led_off); if (mode == LED_MODE_OFF) { // set_all_leds_to(led_off); @@ -58,27 +58,27 @@ void initialize_led_mode(uint8_t mode) { } } -void set_all_leds_to(cRGB color) { +void LEDControl::set_all_leds_to(cRGB color) { for (uint8_t i = 0; i < LED_COUNT; i++) { implementation_led_set_crgb_at(i, color); } } -void next_led_mode() { +void LEDControl::next_led_mode() { if (led_mode++ >= LED_MODES) { led_mode = 0; } } -void set_led_mode(uint8_t mode) { +void LEDControl::set_led_mode(uint8_t mode) { led_mode = mode; } -void update_leds(uint8_t numlock_enabled) { +void LEDControl::update_leds(uint8_t numlock_enabled) { if (numlock_enabled) { if (led_mode != LED_SPECIAL_MODE_NUMLOCK) { stored_led_mode = led_mode; @@ -117,7 +117,7 @@ void update_leds(uint8_t numlock_enabled) { -void led_effect_numlock_update() { +void LEDControl::led_effect_numlock_update() { for (uint8_t i = 0; i < 44; i++) { implementation_led_set_crgb_at(i, led_off); } @@ -129,11 +129,11 @@ void led_effect_numlock_update() { implementation_led_sync(); } -void led_effect_steady_update() { +void LEDControl::led_effect_steady_update() { implementation_led_sync(); } -void led_compute_breath() { +void LEDControl::led_compute_breath() { // algorithm from http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/ breathe_brightness = (exp(sin(millis()/2000.0*PI)) - 0.36787944)*108.0; // change the brightness for next time through the loop: @@ -148,13 +148,13 @@ void led_compute_breath() { SetHSV(led_breathe,200, 255, breathe_brightness); } -void led_effect_breathe_update() { +void LEDControl::led_effect_breathe_update() { led_compute_breath(); set_all_leds_to(led_breathe); implementation_led_sync(); } -void led_effect_chase_update() { +void LEDControl::led_effect_chase_update() { if (current_chase_counter++ < chase_threshold) { return; } @@ -171,7 +171,7 @@ void led_effect_chase_update() { implementation_led_sync(); } -void led_effect_rainbow_update() { +void LEDControl::led_effect_rainbow_update() { if (rainbow_current_ticks++ < rainbow_ticks) { return; } else { @@ -186,7 +186,7 @@ void led_effect_rainbow_update() { implementation_led_sync(); } -void led_effect_rainbow_wave_update() { +void LEDControl::led_effect_rainbow_wave_update() { if (rainbow_current_ticks++ < rainbow_wave_ticks) { return; } else { @@ -208,7 +208,7 @@ void led_effect_rainbow_wave_update() { implementation_led_sync(); } -void led_bootup() { +void LEDControl::led_bootup() { set_all_leds_to(led_off); led_type_letter(LED_K); @@ -230,7 +230,7 @@ void led_bootup() { } -void led_type_letter(uint8_t letter) { +void LEDControl::led_type_letter(uint8_t letter) { implementation_led_set_crgb_at(letter,led_bright_red); implementation_led_sync(); delay(250); @@ -240,11 +240,12 @@ void led_type_letter(uint8_t letter) { } - -void SetHSV(cRGB crgb, int hue, byte sat, byte val) { +/* SetHSV from light_ws2812. Their source was: + * getRGB() function based on + * dim_curve idea by Jims + * */ +void LEDControl::SetHSV(cRGB crgb, int hue, byte sat, byte val) { /* convert hue, saturation and brightness ( HSB/HSV ) to RGB - The dim_curve is used only on brightness/value and on saturation (inverted). - This looks the most natural. */ int base; diff --git a/led_control.h b/led_control.h index e69e5701..99ca063c 100644 --- a/led_control.h +++ b/led_control.h @@ -15,21 +15,24 @@ #define LED_SPECIAL_MODE_NUMLOCK 100 -void update_leds(uint8_t numlock_enabled); -void set_all_leds_to(cRGB color); +class LEDControl { + public: + void next_led_mode(); + void led_bootup(); + void update_leds(uint8_t numlock_enabled); + void led_type_letter(uint8_t letter); void set_led_mode(uint8_t mode); -void next_led_mode(); + private: void set_key_color(uint8_t row, uint8_t col, cRGB color); cRGB get_key_color(uint8_t row, uint8_t col); - void led_compute_breath(); void led_effect_breathe_init(); void led_effect_rainbow_init(); void led_effect_chase_init(); void led_effect_steady_init(); -void led_Effect_heatmap_init(); +void led_effect_heatmap_init(); void led_effect_breathe_update(); void led_effect_rainbow_update(); @@ -38,7 +41,9 @@ void led_effect_chase_update(); void led_effect_steady_update(); void led_effect_heatmap_update(); void led_effect_numlock_update(); -void led_bootup(); -void led_type_letter(uint8_t letter); +void set_all_leds_to(cRGB color); void SetHSV(cRGB crgb, int hue, byte sat, byte val); +void initialize_led_mode(uint8_t mode); +}; + From 0ae7e1b0fdffe41db6c736b4f2f15a71da04ff9b Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Tue, 26 Jan 2016 17:05:02 -0800 Subject: [PATCH 5/5] First pass at bundling the LED code into its own class --- led_control.cpp | 41 ++++++----------------- led_control.h | 86 +++++++++++++++++++++++++++++++++---------------- 2 files changed, 68 insertions(+), 59 deletions(-) diff --git a/led_control.cpp b/led_control.cpp index 2f2bdceb..25bb545a 100644 --- a/led_control.cpp +++ b/led_control.cpp @@ -1,37 +1,14 @@ #include "led_control.h" -static uint8_t led_mode; -static uint8_t last_led_mode; -static uint8_t stored_led_mode; -static uint8_t pos = 0; - -static cRGB led_off = { .r = 0, .g = 0, .b = 0 }; -static cRGB led_steady = { .r = 0, .g = 255, .b = 0}; -static cRGB led_blue = { .r = 0, .g = 0, .b = 255 }; -static cRGB led_dark_blue = { .r = 0, .g = 0, .b = 127 }; -static cRGB led_bright_red = { .r = 255, .g = 0, .b = 0}; -static cRGB led_breathe; -static cRGB rainbow; - - -static uint8_t rainbow_hue = 0; //stores 0 to 614 -static uint8_t rainbow_steps = 1; //number of hues we skip in a 360 range per update -static uint8_t rainbow_wave_steps =1; //number of hues we skip in a 360 range per update - -static byte rainbow_saturation = 255; -static byte rainbow_value = 190; - -static long rainbow_wave_ticks = 1; //delays between update -static long rainbow_ticks = 5; //delays between update -static long rainbow_current_ticks =0; -static uint8_t breathe_brightness = 0; // how bright the LED is -static uint8_t breathe_fadeAmount = 1; // how many pouint8_ts to fade the LED by - -static uint8_t chase_pixels = 1; -static uint8_t chase_threshold = 6; -static uint8_t current_chase_counter = 0; -// End RGB stuff - +uint8_t LEDControl::pos= 0; +uint8_t LEDControl::rainbow_hue=0; +uint8_t LEDControl::rainbow_steps = 1; +uint8_t LEDControl::rainbow_wave_steps=1; +long LEDControl::rainbow_current_ticks =0; +uint8_t LEDControl::breathe_brightness=0; +uint8_t LEDControl::breathe_fadeAmount=1; +uint8_t LEDControl::chase_pixels= 1; +uint8_t LEDControl::current_chase_counter = 0; void LEDControl::set_key_color(byte row, byte col, cRGB color) { implementation_led_set_crgb_at(row, col, color); diff --git a/led_control.h b/led_control.h index 99ca063c..f6ebaf71 100644 --- a/led_control.h +++ b/led_control.h @@ -17,33 +17,65 @@ class LEDControl { - public: - void next_led_mode(); - void led_bootup(); - void update_leds(uint8_t numlock_enabled); - void led_type_letter(uint8_t letter); -void set_led_mode(uint8_t mode); - - private: -void set_key_color(uint8_t row, uint8_t col, cRGB color); -cRGB get_key_color(uint8_t row, uint8_t col); -void led_compute_breath(); -void led_effect_breathe_init(); -void led_effect_rainbow_init(); -void led_effect_chase_init(); -void led_effect_steady_init(); -void led_effect_heatmap_init(); - -void led_effect_breathe_update(); -void led_effect_rainbow_update(); -void led_effect_rainbow_wave_update(); -void led_effect_chase_update(); -void led_effect_steady_update(); -void led_effect_heatmap_update(); -void led_effect_numlock_update(); -void set_all_leds_to(cRGB color); -void SetHSV(cRGB crgb, int hue, byte sat, byte val); -void initialize_led_mode(uint8_t mode); + public: + void next_led_mode(); + void led_bootup(); + void update_leds(uint8_t numlock_enabled); + void led_type_letter(uint8_t letter); + void set_led_mode(uint8_t mode); + + private: + uint8_t led_mode; + uint8_t last_led_mode; + uint8_t stored_led_mode; + static uint8_t pos; + + cRGB led_off = { .r = 0, .g = 0, .b = 0 }; + cRGB led_steady = { .r = 0, .g = 255, .b = 0}; + cRGB led_blue = { .r = 0, .g = 0, .b = 255 }; + cRGB led_dark_blue = { .r = 0, .g = 0, .b = 127 }; + cRGB led_bright_red = { .r = 255, .g = 0, .b = 0}; + cRGB led_breathe; + cRGB rainbow; + + + static uint8_t rainbow_hue; //stores 0 to 614 + + static uint8_t rainbow_steps; //number of hues we skip in a 360 range per update + static uint8_t rainbow_wave_steps; //number of hues we skip in a 360 range per update + + static const byte rainbow_saturation = 255; + static const byte rainbow_value = 190; + + static const long rainbow_wave_ticks = 1; //delays between update + static const long rainbow_ticks = 5; //delays between update + static long rainbow_current_ticks; + + static uint8_t breathe_brightness; // how bright the LED is + static uint8_t breathe_fadeAmount; // how many points to fade the LED by + static uint8_t chase_pixels; + static uint8_t current_chase_counter; + static const uint8_t chase_threshold = 6; +// End RGB stuff + void set_key_color(uint8_t row, uint8_t col, cRGB color); + cRGB get_key_color(uint8_t row, uint8_t col); + void led_compute_breath(); + void led_effect_breathe_init(); + void led_effect_rainbow_init(); + void led_effect_chase_init(); + void led_effect_steady_init(); + void led_effect_heatmap_init(); + + void led_effect_breathe_update(); + void led_effect_rainbow_update(); + void led_effect_rainbow_wave_update(); + void led_effect_chase_update(); + void led_effect_steady_update(); + void led_effect_heatmap_update(); + void led_effect_numlock_update(); + void set_all_leds_to(cRGB color); + void SetHSV(cRGB crgb, int hue, byte sat, byte val); + void initialize_led_mode(uint8_t mode); };