Skip to content

Commit

Permalink
feat: let the keyboard scream
Browse files Browse the repository at this point in the history
  • Loading branch information
heiso committed Nov 6, 2024
1 parent 8f0bd27 commit 5597a52
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
3 changes: 3 additions & 0 deletions firmware/Core/Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ extern "C" {
#define DEFAULT_TRIGGER_OFFSET 64
#define DEFAULT_RESET_THRESHOLD 3
#define DEFAULT_RAPID_TRIGGER_OFFSET 40
#define DEFAULT_SCREAMING_VELOCITY_TRIGGER 45

#define IDLE_VALUE_APPROX 1800
#define MAX_DISTANCE_APPROX 500
Expand Down Expand Up @@ -97,6 +98,7 @@ struct __attribute__((__packed__)) actuation {
uint8_t trigger_offset;
uint8_t rapid_trigger_offset;
uint8_t is_continuous_rapid_trigger_enabled;
uint8_t is_screaming;
uint32_t triggered_at;
};

Expand Down Expand Up @@ -134,6 +136,7 @@ struct user_config {
uint8_t trigger_offset;
uint8_t reset_threshold;
uint8_t rapid_trigger_offset;
uint8_t screaming_velocity_trigger;
uint16_t keymaps[LAYERS_COUNT][MATRIX_ROWS][MATRIX_COLS];
};
/* USER CODE END ET */
Expand Down
13 changes: 13 additions & 0 deletions firmware/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const struct user_config default_user_config = {
.trigger_offset = DEFAULT_TRIGGER_OFFSET,
.reset_threshold = DEFAULT_RESET_THRESHOLD,
.rapid_trigger_offset = DEFAULT_RAPID_TRIGGER_OFFSET,
.screaming_velocity_trigger = DEFAULT_SCREAMING_VELOCITY_TRIGGER,
.keymaps = {
// clang-format off
[_BASE_LAYER] = {
Expand Down Expand Up @@ -553,6 +554,7 @@ void init_key(uint8_t adc_channel, uint8_t amux_channel, uint8_t row, uint8_t co
key->calibration.max_distance = MAX_DISTANCE_APPROX;

key->actuation.status = STATUS_RESET;
key->actuation.is_screaming = 0;
key->actuation.trigger_offset = user_config.trigger_offset;
key->actuation.reset_offset = user_config.trigger_offset - user_config.reset_threshold;
key->actuation.rapid_trigger_offset = user_config.rapid_trigger_offset;
Expand Down Expand Up @@ -604,6 +606,9 @@ void add_to_hid_report(struct key *key, uint8_t layer) {
for (uint8_t i = 0; i < 6; i++) {
if (keycodes[i] == 0) {
keycodes[i] = key->layers[layer].value;
if (key->actuation.is_screaming) {
modifiers |= get_bitmask_for_modifier(HID_KEY_SHIFT_LEFT);
}
should_send_keyboard_report = 1;
break;
}
Expand All @@ -628,6 +633,10 @@ void remove_from_hid_report(struct key *key, uint8_t layer) {
for (uint8_t i = 0; i < 6; i++) {
if (keycodes[i] == key->layers[layer].value) {
keycodes[i] = 0;
if (key->actuation.is_screaming) {
key->actuation.is_screaming = 0;
modifiers &= ~get_bitmask_for_modifier(HID_KEY_SHIFT_LEFT);
}
should_send_keyboard_report = 1;
break;
}
Expand Down Expand Up @@ -760,6 +769,10 @@ void update_key_actuation(struct key *key) {
} else {
key->actuation.status = STATUS_TRIGGERED;
key_triggered = 1;
// if the key is violently pressed, automatically add the MAJ modifier :)
if (key->state.velocity > user_config.screaming_velocity_trigger) {
key->actuation.is_screaming = 1;
}
add_to_hid_report(key, _BASE_LAYER);
}
key->actuation.triggered_at = now;
Expand Down
19 changes: 19 additions & 0 deletions web-app/app/routes/_layout.configurator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,25 @@ export default function Index() {
{Number(((userConfig.resetThreshold * 4) / 255).toFixed(2))} mm
</span>
</div>

<div className="flex gap-2 items-center">
<Label>Screaming Velocity Trigger</Label>
<input
className="cursor-pointer"
min={1}
max={255}
step={1}
type="range"
onChange={(event) => {
setUserConfig({
...userConfig,
screamingVelocityTrigger: Number(event.target.value),
})
}}
value={userConfig.screamingVelocityTrigger}
/>
<span className="text-slate-400">{Number(userConfig.screamingVelocityTrigger)}</span>
</div>
</div>

<div className="space-x-4">
Expand Down
7 changes: 6 additions & 1 deletion web-app/app/useDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type UserConfig = {
triggerOffset: number
resetThreshold: number
rapidTriggerOffset: number
screamingVelocityTrigger: number
keymaps: ArrayBuffer
}

Expand All @@ -44,7 +45,8 @@ function parseUserConfig(config: DataView): UserConfig {
triggerOffset: config.getUint8(0),
resetThreshold: config.getUint8(1),
rapidTriggerOffset: config.getUint8(2),
keymaps: config.buffer.slice(3),
screamingVelocityTrigger: config.getUint8(3),
keymaps: config.buffer.slice(4),
}
}

Expand All @@ -53,6 +55,7 @@ function formatUserConfig(config: UserConfig): BufferSource {
config.triggerOffset,
config.resetThreshold,
config.rapidTriggerOffset,
config.screamingVelocityTrigger,
...new Uint8Array(config.keymaps),
])
}
Expand Down Expand Up @@ -105,6 +108,7 @@ export type Key = {
triggerOffset: number // uint8_t
rapidTriggerOffset: number // uint8_t
isContinuousRapidTriggerEnabled: boolean // uint8_t
isSreaming: boolean // uint8_t
triggeredAt: number // uint32_t
}
}
Expand Down Expand Up @@ -169,6 +173,7 @@ function parseKeys(data: DataView): Key[] {
triggerOffset: getValue(data, 8),
rapidTriggerOffset: getValue(data, 8),
isContinuousRapidTriggerEnabled: Boolean(getValue(data, 8)),
isSreaming: Boolean(getValue(data, 8)),
triggeredAt: getValue(data, 32),
},
})
Expand Down

0 comments on commit 5597a52

Please sign in to comment.