#ifndef RAW_ENABLE
# error "RAW_ENABLE is not enabled"
#endif
#ifndef DYNAMIC_KEYMAP_ENABLE
# error "DYNAMIC_KEYMAP_ENABLE is not enabled"
#endif
#if defined(BACKLIGHT_ENABLE) && !defined(VIA_CUSTOM_LIGHTING_ENABLE)
# define VIA_QMK_BACKLIGHT_ENABLE
#endif
#if defined(RGBLIGHT_ENABLE) && !defined(VIA_CUSTOM_LIGHTING_ENABLE)
# define VIA_QMK_RGBLIGHT_ENABLE
#endif
#include "quantum.h"
#include "via.h"
#include "raw_hid.h"
#include "dynamic_keymap.h"
#include "tmk_core/common/eeprom.h"
#include "version.h"
#include "via_ensure_keycode.h"
#if defined(VIA_QMK_BACKLIGHT_ENABLE)
void via_qmk_backlight_set_value(uint8_t *data);
void via_qmk_backlight_get_value(uint8_t *data);
#endif
#if defined(VIA_QMK_RGBLIGHT_ENABLE)
void via_qmk_rgblight_set_value(uint8_t *data);
void via_qmk_rgblight_get_value(uint8_t *data);
#endif
bool via_eeprom_is_valid(void) {
char * p = QMK_BUILDDATE; uint8_t magic0 = ((p[2] & 0x0F) << 4) | (p[3] & 0x0F);
uint8_t magic1 = ((p[5] & 0x0F) << 4) | (p[6] & 0x0F);
uint8_t magic2 = ((p[8] & 0x0F) << 4) | (p[9] & 0x0F);
return (eeprom_read_byte((void *)VIA_EEPROM_MAGIC_ADDR + 0) == magic0 && eeprom_read_byte((void *)VIA_EEPROM_MAGIC_ADDR + 1) == magic1 && eeprom_read_byte((void *)VIA_EEPROM_MAGIC_ADDR + 2) == magic2);
}
void via_eeprom_set_valid(bool valid) {
char * p = QMK_BUILDDATE; uint8_t magic0 = ((p[2] & 0x0F) << 4) | (p[3] & 0x0F);
uint8_t magic1 = ((p[5] & 0x0F) << 4) | (p[6] & 0x0F);
uint8_t magic2 = ((p[8] & 0x0F) << 4) | (p[9] & 0x0F);
eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 0, valid ? magic0 : 0xFF);
eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 1, valid ? magic1 : 0xFF);
eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 2, valid ? magic2 : 0xFF);
}
void via_eeprom_reset(void) {
via_eeprom_set_valid(false);
eeconfig_disable();
}
__attribute__((weak)) void via_init_kb(void) {}
void via_init(void) {
via_init_kb();
if (via_eeprom_is_valid()) {
} else {
via_set_layout_options(VIA_EEPROM_LAYOUT_OPTIONS_DEFAULT);
dynamic_keymap_reset();
dynamic_keymap_macro_reset();
via_eeprom_set_valid(true);
}
}
uint32_t via_get_layout_options(void) {
uint32_t value = 0;
void *source = (void *)(VIA_EEPROM_LAYOUT_OPTIONS_ADDR);
for (uint8_t i = 0; i < VIA_EEPROM_LAYOUT_OPTIONS_SIZE; i++) {
value = value << 8;
value |= eeprom_read_byte(source);
source++;
}
return value;
}
void via_set_layout_options(uint32_t value) {
void *target = (void *)(VIA_EEPROM_LAYOUT_OPTIONS_ADDR + VIA_EEPROM_LAYOUT_OPTIONS_SIZE - 1);
for (uint8_t i = 0; i < VIA_EEPROM_LAYOUT_OPTIONS_SIZE; i++) {
eeprom_update_byte(target, value & 0xFF);
value = value >> 8;
target--;
}
}
bool process_record_via(uint16_t keycode, keyrecord_t *record) {
if (record->event.pressed) {
if (keycode >= MACRO00 && keycode <= MACRO15) {
uint8_t id = keycode - MACRO00;
dynamic_keymap_macro_send(id);
return false;
}
}
switch (keycode) {
case FN_MO13:
if (record->event.pressed) {
layer_on(1);
update_tri_layer(1, 2, 3);
} else {
layer_off(1);
update_tri_layer(1, 2, 3);
}
return false;
break;
case FN_MO23:
if (record->event.pressed) {
layer_on(2);
update_tri_layer(1, 2, 3);
} else {
layer_off(2);
update_tri_layer(1, 2, 3);
}
return false;
break;
}
return true;
}
__attribute__((weak)) void raw_hid_receive_kb(uint8_t *data, uint8_t length) {
uint8_t *command_id = &(data[0]);
*command_id = id_unhandled;
}
void raw_hid_receive(uint8_t *data, uint8_t length) {
uint8_t *command_id = &(data[0]);
uint8_t *command_data = &(data[1]);
switch (*command_id) {
case id_get_protocol_version: {
command_data[0] = VIA_PROTOCOL_VERSION >> 8;
command_data[1] = VIA_PROTOCOL_VERSION & 0xFF;
break;
}
case id_get_keyboard_value: {
switch (command_data[0]) {
case id_uptime: {
uint32_t value = timer_read32();
command_data[1] = (value >> 24) & 0xFF;
command_data[2] = (value >> 16) & 0xFF;
command_data[3] = (value >> 8) & 0xFF;
command_data[4] = value & 0xFF;
break;
}
case id_layout_options: {
uint32_t value = via_get_layout_options();
command_data[1] = (value >> 24) & 0xFF;
command_data[2] = (value >> 16) & 0xFF;
command_data[3] = (value >> 8) & 0xFF;
command_data[4] = value & 0xFF;
break;
}
case id_switch_matrix_state: {
#if ((MATRIX_COLS / 8 + 1) * MATRIX_ROWS <= 28)
uint8_t i = 1;
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
matrix_row_t value = matrix_get_row(row);
# if (MATRIX_COLS > 24)
command_data[i++] = (value >> 24) & 0xFF;
# endif
# if (MATRIX_COLS > 16)
command_data[i++] = (value >> 16) & 0xFF;
# endif
# if (MATRIX_COLS > 8)
command_data[i++] = (value >> 8) & 0xFF;
# endif
command_data[i++] = value & 0xFF;
}
#endif
break;
}
default: {
raw_hid_receive_kb(data, length);
break;
}
}
break;
}
case id_set_keyboard_value: {
switch (command_data[0]) {
case id_layout_options: {
uint32_t value = ((uint32_t)command_data[1] << 24) | ((uint32_t)command_data[2] << 16) | ((uint32_t)command_data[3] << 8) | (uint32_t)command_data[4];
via_set_layout_options(value);
break;
}
default: {
raw_hid_receive_kb(data, length);
break;
}
}
break;
}
case id_dynamic_keymap_get_keycode: {
uint16_t keycode = dynamic_keymap_get_keycode(command_data[0], command_data[1], command_data[2]);
command_data[3] = keycode >> 8;
command_data[4] = keycode & 0xFF;
break;
}
case id_dynamic_keymap_set_keycode: {
dynamic_keymap_set_keycode(command_data[0], command_data[1], command_data[2], (command_data[3] << 8) | command_data[4]);
break;
}
case id_dynamic_keymap_reset: {
dynamic_keymap_reset();
break;
}
case id_lighting_set_value: {
#if defined(VIA_QMK_BACKLIGHT_ENABLE)
via_qmk_backlight_set_value(command_data);
#endif
#if defined(VIA_QMK_RGBLIGHT_ENABLE)
via_qmk_rgblight_set_value(command_data);
#endif
#if defined(VIA_CUSTOM_LIGHTING_ENABLE)
raw_hid_receive_kb(data, length);
#endif
#if !defined(VIA_QMK_BACKLIGHT_ENABLE) && !defined(VIA_QMK_RGBLIGHT_ENABLE) && !defined(VIA_CUSTOM_LIGHTING_ENABLE)
*command_id = id_unhandled;
#endif
break;
}
case id_lighting_get_value: {
#if defined(VIA_QMK_BACKLIGHT_ENABLE)
via_qmk_backlight_get_value(command_data);
#endif
#if defined(VIA_QMK_RGBLIGHT_ENABLE)
via_qmk_rgblight_get_value(command_data);
#endif
#if defined(VIA_CUSTOM_LIGHTING_ENABLE)
raw_hid_receive_kb(data, length);
#endif
#if !defined(VIA_QMK_BACKLIGHT_ENABLE) && !defined(VIA_QMK_RGBLIGHT_ENABLE) && !defined(VIA_CUSTOM_LIGHTING_ENABLE)
*command_id = id_unhandled;
#endif
break;
}
case id_lighting_save: {
#if defined(VIA_QMK_BACKLIGHT_ENABLE)
eeconfig_update_backlight_current();
#endif
#if defined(VIA_QMK_RGBLIGHT_ENABLE)
eeconfig_update_rgblight_current();
#endif
#if defined(VIA_CUSTOM_LIGHTING_ENABLE)
raw_hid_receive_kb(data, length);
#endif
#if !defined(VIA_QMK_BACKLIGHT_ENABLE) && !defined(VIA_QMK_RGBLIGHT_ENABLE) && !defined(VIA_CUSTOM_LIGHTING_ENABLE)
*command_id = id_unhandled;
#endif
break;
}
case id_dynamic_keymap_macro_get_count: {
command_data[0] = dynamic_keymap_macro_get_count();
break;
}
case id_dynamic_keymap_macro_get_buffer_size: {
uint16_t size = dynamic_keymap_macro_get_buffer_size();
command_data[0] = size >> 8;
command_data[1] = size & 0xFF;
break;
}
case id_dynamic_keymap_macro_get_buffer: {
uint16_t offset = (command_data[0] << 8) | command_data[1];
uint16_t size = command_data[2]; dynamic_keymap_macro_get_buffer(offset, size, &command_data[3]);
break;
}
case id_dynamic_keymap_macro_set_buffer: {
uint16_t offset = (command_data[0] << 8) | command_data[1];
uint16_t size = command_data[2]; dynamic_keymap_macro_set_buffer(offset, size, &command_data[3]);
break;
}
case id_dynamic_keymap_macro_reset: {
dynamic_keymap_macro_reset();
break;
}
case id_dynamic_keymap_get_layer_count: {
command_data[0] = dynamic_keymap_get_layer_count();
break;
}
case id_dynamic_keymap_get_buffer: {
uint16_t offset = (command_data[0] << 8) | command_data[1];
uint16_t size = command_data[2]; dynamic_keymap_get_buffer(offset, size, &command_data[3]);
break;
}
case id_dynamic_keymap_set_buffer: {
uint16_t offset = (command_data[0] << 8) | command_data[1];
uint16_t size = command_data[2]; dynamic_keymap_set_buffer(offset, size, &command_data[3]);
break;
}
default: {
*command_id = id_unhandled;
break;
}
}
raw_hid_send(data, length);
}
#if defined(VIA_QMK_BACKLIGHT_ENABLE)
# if BACKLIGHT_LEVELS == 0
# error BACKLIGHT_LEVELS == 0
# endif
void via_qmk_backlight_get_value(uint8_t *data) {
uint8_t *value_id = &(data[0]);
uint8_t *value_data = &(data[1]);
switch (*value_id) {
case id_qmk_backlight_brightness: {
value_data[0] = ((uint16_t)get_backlight_level()) * 255 / BACKLIGHT_LEVELS;
break;
}
case id_qmk_backlight_effect: {
# ifdef BACKLIGHT_BREATHING
value_data[0] = is_backlight_breathing() ? 1 : 0;
# else
value_data[0] = 0;
# endif
break;
}
}
}
void via_qmk_backlight_set_value(uint8_t *data) {
uint8_t *value_id = &(data[0]);
uint8_t *value_data = &(data[1]);
switch (*value_id) {
case id_qmk_backlight_brightness: {
backlight_level_noeeprom(((uint16_t)value_data[0]) * BACKLIGHT_LEVELS / 255);
break;
}
case id_qmk_backlight_effect: {
# ifdef BACKLIGHT_BREATHING
if (value_data[0] == 0) {
backlight_disable_breathing();
} else {
backlight_enable_breathing();
}
# endif
break;
}
}
}
#endif
#if defined(VIA_QMK_RGBLIGHT_ENABLE)
void via_qmk_rgblight_get_value(uint8_t *data) {
uint8_t *value_id = &(data[0]);
uint8_t *value_data = &(data[1]);
switch (*value_id) {
case id_qmk_rgblight_brightness: {
value_data[0] = rgblight_get_val();
break;
}
case id_qmk_rgblight_effect: {
value_data[0] = rgblight_get_mode();
break;
}
case id_qmk_rgblight_effect_speed: {
value_data[0] = rgblight_get_speed();
break;
}
case id_qmk_rgblight_color: {
value_data[0] = rgblight_get_hue();
value_data[1] = rgblight_get_sat();
break;
}
}
}
void via_qmk_rgblight_set_value(uint8_t *data) {
uint8_t *value_id = &(data[0]);
uint8_t *value_data = &(data[1]);
switch (*value_id) {
case id_qmk_rgblight_brightness: {
rgblight_sethsv_noeeprom(rgblight_get_hue(), rgblight_get_sat(), value_data[0]);
break;
}
case id_qmk_rgblight_effect: {
rgblight_mode_noeeprom(value_data[0]);
if (value_data[0] == 0) {
rgblight_disable_noeeprom();
} else {
rgblight_enable_noeeprom();
}
break;
}
case id_qmk_rgblight_effect_speed: {
rgblight_set_speed_noeeprom(value_data[0]);
break;
}
case id_qmk_rgblight_color: {
rgblight_sethsv_noeeprom(value_data[0], value_data[1], rgblight_get_val());
break;
}
}
}
#endif