Skip to content

Instantly share code, notes, and snippets.

@Steve973
Created October 22, 2024 21:30
Show Gist options
  • Save Steve973/8bcd052bd5e49baa71caec65a787380c to your computer and use it in GitHub Desktop.
Save Steve973/8bcd052bd5e49baa71caec65a787380c to your computer and use it in GitHub Desktop.
int8_t calculate_direction(bool rotate) {
vikstik_coordinate_t coordinates = read_vikstik_raw();
int8_t direction = calculate_raw_direction(coordinates);
if (rotate && direction > -1) {
switch (vikstik_config.up_orientation) {
case JS_LEFT:
direction = (direction + 3) % ORIENTATION_COUNT;
break;
case JS_DOWN:
direction = (direction + 2) % ORIENTATION_COUNT;
break;
case JS_RIGHT:
direction = (direction + 1) % ORIENTATION_COUNT;
break;
case JS_UP:
default:
break;
}
}
return direction;
}
/**
* @brief Handles actions for the LOWER layer based on the direction.
*
* This function performs specific actions when the active layer is LOWER.
* The actions are determined by the direction provided as input.
*
* @param direction The direction of the joystick movement. Expected values are:
* - UP: Perform the action associated with the UP direction.
* - DOWN: Perform the action associated with the DOWN direction.
* - RIGHT: Perform the action associated with the RIGHT direction.
* - LEFT: Perform the action associated with the LEFT direction.
*/
static void handle_rgb_step_and_val(int8_t direction) {
switch (direction) {
case JS_UP:
fp_rgblight_step();
break;
case JS_DOWN:
fp_rgblight_step_reverse();
break;
case JS_RIGHT:
fp_rgblight_increase_val();
break;
case JS_LEFT:
fp_rgblight_decrease_val();
break;
default:
break;
}
}
/**
* @brief Handles actions for the RAISE layer based on the direction.
*
* This function performs specific actions when the active layer is RAISE.
* The actions are determined by the direction provided as input.
*
* @param direction The direction of the joystick direction. Expected values are:
* - UP: Perform the action associated with the UP direction.
* - DOWN: Perform the action associated with the DOWN direction.
* - RIGHT: Perform the action associated with the RIGHT direction.
* - LEFT: Perform the action associated with the LEFT direction.
*/
static void handle_rgb_sat_and_hue(int8_t direction) {
switch (direction) {
case JS_UP:
fp_rgblight_increase_sat();
break;
case JS_DOWN:
fp_rgblight_decrease_sat();
break;
case JS_RIGHT:
fp_rgblight_increase_hue();
break;
case JS_LEFT:
fp_rgblight_decrease_hue();
break;
default:
break;
}
}
/**
* @brief Handles actions for the ADJUST layer based on the direction.
*
* This function performs specific actions when the active layer is ADJUST.
* The actions are determined by the direction provided as input.
*
* @param direction The direction of the joystick direction. Expected values are:
* - UP: Perform the action associated with the UP direction.
* - DOWN: Perform the action associated with the DOWN direction.
* - RIGHT: Perform the action associated with the RIGHT direction.
* - LEFT: Perform the action associated with the LEFT direction.
*/
static void handle_rgb_toggle_and_speed(int8_t direction) {
switch (direction) {
case JS_UP:
rgb_matrix_enable();
break;
case JS_DOWN:
rgb_matrix_disable();
break;
case JS_RIGHT:
rgb_matrix_increase_speed();
break;
case JS_LEFT:
rgb_matrix_decrease_speed();
break;
default:
break;
}
}
/**
* @brief Validate that the rgb layers instance is defined, and valid.
*
* This method verifies that the layers instance is not null, and that each
* designated layer is associated with a unique layer number.
*/
static bool validate_rgb_layer_def(js_rgb_layer_t* layers) {
return layers != NULL &&
layers->rgb_step_and_val_layer != layers->rgb_sat_and_hue_layer &&
layers->rgb_step_and_val_layer != layers->rgb_toggle_and_speed_layer &&
layers->rgb_sat_and_hue_layer != layers->rgb_toggle_and_speed_layer;
}
/**
* @brief Handles the joystick input and processes it according to the current mode.
*
* This function reads the joystick input, processes it based on the current mode,
* and updates the joystick axes accordingly.
*/
static void handle_vikstik(void) {
int8_t layer = get_highest_layer(layer_state);
bool layer_def_valid = validate_rgb_layer_def(js_rgb_layers);
if (layer == js_rgb_layers->rgb_step_and_val_layer && layer_def_valid) {
handle_rgb_step_and_val(calculate_direction(true));
} else if (layer == js_rgb_layers->rgb_sat_and_hue_layer && layer_def_valid) {
handle_rgb_sat_and_hue(calculate_direction(true));
} else if (layer == js_rgb_layers->rgb_toggle_and_speed_layer && layer_def_valid) {
handle_rgb_toggle_and_speed(calculate_direction(true));
} else if (vikstik_config.mode < VIKSTIK_SM_END) {
vikstik_coordinate_t coordinates = read_vikstik();
stick_modes[vikstik_config.mode](coordinates.x_coordinate, coordinates.y_coordinate);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment