2021-07-09 01:27:26 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2021-07-09 04:38:45 +02:00
|
|
|
/* Based on https://github.com/qmk/qmk_firmware/pull/13036 */
|
2021-07-09 01:27:26 +02:00
|
|
|
|
|
|
|
#include "vialrgb.h"
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
2021-07-09 04:38:45 +02:00
|
|
|
#include <string.h>
|
2021-07-09 01:27:26 +02:00
|
|
|
#include "rgb_matrix.h"
|
|
|
|
#include "vial.h"
|
|
|
|
|
2021-07-09 04:38:45 +02:00
|
|
|
typedef struct {
|
|
|
|
uint16_t vialrgb_id;
|
|
|
|
uint16_t qmk_id;
|
|
|
|
} vialrgb_supported_mode_t;
|
|
|
|
|
2021-07-09 23:54:33 +02:00
|
|
|
#include "vialrgb_effects.inc"
|
2021-07-09 04:38:45 +02:00
|
|
|
|
|
|
|
#define SUPPORTED_MODES_LENGTH (sizeof(supported_modes)/sizeof(*supported_modes))
|
|
|
|
|
|
|
|
static void get_supported(uint8_t *args, uint8_t length) {
|
|
|
|
/* retrieve supported effects (VialRGB IDs) with ID > gt */
|
|
|
|
uint16_t gt;
|
|
|
|
memcpy(>, args, sizeof(gt));
|
|
|
|
memset(args, 0xFF, length);
|
|
|
|
for (size_t i = 0; i < SUPPORTED_MODES_LENGTH; ++i) {
|
|
|
|
uint16_t id = pgm_read_word(&supported_modes[i].vialrgb_id);
|
|
|
|
if (id > gt && length >= sizeof(id)) {
|
|
|
|
memcpy(args, &id, sizeof(id));
|
|
|
|
length -= sizeof(id);
|
|
|
|
args += sizeof(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t qmk_id_to_vialrgb_id(uint16_t id) {
|
|
|
|
for (size_t i = 0; i < SUPPORTED_MODES_LENGTH; ++i) {
|
|
|
|
uint16_t qmk_id = pgm_read_word(&supported_modes[i].qmk_id);
|
|
|
|
uint16_t vialrgb_id = pgm_read_word(&supported_modes[i].vialrgb_id);
|
|
|
|
if (qmk_id == id)
|
|
|
|
return vialrgb_id;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t vialrgb_id_to_qmk_id(uint16_t id) {
|
|
|
|
for (size_t i = 0; i < SUPPORTED_MODES_LENGTH; ++i) {
|
|
|
|
uint16_t qmk_id = pgm_read_word(&supported_modes[i].qmk_id);
|
|
|
|
uint16_t vialrgb_id = pgm_read_word(&supported_modes[i].vialrgb_id);
|
|
|
|
if (vialrgb_id == id)
|
|
|
|
return qmk_id;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2021-07-09 01:27:26 +02:00
|
|
|
|
|
|
|
void vialrgb_get_value(uint8_t *data, uint8_t length) {
|
|
|
|
if (length != VIAL_RAW_EPSIZE) return;
|
|
|
|
|
|
|
|
/* data[0] is used by VIA command id */
|
|
|
|
uint8_t cmd = data[1];
|
|
|
|
uint8_t *args = &data[2];
|
|
|
|
switch (cmd) {
|
2021-07-09 02:20:27 +02:00
|
|
|
case vialrgb_get_info:
|
|
|
|
args[0] = VIALRGB_PROTOCOL_VERSION & 0xFF;
|
|
|
|
args[1] = VIALRGB_PROTOCOL_VERSION >> 8;
|
|
|
|
args[2] = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
|
|
|
|
break;
|
2021-07-09 01:27:26 +02:00
|
|
|
case vialrgb_get_mode: {
|
2021-07-09 04:38:45 +02:00
|
|
|
args[0] = qmk_id_to_vialrgb_id(rgb_matrix_get_mode());
|
2021-07-09 01:27:26 +02:00
|
|
|
args[1] = rgb_matrix_get_speed();
|
|
|
|
args[2] = rgb_matrix_get_hue();
|
|
|
|
args[3] = rgb_matrix_get_sat();
|
|
|
|
args[4] = rgb_matrix_get_val();
|
|
|
|
break;
|
|
|
|
}
|
2021-07-09 04:38:45 +02:00
|
|
|
case vialrgb_get_supported: {
|
|
|
|
get_supported(args, length - 2);
|
|
|
|
break;
|
|
|
|
}
|
2021-07-09 01:27:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void vialrgb_set_value(uint8_t *data, uint8_t length) {
|
|
|
|
if (length != VIAL_RAW_EPSIZE) return;
|
|
|
|
|
|
|
|
/* data[0] is used by VIA command id */
|
|
|
|
uint8_t cmd = data[1];
|
|
|
|
uint8_t *args = &data[2];
|
|
|
|
switch (cmd) {
|
|
|
|
case vialrgb_set_mode: {
|
2021-07-09 04:38:45 +02:00
|
|
|
rgb_matrix_mode_noeeprom(vialrgb_id_to_qmk_id(args[0]));
|
2021-07-09 01:27:26 +02:00
|
|
|
rgb_matrix_set_speed_noeeprom(args[1]);
|
|
|
|
rgb_matrix_sethsv_noeeprom(args[2], args[3], args[4]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void vialrgb_save(uint8_t *data, uint8_t length) {
|
|
|
|
(void)data;
|
|
|
|
(void)length;
|
|
|
|
|
|
|
|
eeconfig_update_rgb_matrix();
|
|
|
|
}
|