Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VIA Protocol 12 changes #104

Merged
merged 14 commits into from
Feb 28, 2023
1 change: 1 addition & 0 deletions src/components/panes/debug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ export const Debug: FC = () => {
.map((_) =>
api.getKeyboardValue(
KeyboardValue.SWITCH_MATRIX_STATE,
[],
20,
),
);
Expand Down
39 changes: 25 additions & 14 deletions src/components/panes/test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
setTestMatrixEnabled,
} from 'src/store/settingsSlice';
import {useSize} from 'src/utils/use-size';
import type {ConnectedDevice} from 'src/types/types';

const Container = styled.div`
display: flex;
Expand Down Expand Up @@ -88,24 +89,32 @@ export const Test: FC = () => {
}
};

const useMatrixTest = async () => {
const useMatrixTest = async (protocol: number) => {
if (startTest && api && selectedDefinition) {
const {cols, rows} = selectedDefinition.matrix;
const bytesPerRow = Math.ceil(cols / 8);
const rowsPerQuery = Math.floor(28 / bytesPerRow);
try {
const newFlat = (await api.getKeyboardValue(
KeyboardValue.SWITCH_MATRIX_STATE,
bytesPerRow * rows,
)) as number[];

const keysChanges =
0 !==
newFlat.reduce<number>((prev, val, byteIdx) => {
return (prev + val) ^ (flat[byteIdx] || 0);
}, 0);
let newFlat: number[] = [];
for (let offset = 0; offset < rows; offset += rowsPerQuery) {
const querySize = Math.min(
rows * bytesPerRow - newFlat.length, // bytes remaining
bytesPerRow * rowsPerQuery, // max bytes per query
);
newFlat.push(
...((await api.getKeyboardValue(
KeyboardValue.SWITCH_MATRIX_STATE,
protocol >= 12 ? [offset] : [],
querySize,
)) as number[]),
);
}
const keysChanges = newFlat.some(
(val, byteIdx) => val ^ (flat[byteIdx] || 0),
);
if (!keysChanges) {
await api.timeout(20);
useMatrixTest();
useMatrixTest(protocol);
return;
}
setSelectedKeys((selectedKeys) => {
Expand Down Expand Up @@ -137,7 +146,7 @@ export const Test: FC = () => {
});
flat = newFlat;
await api.timeout(20);
useMatrixTest();
useMatrixTest(protocol);
} catch (e) {
startTest = false;
dispatch(setTestMatrixEnabled(false));
Expand Down Expand Up @@ -222,7 +231,9 @@ export const Test: FC = () => {

if (val) {
setSelectedKeys({});
useMatrixTest();
useMatrixTest(
(selectedDevice as ConnectedDevice).protocol,
);
} else {
setSelectedKeys({});
}
Expand Down
8 changes: 5 additions & 3 deletions src/components/positioned-keyboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
isAlpha,
isNumericSymbol,
isNumericOrShiftedSymbol,
isMacro,
isMacroKeycodeByte,
getMacroKeycodeIndex,
getShortNameForKeycode,
} from '../utils/key';
import type {IKeycode} from '../utils/key';
Expand Down Expand Up @@ -421,8 +422,9 @@ export const getLabel = (
'';
}
let macroExpression: string | undefined;
if (isMacro(label)) {
macroExpression = macros.expressions[label.substring(1) as any];
if (isMacroKeycodeByte(keycodeByte, basicKeyToByte)) {
const macroKeycodeIdx = getMacroKeycodeIndex(keycodeByte, basicKeyToByte);
macroExpression = macros.expressions[macroKeycodeIdx];
}

if (isAlpha(label) || isNumericOrShiftedSymbol(label)) {
Expand Down
2 changes: 1 addition & 1 deletion src/store/definitionsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export const loadLayoutOptions = (): AppThunk => async (dispatch, getState) => {

const {api, device} = connectedDevice;
try {
const res = await api.getKeyboardValue(KeyboardValue.LAYOUT_OPTIONS, 4);
const res = await api.getKeyboardValue(KeyboardValue.LAYOUT_OPTIONS, [], 4);
const options = unpackBits(
bytesIntoNum(res),
selectedDefinition.layouts.labels.map((layoutLabel: string[] | string) =>
Expand Down
12 changes: 12 additions & 0 deletions src/utils/advanced-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const quantumRangesKeys = [
'_QK_LAYER_TAP_TOGGLE_MAX',
'_QK_KB',
'_QK_KB_MAX',
'_QK_MACRO',
'_QK_MACRO_MAX',
];

const quantumRanges = (
Expand Down Expand Up @@ -74,6 +76,7 @@ const topLevelMacroToValue = {
OSM: '_QK_ONE_SHOT_MOD', //OSM(mod)
TT: '_QK_LAYER_TAP_TOGGLE', // TT(layer)
CUSTOM: '_QK_KB', // CUSTOM(n)
MACRO: '_QK_MACRO', // MACRO(n)
};

const modifierKeyToValue = {
Expand Down Expand Up @@ -202,6 +205,7 @@ export const advancedKeycodeToString = (
case '_QK_LAYER_TAP_TOGGLE':
case '_QK_TO':
case '_QK_KB':
case '_QK_MACRO':
humanReadable += remainder + ')';
break;
case '_QK_LAYER_TAP':
Expand Down Expand Up @@ -345,6 +349,14 @@ const parseTopLevelMacro = (
}
return 0;
}
case 'MACRO': {
const n = Number.parseInt(parameter);
const nMax = basicKeyToByte._QK_MACRO_MAX - basicKeyToByte._QK_MACRO;
if (n >= 0 && n <= nMax) {
return basicKeyToByte[topLevelMacroToValue[topLevelKey]] + n;
}
return 0;
}
default:
return 0;
}
Expand Down
45 changes: 2 additions & 43 deletions src/utils/key-to-byte/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export default {
_QK_LAYER_TAP_TOGGLE: 0x5800,
_QK_LAYER_TAP_TOGGLE_MAX: 0x581f,
_QK_LAYER_MOD_MASK: 0x0f,
_QK_MACRO: 0x5f12,
_QK_MACRO_MAX: 0x5f21,
_QK_KB: 0x5f80,
_QK_KB_MAX: 0x5f8f,
KC_NO: 0x0000,
Expand Down Expand Up @@ -79,7 +81,6 @@ export default {
KC_SCLN: 0x0033,
KC_QUOT: 0x0034,
KC_GRV: 0x0035,
KC_ZKHK: 0x0035,
KC_COMM: 0x0036,
KC_DOT: 0x0037,
KC_SLSH: 0x0038,
Expand Down Expand Up @@ -287,27 +288,6 @@ export default {
KC_RCPC: 0x5cf4,
KC_LAPO: 0x5cf5,
KC_RAPC: 0x5cf6,
KC_TILD: 0x0235,
KC_EXLM: 0x021e,
KC_AT: 0x021f,
KC_HASH: 0x0220,
KC_DLR: 0x0221,
KC_PERC: 0x0222,
KC_CIRC: 0x0223,
KC_AMPR: 0x0224,
KC_ASTR: 0x0225,
KC_LPRN: 0x0226,
KC_RPRN: 0x0227,
KC_UNDS: 0x022d,
KC_PLUS: 0x022e,
KC_LCBR: 0x022f,
KC_RCBR: 0x0230,
KC_COLN: 0x0233,
KC_PIPE: 0x0231,
KC_LT: 0x0236,
KC_GT: 0x0237,
KC_QUES: 0x0238,
KC_DQUO: 0x0234,
BR_INC: 0x5f00,
BR_DEC: 0x5f01,
EF_INC: 0x5f02,
Expand All @@ -324,25 +304,4 @@ export default {
S2_DEC: 0x5f0d,
FN_MO13: 0x5f10,
FN_MO23: 0x5f11,
MACRO00: 0x5f12,
MACRO01: 0x5f13,
MACRO02: 0x5f14,
MACRO03: 0x5f15,
MACRO04: 0x5f16,
MACRO05: 0x5f17,
MACRO06: 0x5f18,
MACRO07: 0x5f19,
MACRO08: 0x5f1a,
MACRO09: 0x5f1b,
MACRO10: 0x5f1c,
MACRO11: 0x5f1d,
MACRO12: 0x5f1e,
MACRO13: 0x5f1f,
MACRO14: 0x5f20,
MACRO15: 0x5f21,
FN_TT13: 0x2f31,
FN_TT23: 0x2f32,
SPC_FN1: 0x412c,
SPC_FN2: 0x422c,
SPC_FN3: 0x432c,
};
40 changes: 40 additions & 0 deletions src/utils/key-to-byte/deprecated-keycodes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,44 @@
export default {
KC_TILD: 'S(KC_GRV)',
KC_EXLM: 'S(KC_1)',
KC_AT: 'S(KC_2)',
KC_HASH: 'S(KC_3)',
KC_DLR: 'S(KC_4)',
KC_PERC: 'S(KC_5)',
KC_CIRC: 'S(KC_6)',
KC_AMPR: 'S(KC_7)',
KC_ASTR: 'S(KC_8)',
KC_LPRN: 'S(KC_9)',
KC_RPRN: 'S(KC_0)',
KC_UNDS: 'S(KC_MINS)',
KC_PLUS: 'S(KC_EQL)',
KC_LCBR: 'S(KC_LBRC)',
KC_RCBR: 'S(KC_RBRC)',
KC_PIPE: 'S(KC_BSLS)',
KC_COLN: 'S(KC_SCLN)',
KC_DQUO: 'S(KC_QUOT)',
KC_LT: 'S(KC_COMM)',
KC_GT: 'S(KC_DOT)',
KC_QUES: 'S(KC_SLSH)',
SPC_FN1: 'LT(1,KC_SPC)',
SPC_FN2: 'LT(2,KC_SPC)',
SPC_FN3: 'LT(3,KC_SPC)',
MACRO00: 'MACRO(0)',
MACRO01: 'MACRO(1)',
MACRO02: 'MACRO(2)',
MACRO03: 'MACRO(3)',
MACRO04: 'MACRO(4)',
MACRO05: 'MACRO(5)',
MACRO06: 'MACRO(6)',
MACRO07: 'MACRO(7)',
MACRO08: 'MACRO(8)',
MACRO09: 'MACRO(9)',
MACRO10: 'MACRO(10)',
MACRO11: 'MACRO(11)',
MACRO12: 'MACRO(12)',
MACRO13: 'MACRO(13)',
MACRO14: 'MACRO(14)',
MACRO15: 'MACRO(15)',
USER00: 'CUSTOM(0)',
USER01: 'CUSTOM(1)',
USER02: 'CUSTOM(2)',
Expand Down
4 changes: 3 additions & 1 deletion src/utils/key-to-byte/dictionary-store.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import basicKeyToByte from './default';
import v10BasicKeyToByte from './v10';
import v11BasicKeyToByte from './v11';
import v12BasicKeyToByte from './v12';
export function getBasicKeyDict(version: number) {
switch (version) {
case 13:
case 12: {
return v11BasicKeyToByte;
return v12BasicKeyToByte;
}
case 11: {
return v11BasicKeyToByte;
Expand Down
45 changes: 2 additions & 43 deletions src/utils/key-to-byte/v10.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export default {
_QK_LAYER_TAP_TOGGLE: 0x5800,
_QK_LAYER_TAP_TOGGLE_MAX: 0x581f,
_QK_LAYER_MOD_MASK: 0x0f,
_QK_MACRO: 0x5f12,
_QK_MACRO_MAX: 0x5f21,
_QK_KB: 0x5f80,
_QK_KB_MAX: 0x5f8f,
KC_NO: 0x0000,
Expand Down Expand Up @@ -79,7 +81,6 @@ export default {
KC_SCLN: 0x0033,
KC_QUOT: 0x0034,
KC_GRV: 0x0035,
KC_ZKHK: 0x0035,
KC_COMM: 0x0036,
KC_DOT: 0x0037,
KC_SLSH: 0x0038,
Expand Down Expand Up @@ -287,27 +288,6 @@ export default {
KC_RCPC: 0x5cf4,
KC_LAPO: 0x5cf5,
KC_RAPC: 0x5cf6,
KC_TILD: 0x0235,
KC_EXLM: 0x021e,
KC_AT: 0x021f,
KC_HASH: 0x0220,
KC_DLR: 0x0221,
KC_PERC: 0x0222,
KC_CIRC: 0x0223,
KC_AMPR: 0x0224,
KC_ASTR: 0x0225,
KC_LPRN: 0x0226,
KC_RPRN: 0x0227,
KC_UNDS: 0x022d,
KC_PLUS: 0x022e,
KC_LCBR: 0x022f,
KC_RCBR: 0x0230,
KC_COLN: 0x0233,
KC_PIPE: 0x0231,
KC_LT: 0x0236,
KC_GT: 0x0237,
KC_QUES: 0x0238,
KC_DQUO: 0x0234,
BR_INC: 0x5f00,
BR_DEC: 0x5f01,
EF_INC: 0x5f02,
Expand All @@ -324,25 +304,4 @@ export default {
S2_DEC: 0x5f0d,
FN_MO13: 0x5f10,
FN_MO23: 0x5f11,
MACRO00: 0x5f12,
MACRO01: 0x5f13,
MACRO02: 0x5f14,
MACRO03: 0x5f15,
MACRO04: 0x5f16,
MACRO05: 0x5f17,
MACRO06: 0x5f18,
MACRO07: 0x5f19,
MACRO08: 0x5f1a,
MACRO09: 0x5f1b,
MACRO10: 0x5f1c,
MACRO11: 0x5f1d,
MACRO12: 0x5f1e,
MACRO13: 0x5f1f,
MACRO14: 0x5f20,
MACRO15: 0x5f21,
FN_TT13: 0x2f31,
FN_TT23: 0x2f32,
SPC_FN1: 0x412c,
SPC_FN2: 0x422c,
SPC_FN3: 0x432c,
};
Loading