Pikatea GB4 RGB profiles

Using users/custom keycodes to make 3 RGB profiles that can be switched between in the Vial UI
 avatar
annamaniacs
c_cpp
3 years ago
4.7 kB
7
Indexable
/*
    File: vial.json (existing)
    I added this between "productId" and "lighting"
*/
// names and descriptions in vial UI
    "customKeycodes": [
      {"name": "Default",
       "title": "Default RGB profile: Layer0 green, Layer1 purple, Layer2 white",
       "shortName": "RGB_USER_DEFAULT"
      },
      {"name": "Sunlight",
       "title": "RGB profile: Layer0 yellow, Layer1 goldenrod, Layer2 orange",
       "shortname": "RGB_USER_1"
      },
      {"name": "Blues",
       "title": "RGB profile: Layer0 azure, Layer1 sky blue, Layer2 dark blue",
       "shortName": "RGB_USER_2"
      }
    ],

/*
    New file: rgb_profiles.h
    to keep things a little cleaner
*/
#pragma once

#define HSV_GREEN_CUSTOM 106, 255, 255
#define HSV_PURPLE_CUSTOM   198, 255, 255
#define HSV_BLUE_CUSTOM   140, 255, 255
#define DEFAULT_COLOR HSV_GREEN_CUSTOM

const rgblight_segment_t PROGMEM capslock_layer[] = RGBLIGHT_LAYER_SEGMENTS(
    {0, 5, HSV_RED}      
);

// Default RGB profile
const rgblight_segment_t PROGMEM default_layer0[] = RGBLIGHT_LAYER_SEGMENTS(
    {0, 5, HSV_GREEN_CUSTOM}
);
const rgblight_segment_t PROGMEM default_layer1[] = RGBLIGHT_LAYER_SEGMENTS(
    {0, 5, HSV_PURPLE_CUSTOM}
);
const rgblight_segment_t PROGMEM default_layer2[] = RGBLIGHT_LAYER_SEGMENTS(
    {0, 5, HSV_WHITE}
);

// User 1 RGB profile
const rgblight_segment_t PROGMEM user1_layer0[] = RGBLIGHT_LAYER_SEGMENTS(
    {0, 5, HSV_YELLOW}
);
const rgblight_segment_t PROGMEM user1_layer1[] = RGBLIGHT_LAYER_SEGMENTS(
    {0, 5, HSV_GOLDENROD}
);
const rgblight_segment_t PROGMEM user1_layer2[] = RGBLIGHT_LAYER_SEGMENTS(
    {0, 5, HSV_ORANGE}
);

// User 2 RGB profile
const rgblight_segment_t PROGMEM user2_layer0[] = RGBLIGHT_LAYER_SEGMENTS(
    {0, 5, HSV_CYAN}
);
const rgblight_segment_t PROGMEM user2_layer1[] = RGBLIGHT_LAYER_SEGMENTS(
    {0, 5, HSV_BLUE_CUSTOM}
);
const rgblight_segment_t PROGMEM user2_layer2[] = RGBLIGHT_LAYER_SEGMENTS(
    {0, 5, HSV_BLUE}
);

// Default RGB profile
const rgblight_segment_t* const PROGMEM default_rgb_layers[] = RGBLIGHT_LAYERS_LIST(
    default_layer0,
    default_layer1,    
    default_layer2,
    capslock_layer  
);

// RGB profile user1
const rgblight_segment_t* const PROGMEM user1_rgb_layers[] = RGBLIGHT_LAYERS_LIST(
    user1_layer0,
    user1_layer1,    
    user1_layer2,
    capslock_layer  
);

// RGB profile user2
const rgblight_segment_t* const PROGMEM user2_rgb_layers[] = RGBLIGHT_LAYERS_LIST(
    user2_layer0,
    user2_layer1,    
    user2_layer2,
    capslock_layer  
);

/*
    File: keymap.c (existing)
*/

#include "rgb_profiles.h"

// RGB profile list
enum blender_keycode {
    RGB_USER_DEFAULT = USER00,  
    RGB_USER_1, 
    RGB_USER_2
};

// initial setup stuff
typedef union {
  uint32_t raw;
  struct {
    bool     rgb_layer_change: 1;
  };
} user_config_t;

user_config_t user_config;

// set initial color
void eeconfig_init_user(void) {  // EEPROM is getting reset!
    user_config.raw = 0;
    user_config.rgb_layer_change = true; // We want this enabled by default
    eeconfig_update_user(user_config.raw); // Write default value to EEPROM now

    // use the non noeeprom versions, to write these values to EEPROM too
    rgblight_enable(); 
    rgblight_setrgb(DEFAULT_COLOR); 
}

void keyboard_post_init_user(void) {
    // Enable the LED layers and set defaults
    rgblight_layers = default_rgb_layers;
    default_layer_set(0);
    layer_state_set_user(0);
}

// color change on layer change
layer_state_t layer_state_set_user(layer_state_t state) {
    rgblight_set_layer_state(1, layer_state_cmp(state, 1));
    rgblight_set_layer_state(2, layer_state_cmp(state, 2));
    rgblight_set_layer_state(0, layer_state_cmp(state, 0));
    
    return state;
}

// color change on caps lock
bool led_update_user(led_t led_state) {
    rgblight_set_layer_state(3, led_state.caps_lock);
    return true;
}

// change RGB profile
bool process_record_user(uint16_t keycode, keyrecord_t *record) {
	switch (keycode) {
		case RGB_USER_DEFAULT:
            if (record->event.pressed) { 
                rgblight_layers = default_rgb_layers;
                default_layer_set(0);
                layer_state_set_user(0);
            }
            return 0;
        case RGB_USER_1:
            if (record->event.pressed) {
                rgblight_layers = user1_rgb_layers;
                default_layer_set(0);
                layer_state_set_user(0);
            }
            return 0;
        case RGB_USER_2:
            if (record->event.pressed) {
                rgblight_layers = user2_rgb_layers;
                default_layer_set(0);
                layer_state_set_user(0);
            }
            return 0;
        default:
            return true;
    }
}
Editor is loading...