-
-
Notifications
You must be signed in to change notification settings - Fork 39.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
194 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Potentiometers | ||
|
||
Add this to your `rules.mk`: | ||
|
||
```make | ||
POTENTIOMETER_ENABLE = yes | ||
``` | ||
|
||
and this to your `config.h`: | ||
|
||
```c | ||
#define POTENTIOMETER_PINS { B0 } | ||
``` | ||
## Callbacks | ||
The callback functions can be inserted into your `<keyboard>.c`: | ||
```c | ||
bool potentiometer_update_kb(uint8_t index, uint16_t value) { | ||
if (!potentiometer_update_user(index, value)) { | ||
midi_send_cc(&midi_device, 2, 0x3E, 0x7F + (value >> 3)); | ||
} | ||
return true; | ||
} | ||
``` | ||
|
||
or `keymap.c`: | ||
|
||
```c | ||
bool potentiometer_update_user(uint8_t index, uint16_t value) { | ||
midi_send_cc(&midi_device, 2, 0x3E, 0x7F + (value >> 3)); | ||
return false; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2023 QMK | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
#include "potentiometer.h" | ||
#include "gpio.h" | ||
#include "util.h" | ||
#include "timer.h" | ||
#include "analog.h" | ||
|
||
#ifndef POTENTIOMETER_THROTTLE_MS | ||
# define POTENTIOMETER_THROTTLE_MS 1 | ||
#endif | ||
|
||
#ifndef POTENTIOMETER_OUTPUT_MIN_VALUE | ||
# define POTENTIOMETER_OUTPUT_MIN_VALUE 0 | ||
#endif | ||
#ifndef POTENTIOMETER_OUTPUT_MAX_VALUE | ||
# define POTENTIOMETER_OUTPUT_MAX_VALUE 127 | ||
#endif | ||
#ifndef POTENTIOMETER_ADC_MIN_VALUE | ||
# define POTENTIOMETER_ADC_MIN_VALUE 0 | ||
#endif | ||
#ifndef POTENTIOMETER_ADC_MAX_VALUE | ||
# define POTENTIOMETER_ADC_MAX_VALUE (1 << 10) | ||
#endif | ||
|
||
static pin_t potentiometer_pads[] = POTENTIOMETER_PINS; | ||
#define NUM_POTENTIOMETERS (ARRAY_SIZE(potentiometer_pads)) | ||
|
||
__attribute__((weak)) bool potentiometer_update_user(uint8_t index, uint16_t value) { | ||
return true; | ||
} | ||
|
||
__attribute__((weak)) bool potentiometer_update_kb(uint8_t index, uint16_t value) { | ||
return potentiometer_update_user(index, value); | ||
} | ||
|
||
__attribute__((weak)) bool potentiometer_throttle_task(void) { | ||
#if (POTENTIOMETER_THROTTLE_MS > 0) | ||
static uint32_t last_exec = 0; | ||
if (timer_elapsed32(last_exec) < POTENTIOMETER_THROTTLE_MS) { | ||
return false; | ||
} | ||
last_exec = timer_read32(); | ||
#endif | ||
return true; | ||
} | ||
|
||
__attribute__((weak)) uint16_t potentiometer_map(uint8_t index, uint16_t value) { | ||
(void)index; | ||
|
||
uint32_t a = POTENTIOMETER_OUTPUT_MIN_VALUE; | ||
uint32_t b = POTENTIOMETER_OUTPUT_MAX_VALUE; | ||
uint32_t min = POTENTIOMETER_ADC_MIN_VALUE; | ||
uint32_t max = POTENTIOMETER_ADC_MAX_VALUE; | ||
|
||
// Scale value to min/max using the adc range | ||
return ((b - a) * (value - min) / (max - min)) + a; | ||
} | ||
|
||
__attribute__((weak)) bool potentiometer_filter(uint8_t index, uint16_t value) { | ||
// ADC currently limited to max of 12 bits - init to max 16 ensures | ||
// we can correctly capture even a raw first sample at max adc bounds | ||
static bool potentiometer_state[NUM_POTENTIOMETERS] = {UINT16_MAX}; | ||
|
||
if (value == potentiometer_state[index]) { | ||
return false; | ||
} | ||
|
||
potentiometer_state[index] = value; | ||
return true; | ||
} | ||
|
||
bool potentiometer_task(void) { | ||
if (!potentiometer_throttle_task()) { | ||
return false; | ||
} | ||
|
||
bool changed = false; | ||
for (uint8_t index = 0; index < NUM_POTENTIOMETERS; index++) { | ||
uint16_t raw = analogReadPin(potentiometer_pads[index]); | ||
uint16_t value = potentiometer_map(index, raw); | ||
if (potentiometer_filter(index, value)) { | ||
changed |= true; | ||
|
||
potentiometer_update_kb(index, value); | ||
} | ||
} | ||
|
||
return changed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2023 QMK | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
#pragma once | ||
|
||
#include <stdbool.h> | ||
#include <stdint.h> | ||
|
||
/** \brief user hook called when sampled value has changed | ||
*/ | ||
bool potentiometer_update_user(uint8_t index, uint16_t value); | ||
|
||
/** \brief keyboard hook called when sampled value has changed | ||
*/ | ||
bool potentiometer_update_kb(uint8_t index, uint16_t value); | ||
|
||
/** \brief Handle various subsystem background tasks | ||
*/ | ||
bool potentiometer_task(void); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters